| Issue |
241246
|
| Summary |
[LoopUnroll] cloneNoAliasScopes splits a disjoint-scope domain; gives false NoAlias (miscompile at -O3)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
jeffchen006
|
Inlined `restrict` scopes use one disjoint-scope domain. When the unroller clones the scopes, only scopes that still have a `noalias.scope.decl` move to the new domain. The domain is split and ScopedNoAliasAA returns a false NoAlias; SLP then merges stores across a call that reads them.
**Reproducer** (`repro.c`)
```c
/* copy(b, c) copies c[0..1] to b[0..1], then h squares b[0..1]. The three calls
fill a[0..5] with the squares of a[5..10]; a[0..3] must be 1 4 9 16.
Clang -O3 prints 1 4 3 4: the squares of the second call are lost. */
int printf(const char *, ...);
typedef void (*fn)(unsigned short *, unsigned short *, unsigned);
void mul(unsigned short *p, unsigned short *q, unsigned n) {
for (unsigned i = 0; i < n; i++)
p[i] = p[i] * q[i];
}
fn volatile hv = mul; /* unknown call target for the optimizer */
unsigned short a[11] = {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6};
void copy(unsigned short *restrict b, unsigned short *restrict c, unsigned n, fn h) {
for (unsigned i = 0; i < n; i++)
b[i] = c[i];
h(b, b, n);
}
int main(void) {
fn h = hv;
for (unsigned j = 0; j < 3; j++)
copy(a + j * 2, a + 5 + j * 2, 2, h);
printf("%u %u %u %u\n", a[0], a[1], a[2], a[3]);
return 0;
}
```
**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang -O0 repro.c -o o0
$ ./o0
1 4 9 16
$ clang -O3 repro.c -o o3
$ ./o3
1 4 3 4
```
Expected (as `-O0`): `1 4 9 16`. `-O3` prints `1 4 3 4`.
**Why the expected output is right**
Restrict rules (C11 6.7.3.1): in each call `copy(b, c, 2, h)` the program modifies through `b` only `b[0..1]` and reads through `c` only `c[0..1]`, which do not overlap `b[0..1]`. No object is modified through one restrict pointer and accessed through the other. UBSan/ASan/MSan at -O0 are clean.
**Possible Root cause**
`llvm/lib/Transforms/Utils/CloneFunction.cpp`, `llvm::cloneNoAliasScopes` / `adaptNoAliasScopes` (used by LoopUnroll via `cloneAndAdaptNoAliasScopes`). For a disjoint-scope domain (`!{!D, i1 true, !"rk0"}`, used by the inliner since de1a66d7dbe1) it clones the domain, but it clones only the scopes that have a decl in the loop. InstCombine legally deletes the decl of the unused `b` scope. In each unrolled copy the `c` scope moves to a new domain and the `b` scope stays in the old one. In the new domain the store has only `{c}`, a subset of the call's `!noalias {c}` -> NoAlias. Before the split it was MayAlias. Came with c851fb6892a6 (#218768) + de1a66d7dbe1 (#218769).
**Possible Proposed fix**
When a domain is cloned, map every scope of that domain that the adapted instructions use into the clone, not only the declared ones:
```diff
- if (auto *NewMD = ClonedScopes.lookup(MD)) {
+ MDNode *NewMD = ClonedScopes.lookup(MD);
+ if (!NewMD)
+ if (MDNode *NewDom = ClonedDomains.lookup(AliasScopeNode(MD).getDomain())) {
+ NewMD = MDB.createAnonymousAliasScope(NewDom, <name>);
+ ClonedScopes[MD] = NewMD;
+ }
+ if (NewMD) {
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs