[llvm-branch-commits] [llvm] AMDGPU: Fold inline immediates in peephole-opt (PR #208423)

Frederik Harwath via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Jul 9 04:54:11 PDT 2026


================
@@ -3899,9 +3915,41 @@ bool SIInstrInfo::foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
 
       return true;
     }
+
+    return false;
   }
 
-  return false;
+  // Early exit for generic instructions which will never fold an immediate.
+  if (!isTargetSpecificOpcode(UseMI.getOpcode()))
+    return false;
+
+  // Directly fold inline immediates into the uses. These should be free-ish
+  // regardless of the uses.
+  bool FoldedInlineImm = false;
+
+  for (MachineOperand &UseMO : UseMI.explicit_uses()) {
+    if (!UseMO.isReg() || UseMO.getReg() != Reg)
+      continue;
+
+    unsigned UseOpIdx = UseMO.getOperandNo();
+
+    int64_t ImmVal = Imm;
+    if (unsigned UseSubReg = UseMO.getSubReg()) {
+      std::optional<int64_t> SubImm = extractSubregFromImm(Imm, UseSubReg);
+      if (!SubImm)
+        continue;
+      ImmVal = *SubImm;
+    }
+
+    if (!isInlineConstant(UseMI, UseOpIdx, ImmVal))
+      continue;
+
+    UseMO.ChangeToImmediate(ImmVal);
+    FoldedInlineImm = true;
+  }
+
+  // TODO: Consider applying tryConstantFoldOp here
----------------
frederik-h wrote:

Why only "consider", i.e. why not do it now?

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


More information about the llvm-branch-commits mailing list