Skip to content

Commit da2d5e7

Browse files
snehasishIanWood1
authored andcommitted
Reapply [Metadata] Preserve MD_prof when merging instructions when one is missing. (llvm#135418)
Preserve branch weight metadata when merging instructions if one of the instructions is missing metadata. This is similar in behaviour to what we do today for other types of metadata such as mmra, memprof and callsite metadata. Also add a legality check when merging prof metadata based on instruction type. Without this check GVN PRE optimizations result in prof metadata on phi nodes which break the module verifier. Build failure caught by https://lab.llvm.org/buildbot/#/builders/113/builds/6621 ``` !9185 = !{!"branch_weights", i32 3912, i32 802} Wrong number of operands !9185 = !{!"branch_weights", i32 3912, i32 802} fatal error: error in backend: Broken module found, compilation aborted! ``` Reverts llvm#134200 with additional changes.
1 parent 9e589af commit da2d5e7

File tree

5 files changed

+218
-6
lines changed

5 files changed

+218
-6
lines changed

llvm/lib/IR/Metadata.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,26 @@ MDNode *MDNode::mergeDirectCallProfMetadata(MDNode *A, MDNode *B,
12231223
MDNode *MDNode::getMergedProfMetadata(MDNode *A, MDNode *B,
12241224
const Instruction *AInstr,
12251225
const Instruction *BInstr) {
1226+
// Check that it is legal to merge prof metadata based on the opcode.
1227+
auto IsLegal = [](const Instruction &I) -> bool {
1228+
switch (I.getOpcode()) {
1229+
case Instruction::Invoke:
1230+
case Instruction::Br:
1231+
case Instruction::Switch:
1232+
case Instruction::Call:
1233+
case Instruction::IndirectBr:
1234+
case Instruction::Select:
1235+
case Instruction::CallBr:
1236+
return true;
1237+
default:
1238+
return false;
1239+
}
1240+
};
1241+
if (AInstr && !IsLegal(*AInstr))
1242+
return nullptr;
1243+
if (BInstr && !IsLegal(*BInstr))
1244+
return nullptr;
1245+
12261246
if (!(A && B)) {
12271247
return A ? A : B;
12281248
}

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,9 +3353,10 @@ static void combineMetadata(Instruction *K, const Instruction *J,
33533353
case LLVMContext::MD_invariant_group:
33543354
// Preserve !invariant.group in K.
33553355
break;
3356-
// Keep empty cases for mmra, memprof, and callsite to prevent them from
3357-
// being removed as unknown metadata. The actual merging is handled
3356+
// Keep empty cases for prof, mmra, memprof, and callsite to prevent them
3357+
// from being removed as unknown metadata. The actual merging is handled
33583358
// separately below.
3359+
case LLVMContext::MD_prof:
33593360
case LLVMContext::MD_mmra:
33603361
case LLVMContext::MD_memprof:
33613362
case LLVMContext::MD_callsite:
@@ -3384,10 +3385,6 @@ static void combineMetadata(Instruction *K, const Instruction *J,
33843385
if (!AAOnly)
33853386
K->setMetadata(Kind, JMD);
33863387
break;
3387-
case LLVMContext::MD_prof:
3388-
if (!AAOnly && DoesKMove)
3389-
K->setMetadata(Kind, MDNode::getMergedProfMetadata(KMD, JMD, K, J));
3390-
break;
33913388
case LLVMContext::MD_noalias_addrspace:
33923389
if (DoesKMove)
33933390
K->setMetadata(Kind,
@@ -3434,6 +3431,16 @@ static void combineMetadata(Instruction *K, const Instruction *J,
34343431
K->setMetadata(LLVMContext::MD_callsite,
34353432
MDNode::getMergedCallsiteMetadata(KCallSite, JCallSite));
34363433
}
3434+
3435+
// Merge prof metadata.
3436+
// Handle separately to support cases where only one instruction has the
3437+
// metadata.
3438+
auto *JProf = J->getMetadata(LLVMContext::MD_prof);
3439+
auto *KProf = K->getMetadata(LLVMContext::MD_prof);
3440+
if (!AAOnly && (JProf || KProf)) {
3441+
K->setMetadata(LLVMContext::MD_prof,
3442+
MDNode::getMergedProfMetadata(KProf, JProf, K, J));
3443+
}
34373444
}
34383445

34393446
void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes=gvn -S < %s | FileCheck %s
3+
4+
; Make sure that performing PRE does not add prof metadata onto newly created
5+
; phi nodes.
6+
define fastcc ptr @foo(i8 %__pred.1.val) {
7+
; CHECK-LABEL: define fastcc ptr @foo(
8+
; CHECK-SAME: i8 [[__PRED_1_VAL:%.*]]) {
9+
; CHECK-NEXT: [[ENTRY:.*:]]
10+
; CHECK-NEXT: switch i64 0, label %[[SW_BB:.*]] [
11+
; CHECK-NEXT: i64 1, label %[[ENTRY_SW_BB26_CRIT_EDGE:.*]]
12+
; CHECK-NEXT: i64 0, label %[[ENTRY_SW_BB21_CRIT_EDGE:.*]]
13+
; CHECK-NEXT: ]
14+
; CHECK: [[ENTRY_SW_BB21_CRIT_EDGE]]:
15+
; CHECK-NEXT: [[DOTPRE:%.*]] = trunc i8 [[__PRED_1_VAL]] to i1
16+
; CHECK-NEXT: [[DOTPRE1:%.*]] = select i1 [[DOTPRE]], i64 0, i64 4
17+
; CHECK-NEXT: br label %[[SW_BB21:.*]]
18+
; CHECK: [[ENTRY_SW_BB26_CRIT_EDGE]]:
19+
; CHECK-NEXT: [[DOTPRE2:%.*]] = trunc i8 [[__PRED_1_VAL]] to i1
20+
; CHECK-NEXT: [[DOTPRE3:%.*]] = select i1 [[DOTPRE2]], i64 0, i64 4, !prof [[PROF0:![0-9]+]]
21+
; CHECK-NEXT: br label %[[SW_BB26:.*]]
22+
; CHECK: [[SW_BB]]:
23+
; CHECK-NEXT: [[L78:%.*]] = trunc i8 [[__PRED_1_VAL]] to i1
24+
; CHECK-NEXT: [[C79:%.*]] = select i1 [[L78]], i64 0, i64 4
25+
; CHECK-NEXT: br label %[[SW_BB21]]
26+
; CHECK: [[SW_BB21]]:
27+
; CHECK-NEXT: [[C84_PRE_PHI:%.*]] = phi i64 [ [[DOTPRE1]], %[[ENTRY_SW_BB21_CRIT_EDGE]] ], [ [[C79]], %[[SW_BB]] ]
28+
; CHECK-NEXT: [[L83_PRE_PHI:%.*]] = phi i1 [ [[DOTPRE]], %[[ENTRY_SW_BB21_CRIT_EDGE]] ], [ [[L78]], %[[SW_BB]] ]
29+
; CHECK-NEXT: br label %[[SW_BB26]]
30+
; CHECK: [[SW_BB26]]:
31+
; CHECK-NEXT: [[C89_PRE_PHI:%.*]] = phi i64 [ [[DOTPRE3]], %[[ENTRY_SW_BB26_CRIT_EDGE]] ], [ [[C84_PRE_PHI]], %[[SW_BB21]] ]
32+
; CHECK-NEXT: [[L88_PRE_PHI:%.*]] = phi i1 [ [[DOTPRE2]], %[[ENTRY_SW_BB26_CRIT_EDGE]] ], [ [[L83_PRE_PHI]], %[[SW_BB21]] ]
33+
; CHECK-NEXT: ret ptr null
34+
;
35+
entry:
36+
switch i64 0, label %sw.bb [
37+
i64 1, label %sw.bb26
38+
i64 0, label %sw.bb21
39+
]
40+
41+
sw.bb: ; preds = %entry
42+
%l78 = trunc i8 %__pred.1.val to i1
43+
%c79 = select i1 %l78, i64 0, i64 4
44+
br label %sw.bb21
45+
46+
sw.bb21: ; preds = %sw.bb, %entry
47+
%l83 = trunc i8 %__pred.1.val to i1
48+
%c84 = select i1 %l83, i64 0, i64 4
49+
br label %sw.bb26
50+
51+
sw.bb26: ; preds = %sw.bb21, %entry
52+
%l88 = trunc i8 %__pred.1.val to i1
53+
%c89 = select i1 %l88, i64 0, i64 4, !prof !0
54+
ret ptr null
55+
}
56+
57+
!0 = !{!"branch_weights", i32 3994, i32 883}
58+
;.
59+
; CHECK: [[PROF0]] = !{!"branch_weights", i32 3994, i32 883}
60+
;.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --version 2
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
3+
4+
; Test case based on C++ code with manualy annotated !prof metadata.
5+
; This is to test that when calls to 'func1' from 'if.then' block
6+
; and 'if.else' block are hoisted, the branch_weights are merged and
7+
; attached to merged call rather than dropped.
8+
;
9+
; int func1(int a, int b) ;
10+
; int func2(int a, int b) ;
11+
12+
; int func(int a, int b, bool c) {
13+
; int sum= 0;
14+
; if(c) {
15+
; sum += func1(a, b);
16+
; } else {
17+
; sum += func1(a, b);
18+
; sum -= func2(a, b);
19+
; }
20+
; return sum;
21+
; }
22+
define i32 @_Z4funciib(i32 %a, i32 %b, i1 %c) {
23+
; HOIST-LABEL: define i32 @_Z4funciib
24+
; HOIST-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i1 [[C:%.*]]) {
25+
; HOIST-NEXT: entry:
26+
; HOIST-NEXT: [[CALL:%.*]] = tail call i32 @_Z5func1ii(i32 [[A]], i32 [[B]]), !prof [[PROF0:![0-9]+]]
27+
; HOIST-NEXT: br i1 [[C]], label [[IF_END:%.*]], label [[IF_ELSE:%.*]]
28+
; HOIST: if.else:
29+
; HOIST-NEXT: [[CALL3:%.*]] = tail call i32 @_Z5func2ii(i32 [[A]], i32 [[B]])
30+
; HOIST-NEXT: [[SUB:%.*]] = sub i32 [[CALL]], [[CALL3]]
31+
; HOIST-NEXT: br label [[IF_END]]
32+
; HOIST: if.end:
33+
; HOIST-NEXT: [[SUM_0:%.*]] = phi i32 [ [[SUB]], [[IF_ELSE]] ], [ [[CALL]], [[ENTRY:%.*]] ]
34+
; HOIST-NEXT: ret i32 [[SUM_0]]
35+
;
36+
entry:
37+
br i1 %c, label %if.then, label %if.else
38+
39+
if.then: ; preds = %entry
40+
%call = tail call i32 @_Z5func1ii(i32 %a, i32 %b)
41+
br label %if.end
42+
43+
if.else: ; preds = %entry
44+
%call1 = tail call i32 @_Z5func1ii(i32 %a, i32 %b), !prof !0
45+
%call3 = tail call i32 @_Z5func2ii(i32 %a, i32 %b)
46+
%sub = sub i32 %call1, %call3
47+
br label %if.end
48+
49+
if.end: ; preds = %if.else, %if.then
50+
%sum.0 = phi i32 [ %call, %if.then ], [ %sub, %if.else ]
51+
ret i32 %sum.0
52+
}
53+
54+
declare i32 @_Z5func1ii(i32, i32)
55+
56+
declare i32 @_Z5func2ii(i32, i32)
57+
58+
!0 = !{!"branch_weights", i32 10}
59+
60+
;.
61+
; HOIST: [[PROF0]] = !{!"branch_weights", i32 10}
62+
;.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --version 2
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
3+
4+
5+
; Test case based on the following C++ code with manualy annotated !prof metadata.
6+
; This is to test that when calls to 'func1' from 'if.then' and 'if.else' are
7+
; sinked, the branch weights are merged and attached to sinked call.
8+
;
9+
; int func1(int a, int b) ;
10+
; int func2(int a, int b) ;
11+
12+
; int func(int a, int b, bool c) {
13+
; int sum = 0;
14+
; if (c) {
15+
; sum += func1(a,b);
16+
; } else {
17+
; b -= func2(a,b);
18+
; sum += func1(a,b);
19+
; }
20+
; return sum;
21+
; }
22+
23+
define i32 @_Z4funciib(i32 %a, i32 %b, i1 %c) {
24+
; SINK-LABEL: define i32 @_Z4funciib
25+
; SINK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]], i1 [[C:%.*]]) {
26+
; SINK-NEXT: entry:
27+
; SINK-NEXT: br i1 [[C]], label [[IF_END:%.*]], label [[IF_ELSE:%.*]]
28+
; SINK: if.else:
29+
; SINK-NEXT: [[CALL1:%.*]] = tail call i32 @_Z5func2ii(i32 [[A]], i32 [[B]])
30+
; SINK-NEXT: [[SUB:%.*]] = sub i32 [[B]], [[CALL1]]
31+
; SINK-NEXT: br label [[IF_END]]
32+
; SINK: if.end:
33+
; SINK-NEXT: [[SUB_SINK:%.*]] = phi i32 [ [[SUB]], [[IF_ELSE]] ], [ [[B]], [[ENTRY:%.*]] ]
34+
; SINK-NEXT: [[CALL2:%.*]] = tail call i32 @_Z5func1ii(i32 [[A]], i32 [[SUB_SINK]]), !prof [[PROF0:![0-9]+]]
35+
; SINK-NEXT: ret i32 [[CALL2]]
36+
;
37+
entry:
38+
br i1 %c, label %if.then, label %if.else
39+
40+
if.then: ; preds = %entry
41+
%call = tail call i32 @_Z5func1ii(i32 %a, i32 %b), !prof !0
42+
br label %if.end
43+
44+
if.else: ; preds = %entry
45+
%call1 = tail call i32 @_Z5func2ii(i32 %a, i32 %b)
46+
%sub = sub i32 %b, %call1
47+
%call2 = tail call i32 @_Z5func1ii(i32 %a, i32 %sub)
48+
br label %if.end
49+
50+
if.end: ; preds = %if.else, %if.then
51+
%sum.0 = phi i32 [ %call, %if.then ], [ %call2, %if.else ]
52+
ret i32 %sum.0
53+
}
54+
55+
declare i32 @_Z5func1ii(i32, i32)
56+
57+
declare i32 @_Z5func2ii(i32, i32)
58+
59+
!0 = !{!"branch_weights", i32 10}
60+
61+
;.
62+
; SINK: [[PROF0]] = !{!"branch_weights", i32 10}
63+
;.

0 commit comments

Comments
 (0)