[llvm] [AMDGPU] Fix SIFoldOperands miscompiling values that leave a divergent loop (PR #203256)

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 11 09:12:50 PDT 2026


================
@@ -970,6 +982,22 @@ bool SIFoldOperandsImpl::isUseSafeToFold(const MachineInstr &MI,
   return !TII->isSDWA(MI);
 }
 
+// An SGPR->VGPR copy inside a divergent loop latches each lane value as it
+// exits. Folding its scalar source into a use after the loop would make every
+// lane read the same reconverged value, so do not fold across the loop exit.
+bool SIFoldOperandsImpl::isRegFoldSafeAcrossLoopExit(
+    const FoldableDef &OpToFold, const MachineInstr &UseMI) const {
+  if (!OpToFold.isReg())
+    return true;
+  const MachineInstr *DefMI = OpToFold.DefMI;
+  if (!DefMI || !DefMI->isCopy() ||
+      !TRI->isVGPR(*MRI, DefMI->getOperand(0).getReg()) ||
----------------
aobolensk wrote:

Applied

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


More information about the llvm-commits mailing list