Skip to content

Commit 31a3f5c

Browse files
committed
[ir] MD_prof is not UB-implying
1 parent 82b2df1 commit 31a3f5c

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

llvm/lib/IR/Instruction.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,13 @@ void Instruction::dropUBImplyingAttrsAndMetadata() {
557557
// !range, !nonnull and !align produce poison, so they are safe to speculate.
558558
// !noundef and various AA metadata must be dropped, as it generally produces
559559
// immediate undefined behavior.
560-
unsigned KnownIDs[] = {LLVMContext::MD_annotation, LLVMContext::MD_range,
561-
LLVMContext::MD_nonnull, LLVMContext::MD_align};
562-
dropUBImplyingAttrsAndUnknownMetadata(KnownIDs);
560+
static const unsigned KnownIDs[] = {
561+
LLVMContext::MD_annotation, LLVMContext::MD_range,
562+
LLVMContext::MD_nonnull, LLVMContext::MD_align, LLVMContext::MD_prof};
563+
ArrayRef<unsigned> K(KnownIDs);
564+
if (ProfcheckDisableMetadataFixes)
565+
K = K.drop_back();
566+
dropUBImplyingAttrsAndUnknownMetadata(K);
563567
}
564568

565569
bool Instruction::hasUBImplyingAttrs() const {

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

Lines changed: 46 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}
@@ -67,4 +111,6 @@ end:
67111
; CHECK: [[PROF6]] = !{!"branch_weights", i32 5, i32 7}
68112
; CHECK: [[DBG7]] = !DILocation(line: 3, column: 22, scope: [[META3]])
69113
; CHECK: [[PROF8]] = !{!"branch_weights", i32 13, i32 11}
114+
; CHECK: [[PROF9]] = !{!"branch_weights", i32 101, i32 189}
115+
; CHECK: [[PROF10]] = !{!"branch_weights", i32 67, i32 1}
70116
;.

0 commit comments

Comments
 (0)