llvm / llvm/llvm-project

Miscompilation at -O1 with profile data using PGO

Open
#191,946 1 comment 0 reactions 0 assignees View on GitHub
generated by fuzzer miscompilation PGO regression:22
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

small.c
```c
#include
unsigned b;
int c = 1, e;
unsigned d(int g, short p2, char i) {
for (; g < i; g += 10)
e = 0;
e = p2;
return g;
}
static int f2(unsigned g) {
int a = 0;
a:
0 || 0;
for (; a < 10; a++)
for (; b; b++) {
switch (0)
if (1)
if (1)
;
switch (0)
;
if (g)
for (;;)
;
}
}
static char f1() {
int f = d(b, c, b);
f2(f);
}
int main() {
f1();
printf("%d\n", e);
}
```

profile.txt
```
small.c:f1
# Func Hash:
0
# Num Counters:
1
# Counter Values:
0

small.c:f2
# Func Hash:
930344025807534475
# Num Counters:
10
# Counter Values:
0
0
0
0
0
0
0
0
0
8
```

Clang miscompiles the above code at -O1 with profile data using PGO.

Generate the profile data with:
```
$ llvm-profdata merge -o test.profdata profile.txt
```

Correct compilation without PGO:
```
$ clang-trunk -O1 small.c && ./a.out
1
```

Miscompile with PGO:
```
$ clang-trunk -O1 -fprofile-instr-use=test.profdata small.c && ./a.out
0
```

Should be a clang-22 regression.
Bisected to [5af27f8](https://github.com/llvm/llvm-project/commit/5af27f8c208b4ba13f339801c9188cfc19cebdc0), which is committed by @zsrkmyn.

Label: generated by fuzzer

Contributor guide

Open the contributing guide

Research direction

Reproduce the difference using small.c, profile.txt, llvm-profdata merge, and the two clang-trunk commands. Then inspect the regression introduced by commit 5af27f8 and trace how the supplied PGO counters affect -O1. Done means the PGO build produces the correct output, 1, for this case without breaking the non-PGO result.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.