Issue 244164
Summary [EarlyCSE] CSE of a readnone call keeps the surviving call's `!range`, so users of the replaced call (which had no `!range`) see poison
Labels miscompilation, llvm:transforms, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

There is no C path (clang attaches `!range` only to loads, never to calls; the Attributor and AMDGPU's work-item id lowering do attach it to calls). Run with `clang -O2 repro-main.ll -o r && ./r`:

```llvm
; [EarlyCSE] CSE of a readnone call keeps the surviving call's !range: %b (no !range) is replaced
; by %a (!range [0,10)), so `icmp ult %b, 10` folds to true although g(5) = 55.
;
; IR-only: clang never attaches !range to calls (only to loads); passes such as the Attributor and
; AMDGPU's work-item id lowering do.
;
; Godbolt: language "LLVM IR", compiler "x86-64 clang (trunk)", flags: -O2, "Execute the code"
;   (also -O1/-O3; `opt -passes=early-cse,instcombine` shows `ret i32 1`).
;
; EXPECTED (correct compiler; clang -O0), exit 0:
; f(5) = 0 (expected 0)
; BUGGY (clang trunk -O2), exit 1:
;   f(5) = 1 (expected 0)

@x = global i32 5
@sink = global i32 0
@fmt = private constant [24 x i8] c"f(5) = %d (expected 0)\0A\00"
declare i32 @printf(ptr, ...)

define i32 @f(i32 %l, ptr %p) noinline {
  %a = call i32 @g(i32 %l) #0, !range !0
  store i32 %a, ptr %p
  %b = call i32 @g(i32 %l) #0
  %c = icmp ult i32 %b, 10
  %r = zext i1 %c to i32
  ret i32 %r
}

define i32 @g(i32 %x) noinline #0 {
  %r = add i32 %x, 50 ; g(5) = 55, outside [0,10): only %a is poison
  ret i32 %r
}

define i32 @main() {
  %l = load volatile i32, ptr @x
  %r = call i32 @f(i32 %l, ptr @sink)
  call i32 (ptr, ...) @printf(ptr @fmt, i32 %r)
 ret i32 %r
}

attributes #0 = { nounwind willreturn memory(none) }
!0 = !{i32 0, i32 10}
```

Resulted in this output:
```
$ clang -O2 repro-main.ll -o r && ./r
f(5) = 1 (expected 0) (exit 1; also -O1 / -O3)
$ clang -O0 repro-main.ll -o r && ./r
f(5) = 0 (expected 0)                          (exit 0)

opt -passes=early-cse:  %b is replaced by %a, which keeps !range !{i32 0, i32 10};
then instcombine: %c = icmp ult i32 %a, 10  ->  true;  @f becomes `ret i32 1`.
(gvn / newgvn perform the same CSE but drop the !range.)
```

Trunk: https://godbolt.org/z/1hr87x8qE (LLVM IR, x86-64 clang trunk -O2, executed; reproduces on 81d345fc0e, 2026-11-02)

Root cause: `EarlyCSE` (llvm/lib/Transforms/Scalar/EarlyCSE.cpp) CSEs a `memory(none)` call through `AvailableValues` (and a `readonly` call through `AvailableCalls`), and in both cases only calls `combineIRFlags(Inst, V)`. For calls, that function intersects the poison-generating *attributes* (`tryIntersectAttributes`), but it never combines *metadata*. The surviving call keeps its `!range`, so where the replaced call returned an out-of-range value (55 here, which is not poison for `%b`), all of `%b`'s users now see poison. The load path (`combineMetadataForCSE(I, &Inst, false)`), GVN and NewGVN all handle this correctly.

Suggested fix: Also combine metadata for calls in `combineIRFlags`. `combineMetadataForCSE` drops `!range` unless both calls carry it, in which case the ranges are unioned:
```diff
     // For NDEBUG Compile.
     (void)Success;
+    // Likewise for poison-generating metadata (!range, ...): the surviving
+    // call must not keep metadata that the replaced call does not have.
+ combineMetadataForCSE(cast<Instruction>(To), &From, /*DoesKMove=*/false);
 }
```
(fix.diff applies to 81d345fc0e.)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to