| Issue |
240250
|
| Summary |
[LoopFlatten] gep+gep is rewritten to a GEP indexed by the narrow flattened IV, which passes the signed maximum and is sign-extended to a negative offset
|
| Labels |
miscompilation,
loopoptim,
generated by fuzzer
|
| Assignees |
|
| Reporter |
ChoKyuWon
|
### reproducer
godbolt: https://godbolt.org/z/6qW8Pzxbe
alive: https://alive2.llvm.org/ce/z/kSwqkC
```llvm
define i32 @main() {
entry:
%arr = alloca [256 x i8]
br label %outer
outer:
%i = phi i8 [ 0, %entry ], [ %i.next, %outer.latch ]
br label %inner
inner:
%j = phi i8 [ 0, %outer ], [ %j.next, %inner.latch ]
%row.off = mul i8 %i, 101
%row = getelementptr inbounds i8, ptr %arr, i8 %row.off
%p = getelementptr inbounds i8, ptr %row, i8 %j
store i8 7, ptr %p
br label %inner.latch
inner.latch:
%j.next = add i8 %j, 1
%j.cmp = icmp ult i8 %j.next, 101
br i1 %j.cmp, label %inner, label %outer.latch
outer.latch:
%i.next = add i8 %i, 1
%i.cmp = icmp ult i8 %i.next, 2
br i1 %i.cmp, label %outer, label %exit
exit:
ret i32 0
}
```
```console
$ opt -S -passes=loop-flatten issue-repro.ll -o out.ll
$ cat out.ll
...
entry:
%arr = alloca [256 x i8], align 1
%flatten.tripcount = mul i8 101, 2
br label %outer
outer: ; preds = %outer.latch, %entry
%i = phi i8 [ 0, %entry ], [ %i.next, %outer.latch ]
%flatten.p = getelementptr inbounds i8, ptr %arr, i8 %i
br label %inner
...
store i8 7, ptr %flatten.p, align 1
...
outer.latch: ; preds = %inner.latch
%i.next = add i8 %i, 1
%i.cmp = icmp ult i8 %i.next, %flatten.tripcount
br i1 %i.cmp, label %outer, label %exit
...
$ llubi --max-steps 1000000 issue-repro.ll
$ llubi --max-steps 1000000 out.ll
Stacktrace:
#0 store i8 7, ptr %flatten.p, align 1 at @main out.ll:20
Immediate UB detected: Invalid memory access with a poison pointer.
error: Execution of function 'main' failed.
$ alive-tv --disable-undef-input --disable-poison-input -passes=loop-flatten --src-unroll=202 --tgt-unroll=202 issue-repro.ll
...
Transformation doesn't verify!
ERROR: Source is more defined than target
...
ptr %flatten.p#128 = pointer(local, block_id=0, offset=127)
...
ptr %flatten.p#129 = poison
...
Summary:
0 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
```
### suggested fix
When the IVs are not widened (here no legal integer types are known; AVR, `n8` with 16-bit pointers, hits the same path), `DoFlattenLoopPair` in `llvm/lib/Transforms/Scalar/LoopFlatten.cpp` replaces the gep+gep use with `getelementptr %base, %outer.iv`, so the i8 IV (0..201) is sign-extended as an index and 128 becomes -128.
`checkOverflow` (`computeOverflowForUnsignedMul`) only proves that the trip-count product fits the unsigned range; the source indices `%i * 101` and `%j` each stay below 128; the fixes suggested in #234744 and #237378 (unsigned predicate, dropping `nsw`) do not touch the GEP index.
For a gep+gep use whose index type is narrower than the pointer index width, require the product to fit the signed range of that type (or bail).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs