@@ -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.
0 commit comments