[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:03:16 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) {
+ LLVM_DEBUG(dbgs() << "Removing dead round mode write: "
+ << *PendingWrite);
+ PendingWrite->eraseFromParent();
+ ++NumRoundModeWritesRemoved;
+ Changed = true;
+ PendingWrite = nullptr;
+ CurrentRoundMode = RoundModeBeforePendingWrite;
+ }
+
+ if (CurrentRoundMode == NewRoundMode) {
+ LLVM_DEBUG(dbgs() << "Removing redundant round mode write: " << MI);
+ MI.eraseFromParent();
+ ++NumRoundModeWritesRemoved;
+ Changed = true;
+ continue;
+ }
+
+ RoundModeBeforePendingWrite = CurrentRoundMode;
+ PendingWrite = &MI;
+ CurrentRoundMode = NewRoundMode;
+ continue;
+ }
+
+ if (!PendingWrite && !CurrentRoundMode)
+ continue;
+
+ if (MI.isCall() || MI.isInlineAsm() ||
+ MI.modifiesRegister(AMDGPU::MODE, TRI)) {
+ PendingWrite = nullptr;
+ CurrentRoundMode.reset();
+ continue;
+ }
+
+ if (MI.readsRegister(AMDGPU::MODE, TRI) || MI.hasUnmodeledSideEffects()) {
+ PendingWrite = nullptr;
+ }
----------------
shiltian wrote:
```suggestion
if (MI.readsRegister(AMDGPU::MODE, TRI) || MI.hasUnmodeledSideEffects())
PendingWrite = nullptr;
```
https://github.com/llvm/llvm-project/pull/216305
More information about the llvm-commits
mailing list