Skip to content

Commit 5779a1a

Browse files
committed
[WebAssemblyLowerEmscriptenEHSjLj] Avoid setting import_name where possible
Most of these symbols are just normal C symbols that get imported from wither libcompiler-rt or from emscripten's JS library code. In most cases it should not be necessary to give them explicit import names. The advantage of doing this is that we can wasm-ld can/will fail with a useful error message when these symbols are missing. As opposed to today where it will simply import them and defer errors until later (when they are less specific).
1 parent 1e85e5a commit 5779a1a

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,13 @@ static std::string getSignature(FunctionType *FTy) {
440440
return Sig;
441441
}
442442

443+
static Function *getFunction(FunctionType *Ty, const Twine &Name, Module *M) {
444+
return Function::Create(Ty, GlobalValue::ExternalLinkage, Name, M);
445+
}
446+
443447
static Function *getEmscriptenFunction(FunctionType *Ty, const Twine &Name,
444448
Module *M) {
445-
Function* F = Function::Create(Ty, GlobalValue::ExternalLinkage, Name, M);
449+
Function *F = getFunction(Ty, Name, M);
446450
// Tell the linker that this function is expected to be imported from the
447451
// 'env' module.
448452
if (!F->hasFnAttribute("wasm-import-module")) {
@@ -927,11 +931,11 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
927931
// exception handling and setjmp/longjmp handling
928932
ThrewGV = getGlobalVariable(M, getAddrIntType(&M), TM, "__THREW__");
929933
ThrewValueGV = getGlobalVariable(M, IRB.getInt32Ty(), TM, "__threwValue");
930-
GetTempRet0F = getEmscriptenFunction(
931-
FunctionType::get(IRB.getInt32Ty(), false), "getTempRet0", &M);
932-
SetTempRet0F = getEmscriptenFunction(
933-
FunctionType::get(IRB.getVoidTy(), IRB.getInt32Ty(), false),
934-
"setTempRet0", &M);
934+
GetTempRet0F = getFunction(FunctionType::get(IRB.getInt32Ty(), false),
935+
"getTempRet0", &M);
936+
SetTempRet0F =
937+
getFunction(FunctionType::get(IRB.getVoidTy(), IRB.getInt32Ty(), false),
938+
"setTempRet0", &M);
935939
GetTempRet0F->setDoesNotThrow();
936940
SetTempRet0F->setDoesNotThrow();
937941

@@ -942,13 +946,13 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
942946
// Register __resumeException function
943947
FunctionType *ResumeFTy =
944948
FunctionType::get(IRB.getVoidTy(), IRB.getPtrTy(), false);
945-
ResumeF = getEmscriptenFunction(ResumeFTy, "__resumeException", &M);
949+
ResumeF = getFunction(ResumeFTy, "__resumeException", &M);
946950
ResumeF->addFnAttr(Attribute::NoReturn);
947951

948952
// Register llvm_eh_typeid_for function
949953
FunctionType *EHTypeIDTy =
950954
FunctionType::get(IRB.getInt32Ty(), IRB.getPtrTy(), false);
951-
EHTypeIDF = getEmscriptenFunction(EHTypeIDTy, "llvm_eh_typeid_for", &M);
955+
EHTypeIDF = getFunction(EHTypeIDTy, "llvm_eh_typeid_for", &M);
952956
}
953957

954958
// Functions that contains calls to setjmp but don't have other longjmpable
@@ -988,14 +992,14 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
988992
// Register emscripten_longjmp function
989993
FunctionType *FTy = FunctionType::get(
990994
IRB.getVoidTy(), {getAddrIntType(&M), IRB.getInt32Ty()}, false);
991-
EmLongjmpF = getEmscriptenFunction(FTy, "emscripten_longjmp", &M);
995+
EmLongjmpF = getFunction(FTy, "emscripten_longjmp", &M);
992996
EmLongjmpF->addFnAttr(Attribute::NoReturn);
993997
} else { // EnableWasmSjLj
994998
Type *Int8PtrTy = IRB.getPtrTy();
995999
// Register __wasm_longjmp function, which calls __builtin_wasm_longjmp.
9961000
FunctionType *FTy = FunctionType::get(
9971001
IRB.getVoidTy(), {Int8PtrTy, IRB.getInt32Ty()}, false);
998-
WasmLongjmpF = getEmscriptenFunction(FTy, "__wasm_longjmp", &M);
1002+
WasmLongjmpF = getFunction(FTy, "__wasm_longjmp", &M);
9991003
WasmLongjmpF->addFnAttr(Attribute::NoReturn);
10001004
}
10011005

