Skip to content

Commit 830cf36

Browse files
authored
[LVI][ValueTracking] Take UB-implying attributes into account in isSafeToSpeculativelyExecute (llvm#137604)
Closes llvm#137582. In the original case, LVI uses the edge information in `%entry -> %if.end` to get a more precise result. However, since the call to `smin` has an `noundef` return attribute, an immediate UB will be triggered after optimization. Currently, `isSafeToSpeculativelyExecuteWithOpcode(%min)` returns true because llvm@6a288c1 only checks whether the function is speculatable. However, it is not enough in this case. This patch takes UB-implying attributes into account if `IgnoreUBImplyingAttrs` is set to false. If it is set to true, the caller is responsible for correctly propagating UB-implying attributes.
1 parent 4075a36 commit 830cf36

File tree

6 files changed

+86
-19
lines changed

6 files changed

+86
-19
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,31 +539,41 @@ bool isNotCrossLaneOperation(const Instruction *I);
539539
/// move the instruction as long as the correct dominance relationships for
540540
/// the operands and users hold.
541541
///
542+
/// If \p UseVariableInfo is true, the information from non-constant operands
543+
/// will be taken into account.
544+
///
545+
/// If \p IgnoreUBImplyingAttrs is true, UB-implying attributes will be ignored.
546+
/// The caller is responsible for correctly propagating them after hoisting.
547+
///
542548
/// This method can return true for instructions that read memory;
543549
/// for such instructions, moving them may change the resulting value.
544550
bool isSafeToSpeculativelyExecute(const Instruction *I,
545551
const Instruction *CtxI = nullptr,
546552
AssumptionCache *AC = nullptr,
547553
const DominatorTree *DT = nullptr,
548554
const TargetLibraryInfo *TLI = nullptr,
549-
bool UseVariableInfo = true);
555+
bool UseVariableInfo = true,
556+
bool IgnoreUBImplyingAttrs = true);
550557

551558
inline bool isSafeToSpeculativelyExecute(const Instruction *I,
552559
BasicBlock::iterator CtxI,
553560
AssumptionCache *AC = nullptr,
554561
const DominatorTree *DT = nullptr,
555562
const TargetLibraryInfo *TLI = nullptr,
556-
bool UseVariableInfo = true) {
563+
bool UseVariableInfo = true,
564+
bool IgnoreUBImplyingAttrs = true) {
557565
// Take an iterator, and unwrap it into an Instruction *.
558-
return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI, UseVariableInfo);
566+
return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI, UseVariableInfo,
567+
IgnoreUBImplyingAttrs);
559568
}
560569

561570
/// Don't use information from its non-constant operands. This helper is used
562571
/// when its operands are going to be replaced.
563-
inline bool
564-
isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I) {
572+
inline bool isSafeToSpeculativelyExecuteWithVariableReplaced(
573+
const Instruction *I, bool IgnoreUBImplyingAttrs = true) {
565574
return isSafeToSpeculativelyExecute(I, nullptr, nullptr, nullptr, nullptr,
566-
/*UseVariableInfo=*/false);
575+
/*UseVariableInfo=*/false,
576+
IgnoreUBImplyingAttrs);
567577
}
568578

569579
/// This returns the same result as isSafeToSpeculativelyExecute if Opcode is
@@ -586,7 +596,8 @@ isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I) {
586596
bool isSafeToSpeculativelyExecuteWithOpcode(
587597
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI = nullptr,
588598
AssumptionCache *AC = nullptr, const DominatorTree *DT = nullptr,
589-
const TargetLibraryInfo *TLI = nullptr, bool UseVariableInfo = true);
599+
const TargetLibraryInfo *TLI = nullptr, bool UseVariableInfo = true,
600+
bool IgnoreUBImplyingAttrs = true);
590601

591602
/// Returns true if the result or effects of the given instructions \p I
592603
/// depend values not reachable through the def use graph.

llvm/include/llvm/IR/Instruction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ class Instruction : public User,
585585
/// This should be used when speculating instructions.
586586
void dropUBImplyingAttrsAndMetadata();
587587

588+
/// Return true if this instruction has UB-implying attributes
589+
/// that can cause immediate undefined behavior.
590+
bool hasUBImplyingAttrs() const LLVM_READONLY;
591+
588592
/// Determine whether the exact flag is set.
589593
bool isExact() const LLVM_READONLY;
590594

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,8 @@ ValueLatticeElement LazyValueInfoImpl::getValueAtUse(const Use &U) {
17011701
// of a cycle, we might end up reasoning about values from different cycle
17021702
// iterations (PR60629).
17031703
if (!CurrI->hasOneUse() ||
1704-
!isSafeToSpeculativelyExecuteWithVariableReplaced(CurrI))
1704+
!isSafeToSpeculativelyExecuteWithVariableReplaced(
1705+
CurrI, /*IgnoreUBImplyingAttrs=*/false))
17051706
break;
17061707
CurrU = &*CurrI->use_begin();
17071708
}

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7201,20 +7201,19 @@ bool llvm::isNotCrossLaneOperation(const Instruction *I) {
72017201
!isa<CallBase, BitCastInst, ExtractElementInst>(I);
72027202
}
72037203

