[llvm] [AMDGPU] Eliminate redundant s_round_mode writes (PR #216305)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 18:09:08 PDT 2026


================
@@ -514,6 +521,68 @@ bool SIPreEmitPeephole::removeExeczBranch(MachineInstr &MI,
   return true;
 }
 
+/// Remove writes to the FP round mode that can never be observed: either the
+/// value written is already live in MODE, or a later s_round_mode overwrites
+/// the whole round mode field before anything reads it.
+///
+/// This is a purely intra-block analysis: the round mode on entry to \p MBB is
+/// unknown, and a write that is still live at the end of the block is kept for
+/// the benefit of the successors.
+bool SIPreEmitPeephole::removeRedundantRoundMode(
+    MachineBasicBlock &SrcMBB) const {
+  MachineInstr *PendingWrite = nullptr;
+  std::optional<int64_t> CurrentRoundMode;
+  std::optional<int64_t> RoundModeBeforePendingWrite;
+  bool Changed = false;
+
+  for (MachineInstr &MI : make_early_inc_range(SrcMBB.instrs())) {
+    if (MI.isDebugInstr())
+      continue;
+
+    if (MI.getOpcode() == AMDGPU::S_ROUND_MODE) {
+      int64_t NewRoundMode = MI.getOperand(0).getImm();
+
+      if (PendingWrite && CurrentRoundMode != NewRoundMode) {
----------------
shiltian wrote:

If the old mode set instruction is not consumed, we can always remove it no matter whether its mode is same as the new one, right? And then the new mode set becomes PendingWrite.

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


More information about the llvm-commits mailing list