Skip to content

Commit 336ba46

Browse files
committed
[ir] MD_prof is not UB-implying
1 parent b8aa221 commit 336ba46

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

llvm/include/llvm/IR/Instruction.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,10 @@ class Instruction : public User,
584584
dropUBImplyingAttrsAndUnknownMetadata(ArrayRef<unsigned> KnownIDs = {});
585585

586586
/// Drop any attributes or metadata that can cause immediate undefined
587-
/// behavior. Retain other attributes/metadata on a best-effort basis.
588-
/// This should be used when speculating instructions.
589-
LLVM_ABI void dropUBImplyingAttrsAndMetadata();
587+
/// behavior. Retain other attributes/metadata on a best-effort basis, as well
588+
/// as those passed in `Keep`. This should be used when speculating
589+
/// instructions.
590+
LLVM_ABI void dropUBImplyingAttrsAndMetadata(ArrayRef<unsigned> Keep = {});
590591

591592
/// Return true if this instruction has UB-implying attributes
592593
/// that can cause immediate undefined behavior.

llvm/lib/IR/Instruction.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,14 +554,18 @@ void Instruction::dropUBImplyingAttrsAndUnknownMetadata(
554554
CB->removeRetAttrs(UBImplyingAttributes);
555555
}
556556

557-
void Instruction::dropUBImplyingAttrsAndMetadata() {
557+
void Instruction::dropUBImplyingAttrsAndMetadata(ArrayRef<unsigned> Keep) {
558558
// !annotation metadata does not impact semantics.
559559
// !range, !nonnull and !align produce poison, so they are safe to speculate.
560560
// !noundef and various AA metadata must be dropped, as it generally produces
561561
// immediate undefined behavior.
562-
unsigned KnownIDs[] = {LLVMContext::MD_annotation, LLVMContext::MD_range,
563-
LLVMContext::MD_nonnull, LLVMContext::MD_align};
564-
dropUBImplyingAttrsAndUnknownMetadata(KnownIDs);
562+
static const unsigned KnownIDs[] = {
563+
LLVMContext::MD_annotation, LLVMContext::MD_range,
564+
LLVMContext::MD_nonnull, LLVMContext::MD_align};
565+
SmallVector<unsigned> KeepIDs(Keep.size() + std::size(KnownIDs));
566+
append_range(KeepIDs, KnownIDs);
567+
append_range(KeepIDs, Keep);
568+
dropUBImplyingAttrsAndUnknownMetadata(KeepIDs);
565569
}
566570

567571
bool Instruction::hasUBImplyingAttrs() const {

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,8 +1698,12 @@ static void hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
16981698
// The check on hasMetadataOtherThanDebugLoc is to prevent us from burning
16991699
// time in isGuaranteedToExecute if we don't actually have anything to
17001700
// drop. It is a compile time optimization, not required for correctness.
1701-
!SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop))
1702-
I.dropUBImplyingAttrsAndMetadata();
1701+
!SafetyInfo->isGuaranteedToExecute(I, DT, CurLoop)) {
1702+
if (ProfcheckDisableMetadataFixes)
1703+
I.dropUBImplyingAttrsAndMetadata();
1704+
else
1705+
I.dropUBImplyingAttrsAndMetadata({LLVMContext::MD_prof});
1706+
}
17031707

17041708
if (isa<PHINode>(I))
17051709
// Move the new node to the end of the phi list in the destination block.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals --version 2
2+
; Test that hoisting conditional branches copies the debug and profiling info
3+
; metadata from the branch being hoisted.
4+
; RUN: opt -S -passes=licm %s -o - | FileCheck %s
5+
6+
declare i32 @foo()
7+
8+
; to_hoist should get hoisted, and that should not result
9+
; in a loss of profiling info
10+
define i32 @hoist_select(i1 %cond, i32 %a, i32 %b) nounwind {
11+
; CHECK-LABEL: define i32 @hoist_select
12+
; CHECK-SAME: (i1 [[COND:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR0:[0-9]+]] {
13+
; CHECK-NEXT: entry:
14+
; CHECK-NEXT: [[TO_HOIST:%.*]] = select i1 [[COND]], i32 [[A]], i32 [[B]], !prof [[PROF0:![0-9]+]]
15+
; CHECK-NEXT: br label [[L0:%.*]]
16+
; CHECK: L0:
17+
; CHECK-NEXT: [[G:%.*]] = call i32 @foo()
18+
; CHECK-NEXT: [[SUM:%.*]] = add i32 [[G]], [[TO_HOIST]]
19+
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[SUM]], 0
20+
; CHECK-NEXT: br i1 [[C]], label [[L0]], label [[EXIT:%.*]], !prof [[PROF1:![0-9]+]]
21+
; CHECK: exit:
22+
; CHECK-NEXT: [[SUM_LCSSA:%.*]] = phi i32 [ [[SUM]], [[L0]] ]
23+
; CHECK-NEXT: ret i32 [[SUM_LCSSA]]
24+
;
25+
entry:
26+
br label %L0
27+
L0:
28+
%g = call i32 @foo()
29+
%to_hoist = select i1 %cond, i32 %a, i32 %b, !prof !0
30+
%sum = add i32 %g, %to_hoist
31+
%c = icmp eq i32 %sum, 0
32+
br i1 %c, label %L0, label %exit, !prof !1
33+
34+
exit:
35+
ret i32 %sum
36+
}
37+
38+
!0 = !{!"branch_weights", i32 2, i32 5}
39+
!1 = !{!"branch_weights", i32 101, i32 189}
40+
;.
41+
; CHECK: attributes #[[ATTR0]] = { nounwind }
42+
;.
43+
; CHECK: [[PROF0]] = !{!"branch_weights", i32 2, i32 5}
44+
; CHECK: [[PROF1]] = !{!"branch_weights", i32 101, i32 189}
45+
;.

0 commit comments

Comments
 (0)