| Issue |
237941
|
| Summary |
[JumpThreading][ProfInfo] JumpThreading drops !prof branch_weights instead of transferring them to the predecessor
|
| Labels |
|
| Assignees |
|
| Reporter |
mark-sed
|
I have noticed that we drop branch weights in jump threading which then leads to worse code generation.
```llvm
declare void @use(i32)
declare void @sideeffect()
define void @f(i1 %p) {
entry:
br i1 %p, label %a, label %b
a:
call void @use(i32 1)
br label %join
b:
call void @use(i32 2)
br label %join
join:
br i1 %p, label %t, label %e, !prof !0
t:
call void @sideeffect()
ret void
e:
ret void
}
!0 = !{!"branch_weights", i32 100, i32 1}
```
```bash
$OPT -S -passes=jump-threading dup-cond.ll
```
```llvm
; ModuleID = 'dup-cond.ll'
source_filename = "dup-cond.ll"
declare void @use(i32)
declare void @sideeffect()
define void @f(i1 %p) {
entry:
br i1 %p, label %t, label %e
t: ; preds = %entry
call void @use(i32 1)
call void @sideeffect()
ret void
e: ; preds = %entry
call void @use(i32 2)
ret void
}
```
Output has no !prof anywhere. Entry becomes `br i1 %p, label %t, label %e` without `!prof`, even though the branch it absorbed was 100:1. The control-flow decision survived, but the profile did not and the metadata were not meaningless.
```bash
$OPT -S -passes=simplifycfg dup-cond.ll | grep 'br i1 %p' # -> ..., !prof !0
$OPT -S -passes=correlated-propagation dup-cond.ll | grep 'br i1 %p' # -> ..., !prof !0
```
These 2 passes both keep the `!prof !0`.
Another example is in `min.ll` test that I've included.
Here is the above code with `--enable-profcheck`:
```bash
$OPT -S -passes=jump-threading --enable-profcheck dup-cond.ll ; echo "exit=$?"
```
```llvm
define void @f(i1 %p) !prof !0 {
entry:
br i1 %p, label %t, label %e, !prof !1
...
!1 = !{!"branch_weights", i32 1, i32 1}
exit=0
```
Clean pass, no diagnostics, on both `dup-cond.ll` and `min.ll`. prof-inject annotates every branch up front, so entry already carries weights before threading runs, and the verifier only checks that metadata is present - not that a known ratio was propagated. **Weights 100:1 get silently replaced by the injected 1:1 and nothing complains**.
## Code gen impact
I had Claude generate tests which are the same, but one includes profile data and the other does not use it to demonstrate the impact of this in codegen - these 2 files are included (`loop-2way-noprof.ll` and `loop-2way.ll`) bellow.
```bash
$OPT -S -passes=simplifycfg loop-2way.ll
```
```llvm
; ModuleID = 'loop-2way.ll'
source_filename = "loop-2way.ll"
define void @merge(ptr noundef %a1, ptr noundef %a2, ptr noundef %dst, i32 noundef %hi1, i32 noundef %hi2) {
entry:
br label %loop
loop: ; preds = %latch, %entry
%lo1 = phi i32 [ 0, %entry ], [ %lo1.next, %latch ]
%lo2 = phi i32 [ 0, %entry ], [ %lo2.next, %latch ]
%k = phi i32 [ 0, %entry ], [ %k.next, %latch ]
%i1 = sext i32 %lo1 to i64
%p1 = getelementptr inbounds double, ptr %a1, i64 %i1
%v1 = load double, ptr %p1, align 8
%i2 = sext i32 %lo2 to i64
%p2 = getelementptr inbounds double, ptr %a2, i64 %i2
%v2 = load double, ptr %p2, align 8
%take1 = fcmp olt double %v1, %v2
br i1 %take1, label %from.a1, label %from.a2, !prof !0
from.a1: ; preds = %loop
%lo1.inc = add nsw i32 %lo1, 1
br label %latch
from.a2: ; preds = %loop
%lo2.inc = add nsw i32 %lo2, 1
br label %latch
latch: ; preds = %from.a2, %from.a1
%lo1.next = phi i32 [ %lo1.inc, %from.a1 ], [ %lo1, %from.a2 ]
%lo2.next = phi i32 [ %lo2, %from.a1 ], [ %lo2.inc, %from.a2 ]
%val = phi double [ %v1, %from.a1 ], [ %v2, %from.a2 ]
%ik = sext i32 %k to i64
%pk = getelementptr inbounds double, ptr %dst, i64 %ik
store double %val, ptr %pk, align 8
%k.next = add nsw i32 %k, 1
%c1 = icmp slt i32 %lo1.next, %hi1
%c2 = icmp slt i32 %lo2.next, %hi2
%cont = and i1 %c1, %c2
br i1 %cont, label %loop, label %exit
exit: ; preds = %latch
ret void
}
!0 = !{!"branch_weights", i32 100, i32 1}
```
```bash
$OPT -S -passes=simplifycfg loop-2way-noprof.ll
```
```llvm
; ModuleID = 'loop-2way-noprof.ll'
source_filename = "loop-2way-noprof.ll"
define void @merge(ptr noundef %a1, ptr noundef %a2, ptr noundef %dst, i32 noundef %hi1, i32 noundef %hi2) {
entry:
br label %loop
loop: ; preds = %loop, %entry
%lo1 = phi i32 [ 0, %entry ], [ %lo1.next, %loop ]
%lo2 = phi i32 [ 0, %entry ], [ %lo2.next, %loop ]
%k = phi i32 [ 0, %entry ], [ %k.next, %loop ]
%i1 = sext i32 %lo1 to i64
%p1 = getelementptr inbounds double, ptr %a1, i64 %i1
%v1 = load double, ptr %p1, align 8
%i2 = sext i32 %lo2 to i64
%p2 = getelementptr inbounds double, ptr %a2, i64 %i2
%v2 = load double, ptr %p2, align 8
%take1 = fcmp olt double %v1, %v2
%lo1.inc = add nsw i32 %lo1, 1
%lo2.inc = add nsw i32 %lo2, 1
%lo1.next = select i1 %take1, i32 %lo1.inc, i32 %lo1
%lo2.next = select i1 %take1, i32 %lo2, i32 %lo2.inc
%val = select i1 %take1, double %v1, double %v2
%ik = sext i32 %k to i64
%pk = getelementptr inbounds double, ptr %dst, i64 %ik
store double %val, ptr %pk, align 8
%k.next = add nsw i32 %k, 1
%c1 = icmp slt i32 %lo1.next, %hi1
%c2 = icmp slt i32 %lo2.next, %hi2
%cont = and i1 %c1, %c2
br i1 %cont, label %loop, label %exit
exit: ; preds = %loop
ret void
}
```
Identical IR apart from `!prof`. The profiled one keeps the branch, the unprofiled one if-converts into three selects. So `foldTwoEntryPHINode` demonstrably reads the weights we just lost.
## Reproducers
Here are tests to reproduce this issue: [reproducers.zip](https://github.com/user-attachments/files/34269214/reproducers.zip)
Reproduces at `7b6a8d7`, 2026-11-02.
## Related changes
In #234022 @boomanaiden154 had work around profiles in JumpThreading, but that does not concern this case.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs