Skip to content

Commit 0ca2132

Browse files
committed
[WebAssembly] Improve EH/SjLj error messages
This includes a function name and a relevant instruction in error messages when possible, making them more helpful. Reviewed By: dschuff Differential Revision: https://reviews.llvm.org/D120678
1 parent 57f72ad commit 0ca2132

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,9 +1314,14 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runSjLjOnFunction(Function &F) {
13141314
BasicBlock *BB = CB->getParent();
13151315
if (BB->getParent() != &F) // in other function
13161316
continue;
1317-
if (CB->getOperandBundle(LLVMContext::OB_funclet))
1318-
report_fatal_error(
1319-
"setjmp within a catch clause is not supported in Wasm EH");
1317+
if (CB->getOperandBundle(LLVMContext::OB_funclet)) {
1318+
std::string S;
1319+
raw_string_ostream SS(S);
1320+
SS << "In function " + F.getName() +
1321+
": setjmp within a catch clause is not supported in Wasm EH:\n";
1322+
SS << *CB;
1323+
report_fatal_error(StringRef(SS.str()));
1324+
}
13201325

13211326
CallInst *CI = nullptr;
13221327
// setjmp cannot throw. So if it is an invoke, lower it to a call
@@ -1492,10 +1497,16 @@ void WebAssemblyLowerEmscriptenEHSjLj::handleLongjmpableCallsForEmscriptenSjLj(
14921497
for (unsigned I = 0; I < BBs.size(); I++) {
14931498
BasicBlock *BB = BBs[I];
14941499
for (Instruction &I : *BB) {
1495-
if (isa<InvokeInst>(&I))
1496-
report_fatal_error("When using Wasm EH with Emscripten SjLj, there is "
1497-
"a restriction that `setjmp` function call and "
1498-
"exception cannot be used within the same function");
1500+
if (isa<InvokeInst>(&I)) {
1501+
std::string S;
1502+
raw_string_ostream SS(S);
1503+
SS << "In function " << F.getName()
1504+
<< ": When using Wasm EH with Emscripten SjLj, there is a "
1505+
"restriction that `setjmp` function call and exception cannot be "
1506+
"used within the same function:\n";
1507+
SS << I;
1508+
report_fatal_error(StringRef(SS.str()));
1509+
}
14991510
auto *CI = dyn_cast<CallInst>(&I);
15001511
if (!CI)
15011512
continue;

llvm/test/CodeGen/WebAssembly/wasm-eh-em-sjlj-error.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ attributes #0 = { returns_twice }
5050
attributes #1 = { noreturn }
5151
attributes #2 = { nounwind }
5252

53-
; CHECK: LLVM ERROR: When using Wasm EH with Emscripten SjLj, there is a restriction that `setjmp` function call and exception cannot be used within the same function
53+
; CHECK: LLVM ERROR: In function wasm_eh_emscripten_sjlj_error: When using Wasm EH with Emscripten SjLj, there is a restriction that `setjmp` function call and exception cannot be used within the same function
54+
; CHECK-NEXT: invoke void @foo()
55+
; CHECK-NEXT: to label %try.cont unwind label %catch.dispatch

llvm/test/CodeGen/WebAssembly/wasm-eh-sjlj-setjmp-within-catch.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ catch: ; preds = %catch.start
2828
%6 = bitcast i8* %5 to i32*
2929
%7 = load i32, i32* %6, align 4
3030
%arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
31-
; CHECK: LLVM ERROR: setjmp within a catch clause is not supported in Wasm EH
31+
; CHECK: LLVM ERROR: In function setjmp_within_catch: setjmp within a catch clause is not supported in Wasm EH
32+
; CHECK-NEXT: %call = invoke i32 @setjmp
3233
%call = invoke i32 @setjmp(%struct.__jmp_buf_tag* noundef %arraydecay) #2 [ "funclet"(token %1) ]
3334
to label %invoke.cont1 unwind label %ehcleanup
3435

@@ -49,7 +50,6 @@ ehcleanup: ; preds = %catch
4950
cleanupret from %8 unwind to caller
5051
}
5152

52-
5353
declare void @foo()
5454
declare i32 @__gxx_wasm_personality_v0(...)
5555
; Function Attrs: nounwind

0 commit comments

Comments
 (0)