Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tslang/include/TypeScript/Pass/ExportFixPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 16 additions & 2 deletions tslang/lib/TypeScriptExportPass/ExportFixPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +55,7 @@ struct ExportFixPassCode
// set DLLExport
if (isWindowsMSVCEnvironment)
F.setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
exportFromWasm(F);
MadeChange = true;
}

Expand All @@ -63,6 +76,7 @@ struct ExportFixPassCode
// set DLLExport
if (isWindowsMSVCEnvironment)
F.setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
exportFromWasm(F);
MadeChange = true;
}

Expand Down Expand Up @@ -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())
Expand Down
32 changes: 30 additions & 2 deletions tslang/tslang/exe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tslang/tslang/obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,27 @@ int setupTargetTriple(llvm::Module *llvmModule, std::unique_ptr<llvm::TargetMach
}

InitializeOptions(TheTriple);

// Every function and global in a section of its own (a COMDAT on Windows), as clang does with
// -ffunction-sections -fdata-sections: the linker can then drop what a program never uses. Off,
// a module is one section, and a program that calls one default-library function linked all of
// lib.ts and, through its references, the RegExp, Date and HTTP wrappers too. --function-sections
// / --data-sections given on the command line still win.
auto &registeredOptions = llvm::cl::getRegisteredOptions();
auto notGiven = [&](llvm::StringRef name) {
auto it = registeredOptions.find(name);
return it == registeredOptions.end() || it->second->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())
Expand Down
2 changes: 1 addition & 1 deletion tslang/tslang/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ std::function<llvm::Error(llvm::Module *)> 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)
{
Expand Down
Loading