Skip to content

Preserve lifetime marker's order #2

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6057,6 +6057,11 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
Builder.CreateStore(errorResult, swiftErrorArg);
}

// Explicitly call CallLifetimeEnd::Emit just to re-use the code even though
// we can't use the full cleanup mechanism.
for (CallLifetimeEnd &LifetimeEnd : reverse(CallLifetimeEndAfterCall))
LifetimeEnd.Emit(*this, /*Flags=*/{});

// Emit any call-associated writebacks immediately. Arguably this
// should happen after any return-value munging.
if (CallArgs.hasWritebacks())
Expand Down Expand Up @@ -6194,11 +6199,6 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
AllocAlignAttrEmitter.EmitAsAnAssumption(Loc, RetTy, Ret);
}

// Explicitly call CallLifetimeEnd::Emit just to re-use the code even though
// we can't use the full cleanup mechanism.
for (CallLifetimeEnd &LifetimeEnd : CallLifetimeEndAfterCall)
LifetimeEnd.Emit(*this, /*Flags=*/{});

if (!ReturnValue.isExternallyDestructed() &&
RetTy.isDestructedType() == QualType::DK_nontrivial_c_struct)
pushDestroy(QualType::DK_nontrivial_c_struct, Ret.getAggregateAddress(),
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ bool MemCpyOptPass::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) {
/// Takes a memcpy and a call that it depends on,
/// and checks for the possibility of a call slot optimization by having
/// the call write its result directly into the destination of the memcpy.
/// FIXME: it breaks the order of lifetime markers.
bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
Instruction *cpyStore, Value *cpyDest,
Value *cpySrc, TypeSize cpySize,
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2832,7 +2832,11 @@ class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
OldPtr = cast<Instruction>(OldUse->get());

Instruction *OldUserI = cast<Instruction>(OldUse->getUser());
IRB.SetInsertPoint(OldUserI);
auto *OldUserII = dyn_cast<IntrinsicInst>(OldUserI);
if (OldUserII && OldUserII->getIntrinsicID() == Intrinsic::lifetime_end)
IRB.SetInsertPoint(OldUserI->getNextNode());
else
IRB.SetInsertPoint(OldUserI);
IRB.SetCurrentDebugLocation(OldUserI->getDebugLoc());
IRB.getInserter().SetNamePrefix(Twine(NewAI.getName()) + "." +
Twine(BeginOffset) + ".");
Expand Down
23 changes: 13 additions & 10 deletions llvm/lib/Transforms/Utils/InlineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,7 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
if ((InsertLifetime || Caller->isPresplitCoroutine()) &&
!IFI.StaticAllocas.empty()) {
IRBuilder<> builder(&*FirstNewBlock, FirstNewBlock->begin());
SmallVector<std::pair<AllocaInst *, ConstantInt *>> AllocasWithLifetimeEnd;
for (AllocaInst *AI : IFI.StaticAllocas) {
// Don't mark swifterror allocas. They can't have bitcast uses.
if (AI->isSwiftError())
Expand Down Expand Up @@ -3012,17 +3013,19 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
}

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.
if (InlinedMustTailCalls &&
RI->getParent()->getTerminatingMustTailCall())
continue;
if (InlinedDeoptimizeCalls &&
RI->getParent()->getTerminatingDeoptimizeCall())
continue;
AllocasWithLifetimeEnd.emplace_back(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.
if (InlinedMustTailCalls && RI->getParent()->getTerminatingMustTailCall())
continue;
if (InlinedDeoptimizeCalls &&
RI->getParent()->getTerminatingDeoptimizeCall())
continue;
for (auto [AI, AllocaSize] : reverse(AllocasWithLifetimeEnd))
IRBuilder<>(RI).CreateLifetimeEnd(AI, AllocaSize);
}
}
}

Expand Down