Skip to content

[WinEH] Emit state stores for SEH scopes #116546

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 4 commits into from
Nov 27, 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
21 changes: 21 additions & 0 deletions llvm/lib/Target/X86/X86WinEHState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,28 @@ int WinEHStatePass::getBaseStateForBB(
return BaseState;
}

static bool isIntrinsic(const CallBase &Call, Intrinsic::ID ID) {
const Function *CF = Call.getCalledFunction();
return CF && CF->isIntrinsic() && CF->getIntrinsicID() == ID;
}

static bool isSehScopeEnd(const CallBase &Call) {
return isIntrinsic(Call, Intrinsic::seh_scope_end);
}

static bool isSehScopeBegin(const CallBase &Call) {
return isIntrinsic(Call, Intrinsic::seh_scope_begin);
}

// Calculate the state a call-site is in.
int WinEHStatePass::getStateForCall(
DenseMap<BasicBlock *, ColorVector> &BlockColors, WinEHFuncInfo &FuncInfo,
CallBase &Call) {
if (auto *II = dyn_cast<InvokeInst>(&Call)) {
if (isSehScopeEnd(*II)) {
return getBaseStateForBB(BlockColors, FuncInfo, II->getNormalDest());
}

// Look up the state number of the EH pad this unwinds to.
assert(FuncInfo.InvokeStateMap.count(II) && "invoke has no state!");
return FuncInfo.InvokeStateMap[II];
Expand Down Expand Up @@ -610,6 +627,10 @@ static int getSuccState(DenseMap<BasicBlock *, int> &InitialStates, Function &F,

bool WinEHStatePass::isStateStoreNeeded(EHPersonality Personality,
CallBase &Call) {
if (isSehScopeBegin(Call) || isSehScopeEnd(Call)) {
return true;
}

// If the function touches memory, it needs a state store.
if (isAsynchronousEHPersonality(Personality))
return !Call.doesNotAccessMemory();
Expand Down
45 changes: 45 additions & 0 deletions llvm/test/CodeGen/WinEH/wineh-scope-statenumbering.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
; RUN: opt -mtriple=i386-pc-windows-msvc -S -x86-winehstate < %s | FileCheck %s

target datalayout = "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32-a:0:32-S32"
target triple = "i386-pc-windows-msvc19.42.34433"

%struct.Destructor = type { ptr }

define dso_local void @"?HandleDestructorCallWithException@@YAXPA_N@Z"(ptr noundef %destructorCalled) personality ptr @__CxxFrameHandler3 {
entry:
%destructorCalled.addr = alloca ptr, align 4
%x = alloca %struct.Destructor, align 4
store ptr %destructorCalled, ptr %destructorCalled.addr, align 4
%0 = load ptr, ptr %destructorCalled.addr, align 4
%call = call x86_thiscallcc noundef ptr @"??0Destructor@@QAE@PA_N@Z"(ptr noundef nonnull align 4 dereferenceable(4) %x, ptr noundef %0)
; CHECK: store i32 0, ptr %9, align 4
; CHECK-NEXT: invoke void @llvm.seh.scope.begin()
invoke void @llvm.seh.scope.begin()
to label %invoke.cont unwind label %ehcleanup

invoke.cont:
store i32 1, ptr inttoptr (i32 1 to ptr), align 4
; CHECK: store i32 -1, ptr %10, align 4
; CHECK-NEXT: invoke void @llvm.seh.scope.end()
invoke void @llvm.seh.scope.end()
to label %invoke.cont1 unwind label %ehcleanup

invoke.cont1:
call x86_thiscallcc void @"??1Destructor@@QAE@XZ"(ptr noundef nonnull align 4 dereferenceable(4) %x) #1
ret void

ehcleanup:
%1 = cleanuppad within none []
call x86_thiscallcc void @"??1Destructor@@QAE@XZ"(ptr noundef nonnull align 4 dereferenceable(4) %x) #1 [ "funclet"(token %1) ]
cleanupret from %1 unwind to caller
}

declare dso_local i32 @__CxxFrameHandler3(...)
declare dso_local void @llvm.seh.scope.begin() #0
declare dso_local void @llvm.seh.scope.end() #0

declare dso_local x86_thiscallcc noundef ptr @"??0Destructor@@QAE@PA_N@Z"(ptr noundef nonnull returned align 4 dereferenceable(4) %this, ptr noundef %destructorCalled)
declare dso_local x86_thiscallcc void @"??1Destructor@@QAE@XZ"(ptr noundef nonnull align 4 dereferenceable(4) %this) #1

attributes #0 = { nounwind memory(none) }
attributes #1 = { nounwind }
Loading