From 233330dbab600f9b1ff2e8980a3b9352ff416a9f Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 12 Nov 2026 19:36:17 +0000 Subject: [PATCH] Smaller programs: functions in sections of their own, the linker drops what is not called A program that used one default-library function linked all of lib.ts, and through its references the RegExp, Date and HTTP wrappers. tslang emitted each module as one section, so no linker could take part of it. - obj.cpp: function and data sections on by default (--function-sections / --data-sections on the command line still win), as clang's -ffunction-sections -fdata-sections. - exe.cpp, Windows: -opt:noicf. link.exe drops unreferenced COMDATs by default, but would also fold functions with identical code into one address; two TypeScript functions must stay apart. - exe.cpp, ELF: -Wl,--gc-sections. What a shared library exports, what the program exports for the libraries it loads, and what libc references (mimalloc's malloc family) stay: measured, the dynamic export table is identical with and without it. - exe.cpp, wasm: a program is no longer linked with --export-all, which rooted every default-library function. It exports what the JS host starts it with, plus its 'export'ed functions, which ExportFixPass now names (wasm-export-name). Libraries keep --export-all. Measured (--opt, a program using Map, sort, toString, toUpperCase): Windows -mm=own 831 KB -> 437 KB, -mm=gc 772 KB -> 409 KB (with DefaultLib's matching change) Linux -mm=own 621 KB -> 177 KB, -mm=gc 591 KB -> 235 KB wasm32 -mm=own 196 KB -> 18.6 KB Co-Authored-By: Claude Opus 5.5 --- .../include/TypeScript/Pass/ExportFixPass.h | 4 ++- .../TypeScriptExportPass/ExportFixPass.cpp | 18 +++++++++-- tslang/tslang/exe.cpp | 32 +++++++++++++++++-- tslang/tslang/obj.cpp | 21 ++++++++++++ tslang/tslang/transform.cpp | 2 +- 5 files changed, 71 insertions(+), 6 deletions(-) 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) {