7204-
bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst,
7205-
const Instruction *CtxI,
7206-
AssumptionCache *AC,
7207-
const DominatorTree *DT,
7208-
const TargetLibraryInfo *TLI,
7209-
bool UseVariableInfo) {
7204+
bool llvm::isSafeToSpeculativelyExecute(
7205+
const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7206+
const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7207+
bool IgnoreUBImplyingAttrs) {
72107208
return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7211-
AC, DT, TLI, UseVariableInfo);
7209+
AC, DT, TLI, UseVariableInfo,
7210+
IgnoreUBImplyingAttrs);
72127211
}
72137212

72147213
bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
72157214
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
72167215
AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7217-
bool UseVariableInfo) {
7216+
bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
72187217
#ifndef NDEBUG
72197218
if (Inst->getOpcode() != Opcode) {
72207219
// Check that the operands are actually compatible with the Opcode override.
@@ -7287,7 +7286,11 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
72877286

72887287
// The called function could have undefined behavior or side-effects, even
72897288
// if marked readnone nounwind.
7290-
return Callee && Callee->isSpeculatable();
7289+
if (!Callee || !Callee->isSpeculatable())
7290+
return false;
7291+
// Since the operands may be changed after hoisting, undefined behavior may
7292+
// be triggered by some UB-implying attributes.
7293+
return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
72917294
}
72927295
case Instruction::VAArg:
72937296
case Instruction::Alloca:

llvm/lib/IR/Instruction.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ void Instruction::dropUBImplyingAttrsAndUnknownMetadata(
532532
if (!CB)
533533
return;
534534
// For call instructions, we also need to drop parameter and return attributes
535-
// that are can cause UB if the call is moved to a location where the
536-
// attribute is not valid.
535+
// that can cause UB if the call is moved to a location where the attribute is
536+
// not valid.
537537
AttributeList AL = CB->getAttributes();
538538
if (AL.isEmpty())
539539
return;
@@ -554,6 +554,20 @@ void Instruction::dropUBImplyingAttrsAndMetadata() {
554554
dropUBImplyingAttrsAndUnknownMetadata(KnownIDs);
555555
}
556556

557+
bool Instruction::hasUBImplyingAttrs() const {
558+
auto *CB = dyn_cast<CallBase>(this);
559+
if (!CB)
560+
return false;
561+
// For call instructions, we also need to check parameter and return
562+
// attributes that can cause UB.
563+
for (unsigned ArgNo = 0; ArgNo < CB->arg_size(); ArgNo++)
564+
if (CB->isPassingUndefUB(ArgNo))
565+
return true;
566+
return CB->hasRetAttr(Attribute::NoUndef) ||
567+
CB->hasRetAttr(Attribute::Dereferenceable) ||
568+
CB->hasRetAttr(Attribute::DereferenceableOrNull);
569+
}
570+
557571
bool Instruction::isExact() const {
558572
return cast<PossiblyExactOperator>(this)->isExact();
559573
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -passes=correlated-propagation -S | FileCheck %s
3+
4+
; Make sure that the optimization does not introduce immediate UB.
5+
6+
define i8 @test(i16 %x) {
7+
; CHECK-LABEL: define range(i8 -128, 1) i8 @test(
8+
; CHECK-SAME: i16 [[X:%.*]]) {
9+
; CHECK-NEXT: [[ENTRY:.*]]:
10+
; CHECK-NEXT: [[OR:%.*]] = or i16 [[X]], 1
11+
; CHECK-NEXT: [[CONV:%.*]] = trunc i16 [[OR]] to i8
12+
; CHECK-NEXT: [[MIN:%.*]] = call noundef i8 @llvm.smin.i8(i8 [[CONV]], i8 0)
13+
; CHECK-NEXT: [[COND:%.*]] = icmp eq i16 [[X]], 0
14+
; CHECK-NEXT: br i1 [[COND]], label %[[IF_END:.*]], label %[[IF_THEN:.*]]
15+
; CHECK: [[IF_THEN]]:
16+
; CHECK-NEXT: br label %[[IF_END]]
17+
; CHECK: [[IF_END]]:
18+
; CHECK-NEXT: [[RES:%.*]] = phi i8 [ [[MIN]], %[[ENTRY]] ], [ 0, %[[IF_THEN]] ]
19+
; CHECK-NEXT: ret i8 [[RES]]
20+
;
21+
entry:
22+
%or = or i16 %x, 1
23+
%conv = trunc i16 %or to i8
24+
%min = call noundef i8 @llvm.smin.i8(i8 %conv, i8 0)
25+
%cond = icmp eq i16 %x, 0
26+
br i1 %cond, label %if.end, label %if.then
27+
28+
if.then:
29+
br label %if.end
30+
31+
if.end:
32+
%res = phi i8 [ %min, %entry ], [ 0, %if.then ]
33+
ret i8 %res
34+
}

0 commit comments

Comments
 (0)