-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Metadata] Preserve MD_prof when merging instructions when one is missing. #132433
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
snehasish
merged 1 commit into
main
from
users/snehasish/03-21-_metadata_preserve_md_prof_when_merging_instructions_when_one_is_missing
Apr 2, 2025
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
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
62 changes: 62 additions & 0 deletions
62
llvm/test/Transforms/SimplifyCFG/merge-direct-call-branch-weights-preserve-hoist.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,62 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --version 2 | ||
; RUN: opt < %s -passes='simplifycfg<no-sink-common-insts;hoist-common-insts>' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s --check-prefix=HOIST | ||
|
||
; Test case based on C++ code with manualy annotated !prof metadata. | ||
; This is to test that when calls to 'func1' from 'if.then' block | ||
; and 'if.else' block are hoisted, the branch_weights are merged and | ||
; attached to merged call rather than dropped. | ||
; | ||
; int func1(int a, int b) ; | ||
; int func2(int a, int b) ; | ||
|
||
; int func(int a, int b, bool c) { | ||
; int sum= 0; | ||
; if(c) { | ||
; sum += func1(a, b); | ||
; } else { | ||
; sum += func1(a, b); | ||
; sum -= func2(a, b); | ||
; } | ||
; return sum; | ||
; } | ||
define i32 @_Z4funciib(i32 %a, i32 %b, i1 %c) { | ||
; HOIST-LABEL: define i32 @_Z4funciib | ||
; HOIST-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i1 [[C:%.*]]) { | ||
; HOIST-NEXT: entry: | ||
; HOIST-NEXT: [[CALL:%.*]] = tail call i32 @_Z5func1ii(i32 [[A]], i32 [[B]]), !prof [[PROF0:![0-9]+]] | ||
; HOIST-NEXT: br i1 [[C]], label [[IF_END:%.*]], label [[IF_ELSE:%.*]] | ||
; HOIST: if.else: | ||
; HOIST-NEXT: [[CALL3:%.*]] = tail call i32 @_Z5func2ii(i32 [[A]], i32 [[B]]) | ||
; HOIST-NEXT: [[SUB:%.*]] = sub i32 [[CALL]], [[CALL3]] | ||
; HOIST-NEXT: br label [[IF_END]] | ||
; HOIST: if.end: | ||
; HOIST-NEXT: [[SUM_0:%.*]] = phi i32 [ [[SUB]], [[IF_ELSE]] ], [ [[CALL]], [[ENTRY:%.*]] ] | ||
; HOIST-NEXT: ret i32 [[SUM_0]] | ||
; | ||
entry: | ||
br i1 %c, label %if.then, label %if.else | ||
|
||
if.then: ; preds = %entry | ||
%call = tail call i32 @_Z5func1ii(i32 %a, i32 %b) | ||
br label %if.end | ||
|
||
if.else: ; preds = %entry | ||
%call1 = tail call i32 @_Z5func1ii(i32 %a, i32 %b), !prof !0 | ||
%call3 = tail call i32 @_Z5func2ii(i32 %a, i32 %b) | ||
%sub = sub i32 %call1, %call3 | ||
br label %if.end | ||
|
||
if.end: ; preds = %if.else, %if.then | ||
%sum.0 = phi i32 [ %call, %if.then ], [ %sub, %if.else ] | ||
ret i32 %sum.0 | ||
} | ||
|
||
declare i32 @_Z5func1ii(i32, i32) | ||
|
||
declare i32 @_Z5func2ii(i32, i32) | ||
|
||
!0 = !{!"branch_weights", i32 10} | ||
|
||
;. | ||
; HOIST: [[PROF0]] = !{!"branch_weights", i32 10} | ||
;. |
63 changes: 63 additions & 0 deletions
63
llvm/test/Transforms/SimplifyCFG/merge-direct-call-branch-weights-preserve-sink.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,63 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --version 2 | ||
; RUN: opt < %s -passes='simplifycfg<sink-common-insts;no-hoist-common-insts>' -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s --check-prefix=SINK | ||
|
||
|
||
; Test case based on the following C++ code with manualy annotated !prof metadata. | ||
; This is to test that when calls to 'func1' from 'if.then' and 'if.else' are | ||
; sinked, the branch weights are merged and attached to sinked call. | ||
; | ||
; int func1(int a, int b) ; | ||
; int func2(int a, int b) ; | ||
|
||
; int func(int a, int b, bool c) { | ||
; int sum = 0; | ||
; if (c) { | ||
; sum += func1(a,b); | ||
; } else { | ||
; b -= func2(a,b); | ||
; sum += func1(a,b); | ||
; } | ||
; return sum; | ||
; } | ||
|
||
define i32 @_Z4funciib(i32 %a, i32 %b, i1 %c) { | ||
; SINK-LABEL: define i32 @_Z4funciib | ||
; SINK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i1 [[C:%.*]]) { | ||
; SINK-NEXT: entry: | ||
; SINK-NEXT: br i1 [[C]], label [[IF_END:%.*]], label [[IF_ELSE:%.*]] | ||
; SINK: if.else: | ||
; SINK-NEXT: [[CALL1:%.*]] = tail call i32 @_Z5func2ii(i32 [[A]], i32 [[B]]) | ||
; SINK-NEXT: [[SUB:%.*]] = sub i32 [[B]], [[CALL1]] | ||
; SINK-NEXT: br label [[IF_END]] | ||
; SINK: if.end: | ||
; SINK-NEXT: [[SUB_SINK:%.*]] = phi i32 [ [[SUB]], [[IF_ELSE]] ], [ [[B]], [[ENTRY:%.*]] ] | ||
; SINK-NEXT: [[CALL2:%.*]] = tail call i32 @_Z5func1ii(i32 [[A]], i32 [[SUB_SINK]]), !prof [[PROF0:![0-9]+]] | ||
; SINK-NEXT: ret i32 [[CALL2]] | ||
; | ||
entry: | ||
br i1 %c, label %if.then, label %if.else | ||
|
||
if.then: ; preds = %entry | ||
%call = tail call i32 @_Z5func1ii(i32 %a, i32 %b), !prof !0 | ||
br label %if.end | ||
|
||
if.else: ; preds = %entry | ||
%call1 = tail call i32 @_Z5func2ii(i32 %a, i32 %b) | ||
%sub = sub i32 %b, %call1 | ||
%call2 = tail call i32 @_Z5func1ii(i32 %a, i32 %sub) | ||
br label %if.end | ||
|
||
if.end: ; preds = %if.else, %if.then | ||
%sum.0 = phi i32 [ %call, %if.then ], [ %call2, %if.else ] | ||
ret i32 %sum.0 | ||
} | ||
|
||
declare i32 @_Z5func1ii(i32, i32) | ||
|
||
declare i32 @_Z5func2ii(i32, i32) | ||
|
||
!0 = !{!"branch_weights", i32 10} | ||
|
||
;. | ||
; SINK: [[PROF0]] = !{!"branch_weights", i32 10} | ||
;. |
Oops, something went wrong.
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.
The old handling was guarded on DoesKMove - what is the implication of removing that?
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.
Removing the condition was intentional. In the case that
DoesKMove
is true, the merging of md_prof can be skipped ifJ
does not have prof metadata (a minor optimization). I felt that it was simpler to just perform the merge regardless. Let me know if you feel otherwise.