[llvm] [AMDGPU] Fix iterator type in fixVALUMaskWriteHazard for bundled instr (PR #178195)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 27 04:47:43 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Dmitry Sidorov (MrSidims)

<details>
<summary>Changes</summary>

Use reverse_instr_iterator instead of reverse_iterator when iterating backwards from MI to find wait instructions. Otherwise if we start from somewhere in the middle of the block we fail with assertion.

Fixes: https://github.com/llvm/llvm-project/issues/172331

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


2 Files Affected:

- (modified) llvm/lib/Target/AMDGPU/GCNHazardRecognizer.cpp (+3-2) 
- (added) llvm/test/CodeGen/AMDGPU/valu-mask-write-hazard-getpc-bundle.ll (+22) 


``````````diff
diff --git a/llvm/lib/Target/AMDGPU/GCNHazardRecognizer.cpp b/llvm/lib/Target/AMDGPU/GCNHazardRecognizer.cpp
index d504d8618b90d..47ff6018b05ef 100644
--- a/llvm/lib/Target/AMDGPU/GCNHazardRecognizer.cpp
+++ b/llvm/lib/Target/AMDGPU/GCNHazardRecognizer.cpp
@@ -3498,8 +3498,9 @@ bool GCNHazardRecognizer::fixVALUMaskWriteHazard(MachineInstr *MI) {
     // This is expected to be a very short walk within the same block.
     SmallVector<MachineInstr *> ToErase;
     unsigned Found = 0;
-    for (MachineBasicBlock::reverse_iterator It = MI->getReverseIterator(),
-                                             End = MI->getParent()->rend();
+    for (MachineBasicBlock::reverse_instr_iterator
+             It = MI->getReverseIterator(),
+             End = MI->getParent()->instr_rend();
          Found < WaitInstrs.size() && It != End; ++It) {
       MachineInstr *WaitMI = &*It;
       // Find next wait instruction.
diff --git a/llvm/test/CodeGen/AMDGPU/valu-mask-write-hazard-getpc-bundle.ll b/llvm/test/CodeGen/AMDGPU/valu-mask-write-hazard-getpc-bundle.ll
new file mode 100644
index 0000000000000..2dcd6695c65a1
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/valu-mask-write-hazard-getpc-bundle.ll
@@ -0,0 +1,22 @@
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -mattr=+wavefrontsize64 < %s | FileCheck %s
+;
+; This test validates that the VALU mask write hazard pass correctly handles
+; bundled instructions.
+
+; CHECK-LABEL: main:
+; CHECK: s_getpc_b64
+; CHECK: s_waitcnt_depctr
+
+define amdgpu_cs i32 @main(i32 %arg, i32 %arg1) {
+bb:
+  %i = udiv i32 %arg, %arg1
+  %i2 = uitofp i32 %i to float
+  %i3 = udiv i32 1, %arg
+  %i4 = uitofp i32 %i3 to float
+  %i5 = call float @llvm.fma.f32(float %i2, float 0.000000e+00, float %i4)
+  %i6 = fptosi float %i5 to i32
+  %i7 = call i32 @func(i32 0, i32 %i6)
+  ret i32 %i7
+}
+
+declare i32 @func(i32, i32)

``````````

</details>


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


More information about the llvm-commits mailing list