Skip to content

[AMDGPU] Search for literals among explicit operands only #130771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

rovka
Copy link
Collaborator

@rovka rovka commented Mar 11, 2025

The code in SIInstrInfo::isOperandLegal crashes in some rare cases when dealing with call pseudos, which are variadic and may thus have more operands than those listed in their instruction descriptions. The excess operands will usually be implicit registers, but may also be the call preserved reg mask.

This patch attempts to fix the issue by iterating only through the explicit operands while searching for literal operands.

The code in SIInstrInfo::isOperandLegal crashes in some rare cases when
dealing with call pseudos, which are variadic and may thus have more
operands than those listed in their instruction descriptions. The excess
operands will usually be implicit registers, but may also be the call
preserved reg mask.

This patch attempts to fix the issue by iterating only through the
explicit operands while searching for literal operands.
@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2025

@llvm/pr-subscribers-backend-amdgpu

Author: Diana Picus (rovka)

Changes

The code in SIInstrInfo::isOperandLegal crashes in some rare cases when dealing with call pseudos, which are variadic and may thus have more operands than those listed in their instruction descriptions. The excess operands will usually be implicit registers, but may also be the call preserved reg mask.

This patch attempts to fix the issue by iterating only through the explicit operands while searching for literal operands.


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

1 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIInstrInfo.cpp (+1-1)
diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
index ae285d069d876..da8533fd5ef5d 100644
--- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
@@ -6063,7 +6063,7 @@ bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx,
     }
   } else if (!IsInlineConst && !MO->isReg() && isSALU(MI)) {
     // There can be at most one literal operand, but it can be repeated.
-    for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
+    for (unsigned i = 0, e = MI.getNumExplicitOperands(); i != e; ++i) {
       if (i == OpIdx)
         continue;
       const MachineOperand &Op = MI.getOperand(i);

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

Testcase?

@@ -0,0 +1,57 @@
//===- llvm/unittests/Target/AMDGPU/ExecMayBeModifiedBeforeAnyUse.cpp -----===//
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong file name

Comment on lines +44 to +53
Register VReg = MRI.createVirtualRegister(&AMDGPU::CCR_SGPR_64RegClass);
MachineInstr *Callee =
BuildMI(*BB, E, DL, TII.get(AMDGPU::S_MOV_B64), VReg).addGlobalAddress(F);
MachineInstr *Call =
BuildMI(*BB, E, DL, TII.get(AMDGPU::SI_CALL), AMDGPU::SGPR30_SGPR31)
.addReg(VReg)
.addImm(0)
.addRegMask(TRI.getCallPreservedMask(*MF, CallingConv::AMDGPU_Gfx))
.addReg(AMDGPU::VGPR0, RegState::Implicit)
.addReg(AMDGPU::VGPR1, RegState::Implicit);
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't isOperandLegal get called in the verifier? If you take the MIR here, and put it in test/MachineVerifier does it fail?

@rovka
Copy link
Collaborator Author

rovka commented Mar 12, 2025

Mirko's fix is already in good shape, let's just merge that one.

@rovka rovka closed this Mar 12, 2025
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