Issue 234099
Summary [NewGVN] Pointer equality propagation breaks noalias, causing a miscompile with newgvn,gvn
Labels new issue
Assignees
Reporter SB1491
    This appears to be a remaining NewGVN case of [#59679](https://github.com/llvm/llvm-project/issues/59679), which was closed by [#217350](https://github.com/llvm/llvm-project/pull/217350).

GVN alone now handles the example correctly, but running `newgvn,gvn` still produces an incorrect result with Compiler Explorer's `opttrunk`, reporting LLVM `24.0.0git`.

### Reproducer

Save as `repro.ll`:

```llvm
@g = global i32 0, align 4

define i32 @f(ptr noalias %p) {
entry:
  store i32 1, ptr %p, align 4
  %eq = icmp eq ptr %p, @g
  br i1 %eq, label %yes, label %join

yes:
  store i32 2, ptr %p, align 4
  br label %join

join:
 %v = load i32, ptr %p, align 4
  ret i32 %v
}

define i32 @main() {
  %r = call i32 @f(ptr @g)
  ret i32 %r
}
```

Run:

```sh
opt -S -verify-each -passes=newgvn,gvn repro.ll -o optimized.ll
```

### Expected behavior

`main()` must return `2`.

In the original program, all memory accesses in `f` use `%p`. The reference to `@g` in `icmp` only compares addresses; it does not access the global's contents. Thus, calling `f(@g)` does not violate the argument's `noalias` contract.

### Actual behavior

The optimized function returns `1` unconditionally:

```llvm
define i32 @f(ptr noalias %p) {
entry:
  store i32 1, ptr %p, align 4
  %eq = icmp eq ptr %p, @g
  br i1 %eq, label %yes, label %join

yes:
  store i32 2, ptr @g, align 4
  br label %join

join:
  ret i32 1
}
```

### Transformation responsible

Running only NewGVN:

```sh
opt -S -verify-each -passes=newgvn repro.ll
```

changes the conditional store from:

```llvm
store i32 2, ptr %p, align 4
```

to:

```llvm
store i32 2, ptr @g, align 4
```

while retaining the final load through `%p`.

GVN subsequently treats the store through `@g` as non-aliasing with the load through the `noalias` argument `%p`, and forwards the initial value `1`.

The successful address comparison does not justify this substitution: it changes the pointer's based-on relationship and introduces a `noalias` violation that was absent from the original program.

### Verification and controls

Using Compiler Explorer's `opttrunk` (`LLVM 24.0.0git`):

| Pipeline | Result for `f(@g)` |
|---|---|
| `gvn` | Correct: `2` |
| `newgvn,gvn` | Incorrect: `1` |
| `newgvn,gvn`, with `noalias` removed | Correct: `2` |
| `default<O2>` | Correct: emitted `main` returns `2` |

For execution verification, I ran the original IR and the IR produced by the remote LLVM 24 build locally with `lli-18` (LLVM 18.1.3). They exited with `2` and `1`, respectively. The default `O2` result above was checked by inspecting the emitted IR.

The explicit `newgvn,gvn` pipeline also reproduces with local LLVM 18.1.3. I do not have the exact commit hash of Compiler Explorer's LLVM 24 build.

### Related implementation and discussion

- [#217350](https://github.com/llvm/llvm-project/pull/217350) restricts pointer replacement with writable globals and adds a regression test using `-passes=gvn`.
- In [NewGVN at commit `310a25aba219`](https://github.com/llvm/llvm-project/blob/310a25aba219fec4b5a76202dc6421e93c807c52/llvm/lib/Transforms/Scalar/NewGVN.cpp#L1568), `performSymbolicPredicateInfoEvaluation` handles `ICMP_EQ` without an equivalent pointer-replacement validity check.
- [This earlier discussion](https://github.com/llvm/llvm-project/issues/34577#issuecomment-1103640170) also suggested applying `canReplacePointersIfEqual` in NewGVN.

It looks like NewGVN needs an equivalent restriction on equality-based pointer substitution, with a regression test covering this pipeline.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to