Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e33997

Browse files
authored
[IR] Add MD_prof to the Keep list of dropUBImplyingAttrsAndMetadata (llvm#154635)
`MD_prof` is safe to keep when e.g. hoisting instructions. Issue llvm#147390
1 parent f019e23 commit 9e33997

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

llvm/lib/IR/Instruction.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,16 +553,17 @@ void Instruction::dropUBImplyingAttrsAndUnknownMetadata(
553553
}
554554

555555
void Instruction::dropUBImplyingAttrsAndMetadata(ArrayRef<unsigned> Keep) {
556-
// !annotation metadata does not impact semantics.
556+
// !annotation and !prof metadata does not impact semantics.
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.
560560
static const unsigned KnownIDs[] = {
561561
LLVMContext::MD_annotation, LLVMContext::MD_range,
562-
LLVMContext::MD_nonnull, LLVMContext::MD_align};
562+
LLVMContext::MD_nonnull, LLVMContext::MD_align, LLVMContext::MD_prof};
563563
SmallVector<unsigned> KeepIDs;
564564
KeepIDs.reserve(Keep.size() + std::size(KnownIDs));
565-
append_range(KeepIDs, KnownIDs);
565+
append_range(KeepIDs, (!ProfcheckDisableMetadataFixes ? KnownIDs
566+
: drop_end(KnownIDs)));
566567
append_range(KeepIDs, Keep);
567568
dropUBImplyingAttrsAndUnknownMetadata(KeepIDs);
568569
}

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,10 +1705,7 @@ static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
17051705
// time in isGuaranteedToExecute if we don't actually have anything to
17061706
// drop. It is a compile time optimization, not required for correctness.
17071707
!SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop)) {
1708-
if (ProfcheckDisableMetadataFixes)
1709-
I.dropUBImplyingAttrsAndMetadata();
1710-
else
1711-
I.dropUBImplyingAttrsAndMetadata({LLVMContext::MD_prof});
1708+
I.dropUBImplyingAttrsAndMetadata();
17121709
}
17131710

17141711
if (isa<PHINode>(I))
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
; NOTE: Assertions have been autogenerated by update_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
22
; Test merging of blocks that only have PHI nodes in them
33
;
44
; RUN: opt < %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S | FileCheck %s
55
;
66

77
define i32 @test(i1 %a, i1 %b) {
8-
; CHECK-LABEL: @test(
9-
; CHECK: M:
10-
; CHECK-NEXT: [[DOT:%.*]] = select i1 %b, i32 0, i32 1
11-
; CHECK-NEXT: [[W:%.*]] = select i1 %a, i32 2, i32 [[DOT]]
8+
; CHECK-LABEL: define i32 @test(
9+
; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]]) {
10+
; CHECK-NEXT: [[M:.*:]]
11+
; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[B]], i32 0, i32 1, !prof [[PROF0:![0-9]+]]
12+
; CHECK-NEXT: [[W:%.*]] = select i1 [[A]], i32 2, i32 [[SPEC_SELECT]], !prof [[PROF1:![0-9]+]]
1213
; CHECK-NEXT: [[R:%.*]] = add i32 [[W]], 1
1314
; CHECK-NEXT: ret i32 [[R]]
1415
;
15-
br i1 %a, label %M, label %O
16+
br i1 %a, label %M, label %O, !prof !0
1617
O: ; preds = %0
17-
br i1 %b, label %N, label %Q
18+
br i1 %b, label %N, label %Q, !prof !1
1819
Q: ; preds = %O
1920
br label %N
2021
N: ; preds = %Q, %O
@@ -27,3 +28,9 @@ M: ; preds = %N, %0
2728
ret i32 %R
2829
}
2930

31+
!0 = !{!"branch_weights", i32 11, i32 7}
32+
!1 = !{!"branch_weights", i32 3, i32 5}
33+
;.
34+
; CHECK: [[PROF0]] = !{!"branch_weights", i32 3, i32 5}
35+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 11, i32 7}
36+
;.

0 commit comments

Comments
 (0)