Issue 242203
Summary [Attributor] Store to a caller's alloca inside a callee called in a loop is deleted; loop counter folded to its initial value (regression since LLVM 16)
Labels new issue
Assignees
Reporter Samtz2483
    `opt -passes=attributor` miscompiles the following IR. `@count(n)` should return `n` (for `n >= 0`); after Attributor, the store in `@inc` is removed and the load of the loop counter in `@count` is replaced by its initial value `0`, so `@count(n)` loops forever for any `n > 0`.

```llvm
define internal void @inc(ptr %p) {
  %v = load i32, ptr %p, align 4
  %n = add nsw i32 %v, 1
  store i32 %n, ptr %p, align 4
  ret void
}

define i32 @count(i32 %n) {
entry:
  %i = alloca i32, align 4
 store i32 0, ptr %i, align 4
  br label %cond

cond:
  %v = load i32, ptr %i, align 4
  %c = icmp slt i32 %v, %n
  br i1 %c, label %body, label %exit

body:
  call void @inc(ptr %i)
  br label %cond

exit:
  ret i32 %v
}
```

Output of `opt -passes=attributor -S` (trunk, LLVM 24.0.0git, on Compiler Explorer; the assertions build gives the same output and no assertion):

```llvm
define internal void @inc(ptr noalias nofree noundef nonnull writeonly align 4 captures(none) dereferenceable(4) %p) #0 {
  ret void
}

define noundef i32 @count(i32 %n) #1 {
entry:
  %i = alloca i32, align 4
  br label %cond

cond:
  %c = icmp slt i32 0, %n
 br i1 %c, label %body, label %exit

body:
  call void @inc(ptr noalias nofree noundef nonnull writeonly align 4 captures(none) dereferenceable(4) %i) #2
  br label %cond

exit:
  ret i32 0
}
```

The store in `@inc` writes the caller's `%i`, and that value is read by the load in `%cond` on the next iteration through the back edge, but Attributor treats the store as dead and the load as always seeing the initial `store i32 0`.

In C terms:

```c
static void inc(int *p) { (*p)++; }
int count(int n) { int i = 0; while (i < n) inc(&i); return i; }
```

**Versions** (Compiler Explorer, same input, `-passes=attributor`):
- correct: 13.0.0, 14.0.0 (typed-pointer version of the same IR), 15.0.0
- wrong: 16.0.0, 17.0.1, 18.1.0, 19.1.0, 20.1.0, 21.1.0, 22.1.0, 23.1.0, trunk
- also wrong locally with Ubuntu's 18.1.8

**Possibly related:** #236104 (loop counter folded to its initial value, all in one function; fix proposed in #236114). This case looks different: the store is in a callee, the same loop with the increment written inline in `@count` is *not* miscompiled by 18.1.8, and the test from #236114 is handled correctly up to 17.0.1 and goes wrong from 18.1.0, whereas this one goes wrong from 16.0.0. I have not checked whether #236114 fixes this case.

**How it was found:** while tuning pass pipelines for code size, `module(iroutliner,attributor)` turned PolyBench `durbin`/`gesummv` into infinite loops. IROutliner had outlined the loop increment `i++` into a function taking `&i`, which produces exactly the pattern above; running Attributor alone on the (correct) post-IROutliner IR reproduces the hang, so IROutliner is not needed. The programs were checked with ASan/UBSan/MSan and valgrind beforehand.

**AI disclosure:** The miscompile was found in my own experiments. I used an AI assistant (Claude) to help 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

Reply via email to