[llvm] [AMDGPU][CodeGen] Place `S_NOP` after `S_SETREG_IMM32_B32` in predecessor MBB (PR #209620)

Lucas Ramirez via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 15 04:10:42 PDT 2026


================
@@ -432,23 +433,68 @@ AMDGPULowerVGPREncoding::handleCoissue(MachineBasicBlock::instr_iterator I) {
   return I;
 }
 
+/// Returns whether \p MI is a S_SETREG_IMM32_B32(MODE).
+static bool isSetregMode(const MachineInstr &MI, const SIInstrInfo &TII) {
+  if (MI.getOpcode() != AMDGPU::S_SETREG_IMM32_B32)
+    return false;
+
+  const MachineOperand *SIMM16Op =
+      TII.getNamedOperand(MI, AMDGPU::OpName::simm16);
+  auto [HwRegId, _Offset, _Size] =
+      AMDGPU::Hwreg::HwregEncoding::decode(SIMM16Op->getImm());
+  return HwRegId == AMDGPU::Hwreg::ID_MODE;
+}
+
+/// Backtracks \p I in \p MBB until we hit a non-meta instruction and returns
+/// whether that instruction is a S_SETREG_IMM32_B32(MODE). Returns false when
+/// there are no non-meta instruction in [MBB.instr_begin(), It).
+static bool previousInstrIsSetRegMode(MachineBasicBlock::instr_iterator &It,
+                                      const MachineBasicBlock &MBB,
+                                      const SIInstrInfo &TII) {
+  while (It != MBB.begin()) {
+    It = std::prev(It);
+    if (isSetregMode(*It, TII))
+      return true;
+    if (!It->isMetaInstruction())
+      return false;
+  }
+  return false;
+}
+
 bool AMDGPULowerVGPREncoding::needNopBeforeSetVGPRMSB(
     MachineBasicBlock::instr_iterator I) {
-  while (I != MBB->begin()) {
-    I = std::prev(I);
-    if (I->getOpcode() == AMDGPU::S_SETREG_IMM32_B32) {
-      MachineOperand *SIMM16Op =
-          TII->getNamedOperand(*I, AMDGPU::OpName::simm16);
-      auto [HwRegId, Offset, Size] =
-          AMDGPU::Hwreg::HwregEncoding::decode(SIMM16Op->getImm());
-      if (HwRegId == AMDGPU::Hwreg::ID_MODE)
+  if (previousInstrIsSetRegMode(I, *MBB, *TII))
+    return true;
+  if (I != MBB->begin())
+    return false;
----------------
lucas-rami wrote:

My apologies I missed the `S_BRANCH` in my new test and got led to believe it worked. I fixed the test and removed the `previousInstrIsSetRegMode` helper which ended up making things more confusing. Thanks for the catch!

https://github.com/llvm/llvm-project/pull/209620


More information about the llvm-commits mailing list