Skip to content

Checking SEHorCXXSEH before disabling lifetime marker #116

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 3 commits into from
Dec 18, 2024
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
50 changes: 30 additions & 20 deletions llvm/lib/CodeGen/AtomicExpandPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,12 @@ bool AtomicExpand::expandAtomicOpToLibcall(
Args.push_back(ConstantInt::get(DL.getIntPtrType(Ctx), Size));
}

bool bShouldDisableLifetimeMarker = false;

if (I->getParent() && I->getParent()->getParent() && I->getParent()->getParent()->hasSEHOrCXXSEH()) {
bShouldDisableLifetimeMarker = true;
}

// 'ptr' argument.
// note: This assumes all address spaces share a common libfunc
// implementation and that addresses are convertable. For systems without
Expand All @@ -1860,9 +1866,11 @@ bool AtomicExpand::expandAtomicOpToLibcall(
if (CASExpected) {
AllocaCASExpected = AllocaBuilder.CreateAlloca(CASExpected->getType());
AllocaCASExpected->setAlignment(AllocaAlignment);
#ifndef _WIN32
Builder.CreateLifetimeStart(AllocaCASExpected, SizeVal64);
#endif
if (!bShouldDisableLifetimeMarker) {

Builder.CreateLifetimeStart(AllocaCASExpected, SizeVal64);
}

Builder.CreateAlignedStore(CASExpected, AllocaCASExpected, AllocaAlignment);
Args.push_back(AllocaCASExpected);
}
Expand All @@ -1876,9 +1884,10 @@ bool AtomicExpand::expandAtomicOpToLibcall(
} else {
AllocaValue = AllocaBuilder.CreateAlloca(ValueOperand->getType());
AllocaValue->setAlignment(AllocaAlignment);
#ifndef _WIN32
Builder.CreateLifetimeStart(AllocaValue, SizeVal64);
#endif
if (!bShouldDisableLifetimeMarker) {
Builder.CreateLifetimeStart(AllocaValue, SizeVal64);
}

Builder.CreateAlignedStore(ValueOperand, AllocaValue, AllocaAlignment);
Args.push_back(AllocaValue);
}
Expand All @@ -1888,9 +1897,10 @@ bool AtomicExpand::expandAtomicOpToLibcall(
if (!CASExpected && HasResult && !UseSizedLibcall) {
AllocaResult = AllocaBuilder.CreateAlloca(I->getType());
AllocaResult->setAlignment(AllocaAlignment);
#ifndef _WIN32
Builder.CreateLifetimeStart(AllocaResult, SizeVal64);
#endif
if (!bShouldDisableLifetimeMarker) {
Builder.CreateLifetimeStart(AllocaResult, SizeVal64);
}

Args.push_back(AllocaResult);
}

Expand Down Expand Up @@ -1920,21 +1930,21 @@ bool AtomicExpand::expandAtomicOpToLibcall(
CallInst *Call = Builder.CreateCall(LibcallFn, Args);
Call->setAttributes(Attr);
Value *Result = Call;
#ifndef _WIN32
// And then, extract the results...
if (ValueOperand && !UseSizedLibcall)
Builder.CreateLifetimeEnd(AllocaValue, SizeVal64);
#endif
if (!bShouldDisableLifetimeMarker) {
// And then, extract the results...
if (ValueOperand && !UseSizedLibcall)
Builder.CreateLifetimeEnd(AllocaValue, SizeVal64);
}
if (CASExpected) {
// The final result from the CAS is {load of 'expected' alloca, bool result
// from call}
Type *FinalResultTy = I->getType();
Value *V = PoisonValue::get(FinalResultTy);
Value *ExpectedOut = Builder.CreateAlignedLoad(
CASExpected->getType(), AllocaCASExpected, AllocaAlignment);
#ifndef _WIN32
Builder.CreateLifetimeEnd(AllocaCASExpected, SizeVal64);
#endif
if (!bShouldDisableLifetimeMarker) {
Builder.CreateLifetimeEnd(AllocaCASExpected, SizeVal64);
}
V = Builder.CreateInsertValue(V, ExpectedOut, 0);
V = Builder.CreateInsertValue(V, Result, 1);
I->replaceAllUsesWith(V);
Expand All @@ -1945,9 +1955,9 @@ bool AtomicExpand::expandAtomicOpToLibcall(
else {
V = Builder.CreateAlignedLoad(I->getType(), AllocaResult,
AllocaAlignment);
#ifndef _WIN32
Builder.CreateLifetimeEnd(AllocaResult, SizeVal64);
#endif
if (!bShouldDisableLifetimeMarker) {
Builder.CreateLifetimeEnd(AllocaResult, SizeVal64);
}
}
I->replaceAllUsesWith(V);
}
Expand Down
14 changes: 10 additions & 4 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3504,16 +3504,22 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
// for the new alloca slice.
Type *PointerTy = IRB.getPtrTy(OldPtr->getType()->getPointerAddressSpace());
Value *Ptr = getNewAllocaSlicePtr(IRB, PointerTy);
#ifndef _WIN32
bool bShouldDisableLifetimeMarker = false;

if (II.getParent() && II.getParent()->getParent() && II.getParent()->getParent()->hasSEHOrCXXSEH()) {
bShouldDisableLifetimeMarker = true;
}

if (!bShouldDisableLifetimeMarker) {
Value *New;
if (II.getIntrinsicID() == Intrinsic::lifetime_start)
New = IRB.CreateLifetimeStart(Ptr, Size);
else
New = IRB.CreateLifetimeEnd(Ptr, Size);

(void)New;
LLVM_DEBUG(dbgs() << " to: " << *New << "\n");
#endif
(void)New;
LLVM_DEBUG(dbgs() << " to: " << *New << "\n");
}
return true;
}

Expand Down
19 changes: 13 additions & 6 deletions llvm/lib/Transforms/Utils/InlineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2610,9 +2610,17 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
AllocaArraySize * AllocaTypeSize);
}
}
#ifndef _WIN32
builder.CreateLifetimeStart(AI, AllocaSize);
#endif


bool bShouldDisableLifetimeMarker = false;

if (AI->getParent() && AI->getParent()->getParent() && AI->getParent()->getParent()->hasSEHOrCXXSEH()) {
bShouldDisableLifetimeMarker = true;
}

if (!bShouldDisableLifetimeMarker)
builder.CreateLifetimeStart(AI, AllocaSize);

for (ReturnInst *RI : Returns) {
// Don't insert llvm.lifetime.end calls between a musttail or deoptimize
// call and a return. The return kills all local allocas.
Expand All @@ -2622,9 +2630,8 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
if (InlinedDeoptimizeCalls &&
RI->getParent()->getTerminatingDeoptimizeCall())
continue;
#ifndef _WIN32
IRBuilder<>(RI).CreateLifetimeEnd(AI, AllocaSize);
#endif
if (!bShouldDisableLifetimeMarker)
IRBuilder<>(RI).CreateLifetimeEnd(AI, AllocaSize);
}
}
}
Expand Down