Skip to content

Conversation

@vporpo
Copy link
Contributor

@vporpo vporpo commented Jan 21, 2026

The code in the test is causing a crash in SIInstrInfo.cpp fixImplicitOperands() in MI.implicit_operands():

  for (auto &Op : MI.implicit_operands()) {

MachineInstr.h:

  mop_range implicit_operands() {
=>  return operands_impl().drop_front(getNumExplicitOperands());
  }

We are trying to drop 1 operand from the operands of MI which are 0.

By early returning we are no longer crashing at that point and we are getting a more meaningful error message:

*** Bad machine code: Too few operands ***
- function:    missing_operand_crash
- basic block: %bb.0  (0x5a9d30ced988)
- instruction: S_WAITCNT_DEPCTR
1 operands expected, but 0 given.

The code is still crashing at a different location, but at least we are getting an error message.

This is crashing in `SIInstrInfo.cpp` `fixImplicitOperands()` in
`MI.implicit_operands()`:
```
  for (auto &Op : MI.implicit_operands()) {
```

MachineInstr.h:
```
  mop_range implicit_operands() {
=>  return operands_impl().drop_front(getNumExplicitOperands());
  }
```
We are trying to drop 1 operand from the operands of MI which are 0.

By early returning we are no longer crashing at that point and we are getting a
more meaningful error message:

```
*** Bad machine code: Too few operands ***
- function:    missing_operand_crash
- basic block: %bb.0  (0x5a9d30ced988)
- instruction: S_WAITCNT_DEPCTR
1 operands expected, but 0 given.
```
@llvmbot
Copy link
Member

llvmbot commented Jan 21, 2026

@llvm/pr-subscribers-backend-amdgpu

Author: vporpo (vporpo)

Changes

The code in the test is causing a crash in SIInstrInfo.cpp fixImplicitOperands() in MI.implicit_operands():

  for (auto &Op : MI.implicit_operands()) {

MachineInstr.h:

  mop_range implicit_operands() {
=>  return operands_impl().drop_front(getNumExplicitOperands());
  }

We are trying to drop 1 operand from the operands of MI which are 0.

By early returning we are no longer crashing at that point and we are getting a more meaningful error message:

*** Bad machine code: Too few operands ***
- function:    missing_operand_crash
- basic block: %bb.0  (0x5a9d30ced988)
- instruction: S_WAITCNT_DEPCTR
1 operands expected, but 0 given.

The code is still crashing at a different location, but at least we are getting an error message.


Full diff: https://github.com/llvm/llvm-project/pull/177065.diff

2 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIInstrInfo.cpp (+3)
  • (added) llvm/test/CodeGen/MIR/AMDGPU/s_wait_alu_missing_operand_crash.mir (+9)
diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
index bd6c58d0f8945..96c16962b9d96 100644
--- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
@@ -10001,6 +10001,9 @@ void SIInstrInfo::fixImplicitOperands(MachineInstr &MI) const {
   if (MI.isInlineAsm())
     return;
 
+  if (MI.getNumOperands() < MI.getNumExplicitOperands())
+    return;
+
   for (auto &Op : MI.implicit_operands()) {
     if (Op.isReg() && Op.getReg() == AMDGPU::VCC)
       Op.setReg(AMDGPU::VCC_LO);
diff --git a/llvm/test/CodeGen/MIR/AMDGPU/s_wait_alu_missing_operand_crash.mir b/llvm/test/CodeGen/MIR/AMDGPU/s_wait_alu_missing_operand_crash.mir
new file mode 100644
index 0000000000000..fd0f415ef8ef3
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/AMDGPU/s_wait_alu_missing_operand_crash.mir
@@ -0,0 +1,9 @@
+# RUN: ! llc -mtriple=amdgcn -run-pass=none %s -o - 2>&1 | FileCheck %s
+
+---
+# CHECK: Bad machine code: Too few operands
+name: missing_operand_crash
+body: |
+  bb.0:
+    S_WAITCNT_DEPCTR
+...

@@ -0,0 +1,9 @@
# RUN: ! llc -mtriple=amdgcn -run-pass=none %s -o - 2>&1 | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-o /dev/null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants