-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[LVI][ValueTracking] Take UB-implying attributes into account in isSafeToSpeculativelyExecute
#137604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[LVI][ValueTracking] Take UB-implying attributes into account in isSafeToSpeculativelyExecute
#137604
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e7d1ec
[CVP] Add pre-commit tests. NFC.
dtcxzyw aea16f2
[CVP][ValueTracking] Take UB-implying attrs into account
dtcxzyw acf5b67
[IR] Address review comments.
dtcxzyw 855d3c0
[ValueTracking] Fix comment. NFC.
dtcxzyw 1c26691
[InstCombine] Address review comments. NFC.
dtcxzyw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7201,20 +7201,19 @@ bool llvm::isNotCrossLaneOperation(const Instruction *I) { | |
!isa<CallBase, BitCastInst, ExtractElementInst>(I); | ||
} | ||
|
||
bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst, | ||
const Instruction *CtxI, | ||
AssumptionCache *AC, | ||
const DominatorTree *DT, | ||
const TargetLibraryInfo *TLI, | ||
bool UseVariableInfo) { | ||
bool llvm::isSafeToSpeculativelyExecute( | ||
const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC, | ||
const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo, | ||
bool AllowRefinement) { | ||
return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI, | ||
AC, DT, TLI, UseVariableInfo); | ||
AC, DT, TLI, UseVariableInfo, | ||
AllowRefinement); | ||
} | ||
|
||
bool llvm::isSafeToSpeculativelyExecuteWithOpcode( | ||
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI, | ||
AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI, | ||
bool UseVariableInfo) { | ||
bool UseVariableInfo, bool AllowRefinement) { | ||
#ifndef NDEBUG | ||
if (Inst->getOpcode() != Opcode) { | ||
// Check that the operands are actually compatible with the Opcode override. | ||
|
@@ -7266,7 +7265,7 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode( | |
return false; | ||
} | ||
case Instruction::Load: { | ||
if (!UseVariableInfo) | ||
if (!UseVariableInfo || !AllowRefinement) | ||
return false; | ||
|
||
const LoadInst *LI = dyn_cast<LoadInst>(Inst); | ||
|
@@ -7287,7 +7286,20 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode( | |
|
||
// The called function could have undefined behavior or side-effects, even | ||
// if marked readnone nounwind. | ||
return Callee && Callee->isSpeculatable(); | ||
if (!Callee || !Callee->isSpeculatable()) | ||
return false; | ||
// Since the operands may be changed after hoisting, undefined behavior may | ||
// be triggered by some UB-implying attributes. | ||
if (!AllowRefinement) { | ||
if (CI->hasRetAttr(Attribute::NoUndef) || | ||
CI->getRetDereferenceableBytes() > 0 || | ||
CI->getRetDereferenceableOrNullBytes() > 0 || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should have a has() query for dropUBImplyingAttrsAndMetadata similar to how we have hasPoisonGeneratingAnnotations and dropPoisonGeneratingAnnotations, so we can keep these things in sync. |
||
any_of(CI->args(), [&](const Use &U) { | ||
return CI->isPassingUndefUB(CI->getArgOperandNo(&U)); | ||
})) | ||
return false; | ||
} | ||
return true; | ||
} | ||
case Instruction::VAArg: | ||
case Instruction::Alloca: | ||
|
34 changes: 34 additions & 0 deletions
34
llvm/test/Transforms/CorrelatedValuePropagation/pr137582.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt < %s -passes=correlated-propagation -S | FileCheck %s | ||
|
||
; Make sure that the optimization does not introduce immediate UB. | ||
|
||
define i8 @test(i16 %x) { | ||
; CHECK-LABEL: define range(i8 -128, 1) i8 @test( | ||
; CHECK-SAME: i16 [[X:%.*]]) { | ||
; CHECK-NEXT: [[ENTRY:.*]]: | ||
; CHECK-NEXT: [[OR:%.*]] = or i16 [[X]], 1 | ||
; CHECK-NEXT: [[CONV:%.*]] = trunc i16 [[OR]] to i8 | ||
; CHECK-NEXT: [[MIN:%.*]] = call noundef i8 @llvm.smin.i8(i8 [[CONV]], i8 0) | ||
; CHECK-NEXT: [[COND:%.*]] = icmp eq i16 [[X]], 0 | ||
; CHECK-NEXT: br i1 [[COND]], label %[[IF_END:.*]], label %[[IF_THEN:.*]] | ||
; CHECK: [[IF_THEN]]: | ||
; CHECK-NEXT: br label %[[IF_END]] | ||
; CHECK: [[IF_END]]: | ||
; CHECK-NEXT: [[RES:%.*]] = phi i8 [ [[MIN]], %[[ENTRY]] ], [ 0, %[[IF_THEN]] ] | ||
; CHECK-NEXT: ret i8 [[RES]] | ||
; | ||
entry: | ||
%or = or i16 %x, 1 | ||
%conv = trunc i16 %or to i8 | ||
%min = call noundef i8 @llvm.smin.i8(i8 %conv, i8 0) | ||
%cond = icmp eq i16 %x, 0 | ||
br i1 %cond, label %if.end, label %if.then | ||
|
||
if.then: | ||
br label %if.end | ||
|
||
if.end: | ||
%res = phi i8 [ %min, %entry ], [ 0, %if.then ] | ||
ret i8 %res | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think AllowRefinement is really the right flag name for this. E.g. refinement to poison is fine.
I'd give this a more explicit name like IgnoreUBImplifyAttrs.