Skip to content

Conversation

@carlobertolli
Copy link
Member

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Oct 19, 2025

@llvm/pr-subscribers-backend-amdgpu

Author: None (carlobertolli)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/SIInstrInfo.cpp (+8)
  • (modified) llvm/test/CodeGen/AMDGPU/sgpr-phys-copy.mir (+9)
diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
index 50447f48a628c..d1556cccf9524 100644
--- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
@@ -845,6 +845,14 @@ void SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
     }
   }
 
+  if (AMDGPU::APERTURE_ClassRegClass.contains(DestReg)) {
+    if (SrcReg == AMDGPU::VCC) {
+      BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg)
+        .addReg(SrcReg, getKillRegState(KillSrc));
+      return;
+    }
+  }
+
   if (RC == &AMDGPU::VGPR_32RegClass) {
     assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) ||
            AMDGPU::SReg_32RegClass.contains(SrcReg) ||
diff --git a/llvm/test/CodeGen/AMDGPU/sgpr-phys-copy.mir b/llvm/test/CodeGen/AMDGPU/sgpr-phys-copy.mir
index f11fe4aa6e00e..78e8788c7f0f0 100644
--- a/llvm/test/CodeGen/AMDGPU/sgpr-phys-copy.mir
+++ b/llvm/test/CodeGen/AMDGPU/sgpr-phys-copy.mir
@@ -67,6 +67,15 @@ body:             |
     $vcc = COPY $src_shared_base
 ...
 
+---
+name: vcc_to_src_shared_base
+body:             |
+  bb.0:
+    ; GFX9-LABEL: name: vcc_to_src_shared_base
+    ; GFX9: $src_shared_base = S_MOV_B64 $vcc
+    $src_shared_base = COPY $vcc
+...
+
 ---
 name: sgpr96_aligned_src_dst
 body:             |

@github-actions
Copy link

github-actions bot commented Oct 19, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@carlobertolli carlobertolli force-pushed the copy_vcc_to_src_shared_based.tr branch from ec8bab7 to 080158a Compare October 19, 2025 02:07
@carlobertolli carlobertolli requested a review from jayfoad October 19, 2025 02:07
Copy link
Contributor

@shiltian shiltian left a comment

Choose a reason for hiding this comment

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

I don't think s_mov_b64 would allow SRC_SHARED_BASE as destination. It is not one of the categories in sdst.

@arsenm
Copy link
Contributor

arsenm commented Oct 20, 2025

I don't think s_mov_b64 would allow SRC_SHARED_BASE as destination. It is not one of the categories in sdst.

Then the instruction definitions are wrong, because this should fail the verifier if it's not valid

@shiltian
Copy link
Contributor

I think it does fail in the verifier, based on #163244 (comment).

@rampitec
Copy link
Collaborator

rampitec commented Oct 20, 2025

This is readonly register, like any src_. Yes, it shall not be used in SDST

@carlobertolli
Copy link
Member Author

carlobertolli commented Oct 20, 2025

Section 5.2 of the MI300 programming guide indicates: "[..] 128-255 can only be used as sources."
shared_base is 235, so it cannot be used as destination.
This does not fail anywhere. I can run lit tests and they pass.
Is there a way to fix the "verifier"? @arsenm

@carlobertolli
Copy link
Member Author

I ran it through the debugger looking at what happens in verifyInstruction in the same file. Both copies from VCC to SHARED_BASE and from SHARED_BASE to VCC get out at

4971 if (SIInstrInfo::isGenericOpcode(Opcode))
(gdb)
4972 return true;

There is no verification done on the src and dst registers if you get out of the function this early.

@carlobertolli
Copy link
Member Author

The comment after that early exit seems relevant:

// FIXME: At this point the COPY verify is done only for non-ssa forms.
// Find a better property to recognize the point where instruction selection
// is just done.
// We can only enforce this check after SIFixSGPRCopies pass so that the
// illegal copies are legalized and thereafter we don't expect a pass
// inserting similar copies.

I'll leave this alone for the time being, but please @arsenm let me know if there is any extra verification you think we should be doing.

@jayfoad
Copy link
Contributor

jayfoad commented Oct 21, 2025

I ran it through the debugger looking at what happens in verifyInstruction in the same file. Both copies from VCC to SHARED_BASE and from SHARED_BASE to VCC get out at

4971 if (SIInstrInfo::isGenericOpcode(Opcode)) (gdb) 4972 return true;

There is no verification done on the src and dst registers if you get out of the function this early.

That sounds like you're looking at a COPY instruction? MachineVerfier should still fail for an S_MOV_B64 instruction like the one you generate in this patch.

@carlobertolli
Copy link
Member Author

@jayfoad can you please point me to where the verification should happen?
I looked in MachineVerifier but the input is the COPY instruction, so no verification of the S_MOV_B64.

@jayfoad
Copy link
Contributor

jayfoad commented Oct 21, 2025

can you please point me to where the verification should happen? I looked in MachineVerifier but the input is the COPY instruction, so no verification of the S_MOV_B64.

You would have to run the verifier after you have converted the COPY to S_MOV_B64, so after the ExpandPostRAPseudos (aka postrapseudos) pass. You could run llc -verifymachineinstrs to verify after every pass.

@carlobertolli
Copy link
Member Author

Thanks, below is what I see. It doesn't report that shared_base is non writable, but a problem with src_shared_base not being a SReg_64 register, which is as good I guess.
I will run this next time I want to write a codegen pass.

# After Post-RA pseudo instruction expansion pass
# Machine code for function vcc_to_src_shared_base: IsSSA, NoPHIs, NoVRegs

bb.0:
$src_shared_base = S_MOV_B64 $vcc

# End machine code for function vcc_to_src_shared_base.

*** Bad machine code: Operand has incorrect register class. ***

  • function: vcc_to_src_shared_base
  • basic block: %bb.0 (0x6331cd9270f8)
  • instruction: $src_shared_base = S_MOV_B64 $vcc

*** Bad machine code: Illegal physical register for instruction ***

  • function: vcc_to_src_shared_base
  • basic block: %bb.0 (0x6331cd9270f8)
  • instruction: $src_shared_base = S_MOV_B64 $vcc
  • operand 0: $src_shared_base
    $src_shared_base is not a SReg_64 register.

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.

6 participants