Skip to content

Commit 31103ef

Browse files
authored
[profcheck] Propagate profile metadata in trunc(select) transform (llvm#162406)
In the `AggressiveInstCombine` pass, profile metadata was being dropped during the transformation of `trunc(select(...))` operations. This change fixes the issue by propagating the metadata from the original `select` to the new `trunc-select` instruction.
1 parent 5a7d8a3 commit 31103ef

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ void TruncInstCombine::ReduceExpressionGraph(Type *SclTy) {
459459
Value *Op0 = I->getOperand(0);
460460
Value *LHS = getReducedOperand(I->getOperand(1), SclTy);
461461
Value *RHS = getReducedOperand(I->getOperand(2), SclTy);
462-
Res = Builder.CreateSelect(Op0, LHS, RHS);
462+
Res = Builder.CreateSelect(Op0, LHS, RHS, "", I);
463463
break;
464464
}
465465
case Instruction::PHI: {

llvm/test/Transforms/AggressiveInstCombine/trunc_select.ll

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33

44
target datalayout = "e-m:m-p1:64:64:64-p:32:32:32-n8:16:32"
55

6-
define dso_local i16 @select_i16(i16 %a, i16 %b, i1 %cond) {
6+
define dso_local i16 @select_i16(i16 %a, i16 %b, i1 %cond) !prof !0 {
77
; CHECK-LABEL: @select_i16(
8+
; CHECK: !prof [[PROF_0:![0-9]+]] {
89
; CHECK-NEXT: entry:
9-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i16 [[A:%.*]], i16 [[B:%.*]]
10+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], i16 [[A:%.*]], i16 [[B:%.*]], !prof [[PROF_1:![0-9]+]]
1011
; CHECK-NEXT: ret i16 [[SEL]]
1112
;
1213
entry:
1314
%conv0 = sext i16 %a to i32
1415
%conv1 = sext i16 %b to i32
15-
%sel = select i1 %cond, i32 %conv0, i32 %conv1
16+
%sel = select i1 %cond, i32 %conv0, i32 %conv1, !prof !1
1617
%conv4 = trunc i32 %sel to i16
1718
ret i16 %conv4
1819
}
@@ -134,3 +135,8 @@ entry:
134135
ret i16 %conv4
135136
}
136137

138+
!0 = !{!"function_entry_count", i64 1000}
139+
!1 = !{!"branch_weights", i32 2, i32 3}
140+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
141+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 2, i32 3}
142+

llvm/test/Transforms/AggressiveInstCombine/trunc_select_cmp.ll

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=aggressive-instcombine -S | FileCheck %s
33

4-
define dso_local i16 @cmp_select_sext_const(i8 %a) {
4+
define dso_local i16 @cmp_select_sext_const(i8 %a) !prof !0 {
55
; CHECK-LABEL: @cmp_select_sext_const(
6+
; CHECK: !prof [[PROF_0:![0-9]+]] {
67
; CHECK-NEXT: entry:
78
; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[A:%.*]] to i32
89
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[CONV]], 109
9-
; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 109, i32 [[CONV]]
10+
; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 109, i32 [[CONV]], !prof [[PROF_1:![0-9]+]]
1011
; CHECK-NEXT: [[CONV4:%.*]] = trunc i32 [[COND]] to i16
1112
; CHECK-NEXT: ret i16 [[CONV4]]
1213
;
1314
entry:
1415
%conv = sext i8 %a to i32
1516
%cmp = icmp slt i32 %conv, 109
16-
%cond = select i1 %cmp, i32 109, i32 %conv
17+
%cond = select i1 %cmp, i32 109, i32 %conv, !prof !1
1718
%conv4 = trunc i32 %cond to i16
1819
ret i16 %conv4
1920
}
@@ -209,3 +210,7 @@ define i16 @cmp_select_unsigned_const_i16Const_noTransformation(i8 %a) {
209210
ret i16 %conv4
210211
}
211212

213+
!0 = !{!"function_entry_count", i64 1000}
214+
!1 = !{!"branch_weights", i32 2, i32 3}
215+
; CHECK: [[PROF_0]] = !{!"function_entry_count", i64 1000}
216+
; CHECK: [[PROF_1]] = !{!"branch_weights", i32 2, i32 3}

0 commit comments

Comments
 (0)