| Issue |
235005
|
| Summary |
[GlobalISel] matchLoadOrCombine compares byte offsets with narrow-load indices and merges overlapping i16 loads into an i32 load
|
| Labels |
miscompilation,
llvm:globalisel,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/WPGGdsMxe
```llvm
target triple = "aarch64-unknown-linux-gnu"
@buf = constant [4 x i8] c"\01\02\03\04"
define i32 @f(ptr %p) {
%lo = load i16, ptr %p, align 1
%lo.z = zext i16 %lo to i32
%q = getelementptr i8, ptr %p, i64 1
%hi = load i16, ptr %q, align 1
%hi.z = zext i16 %hi to i32
%hi.s = shl i32 %hi.z, 16
%r = or i32 %lo.z, %hi.s
ret i32 %r
}
define i32 @main() {
%r = call i32 @f(ptr @buf)
%b3 = lshr i32 %r, 24
ret i32 %b3
}
```
```console
$ llc -O2 -global-isel repro.ll -o -
f: // @f
ldr w0, [x0]
ret
...
$ llc -O2 -global-isel -stop-after=aarch64-prelegalizer-combiner repro.ll -o - | grep G_LOAD
%9:_(i32) = G_LOAD %0(p0) :: (load (s32) from %ir.p, align 1)
$ llc -O2 repro.ll -o -
f: // @f
ldrh w8, [x0]
ldurh w9, [x0, #1]
orr w0, w8, w9, lsl #16
ret
...
$ sed 's/i64 1$/i64 2/' repro.ll | llc -O2 -global-isel -o -
f: // @f
ldrh w8, [x0]
ldrh w9, [x0, #2]
orr w0, w8, w9, lsl #16
ret
...
$ llubi repro.ll; echo $?
3
$ llc -O2 -global-isel -filetype=obj repro.ll -o a.o && aarch64-linux-gnu-gcc -static a.o && qemu-aarch64 ./a.out; echo $?
4
$ llc -O2 -filetype=obj repro.ll -o a.o && aarch64-linux-gnu-gcc -static a.o && qemu-aarch64 ./a.out; echo $?
3
```
### suggested fix
`findLoadOffsetsForLoadOrCombine` records `MemOffset2Idx[DstPos] = Idx`, where `DstPos` (`Shift / MemSizeInBits`) is a narrow-load index but `Idx` is the `G_PTR_ADD` constant in bytes, and `isBigEndian` compares `Idx - LowestIdx` with `littleEndianByteAt(Width, MemOffset)`, i.e. with a narrow-load index.
For `i16` loads the overlapping load at byte offset 1 is accepted as element 1, while the real element at byte offset 2 is rejected.
Reject `Idx % (MemSizeInBits / 8) != 0` and divide `Idx` by `MemSizeInBits / 8` before `SeenIdx`/`MemOffset2Idx`; the `s16_loads_to_*` tests in `prelegalizer-combiner-load-or-pattern.mir` use 1-byte `G_PTR_ADD` offsets and expect the merge.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs