Index: lib/Target/WebAssembly/WebAssemblyLowerEmscriptenExceptions.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyLowerEmscriptenExceptions.cpp +++ lib/Target/WebAssembly/WebAssemblyLowerEmscriptenExceptions.cpp @@ -109,13 +109,20 @@ #define DEBUG_TYPE "wasm-lower-em-exceptions" +static cl::list + Whitelist("emscripten-cxx-exceptions-whitelist", + cl::desc("The list of function names in which Emscripten-style " + "exception handling is enabled (see emscripten " + "EMSCRIPTEN_CATCHING_WHITELIST options)"), + cl::CommaSeparated); + namespace { class WebAssemblyLowerEmscriptenExceptions final : public ModulePass { const char *getPassName() const override { return "WebAssembly Lower Emscripten Exceptions"; } - bool runOnFunction(Function &F); + bool runOnFunction(Function &F, const std::set &WhitelistSet); // Returns __cxa_find_matching_catch_N function, where N = NumClauses + 2. // This is because a landingpad instruction contains two more arguments, // a personality function and a cleanup bit, and __cxa_find_matching_catch_N @@ -273,11 +280,13 @@ EHTypeIdF = Function::Create(EHTypeIdTy, GlobalValue::ExternalLinkage, "llvm_eh_typeid_for", &M); + std::set WhitelistSet(Whitelist.begin(), Whitelist.end()); + bool Changed = false; for (Function &F : M) { if (F.isDeclaration()) continue; - Changed |= runOnFunction(F); + Changed |= runOnFunction(F, WhitelistSet); } if (!Changed) @@ -325,14 +334,16 @@ return true; } -bool WebAssemblyLowerEmscriptenExceptions::runOnFunction(Function &F) { +bool WebAssemblyLowerEmscriptenExceptions::runOnFunction( + Function &F, const std::set &WhitelistSet) { Module &M = *F.getParent(); LLVMContext &C = F.getContext(); IRBuilder<> Builder(C); bool Changed = false; SmallVector ToErase; SmallPtrSet LandingPads; - bool AllowExceptions = true; // will later change based on whitelist option + bool AllowExceptions = + WhitelistSet.empty() || WhitelistSet.count(F.getName()); for (BasicBlock &BB : F) { auto *II = dyn_cast(BB.getTerminator()); Index: test/CodeGen/WebAssembly/lower-em-exceptions-whitelist.ll =================================================================== --- /dev/null +++ test/CodeGen/WebAssembly/lower-em-exceptions-whitelist.ll @@ -0,0 +1,62 @@ +; RUN: opt < %s -wasm-lower-em-exceptions -emscripten-cxx-exceptions-whitelist=do_catch -S | FileCheck %s + +define void @dont_catch() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { +; CHECK-LABEL: @dont_catch( +entry: + invoke void @foo() + to label %invoke.cont unwind label %lpad +; CHECK: entry: +; CHECK-NEXT: call void @foo() +; CHECK-NEXT: br label %invoke.cont + +invoke.cont: ; preds = %entry + br label %try.cont + +lpad: ; preds = %entry + %0 = landingpad { i8*, i32 } + catch i8* null + %1 = extractvalue { i8*, i32 } %0, 0 + %2 = extractvalue { i8*, i32 } %0, 1 + br label %catch + +catch: ; preds = %lpad + %3 = call i8* @__cxa_begin_catch(i8* %1) + call void @__cxa_end_catch() + br label %try.cont + +try.cont: ; preds = %catch, %invoke.cont + ret void +} + +define void @do_catch() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { +; CHECK-LABEL: @do_catch( +entry: + invoke void @foo() + to label %invoke.cont unwind label %lpad +; CHECK: entry: +; CHECK-NEXT: store i1 false, i1* +; CHECK-NEXT: call void @__invoke_void(void ()* @foo) + +invoke.cont: ; preds = %entry + br label %try.cont + +lpad: ; preds = %entry + %0 = landingpad { i8*, i32 } + catch i8* null + %1 = extractvalue { i8*, i32 } %0, 0 + %2 = extractvalue { i8*, i32 } %0, 1 + br label %catch + +catch: ; preds = %lpad + %3 = call i8* @__cxa_begin_catch(i8* %1) + call void @__cxa_end_catch() + br label %try.cont + +try.cont: ; preds = %catch, %invoke.cont + ret void +} + +declare void @foo() +declare i32 @__gxx_personality_v0(...) +declare i8* @__cxa_begin_catch(i8*) +declare void @__cxa_end_catch()