| Issue |
243743
|
| Summary |
[Attributor] Load through a pointer returned by a callee is tagged !invariant.load although the caller overwrites that memory
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
Samtz2483
|
`opt -passes=attributor` adds `!invariant.load` to loads in `@f` below, although `@f` itself overwrites the loaded memory with a `memcpy` right after the first load. A later GVN then folds `@f` to `ret i1 true`.
```llvm
define ptr @get(ptr %t) {
%b = load ptr, ptr %t
ret ptr %b
}
define i1 @f(ptr %t, ptr %src) {
%p = call ptr @get(ptr %t)
%g = getelementptr inbounds i8, ptr %p, i64 8
%old = load i64, ptr %g
call void @llvm.memcpy.p0.p0.i64(ptr %p, ptr %src, i64 16, i1 false)
%new = load i64, ptr %g
%same = icmp eq i64 %old, %new
ret i1 %same
}
declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)
```
`opt -passes=attributor -S` (trunk, [commit / Compiler Explorer]):
```llvm
define i1 @f(ptr nofree readonly align 8 captures(none) %t, ptr nofree readonly captures(none) %src) #1 {
%p = call ptr @get(ptr nofree noundef readonly align 8 captures(none) %t) #3
%g = getelementptr inbounds i8, ptr %p, i64 8
%old = load i64, ptr %g, align 4, !invariant.load !0
call void @llvm.memcpy.p0.p0.i64(ptr noundef %p, ptr readonly captures(none) %src, i64 noundef 16, i1 noundef false) #4
%new = load i64, ptr %g, align 4, !invariant.load !0
%same = icmp eq i64 %old, %new
ret i1 %same
}
```
`opt -passes='attributor,function(gvn)' -S`:
```llvm
define i1 @f(ptr nofree readonly align 8 captures(none) %t, ptr nofree readonly captures(none) %src) #1 {
%p = call ptr @get(ptr nofree noundef readonly align 8 captures(none) %t) #3
%g = getelementptr inbounds i8, ptr %p, i64 8
call void @llvm.memcpy.p0.p0.i64(ptr noundef %p, ptr readonly captures(none) %src, i64 noundef 16, i1 noundef false) #4
ret i1 true
}
```
`-passes=gvn` alone keeps the comparison. The result of `@f` depends on `%src`, so `ret i1 true` is wrong.
Observations:
- If the pointer is loaded directly in `@f` (`%p = load ptr, ptr %t` instead of the call), no `!invariant.load` is added. The call is needed.
- A plain `store` through a second GEP to the same address (instead of the `memcpy`) also gets the load tagged.
- `-passes=attributor-cgscc` does not tag the load.
**Versions** (Compiler Explorer, `-passes='attributor,function(gvn)'`):
- correct: 20.1.0 ...
- wrong: 21.1.0 ... trunk
Compiler Explorer: https://godbolt.org/z/YYEes7bKM
**Related:** #229959 also reported wrong `!invariant.load` tags; its fix (#231635) is included in the trunk I tested, and this case still reproduces. #147706 is about a value written before the load in another function; here the write comes after the load, in the same function.
**How it was found:** while tuning pass pipelines for code size, the pipeline `module(scc-oz-module-inliner,attributor)` made Duktape 2.7.0 (`duktape.c`, clang 18.1.8 bitcode, opt/llc trunk) segfault on every test, e.g. `print(1+2)`. Removing all 729 `!invariant.load` tags from the output fixes it, and keeping just one of them reproduces the crash: a load in `duk_replace()` that reads the old value of a value-stack slot right before the slot is overwritten with `memcpy`. The reduced example above has the same shape. Duktape was checked with ASan/UBSan and MSan beforehand; apart from stack overflows in deeply recursive tests, the only reports were UBSan's null-plus-zero pointer arithmetic and an out-of-range float-to-unsigned conversion in 4 of 1152 tests, while the miscompiled build crashes on every test.
**AI disclosure:** The miscompile was found in my own experiments. I used an AI assistant (Claude) to help localize and reduce the test case and draft this report; I have reviewed and reproduced the results myself.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs