Commit fec2cba
authored
[InstCombine] Fix miscompile when folding a select into a masked load (#216730)
`visitSelectInst` folds:
select(mask, masked.load(ptr, mask, PT), FV)
into:
masked.load(ptr, mask, FV)
The replacement load was previously created at the select, effectively
moving the memory access past any intervening instructions. If one of
them writes the loaded memory, the replacement load reads the updated
value instead of the original one. This was also observed downstream in
[ispc/ispc#3891](ispc/ispc#3891).
The fold was added in `eb8589987267`. The issue is labelled
`regression:22`, so it affects LLVM 22.1 as well as current trunk.
Create the replacement load at the original load's position and require
`FV` to be available there. Otherwise, leave the select unchanged.
Requiring `FV` to be available at the original load means the fold no
longer fires when `FV` is computed between the load and the select. No
existing `llvm/test/Transforms` checks change as a result of this
restriction.
Tests cover an intervening aliasing store and the case where `FV` is
unavailable at the original load. They also guard against carrying over
call-site attributes such as `range` and `noundef` when those attributes
no longer apply.
Fixes #2154531 parent 796a186 commit fec2cba
2 files changed
Lines changed: 51 additions & 5 deletions
File tree
- llvm
- lib/Transforms/InstCombine
- test/Transforms/InstCombine
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5379 | 5379 | | |
5380 | 5380 | | |
5381 | 5381 | | |
5382 | | - | |
5383 | | - | |
5384 | | - | |
5385 | | - | |
5386 | | - | |
| 5382 | + | |
| 5383 | + | |
| 5384 | + | |
| 5385 | + | |
| 5386 | + | |
| 5387 | + | |
| 5388 | + | |
| 5389 | + | |
| 5390 | + | |
| 5391 | + | |
5387 | 5392 | | |
5388 | 5393 | | |
5389 | 5394 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
172 | 212 | | |
173 | 213 | | |
174 | 214 | | |
| |||
184 | 224 | | |
185 | 225 | | |
186 | 226 | | |
| 227 | + | |
0 commit comments