[llvm] Prevent undefined behavior caused by combination of branch and load delay slots on MIPS1 (PR #185427)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 9 07:23:10 PDT 2026
https://github.com/JaberwockySeamonstah created https://github.com/llvm/llvm-project/pull/185427
Under certain conditions the LLVM `MipsDelaySlotFiller` fills a branch delay slot with an instruction requiring a load delay slot. However the `MipsDelaySlotFiller` does not check the filled instruction for hazard which leads to code like this:
```asm
beqz $1, $BB0_5
lbu $2, %lo(_RNvCs5jWYnRsDZoD_3app13CONTROLLERS_A)($2)
# --- Some other instructions
$BB0_5:
andi $1, $2, 1
```
`lbu` got moved into the branch delay slot but has a load delay slot - so when jumping to `$BB0_5` the value for `$2` will not be ready, which leads to undefined behavior.
This PR suggests to declare instructions with a load delay slot to be hazardous for the branch delay slot, only for `MIPS1`. This will prevent the load instructions in the branch delay slot, which has a slight impact on the optimization.
Ideally in case of a load instruction in a branch delay slot, we would want to check the target register and check if it is used in the following instruction and at the branch destination instruction. Code for this is already in place from a previous PR (`bool MipsInstrInfo::SafeInLoadDelaySlot(const MachineInstr &MIInSlot, const MachineInstr &LoadMI) const`), however I'm not experienced enough with the LLVM to identify the `MachineInstr` required for that ideal situation.
If I could get some feedback about this I might be able to stitch it in.
The original issue came from Rust and is described [here rust issue 150676](https://github.com/rust-lang/rust/issues/150676). It was then raised in the LLVM project [here issue 180639](https://github.com/llvm/llvm-project/issues/180639#issuecomment-3874380424) and in the forum [here](https://discourse.llvm.org/t/where-to-start-fixing-an-opt-pass-for-mips1/89857).
>From 92875f42d3ff839aafae45afaee879edc92f0d8e Mon Sep 17 00:00:00 2001
From: Jaby <jaby at william.zone>
Date: Sat, 7 Mar 2026 18:30:38 +0000
Subject: [PATCH] Disable filling delay slots with load delay slots commands
under MIPS1
---
llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp | 29 ++++++++++++++++----
1 file changed, 24 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp b/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp
index f04ce53340c5d..d3965d3671e66 100644
--- a/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp
+++ b/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp
@@ -229,8 +229,8 @@ namespace {
/// This function checks if it is valid to move Candidate to the delay slot
/// and returns true if it isn't. It also updates memory and register
/// dependence information.
- bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
- InspectMemInstr &IM) const;
+ bool delayHasHazard(const MipsSubtarget& STI, const MachineInstr &Candidate,
+ RegDefsUses &RegDU, InspectMemInstr &IM) const;
/// This function searches range [Begin, End) for an instruction that can be
/// moved to the delay slot. Returns true on success.
@@ -789,10 +789,11 @@ bool MipsDelaySlotFiller::searchRange(MachineBasicBlock &MBB, IterTy Begin,
continue;
}
- if (delayHasHazard(*CurrI, RegDU, IM))
+ const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
+
+ if (delayHasHazard(STI, *CurrI, RegDU, IM))
continue;
- const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
bool InMicroMipsMode = STI.inMicroMipsMode();
const MipsInstrInfo *TII = STI.getInstrInfo();
unsigned Opcode = (*Slot).getOpcode();
@@ -1000,7 +1001,8 @@ bool MipsDelaySlotFiller::examinePred(MachineBasicBlock &Pred,
return true;
}
-bool MipsDelaySlotFiller::delayHasHazard(const MachineInstr &Candidate,
+bool MipsDelaySlotFiller::delayHasHazard(const MipsSubtarget& STI,
+ const MachineInstr &Candidate,
RegDefsUses &RegDU,
InspectMemInstr &IM) const {
assert(!Candidate.isKill() &&
@@ -1011,6 +1013,23 @@ bool MipsDelaySlotFiller::delayHasHazard(const MachineInstr &Candidate,
HasHazard |= IM.hasHazard(Candidate);
HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
+ if(STI.hasMips1()) {
+ const MipsInstrInfo* TII = STI.getInstrInfo();
+ const bool HasLoadDelaySlot = TII->HasLoadDelaySlot(Candidate);
+
+ // Having an instruction with a load delay slot in the branch delay slot
+ // seems to not check the load delay slots for hazards.
+ // Not allowing load delay slot commands in branch delays produces always
+ // valid code but may reduce optimization slightly.
+ //
+ // TODO: Ideally we would want to check the register being used in this command
+ // with the registers of the next command and for the jump target. If we can not
+ // locate the jump target, then we assume hazard.
+ if(HasLoadDelaySlot) {
+ return true;
+ }
+ }
+
return HasHazard;
}
More information about the llvm-commits
mailing list