[llvm] [AMDGPU][SIInsertWaitcnts][NFC] Clean up loop (PR #179572)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 4 10:12:54 PST 2026


================
@@ -3010,36 +3010,36 @@ bool SIInsertWaitcnts::insertWaitcntInBlock(MachineFunction &MF,
   MachineInstr *OldWaitcntInstr = nullptr;
   AtomicRMWState RMWState = AtomicRMWState::NotInBlock;
 
+  // NOTE: we may erase Inst and/or may append instrs after Inst while iterating
   for (MachineBasicBlock::instr_iterator Iter = Block.instr_begin(),
                                          E = Block.instr_end();
-       Iter != E;) {
+       Iter != E; ++Iter) {
     MachineInstr &Inst = *Iter;
-    if (Inst.isMetaInstruction()) {
-      ++Iter;
+    if (Inst.isMetaInstruction())
       continue;
-    }
     // Get the atomic RMW block state for current instruction.
     RMWState = getAtomicRMWState(Inst, RMWState);
 
     // Track pre-existing waitcnts that were added in earlier iterations or by
     // the memory legalizer.
     if (isWaitInstr(Inst) ||
         (IsExpertMode && Inst.getOpcode() == AMDGPU::S_WAITCNT_DEPCTR)) {
-      ++Iter;
       bool IsSoftXcnt = isSoftXcnt(Inst);
       // The Memory Legalizer conservatively inserts a soft xcnt before each
       // atomic RMW operation. However, for sequences of back-to-back atomic
       // RMWs, only the first s_wait_xcnt insertion is necessary. Optimize away
       // the redundant soft xcnts when we're inside an atomic RMW block.
-      if (Iter != E && IsSoftXcnt) {
+      if (&Inst != &Block.back() && IsSoftXcnt) {
         // Check if the next instruction can potentially change the atomic RMW
         // state.
-        RMWState = getAtomicRMWState(*Iter, RMWState);
+        RMWState = getAtomicRMWState(*Inst.getNextNode(), RMWState);
       }
 
       if (IsSoftXcnt && RMWState == AtomicRMWState::InsideBlock) {
         // Delete this soft xcnt.
+        auto NextIt = std::next(Iter);
         Inst.eraseFromParent();
----------------
jayfoad wrote:

Maybe MachineInstr::eraseFromParent should return an iterator like Instruction::eraseFromParent does.

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


More information about the llvm-commits mailing list