-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[SLU][profcheck] Estimate branch weights in partial unswitch cases #164035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mtrofin
merged 1 commit into
main
from
users/mtrofin/10-17-_slu_profcheck_estimate_branch_weights_in_partial_unswitch_cases
Oct 30, 2025
+305
−12
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch-profile.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| ; RUN: split-file %s %t | ||
| ; RUN: cat %t/main.ll %t/probable-or.prof > %t/probable-or.ll | ||
| ; RUN: cat %t/main.ll %t/probable-and.prof > %t/probable-and.ll | ||
| ; RUN: opt -passes='loop(simple-loop-unswitch<nontrivial>)' -S %t/probable-or.ll -o -| FileCheck %t/probable-or.prof | ||
| ; RUN: opt -passes='loop(simple-loop-unswitch<nontrivial>)' -S %t/probable-and.ll -o -| FileCheck %t/probable-and.prof | ||
|
|
||
| ;--- main.ll | ||
| declare i32 @a() | ||
| declare i32 @b() | ||
|
|
||
| define i32 @or(ptr %ptr, i1 %cond) !prof !0 { | ||
| entry: | ||
| br label %loop_begin | ||
|
|
||
| loop_begin: | ||
| %v1 = load i1, ptr %ptr | ||
| %cond_or = or i1 %v1, %cond | ||
| br i1 %cond_or, label %loop_a, label %loop_b, !prof !1 | ||
|
|
||
| loop_a: | ||
| call i32 @a() | ||
| br label %latch | ||
|
|
||
| loop_b: | ||
| call i32 @b() | ||
| br label %latch | ||
|
|
||
| latch: | ||
| %v2 = load i1, ptr %ptr | ||
| br i1 %v2, label %loop_begin, label %loop_exit, !prof !2 | ||
|
|
||
| loop_exit: | ||
| ret i32 0 | ||
| } | ||
|
|
||
| define i32 @and(ptr %ptr, i1 %cond) !prof !0 { | ||
| entry: | ||
| br label %loop_begin | ||
|
|
||
| loop_begin: | ||
| %v1 = load i1, ptr %ptr | ||
| %cond_and = and i1 %v1, %cond | ||
| br i1 %cond_and, label %loop_a, label %loop_b, !prof !1 | ||
|
|
||
| loop_a: | ||
| call i32 @a() | ||
| br label %latch | ||
|
|
||
| loop_b: | ||
| call i32 @b() | ||
| br label %latch | ||
|
|
||
| latch: | ||
| %v2 = load i1, ptr %ptr | ||
| br i1 %v2, label %loop_begin, label %loop_exit, !prof !2 | ||
|
|
||
| loop_exit: | ||
| ret i32 0 | ||
| } | ||
|
|
||
| ;--- probable-or.prof | ||
| !0 = !{!"function_entry_count", i32 10} | ||
| !1 = !{!"branch_weights", i32 1, i32 1000} | ||
| !2 = !{!"branch_weights", i32 5, i32 7} | ||
| ; CHECK-LABEL: @or | ||
| ; CHECK-LABEL: entry: | ||
| ; CHECK-NEXT: %cond.fr = freeze i1 %cond | ||
| ; CHECK-NEXT: br i1 %cond.fr, label %entry.split.us, label %entry.split, !prof !1 | ||
| ; CHECK-LABEL: @and | ||
| ; CHECK-LABEL: entry: | ||
| ; CHECK-NEXT: %cond.fr = freeze i1 %cond | ||
| ; CHECK-NEXT: br i1 %cond.fr, label %entry.split, label %entry.split.us, !prof !3 | ||
| ; CHECK: !1 = !{!"branch_weights", i32 1, i32 1000} | ||
| ; CHECK: !3 = !{!"unknown", !"simple-loop-unswitch"} | ||
|
|
||
| ;--- probable-and.prof | ||
| !0 = !{!"function_entry_count", i32 10} | ||
| !1 = !{!"branch_weights", i32 1000, i32 1} | ||
| !2 = !{!"branch_weights", i32 5, i32 7} | ||
| ; CHECK-LABEL: @or | ||
| ; CHECK-LABEL: entry: | ||
| ; CHECK-NEXT: %cond.fr = freeze i1 %cond | ||
| ; CHECK-NEXT: br i1 %cond.fr, label %entry.split.us, label %entry.split, !prof !1 | ||
| ; CHECK-LABEL: @and | ||
| ; CHECK-LABEL: entry: | ||
| ; CHECK-NEXT: %cond.fr = freeze i1 %cond | ||
| ; CHECK-NEXT: br i1 %cond.fr, label %entry.split, label %entry.split.us, !prof !3 | ||
| ; CHECK: !1 = !{!"unknown", !"simple-loop-unswitch"} | ||
| ; CHECK: !3 = !{!"branch_weights", i32 1000, i32 1} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
llvm/test/Transforms/SimpleLoopUnswitch/simple-unswitch-profile.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| ; RUN: split-file %s %t | ||
| ; RUN: cat %t/main.ll %t/probable-or.prof > %t/probable-or.ll | ||
| ; RUN: cat %t/main.ll %t/probable-and.prof > %t/probable-and.ll | ||
| ; RUN: opt -passes='loop-mssa(simple-loop-unswitch)' -S %t/probable-or.ll -o - | FileCheck %t/probable-or.prof | ||
| ; RUN: opt -passes='loop-mssa(simple-loop-unswitch)' -S %t/probable-and.ll -o - | FileCheck %t/probable-and.prof | ||
| ; | ||
| ; RUN: opt -passes='module(print<block-freq>),function(loop-mssa(simple-loop-unswitch)),module(print<block-freq>)' \ | ||
| ; RUN: %t/probable-or.ll -disable-output -simple-loop-unswitch-estimate-profile=0 2>&1 | FileCheck %t/probable-or.prof --check-prefixes=PROFILE-COM,PROFILE-REF | ||
|
|
||
| ; RUN: opt -passes='module(print<block-freq>),function(loop-mssa(simple-loop-unswitch)),module(print<block-freq>)' \ | ||
| ; RUN: %t/probable-or.ll -disable-output -simple-loop-unswitch-estimate-profile=1 2>&1 | FileCheck %t/probable-or.prof --check-prefixes=PROFILE-COM,PROFILE-CHK | ||
|
|
||
| ; RUN: opt -passes='module(print<block-freq>),function(loop-mssa(simple-loop-unswitch)),module(print<block-freq>)' \ | ||
| ; RUN: %t/probable-and.ll -disable-output -simple-loop-unswitch-estimate-profile=0 2>&1 | FileCheck %t/probable-and.prof --check-prefixes=PROFILE-COM,PROFILE-REF | ||
|
|
||
| ; RUN: opt -passes='module(print<block-freq>),function(loop-mssa(simple-loop-unswitch)),module(print<block-freq>)' \ | ||
| ; RUN: %t/probable-and.ll -disable-output -simple-loop-unswitch-estimate-profile=1 2>&1 | FileCheck %t/probable-and.prof --check-prefixes=PROFILE-COM,PROFILE-CHK | ||
|
|
||
| ;--- main.ll | ||
| declare void @some_func() noreturn | ||
|
|
||
| define i32 @or(i1 %cond1, i32 %var1) !prof !0 { | ||
| entry: | ||
| br label %loop_begin | ||
|
|
||
| loop_begin: | ||
| %var3 = phi i32 [%var1, %entry], [%var2, %do_something] | ||
| %cond2 = icmp eq i32 %var3, 10 | ||
| %cond.or = or i1 %cond1, %cond2 | ||
| br i1 %cond.or, label %loop_exit, label %do_something, !prof !1 | ||
|
|
||
| do_something: | ||
| %var2 = add i32 %var3, 1 | ||
| call void @some_func() noreturn nounwind | ||
| br label %loop_begin | ||
|
|
||
| loop_exit: | ||
| ret i32 0 | ||
| } | ||
|
|
||
| define i32 @and(i1 %cond1, i32 %var1) !prof !0 { | ||
| entry: | ||
| br label %loop_begin | ||
|
|
||
| loop_begin: | ||
| %var3 = phi i32 [%var1, %entry], [%var2, %do_something] | ||
| %cond2 = icmp eq i32 %var3, 10 | ||
| %cond.and = and i1 %cond1, %cond2 | ||
| br i1 %cond.and, label %do_something, label %loop_exit, !prof !1 | ||
|
|
||
| do_something: | ||
| %var2 = add i32 %var3, 1 | ||
| call void @some_func() noreturn nounwind | ||
| br label %loop_begin | ||
|
|
||
| loop_exit: | ||
| ret i32 0 | ||
| } | ||
|
|
||
| ;--- probable-or.prof | ||
| !0 = !{!"function_entry_count", i32 10} | ||
| !1 = !{!"branch_weights", i32 1, i32 1000} | ||
| ; CHECK-LABEL: @or | ||
| ; CHECK-LABEL: entry: | ||
| ; CHECK-NEXT: %cond1.fr = freeze i1 %cond1 | ||
| ; CHECK-NEXT: br i1 %cond1.fr, label %loop_exit.split, label %entry.split, !prof !1 | ||
| ; CHECK-LABEL: @and | ||
| ; CHECK-LABEL: entry: | ||
| ; CHECK-NEXT: %cond1.fr = freeze i1 %cond1 | ||
| ; CHECK-NEXT: br i1 %cond1.fr, label %entry.split, label %loop_exit.split, !prof !2 | ||
| ; CHECK: !1 = !{!"branch_weights", i32 1, i32 1000} | ||
| ; CHECK: !2 = !{!"unknown", !"simple-loop-unswitch"} | ||
|
|
||
| ; PROFILE-COM: Printing analysis results of BFI for function 'or': | ||
| ; PROFILE-COM: block-frequency-info: or | ||
| ; PROFILE-COM: - entry: {{.*}} count = 10 | ||
| ; PROFILE-COM: - loop_begin: {{.*}} count = 10010 | ||
| ; PROFILE-COM: - do_something: {{.*}} count = 10000 | ||
| ; PROFILE-COM: - loop_exit: {{.*}} count = 10 | ||
|
|
||
| ; PROFILE-COM: Printing analysis results of BFI for function 'and': | ||
| ; PROFILE-COM: block-frequency-info: and | ||
| ; PROFILE-COM: - entry: {{.*}} count = 10 | ||
| ; PROFILE-COM: - loop_begin: {{.*}} count = 10 | ||
| ; PROFILE-COM: - do_something: {{.*}} count = 0 | ||
| ; PROFILE-COM: - loop_exit: {{.*}} count = 10 | ||
|
|
||
| ; PROFILE-COM: Printing analysis results of BFI for function 'or': | ||
| ; PROFILE-COM: block-frequency-info: or | ||
| ; PROFILE-COM: - entry: {{.*}} count = 10 | ||
| ; PROFILE-REF: - entry.split: {{.*}} count = 5 | ||
| ; PROFILE-CHK: - entry.split: {{.*}} count = 10 | ||
| ; PROFILE-REF: - loop_begin: {{.*}} count = 5005 | ||
| ; PROFILE-CHK: - loop_begin: {{.*}} count = 10000 | ||
| ; PROFILE-REF: - do_something: {{.*}} count = 5000 | ||
| ; PROFILE-CHK: - do_something: {{.*}} count = 9990 | ||
| ; PROFILE-REF: - loop_exit: {{.*}} count = 5 | ||
| ; PROFILE-CHK: - loop_exit: {{.*}} count = 10 | ||
| ; PROFILE-COM: - loop_exit.split: {{.*}} count = 10 | ||
|
|
||
| ; PROFILE-COM: Printing analysis results of BFI for function 'and': | ||
| ; PROFILE-COM: block-frequency-info: and | ||
| ; PROFILE-COM: - entry: {{.*}} count = 10 | ||
| ; PROFILE-COM: - entry.split: {{.*}} count = 5 | ||
| ; PROFILE-COM: - loop_begin: {{.*}} count = 5 | ||
| ; PROFILE-COM: - do_something: {{.*}} count = 0 | ||
| ; PROFILE-COM: - loop_exit: {{.*}} count = 5 | ||
| ; PROFILE-COM: - loop_exit.split: {{.*}} count = 10 | ||
|
|
||
| ;--- probable-and.prof | ||
| !0 = !{!"function_entry_count", i32 10} | ||
| !1 = !{!"branch_weights", i32 1000, i32 1} | ||
| ; CHECK-LABEL: @or | ||
| ; CHECK-LABEL: entry: | ||
| ; CHECK-NEXT: %cond1.fr = freeze i1 %cond1 | ||
| ; CHECK-NEXT: br i1 %cond1.fr, label %loop_exit.split, label %entry.split, !prof !1 | ||
| ; CHECK-LABEL: @and | ||
| ; CHECK-LABEL: entry: | ||
| ; CHECK-NEXT: %cond1.fr = freeze i1 %cond1 | ||
| ; CHECK-NEXT: br i1 %cond1.fr, label %entry.split, label %loop_exit.split, !prof !2 | ||
| ; CHECK: !1 = !{!"unknown", !"simple-loop-unswitch"} | ||
| ; CHECK: !2 = !{!"branch_weights", i32 1000, i32 1} | ||
| ; PROFILE-COM: Printing analysis results of BFI for function 'or': | ||
| ; PROFILE-COM: block-frequency-info: or | ||
| ; PROFILE-COM: - entry: {{.*}}, count = 10 | ||
| ; PROFILE-COM: - loop_begin: {{.*}}, count = 10 | ||
| ; PROFILE-COM: - do_something: {{.*}}, count = 0 | ||
| ; PROFILE-COM: - loop_exit: {{.*}}, count = 10 | ||
|
|
||
| ; PROFILE-COM: Printing analysis results of BFI for function 'and': | ||
| ; PROFILE-COM: block-frequency-info: and | ||
| ; PROFILE-COM: - entry: {{.*}} count = 10 | ||
| ; PROFILE-COM: - loop_begin: {{.*}} count = 10010 | ||
| ; PROFILE-COM: - do_something: {{.*}} count = 10000 | ||
| ; PROFILE-COM: - loop_exit: {{.*}} count = 10 | ||
|
|
||
| ; PROFILE-COM: Printing analysis results of BFI for function 'or': | ||
| ; PROFILE-COM: block-frequency-info: or | ||
| ; PROFILE-COM: - entry: {{.*}} count = 10 | ||
| ; PROFILE-COM: - entry.split: {{.*}} count = 5 | ||
| ; PROFILE-COM: - loop_begin: {{.*}} count = 5 | ||
| ; PROFILE-COM: - do_something: {{.*}} count = 0 | ||
| ; PROFILE-COM: - loop_exit: {{.*}} count = 5 | ||
| ; PROFILE-COM: - loop_exit.split: {{.*}} count = 10 | ||
|
|
||
| ; PROFILE-COM: Printing analysis results of BFI for function 'and': | ||
| ; PROFILE-COM: block-frequency-info: and | ||
| ; PROFILE-COM: - entry: {{.*}} count = 10 | ||
| ; PROFILE-REF: - entry.split: {{.*}} count = 5 | ||
| ; PROFILE-CHK: - entry.split: {{.*}} count = 10 | ||
| ; PROFILE-REF: - loop_begin: {{.*}} count = 5005 | ||
| ; PROFILE-CHK: - loop_begin: {{.*}} count = 10000 | ||
| ; PROFILE-REF: - do_something: {{.*}} count = 5000 | ||
| ; PROFILE-CHK: - do_something: {{.*}} count = 9990 | ||
| ; PROFILE-REF: - loop_exit: {{.*}} count = 5 | ||
| ; PROFILE-CHK: - loop_exit: {{.*}} count = 10 | ||
| ; PROFILE-COM: - loop_exit.split: {{.*}} count = 10 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this change useful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fly-by akin to #161240, and in particular because
ProfcheckDisableMetadataFixesisllvm-qualified, so if I was going to addllvm {might as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the link. If adding
namespace llvm {...}around static variables is worthwhile, it would be nice to extend the coding standards discussion of namespaces vs. static. @nhaehnle