Skip to content

Commit 296ccef

Browse files
committed
[WebAssembly] EmscriptenEHSjLj: Mark __invoke_ functions as imported
This means the linker will be expect them be undefined at link time an will generate imports from the `env` module rather than reporting undefined externals. Differential Revision: https://reviews.llvm.org/D77192
1 parent f203100 commit 296ccef

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ static std::string getSignature(FunctionType *FTy) {
344344
return Sig;
345345
}
346346

347+
static void markAsImported(Function *F) {
348+
// Tell the linker that this function is expected to be imported from the
349+
// 'env' module.
350+
if (!F->hasFnAttribute("wasm-import-module")) {
351+
llvm::AttrBuilder B;
352+
B.addAttribute("wasm-import-module", "env");
353+
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
354+
}
355+
if (!F->hasFnAttribute("wasm-import-name")) {
356+
llvm::AttrBuilder B;
357+
B.addAttribute("wasm-import-name", F->getName());
358+
F->addAttributes(llvm::AttributeList::FunctionIndex, B);
359+
}
360+
}
361+
347362
// Returns __cxa_find_matching_catch_N function, where N = NumClauses + 2.
348363
// This is because a landingpad instruction contains two more arguments, a
349364
// personality function and a cleanup bit, and __cxa_find_matching_catch_N
@@ -360,6 +375,7 @@ WebAssemblyLowerEmscriptenEHSjLj::getFindMatchingCatch(Module &M,
360375
Function *F = Function::Create(
361376
FTy, GlobalValue::ExternalLinkage,
362377
"__cxa_find_matching_catch_" + Twine(NumClauses + 2), &M);
378+
markAsImported(F);
363379
FindMatchingCatches[NumClauses] = F;
364380
return F;
365381
}
@@ -469,6 +485,7 @@ Function *WebAssemblyLowerEmscriptenEHSjLj::getInvokeWrapper(CallOrInvoke *CI) {
469485
CalleeFTy->isVarArg());
470486
Function *F =
471487
Function::Create(FTy, GlobalValue::ExternalLinkage, "__invoke_" + Sig, M);
488+
markAsImported(F);
472489
InvokeWrappers[Sig] = F;
473490
return F;
474491
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ attributes #0 = { returns_twice }
308308
attributes #1 = { noreturn }
309309
attributes #2 = { nounwind }
310310
attributes #3 = { allocsize(0) }
311+
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void" }
312+
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__cxa_find_matching_catch_3" }
313+
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_i8*_i32_%struct.__jmp_buf_tag*" }
314+
; CHECK: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void_%struct.__jmp_buf_tag*_i32" }
311315
; CHECK: attributes #[[ALLOCSIZE_ATTR]] = { allocsize(1) }
312316

313317
!llvm.dbg.cu = !{!2}

0 commit comments

Comments
 (0)