[all-commits] [llvm/llvm-project] 5e29d8: Prevent undefined behavior caused by combination o...
JaberwockySeamonstah via All-commits
all-commits at lists.llvm.org
Mon May 4 14:47:44 PDT 2026
Branch: refs/heads/release/22.x
Home: https://github.com/llvm/llvm-project
Commit: 5e29d8aa0481725f4818c822f8c4e4deb726fe88
https://github.com/llvm/llvm-project/commit/5e29d8aa0481725f4818c822f8c4e4deb726fe88
Author: JaberwockySeamonstah <76793519+JaberwockySeamonstah at users.noreply.github.com>
Date: 2026-05-04 (Mon, 04 May 2026)
Changed paths:
M llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp
M llvm/lib/Target/Mips/MipsInstrInfo.cpp
M llvm/test/CodeGen/Mips/gprestore.ll
M llvm/test/CodeGen/Mips/llvm-ir/load.ll
M llvm/test/CodeGen/Mips/llvm-ir/select-dbl.ll
A llvm/test/CodeGen/Mips/mips1-load-in-delay-slot.ll
M llvm/test/CodeGen/Mips/unalignedload.ll
Log Message:
-----------
Prevent undefined behavior caused by combination of branch and load delay slots on MIPS1 (#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).
Co-authored-by: Jaby <jaby at william.zone>
(cherry picked from commit 458e9c452c103189865632657504174b24b91630)
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list