Skip to content

[SimplifyCFG] Only consider provenance capture in store speculation #138548

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 1 commit into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3057,7 +3057,9 @@ static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
Value *Obj = getUnderlyingObject(StorePtr);
bool ExplicitlyDereferenceableOnly;
if (isWritableObject(Obj, ExplicitlyDereferenceableOnly) &&
!PointerMayBeCaptured(Obj, /*ReturnCaptures=*/false) &&
capturesNothing(
PointerMayBeCaptured(Obj, /*ReturnCaptures=*/false,
CaptureComponents::Provenance)) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar pattern in LICM:

// We know we can hoist the load, but don't have a guaranteed store.
// Check whether the location is writable and thread-local. If it is, then we
// can insert stores along paths which originally didn't have them without
// violating the memory model.
if (StoreSafety == StoreSafetyUnknown) {
Value *Object = getUnderlyingObject(SomePtr);
bool ExplicitlyDereferenceableOnly;
if (isWritableObject(Object, ExplicitlyDereferenceableOnly) &&
(!ExplicitlyDereferenceableOnly ||
isDereferenceablePointer(SomePtr, AccessTy, MDL)) &&
isThreadLocalObject(Object, CurLoop, DT, TTI))
StoreSafety = StoreSafe;
}

bool isNotCapturedBeforeOrInLoop(const Value *V, const Loop *L,
DominatorTree *DT) {
// We can perform the captured-before check against any instruction in the
// loop header, as the loop header is reachable from any instruction inside
// the loop.
// TODO: ReturnCaptures=true shouldn't be necessary here.
return !PointerMayBeCapturedBefore(V, /* ReturnCaptures */ true,
L->getHeader()->getTerminator(), DT);
}
/// Return true if we can prove that a caller cannot inspect the object if an
/// unwind occurs inside the loop.
bool isNotVisibleOnUnwindInLoop(const Value *Object, const Loop *L,
DominatorTree *DT) {
bool RequiresNoCaptureBeforeUnwind;
if (!isNotVisibleOnUnwind(Object, RequiresNoCaptureBeforeUnwind))
return false;
return !RequiresNoCaptureBeforeUnwind ||
isNotCapturedBeforeOrInLoop(Object, L, DT);
}
bool isThreadLocalObject(const Value *Object, const Loop *L, DominatorTree *DT,
TargetTransformInfo *TTI) {
// The object must be function-local to start with, and then not captured
// before/in the loop.
return (isIdentifiedFunctionLocal(Object) &&
isNotCapturedBeforeOrInLoop(Object, L, DT)) ||
(TTI->isSingleThreaded() || SingleThread);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in #141731.

(!ExplicitlyDereferenceableOnly ||
isDereferenceablePointer(StorePtr, StoreTy,
LI->getDataLayout()))) {
Expand Down
7 changes: 2 additions & 5 deletions llvm/test/Transforms/SimplifyCFG/speculate-store.ll
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,8 @@ define i32 @load_before_store_escape_addr_only(i64 %i, i32 %b) {
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[B:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK: if.then:
; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
; CHECK-NEXT: br label [[IF_END]]
; CHECK: if.end:
; CHECK-NEXT: [[SPEC_STORE_SELECT:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[TMP0]]
; CHECK-NEXT: store i32 [[SPEC_STORE_SELECT]], ptr [[ARRAYIDX]], align 4
; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[A]], align 4
; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
Expand Down