From bed5e00cf73bf978aac07b7e624ab3738b2b5d74 Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Wed, 18 Dec 2024 18:21:36 +0800 Subject: [PATCH 1/3] Update AtomicExpandPass.cpp https://github.com/backengineering/llvm-msvc/issues/115 --- llvm/lib/CodeGen/AtomicExpandPass.cpp | 50 ++++++++++++++++----------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/llvm/lib/CodeGen/AtomicExpandPass.cpp b/llvm/lib/CodeGen/AtomicExpandPass.cpp index d1e2bf86c064..1ba2d37563a2 100644 --- a/llvm/lib/CodeGen/AtomicExpandPass.cpp +++ b/llvm/lib/CodeGen/AtomicExpandPass.cpp @@ -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 @@ -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); } @@ -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); } @@ -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); } @@ -1920,11 +1930,11 @@ 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} @@ -1932,9 +1942,9 @@ bool AtomicExpand::expandAtomicOpToLibcall( 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); @@ -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); } From c21e605f2411678ede947bab3c6d64acf666162c Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Wed, 18 Dec 2024 18:23:01 +0800 Subject: [PATCH 2/3] Update InlineFunction.cpp https://github.com/backengineering/llvm-msvc/issues/115 --- llvm/lib/Transforms/Utils/InlineFunction.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp index 7b64522726f3..15e59d511e76 100644 --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -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. @@ -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); } } } From c92893db47a492365b194db0f38762b3125e2fe2 Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Wed, 18 Dec 2024 18:27:45 +0800 Subject: [PATCH 3/3] Update SROA.cpp https://github.com/backengineering/llvm-msvc/issues/115 --- llvm/lib/Transforms/Scalar/SROA.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index 0ac7effb0ec8..a9a800ba153f 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -3504,16 +3504,22 @@ class AllocaSliceRewriter : public InstVisitor { // 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; }