@@ -1009,11 +1013,11 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
10091013
FunctionType *FTy = FunctionType::get(
10101014
IRB.getVoidTy(), {SetjmpFTy->getParamType(0), Int32Ty, Int32PtrTy},
10111015
false);
1012-
WasmSetjmpF = getEmscriptenFunction(FTy, "__wasm_setjmp", &M);
1016+
WasmSetjmpF = getFunction(FTy, "__wasm_setjmp", &M);
10131017

10141018
// Register __wasm_setjmp_test function
10151019
FTy = FunctionType::get(Int32Ty, {Int32PtrTy, Int32PtrTy}, false);
1016-
WasmSetjmpTestF = getEmscriptenFunction(FTy, "__wasm_setjmp_test", &M);
1020+
WasmSetjmpTestF = getFunction(FTy, "__wasm_setjmp_test", &M);
10171021

10181022
// wasm.catch() will be lowered down to wasm 'catch' instruction in
10191023
// instruction selection.

llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj-options.ll

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ target triple = "wasm32-unknown-unknown"
1212
; EH-NOT: .import_name __invoke_void_i32
1313

1414
; SJLJ: .functype emscripten_longjmp (i32, i32) -> ()
15-
; SJLJ: .import_module emscripten_longjmp, env
16-
; SJLJ: .import_name emscripten_longjmp, emscripten_longjmp
1715
; SJLJ-NOT: .functype emscripten_longjmp_jmpbuf
18-
; SJLJ-NOT: .import_module emscripten_longjmp_jmpbuf
19-
; SJLJ-NOT: .import_name emscripten_longjmp_jmpbuf
2016

2117
%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
2218

llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,7 @@ attributes #0 = { returns_twice }
292292
attributes #1 = { noreturn }
293293
attributes #2 = { nounwind }
294294
attributes #3 = { allocsize(0) }
295-
; CHECK-DAG: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="getTempRet0" }
296-
; CHECK-DAG: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="setTempRet0" }
297295
; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void" }
298-
; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__wasm_setjmp" }
299-
; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__wasm_setjmp_test" }
300-
; CHECK-DAG: attributes #{{[0-9]+}} = { noreturn "wasm-import-module"="env" "wasm-import-name"="emscripten_longjmp" }
301296
; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_ptr_i32_ptr" }
302297
; CHECK-DAG: attributes #[[ALLOCSIZE_ATTR]] = { allocsize(1) }
303298

llvm/test/CodeGen/WebAssembly/lower-wasm-ehsjlj.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ catch: ; preds = %catch.start
109109
catchret from %2 to label %catchret.dest
110110
; CHECK: catch: ; preds = %catch.start
111111
; CHECK-NEXT: %exn = load ptr, ptr %exn.slot6, align 4
112-
; CHECK-NEXT: %5 = call ptr @__cxa_begin_catch(ptr %exn) #6 [ "funclet"(token %2) ]
112+
; CHECK-NEXT: %5 = call ptr @__cxa_begin_catch(ptr %exn) #3 [ "funclet"(token %2) ]
113113
; CHECK-NEXT: invoke void @__cxa_end_catch() [ "funclet"(token %2) ]
114114
; CHECK-NEXT: to label %.noexc unwind label %catch.dispatch.longjmp
115115

0 commit comments

Comments
 (0)