Skip to content

[CodeGen] MachineVerifier to check early-clobber constraint #151421

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AbhayKanhere
Copy link
Contributor

Currently MachineVerifier is missing verifying early-clobber operand constraint.
The only other machine operand constraint - TiedTo is already verified.

@AbhayKanhere
Copy link
Contributor Author

Test failures on linux and windows are the same test
CodeGen/AMDGPU/GlobalISel/inst-select-mad_64_32.mir

@AbhayKanhere
Copy link
Contributor Author

@ mbrkusanin I see you have a PR on AMDGPU global Isel. I tried locating the bug (failure with early-clobber not set when G_AMDGPU_MAD_U64_U32 is emitted) and I tried fixing in legalizer llvm/lib/Target/AMDGPU/AMDGPULegalizerInfo.cpp line 4091. but that is not sufficient. I could not locate (with quick searches) where this is emitted in Global ISel. any help appreciated.

@AbhayKanhere AbhayKanhere force-pushed the eng/abhay/machine-verify-early-clobber branch from 984f262 to a0a0504 Compare August 4, 2025 17:36
@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2025

@llvm/pr-subscribers-llvm-globalisel

@llvm/pr-subscribers-backend-amdgpu

Author: Abhay Kanhere (AbhayKanhere)

Changes

Currently MachineVerifier is missing verifying early-clobber operand constraint.
The only other machine operand constraint - TiedTo is already verified.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/MachineVerifier.cpp (+7)
  • (modified) llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp (+1)
diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp
index 01703fe09b79a..ebef1c9034f4a 100644
--- a/llvm/lib/CodeGen/MachineVerifier.cpp
+++ b/llvm/lib/CodeGen/MachineVerifier.cpp
@@ -2325,6 +2325,13 @@ void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
       report("Missing mayStore flag", MI);
   }
 
+  // Verify earlyClobber def operand
+  if (MCID.getOperandConstraint(0, MCOI::EARLY_CLOBBER) != -1) {
+    if (!MI->getOperand(0).isReg())
+      report("Early clobber must be a register", MI);
+    if (!MI->getOperand(0).isEarlyClobber())
+      report("Missing earlyClobber flag", MI);
+  }
   // Debug values must not have a slot index.
   // Other instructions must have one, unless they are inside a bundle.
   if (LiveInts) {
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp b/llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp
index 1a63c48e3666c..8a6ef89a4a062 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp
@@ -584,6 +584,7 @@ bool AMDGPUInstructionSelector::selectG_AMDGPU_MAD_64_32(
   I.setDesc(TII.get(Opc));
   I.addOperand(*MF, MachineOperand::CreateImm(0));
   I.addImplicitDefUseOperands(*MF);
+  I.getOperand(0).setIsEarlyClobber(true);
   return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
 }
 

@AbhayKanhere
Copy link
Contributor Author

This exposed more test fails someone with AMDGPU knowledge should look into whether this is expected. !
@mbrkusanin @amd-arsuresh @arsenm ? Who can help me naviagte these AMD GPU tests updates.

2025-08-04T18:08:20.5815070Z Failed Tests (10):
LLVM :: CodeGen/AMDGPU/GlobalISel/mul-known-bits.i64.ll
LLVM :: CodeGen/AMDGPU/GlobalISel/mul.ll
LLVM :: CodeGen/AMDGPU/GlobalISel/sdiv.i64.ll
LLVM :: CodeGen/AMDGPU/GlobalISel/sdivrem.ll
LLVM :: CodeGen/AMDGPU/GlobalISel/srem.i64.ll
LLVM :: CodeGen/AMDGPU/GlobalISel/udivrem.ll
LLVM :: CodeGen/AMDGPU/div_v2i128.ll
LLVM :: CodeGen/AMDGPU/fptoi.i128.ll
LLVM :: CodeGen/AMDGPU/integer-mad-patterns.ll
LLVM :: CodeGen/AMDGPU/vector-reduce-mul.ll

@AbhayKanhere
Copy link
Contributor Author

FYI I fixed these 10 tests locally and found 11 more. .. Wanted some feedback if this is worth fixing or not.
these are 11 more fails that show up
CodeGen/AMDGPU/GlobalISel/llvm.amdgcn.mfma.gfx90a.ll CodeGen/AMDGPU/GlobalISel/llvm.amdgcn.set.inactive.ll CodeGen/AMDGPU/GlobalISel/llvm.amdgcn.wwm.ll CodeGen/AMDGPU/amdgpu-cs-chain-cc.ll CodeGen/AMDGPU/amdgpu-cs-chain-preserve-cc.ll CodeGen/AMDGPU/llvm.amdgcn.init.whole.wave-w32.ll CodeGen/AMDGPU/llvm.amdgcn.init.whole.wave-w64.ll CodeGen/AMDGPU/llvm.amdgcn.mfma.gfx90a.ll CodeGen/AMDGPU/llvm.amdgcn.mfma.ll CodeGen/AMDGPU/llvm.amdgcn.set.inactive.chain.arg.ll CodeGen/AMDGPU/mfma-loop.ll

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.

Needs tests in test/MachineVerifier

@@ -584,6 +584,7 @@ bool AMDGPUInstructionSelector::selectG_AMDGPU_MAD_64_32(
I.setDesc(TII.get(Opc));
I.addOperand(*MF, MachineOperand::CreateImm(0));
I.addImplicitDefUseOperands(*MF);
I.getOperand(0).setIsEarlyClobber(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a property of the instruction definition and not require manual setting

Copy link
Contributor Author

@AbhayKanhere AbhayKanhere Aug 5, 2025

Choose a reason for hiding this comment

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

earlyClobber and TiedTo are two of the OperandConstraint -- we check for tiedTo but not earlyClobber in machine verifier. on CPU targets register allocators strictly look at earlyClobber on operand. Recently we discovered that when MI.setDesc is used to modify the opcode, this constraint on Operand is not updated. However if new instruction is built and operand added using addOperand() , then earlyClobber is set correctly.. Looks like AMDGPU backend makes heavy use of setDesc (since the earlySelect expects in-place change to opcode) but does not update earlyClobber. I found many instances of this.

Copy link
Contributor Author

@AbhayKanhere AbhayKanhere Aug 5, 2025

Choose a reason for hiding this comment

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

Internally we discussed a variant for MI.setDesc(MCID, bool updateOpConstraint=false) that updates OperandConstraints if passed true, but setDesc is often part of a set of update operations on operands and not the last in the sequence so it is not always possible to update operand constraints there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem here is that MI.setDesc which only updates opcode, and MI.addOperand following MI.buildInstr/ that create new instruction behave differently in how Operand constraints are updated.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, still needs tests

Currently MachineVerifier is missing verifying early-clobber operand
constraint. The only other machine operand constraint -  TiedTo is
already verified.
@AbhayKanhere AbhayKanhere force-pushed the eng/abhay/machine-verify-early-clobber branch from a0a0504 to f70f3ba Compare August 6, 2025 22:32
@AbhayKanhere
Copy link
Contributor Author

I tried machine verifier test to check early-clobber is present. but any test .mir where earlyclobber is not set on instruction when MIR is read-in, that attribute gets set correctly via addOperands. So I am not able to test missing early-clobber crash!

@AbhayKanhere AbhayKanhere requested a review from arsenm August 7, 2025 21:57
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