Skip to content

Commit df2474e

Browse files
committed
[ir] MD_prof is not UB-implying
1 parent cac2eba commit df2474e

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

llvm/lib/IR/Metadata.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
using namespace llvm;
5959

60+
extern cl::opt<bool> ProfcheckDisableMetadataFixes;
61+
6062
MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD)
6163
: Value(Ty, MetadataAsValueVal), MD(MD) {
6264
track();
@@ -1678,6 +1680,8 @@ void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) {
16781680

16791681
// A DIAssignID attachment is debug metadata, don't drop it.
16801682
KnownSet.insert(LLVMContext::MD_DIAssignID);
1683+
if (!ProfcheckDisableMetadataFixes)
1684+
KnownSet.insert(LLVMContext::MD_prof);
16811685

16821686
Value::eraseMetadataIf([&KnownSet](unsigned MDKind, MDNode *Node) {
16831687
return !KnownSet.count(MDKind);

llvm/test/Transforms/LICM/hoist-phi-metadata.ll

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,46 @@ end:
4545
ret void
4646
}
4747

48+
declare i32 @getv()
49+
50+
; indirect.goto.dest2 should get hoisted, and that should not result
51+
; in a loss of profiling info
52+
define i32 @test19(i1 %cond, i1 %cond2, ptr %address, i32 %v1) nounwind {
53+
; CHECK-LABEL: define i32 @test19
54+
; CHECK-SAME: (i1 [[COND:%.*]], i1 [[COND2:%.*]], ptr [[ADDRESS:%.*]], i32 [[V1:%.*]]) #[[ATTR0:[0-9]+]] {
55+
; CHECK-NEXT: entry:
56+
; CHECK-NEXT: [[INDIRECT_GOTO_DEST:%.*]] = select i1 [[COND]], ptr blockaddress(@test19, [[EXIT:%.*]]), ptr [[ADDRESS]], !prof [[PROF9:![0-9]+]]
57+
; CHECK-NEXT: [[INDIRECT_GOTO_DEST2:%.*]] = select i1 [[COND2]], ptr blockaddress(@test19, [[EXIT]]), ptr [[ADDRESS]], !prof [[PROF10:![0-9]+]]
58+
; CHECK-NEXT: br label [[L0:%.*]]
59+
; CHECK: L0:
60+
; CHECK-NEXT: [[V2:%.*]] = call i32 @getv()
61+
; CHECK-NEXT: [[SINKABLE:%.*]] = mul i32 [[V1]], [[V2]]
62+
; CHECK-NEXT: [[SINKABLE2:%.*]] = add i32 [[V1]], [[V2]]
63+
; CHECK-NEXT: indirectbr ptr [[INDIRECT_GOTO_DEST]], [label [[L1:%.*]], label %exit]
64+
; CHECK: L1:
65+
; CHECK-NEXT: indirectbr ptr [[INDIRECT_GOTO_DEST2]], [label [[L0]], label %exit]
66+
; CHECK: exit:
67+
; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[SINKABLE]], [[L0]] ], [ [[SINKABLE2]], [[L1]] ]
68+
; CHECK-NEXT: ret i32 [[R]]
69+
;
70+
entry:
71+
br label %L0
72+
L0:
73+
%indirect.goto.dest = select i1 %cond, ptr blockaddress(@test19, %exit), ptr %address, !prof !10
74+
%v2 = call i32 @getv()
75+
%sinkable = mul i32 %v1, %v2
76+
%sinkable2 = add i32 %v1, %v2
77+
indirectbr ptr %indirect.goto.dest, [label %L1, label %exit]
78+
79+
L1:
80+
%indirect.goto.dest2 = select i1 %cond2, ptr blockaddress(@test19, %exit), ptr %address, !prof !11
81+
indirectbr ptr %indirect.goto.dest2, [label %L0, label %exit]
82+
83+
exit:
84+
%r = phi i32 [%sinkable, %L0], [%sinkable2, %L1]
85+
ret i32 %r
86+
}
87+
4888
!llvm.module.flags = !{!2, !3}
4989

5090
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1)
@@ -57,6 +97,10 @@ end:
5797
!7 = !DILocation(line: 3, column: 22, scope: !4)
5898
!8 = !{!"branch_weights", i32 5, i32 7}
5999
!9 = !{!"branch_weights", i32 13, i32 11}
100+
!10 = !{!"branch_weights", i32 101, i32 189}
101+
!11 = !{!"branch_weights", i32 67, i32 1}
102+
;.
103+
; CHECK: attributes #[[ATTR0]] = { nounwind }
60104
;.
61105
; CHECK: [[META0:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 5}
62106
; CHECK: [[META1:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}

0 commit comments

Comments
 (0)