Skip to content

[WebAssembly] Generate __clang_call_terminate for Emscripten EH #129020

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
Feb 28, 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
9 changes: 7 additions & 2 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5150,9 +5150,14 @@ WebAssemblyCXXABI::emitTerminateForUnexpectedException(CodeGenFunction &CGF,
// Itanium ABI calls __clang_call_terminate(), which __cxa_begin_catch() on
// the violating exception to mark it handled, but it is currently hard to do
// with wasm EH instruction structure with catch/catch_all, we just call
// std::terminate and ignore the violating exception as in CGCXXABI.
// std::terminate and ignore the violating exception as in CGCXXABI in Wasm EH
// and call __clang_call_terminate only in Emscripten EH.
// TODO Consider code transformation that makes calling __clang_call_terminate
// possible.
// in Wasm EH possible.
if (Exn && !EHPersonality::get(CGF).isWasmPersonality()) {
Copy link
Member

Choose a reason for hiding this comment

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

I assume that isWasmPersonality() is false when using emscripten EH; out of curiosity, what is the value of EHPersonality in that case?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't change anything in this case, and I checked now, and it is GNU_CPlusPlus defined here:

static const EHPersonality GNU_CPlusPlus;

EHPersonality::isWasmPersonality() returns true when it is GNU_Wasm_CPlusPlus:

bool isWasmPersonality() const { return this == &GNU_Wasm_CPlusPlus; }

assert(CGF.CGM.getLangOpts().CPlusPlus);
return CGF.EmitNounwindRuntimeCall(getClangCallTerminateFn(CGF.CGM), Exn);
}
return CGCXXABI::emitTerminateForUnexpectedException(CGF, Exn);
}

Expand Down
12 changes: 12 additions & 0 deletions clang/test/CodeGenCXX/wasm-eh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 | FileCheck %s
// RUN: %clang_cc1 %s -triple wasm64-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 | FileCheck %s

// Test code generation for Wasm EH using WebAssembly EH proposal.
// (https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md)

void may_throw();
void dont_throw() noexcept;

Expand Down Expand Up @@ -381,6 +384,15 @@ void test8() {

// CHECK: unreachable

void noexcept_throw() noexcept {
throw 3;
}

// CATCH-LABEL: define void @_Z14noexcept_throwv()
// CHECK: %{{.*}} = cleanuppad within none []
// CHECK-NEXT: call void @_ZSt9terminatev()


// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-DEFAULT
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -Wwasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-ON
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -Wno-wasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-OFF
Expand Down
13 changes: 13 additions & 0 deletions clang/test/CodeGenCXX/wasm-em-eh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %clang_cc1 %s -triple wasm32-unknown-emscripten -fexceptions -fcxx-exceptions -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s

// Test code generation for Wasm's Emscripten (JavaScript-style) EH.

void noexcept_throw() noexcept {
throw 3;
}

// CATCH-LABEL: define void @_Z14noexcept_throwv()
// CHECK: %[[LPAD:.*]] = landingpad { ptr, i32 }
// CHECK-NEXT: catch ptr null
// CHECK-NEXT: %[[EXN:.*]] = extractvalue { ptr, i32 } %[[LPAD]], 0
// CHECK-NEXT: call void @__clang_call_terminate(ptr %[[EXN]])