Skip to content

Commit 147d9d6

Browse files
authored
[WebAssemblyLowerEmscriptenEHSjLj] Avoid setting import_name where possible (#128564)
This change effectively reverts 296ccef (https://reviews.llvm.org/D77192) 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 469757e commit 147d9d6

File tree

4 files changed

+26
-30
lines changed

4 files changed

+26
-30
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

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

443-
static Function *getEmscriptenFunction(FunctionType *Ty, const Twine &Name,
444-
Module *M) {
445-
Function* F = Function::Create(Ty, GlobalValue::ExternalLinkage, Name, M);
443+
static Function *getFunction(FunctionType *Ty, const Twine &Name, Module *M) {
444+
return Function::Create(Ty, GlobalValue::ExternalLinkage, Name, M);
445+
}
446+
447+
static void markAsImported(Function *F) {
446448
// Tell the linker that this function is expected to be imported from the
447-
// 'env' module.
449+
// 'env' module. This is necessary for functions that do not have fixed names
450+
// (e.g. __import_xyz). These names cannot be provided by any kind of shared
451+
// or static library as instead we mark them explictly as imported.
448452
if (!F->hasFnAttribute("wasm-import-module")) {
449-
llvm::AttrBuilder B(M->getContext());
453+
llvm::AttrBuilder B(F->getParent()->getContext());
450454
B.addAttribute("wasm-import-module", "env");
451455
F->addFnAttrs(B);
452456
}
453457
if (!F->hasFnAttribute("wasm-import-name")) {
454-
llvm::AttrBuilder B(M->getContext());
458+
llvm::AttrBuilder B(F->getParent()->getContext());
455459
B.addAttribute("wasm-import-name", F->getName());
456460
F->addFnAttrs(B);
457461
}
458-
return F;
459462
}
460463

461464
// Returns an integer type for the target architecture's address space.
@@ -493,8 +496,9 @@ WebAssemblyLowerEmscriptenEHSjLj::getFindMatchingCatch(Module &M,
493496
PointerType *Int8PtrTy = PointerType::getUnqual(M.getContext());
494497
SmallVector<Type *, 16> Args(NumClauses, Int8PtrTy);
495498
FunctionType *FTy = FunctionType::get(Int8PtrTy, Args, false);
496-
Function *F = getEmscriptenFunction(
499+
Function *F = getFunction(
497500
FTy, "__cxa_find_matching_catch_" + Twine(NumClauses + 2), &M);
501+
markAsImported(F);
498502
FindMatchingCatches[NumClauses] = F;
499503
return F;
500504
}
@@ -586,7 +590,8 @@ Function *WebAssemblyLowerEmscriptenEHSjLj::getInvokeWrapper(CallBase *CI) {
586590

587591
FunctionType *FTy = FunctionType::get(CalleeFTy->getReturnType(), ArgTys,
588592
CalleeFTy->isVarArg());
589-
Function *F = getEmscriptenFunction(FTy, "__invoke_" + Sig, M);
593+
Function *F = getFunction(FTy, "__invoke_" + Sig, M);
594+
markAsImported(F);
590595
InvokeWrappers[Sig] = F;
591596
return F;
592597
}
@@ -927,11 +932,11 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
927932
// exception handling and setjmp/longjmp handling
928933
ThrewGV = getGlobalVariable(M, getAddrIntType(&M), TM, "__THREW__");
929934
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);
935+
GetTempRet0F = getFunction(FunctionType::get(IRB.getInt32Ty(), false),
936+
"getTempRet0", &M);
937+
SetTempRet0F =
938+
getFunction(FunctionType::get(IRB.getVoidTy(), IRB.getInt32Ty(), false),
939+
"setTempRet0", &M);
935940
GetTempRet0F->setDoesNotThrow();
936941
SetTempRet0F->setDoesNotThrow();
937942

@@ -942,13 +947,13 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
942947
// Register __resumeException function
943948
FunctionType *ResumeFTy =
944949
FunctionType::get(IRB.getVoidTy(), IRB.getPtrTy(), false);
945-
ResumeF = getEmscriptenFunction(ResumeFTy, "__resumeException", &M);
950+
ResumeF = getFunction(ResumeFTy, "__resumeException", &M);
946951
ResumeF->addFnAttr(Attribute::NoReturn);
947952

948953
// Register llvm_eh_typeid_for function
949954
FunctionType *EHTypeIDTy =
950955
FunctionType::get(IRB.getInt32Ty(), IRB.getPtrTy(), false);
951-
EHTypeIDF = getEmscriptenFunction(EHTypeIDTy, "llvm_eh_typeid_for", &M);
956+
EHTypeIDF = getFunction(EHTypeIDTy, "llvm_eh_typeid_for", &M);
952957
}
953958

954959
// Functions that contains calls to setjmp but don't have other longjmpable
@@ -988,14 +993,14 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
988993
// Register emscripten_longjmp function
989994
FunctionType *FTy = FunctionType::get(
990995
IRB.getVoidTy(), {getAddrIntType(&M), IRB.getInt32Ty()}, false);
991-
EmLongjmpF = getEmscriptenFunction(FTy, "emscripten_longjmp", &M);
996+
EmLongjmpF = getFunction(FTy, "emscripten_longjmp", &M);
992997
EmLongjmpF->addFnAttr(Attribute::NoReturn);
993998
} else { // EnableWasmSjLj
994999
Type *Int8PtrTy = IRB.getPtrTy();
9951000
// Register __wasm_longjmp function, which calls __builtin_wasm_longjmp.
9961001
FunctionType *FTy = FunctionType::get(
9971002
IRB.getVoidTy(), {Int8PtrTy, IRB.getInt32Ty()}, false);
998-
WasmLongjmpF = getEmscriptenFunction(FTy, "__wasm_longjmp", &M);
1003+
WasmLongjmpF = getFunction(FTy, "__wasm_longjmp", &M);
9991004
WasmLongjmpF->addFnAttr(Attribute::NoReturn);
10001005
}
10011006

@@ -1009,11 +1014,11 @@ bool WebAssemblyLowerEmscriptenEHSjLj::runOnModule(Module &M) {
10091014
FunctionType *FTy = FunctionType::get(
10101015
IRB.getVoidTy(), {SetjmpFTy->getParamType(0), Int32Ty, Int32PtrTy},
10111016
false);
1012-
WasmSetjmpF = getEmscriptenFunction(FTy, "__wasm_setjmp", &M);
1017+
WasmSetjmpF = getFunction(FTy, "__wasm_setjmp", &M);
10131018

10141019
// Register __wasm_setjmp_test function
10151020
FTy = FunctionType::get(Int32Ty, {Int32PtrTy, Int32PtrTy}, false);
1016-
WasmSetjmpTestF = getEmscriptenFunction(FTy, "__wasm_setjmp_test", &M);
1021+
WasmSetjmpTestF = getFunction(FTy, "__wasm_setjmp_test", &M);
10171022

10181023
// wasm.catch() will be lowered down to wasm 'catch' instruction in
10191024
// 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)