Issue 121105
Summary [LLVM] Inlining breaks __builtin_expect branch weight metadata
Labels new issue
Assignees
Reporter sillycross
    Minimal repro:
```
#define likely(expr) __builtin_expect((expr) != 0, 1)
bool f(int** p) {
  return *p != nullptr && **p == 1;
}
void h();
void i();
void g(int** p) {
  if (likely(f(p))) h(); else i();
}
```
Compile: `clang++ test.cpp -O3 -emit-llvm -S` gives following result ([https://godbolt.org/z/81rrxbhab](https://godbolt.org/z/81rrxbhab)):
```
... omitted ...
  %6 = icmp eq i32 %5, 1                    # <- **p == 1 check
  br i1 %6, label %7, label %8, !prof !49   # <- metadata broken
... omitted ...
  # -2147483648 means invalid. Should be "i32 2000, i32 1" (due to __builtin_expect)
  !49 = !{!"branch_weights", !"expected", i32 -2147483648, i32 0}  
```

After bisecting, this bug is introduced by commit [#a1b78fb](https://github.com/llvm/llvm-project/commit/a1b78fb929fccf96acaa0212cf68fee82298e747), authored by Evgeniy Brevnov (ybrev...@azul.com) and reviewed by @xortator (Max Kazantsev). Reverting the commit fixes the issue.

Ironically, this commit is already breaking existing tests by producing invalid metadata (see [#a1b78fb](https://github.com/llvm/llvm-project/commit/a1b78fb929fccf96acaa0212cf68fee82298e747) at the end). Instead of fixing the issues, the author changed the expected test output to the invalid metadata.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to