diff --git a/tslang/include/TypeScript/Pass/ExportFixPass.h b/tslang/include/TypeScript/Pass/ExportFixPass.h index f7c082f05..408496deb 100644 --- a/tslang/include/TypeScript/Pass/ExportFixPass.h +++ b/tslang/include/TypeScript/Pass/ExportFixPass.h @@ -10,9 +10,11 @@ namespace ts { private: bool isWindowsMSVCEnvironment; + bool isWasm; public: - ExportFixPass(bool isWindowsMSVCEnvironment) : isWindowsMSVCEnvironment(isWindowsMSVCEnvironment) { + ExportFixPass(bool isWindowsMSVCEnvironment, bool isWasm = false) + : isWindowsMSVCEnvironment(isWindowsMSVCEnvironment), isWasm(isWasm) { } llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM); diff --git a/tslang/lib/TypeScriptExportPass/ExportFixPass.cpp b/tslang/lib/TypeScriptExportPass/ExportFixPass.cpp index c0e5e1896..38db06324 100644 --- a/tslang/lib/TypeScriptExportPass/ExportFixPass.cpp +++ b/tslang/lib/TypeScriptExportPass/ExportFixPass.cpp @@ -14,11 +14,23 @@ using namespace PatternMatch; struct ExportFixPassCode { bool isWindowsMSVCEnvironment; + bool isWasm; - ExportFixPassCode(bool isWindowsMSVCEnvironment) : isWindowsMSVCEnvironment(isWindowsMSVCEnvironment) + ExportFixPassCode(bool isWindowsMSVCEnvironment, bool isWasm) + : isWindowsMSVCEnvironment(isWindowsMSVCEnvironment), isWasm(isWasm) { } + // wasm: an exported function names itself an export of the module (.export_name), as the + // DLLExport storage class does on Windows. A wasm program is linked without --export-all, which + // kept every default-library function alive (exe.cpp), so this is what keeps the 'export'ed + // ones reachable from the host. + void exportFromWasm(Function &F) + { + if (isWasm && !F.isDeclaration() && !F.hasFnAttribute("wasm-export-name")) + F.addFnAttr("wasm-export-name", F.getName()); + } + bool runOnFunction(Function &F) { auto MadeChange = false; @@ -43,6 +55,7 @@ struct ExportFixPassCode // set DLLExport if (isWindowsMSVCEnvironment) F.setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); + exportFromWasm(F); MadeChange = true; } @@ -63,6 +76,7 @@ struct ExportFixPassCode // set DLLExport if (isWindowsMSVCEnvironment) F.setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); + exportFromWasm(F); MadeChange = true; } @@ -128,7 +142,7 @@ namespace ts { llvm::PreservedAnalyses ExportFixPass::run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) { - ExportFixPassCode LPF{isWindowsMSVCEnvironment}; + ExportFixPassCode LPF{isWindowsMSVCEnvironment, isWasm}; bool MadeChange = false; for (auto &G : M.globals()) diff --git a/tslang/tslang/exe.cpp b/tslang/tslang/exe.cpp index 098bd88c9..7b8d46087 100644 --- a/tslang/tslang/exe.cpp +++ b/tslang/tslang/exe.cpp @@ -1048,6 +1048,12 @@ int linkBinary(const char *argv0, const LinkRequest &request, CompileOptions &co { args.push_back("-luser32"); + // Each function is a COMDAT of its own (obj.cpp), so the linker drops the ones nothing calls + // (/OPT:REF, its default without /DEBUG). It would also fold functions whose code is + // identical into one (/OPT:ICF), but two TypeScript functions are two values that must stay + // apart, so not that. + args.push_back("-Wl,-opt:noicf"); + // Link the static CRT (/MT[d]) to match the prebuilt static LLVM/MLIR libs, // the TypeScript runtime libs and gc.lib. Mixing the static and dynamic CRT // gives the generated program a separate heap/stdout buffer from those libs @@ -1163,10 +1169,16 @@ int linkBinary(const char *argv0, const LinkRequest &request, CompileOptions &co if (shared) { - // something missing in .so compiling + // something missing in .so compiling args.push_back("-Wl,--build-id"); } + // Each function and global is in a section of its own (obj.cpp): drop the ones nothing + // reaches, as link.exe does by default. What a shared library exports, what the program + // exports for the libraries it loads (--export-dynamic-symbol) and what a shared library on + // the command line references (malloc from libc) all stay. + args.push_back("-Wl,--gc-sections"); + // TODO: review some options args.push_back("-frtti"); args.push_back("-fexceptions"); @@ -1278,7 +1290,23 @@ int linkBinary(const char *argv0, const LinkRequest &request, CompileOptions &co else { removeCommandArgs(c.get(), {"crt1.o", "-lc", "clang_rt.builtins"}); - addCommandArgs(c.get(), {"--no-entry", "--export-all", "--allow-undefined"}); + addCommandArgs(c.get(), {"--no-entry", "--allow-undefined"}); + if (shared) + { + addCommandArgs(c.get(), {"--export-all"}); + } + else + { + // A program exports what the host (tslang/test/wasm/host.mjs) starts it with, and the + // functions it marks 'export' (ExportFixPass names those). --export-all rooted every + // default-library function as well, so none of them was ever dropped: a program using + // a Map came out ten times as large. + addCommandArgs(c.get(), {"--export-table", "--export-if-defined=main", + "--export-if-defined=__main_void", "--export-if-defined=__original_main", + "--export-if-defined=__wasm_call_ctors", "--export-if-defined=__heap_base", + "--export-if-defined=__tls_size", "--export-if-defined=__wasm_init_tls"}); + } + if (compileOptions.wasmThreads) { // threads: each runs the module in a worker of its own, on the one memory, which the host diff --git a/tslang/tslang/obj.cpp b/tslang/tslang/obj.cpp index 56bb5c040..8594412b8 100644 --- a/tslang/tslang/obj.cpp +++ b/tslang/tslang/obj.cpp @@ -261,6 +261,27 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptrsecond->getNumOccurrences() == 0; + }; + if (notGiven("function-sections")) + { + Options.FunctionSections = true; + } + + if (notGiven("data-sections")) + { + Options.DataSections = true; + } + // before the target machine is made: its MCAsmInfo takes the exception model from here, and // with none the code generator lowers every invoke to a call (see wasm-enable-eh above) if (TheTriple.isWasm() && !TheTriple.isOSEmscripten()) diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 88a1a8f6e..324f7c91e 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -1310,7 +1310,7 @@ std::function makeCustomPassesWithOptimizingTransfo } llvm::Triple triple(m->getTargetTriple()); - mpm.addPass(ts::ExportFixPass(triple.isWindowsMSVCEnvironment())); + mpm.addPass(ts::ExportFixPass(triple.isWindowsMSVCEnvironment(), triple.isWasm() && !triple.isOSEmscripten())); if (compileOptions.isWasm) {