From a536f5151e4fe4556756ba86762722b405dd01a1 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 15 Nov 2026 15:13:02 +0000 Subject: [PATCH 1/2] Compile time: the module's comdat table is its first operation MLIR's verifier and its translation to LLVM IR look the comdat table up by name for every global and function that has a comdat (a vtable, an RTTI record, a generic's instance), each time scanning the module from its start. The table was created wherever the first such global happened to be lowered. It is now created at the start of the module and moved to the front once the conversion to LLVM is done (the globals lowered since are put in front of it). With the table last in a module of 4,000 classes the translation to LLVM IR takes 2.9 s, with it first 0.5 s. The LLVM IR does not change. Co-Authored-By: Claude Fable 5.2 --- tslang/lib/TypeScript/LowerToLLVM.cpp | 37 +++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 00b847165..7053d6689 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -2495,8 +2495,12 @@ struct FuncOpLowering : public TsLlvmPattern const char *comdatName = "__llvm_comdat"; mlir::LLVM::ComdatOp comdatOp = module.lookupSymbol(comdatName); - if (!comdatOp) + if (!comdatOp) { + // at the start of the module, where a lookup by name finds it at once: see + // moveComdatsFirst + mlir::OpBuilder::InsertionGuard guard(rewriter); + rewriter.setInsertionPointToStart(module.getBody()); comdatOp = rewriter.create(module.getLoc(), comdatName); } @@ -5986,8 +5990,12 @@ struct GlobalOpLowering : public TsLlvmPattern const char *comdatName = "__llvm_comdat"; mlir::LLVM::ComdatOp comdatOp = module.lookupSymbol(comdatName); - if (!comdatOp) + if (!comdatOp) { + // at the start of the module, where a lookup by name finds it at once: see + // moveComdatsFirst + mlir::OpBuilder::InsertionGuard guard(rewriter); + rewriter.setInsertionPointToStart(module.getBody()); comdatOp = rewriter.create(module.getLoc(), comdatName); } @@ -9660,6 +9668,29 @@ static void giveEntryPointAnExitCode(mlir::ModuleOp m, CompileOptions &compileOp } } +// The module's comdat table becomes its first operation. MLIR's verifier and its translation to +// LLVM IR look the table up by name for every global and function that has a comdat (a vtable, an +// RTTI record, a generic's instance), each time scanning the module from its start: with the table +// last in a module of 4,000 classes the translation took 2.9 s, with it first 0.5 s. The globals +// lowered since it was created were put in front of it, so it is moved once the conversion is +// done. Where it stands changes nothing in the LLVM IR. +static void moveComdatsFirst(mlir::ModuleOp m) +{ + auto *body = m.getBody(); + for (auto &moduleOp : *body) + { + if (isa(&moduleOp)) + { + if (&moduleOp != &body->front()) + { + moduleOp.moveBefore(&body->front()); + } + + return; + } + } +} + void TypeScriptToLLVMLoweringPass::runOnOperation() { auto m = getOperation(); @@ -9961,6 +9992,8 @@ void TypeScriptToLLVMLoweringPass::runOnOperation() signalPassFailure(); } + moveComdatsFirst(m); + // For each, a global named for the interface and the vtable that holds the vtable's address: // how SpeculativeDirectCallPass finds every vtable the interface has (FINAL_INTERFACE_IR_PREFIX). // Nothing else reads it. From dd01547ca8cae993d074948f2dc06cfcf42902ff Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 15 Nov 2026 15:13:02 +0000 Subject: [PATCH 2/2] --mlir-timing reports where a compilation spends its time The option was registered but printed nothing: no timing manager was given to the pass manager. It now reports MLIR generation, each MLIR pass, the translation to LLVM IR, LLVM's optimizations and code emission, for every --emit. The JIT ends the process without running destructors, so it prints the report before the program runs. docs/performance.md gets a "Compile time" section with the option and current figures. Co-Authored-By: Claude Fable 5.2 --- docs/performance.md | 29 ++++++++++++++++++++++++++++- tslang/tslang/dump.cpp | 8 ++++++++ tslang/tslang/jit.cpp | 13 ++++++++++++- tslang/tslang/jitcache.cpp | 10 +++++++++- tslang/tslang/obj.cpp | 15 +++++++++++++-- tslang/tslang/transform.cpp | 4 ++++ tslang/tslang/tslang.cpp | 34 ++++++++++++++++++++++++++++++++-- 7 files changed, 106 insertions(+), 7 deletions(-) diff --git a/docs/performance.md b/docs/performance.md index 0f4fb5194..11a6b3abf 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -1,7 +1,8 @@ # Performance Benchmarks of tslang under each memory model against Rust and C++ ports of the same code. -See [memory-models.md](memory-models.md) for what the models are. +See [memory-models.md](memory-models.md) for what the models are. How long the compiler itself +takes is under [Compile time](#compile-time) at the end. **Compared with Rust:** under `-mm=own`, tslang matches Rust on numeric array loops and runs within about 1.35x of idiomatic Rust on an object-heavy ray tracer, about 6.6x faster than the @@ -103,3 +104,29 @@ machine) rather than the baseline x86-64 an executable targets, which made saxpy and hash 15-25% faster than the JIT of the release before it. The same ray tracer on that Linux machine takes 128 ms under `-mm=gc`, 123 under `-mm=none`, 130 under `-mm=own` and 238 under `-mm=rc`, against 103 ms for the C++ port; the benchmarks' READMEs have the full tables. + +## Compile time + +`--mlir-timing` prints where a compilation spent its time when the compiler exits: MLIR +generation, each MLIR pass, the translation to LLVM IR, LLVM's optimizations and code emission. +It works with every `--emit`; under `--emit=jit` the report is printed before the program runs +and does not include the machine code the JIT emits as the program first calls it. + +``` +tslang --emit=obj --mlir-timing program.ts -o program.obj +``` + +Measured on Windows 11 x64 with the release compiler, `--no-default-lib`, on 2026-11-15: + +| input | build | time | +| --- | --- | --- | +| hello world | object | 23 ms | +| hello world | executable | 92 ms | +| the ray tracer benchmark (328 lines) | executable, `--opt_level=3` | 244 ms | +| 2,000 functions of 5 lines | object | 1.7 s | +| 2,000 classes of 5 lines | object | 2.6 s | +| 2,000 classes of 5 lines | object, `--opt_level=3` | 1.7 s | + +Functions compile at about 5,500 lines a second, and twice the source takes twice the time. +Classes take a little more than that: from 2,000 to 4,000 classes MLIR generation takes 2.65 +times as long, every other stage about 2.2 times. diff --git a/tslang/tslang/dump.cpp b/tslang/tslang/dump.cpp index 4a0d68a0b..241ebd6c7 100644 --- a/tslang/tslang/dump.cpp +++ b/tslang/tslang/dump.cpp @@ -3,6 +3,7 @@ #include "mlir/IR/BuiltinOps.h" #include "mlir/ExecutionEngine/ExecutionEngine.h" #include "mlir/Target/LLVMIR/Export.h" +#include "mlir/Support/Timing.h" #include "llvm/IR/LLVMContext.h" @@ -49,13 +50,17 @@ int dumpAST() return 0; } +mlir::TimingScope &compileTiming(); + int dumpLLVMIR(enum Action emitAction, std::string outputFile, mlir::ModuleOp module, CompileOptions &compileOptions) { registerMLIRDialects(module); // Convert the module to LLVM IR in a new LLVM IR context. llvm::LLVMContext llvmContext; + auto translationTiming = compileTiming().nest("Translation to LLVM IR"); auto llvmModule = mlir::translateModuleToLLVMIR(module, llvmContext); + translationTiming.stop(); if (!llvmModule) { llvm::WithColor::error(llvm::errs(), "tslang") << "Failed to emit LLVM IR\n"; @@ -77,6 +82,7 @@ int dumpLLVMIR(enum Action emitAction, std::string outputFile, mlir::ModuleOp mo llvm::InitializeNativeTarget(); llvm::InitializeNativeTargetAsmPrinter(); + auto optimizationTiming = compileTiming().nest("LLVM optimization"); auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions, /*targetMachine=*/nullptr); if (auto err = optPipeline(llvmModule.get())) @@ -85,6 +91,8 @@ int dumpLLVMIR(enum Action emitAction, std::string outputFile, mlir::ModuleOp mo return -1; } + optimizationTiming.stop(); + #ifndef SAVE_VIA_PASS if (emitAction == Action::DumpLLVMIR) diff --git a/tslang/tslang/jit.cpp b/tslang/tslang/jit.cpp index bc3e40315..b43b8378c 100644 --- a/tslang/tslang/jit.cpp +++ b/tslang/tslang/jit.cpp @@ -3,6 +3,7 @@ #include "mlir/ExecutionEngine/ExecutionEngine.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/Target/LLVMIR/Export.h" +#include "mlir/Support/Timing.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ExecutionEngine/JITEventListener.h" @@ -92,6 +93,8 @@ std::string makeAbsolutePath(std::string); std::string getExecutablePath(const char *); int registerMLIRDialects(mlir::ModuleOp); +mlir::TimingScope &compileTiming(); +void reportCompileTiming(); namespace llvm { class TargetMachine; } std::function getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *); @@ -1426,6 +1429,9 @@ static int runProgram(ProgramSymbolLookup lookup, llvm::ArrayRef in // Runs what was added to the JIT: the platform initializers, then the program (runProgram). static int runJitProgram(llvm::orc::LLJIT &jit, llvm::ArrayRef initFunctions, bool hasGCtorsMethod) { + // --mlir-timing: what compiling took, before the program's own time begins + reportCompileTiming(); + // run platform initializers (llvm.global_ctors etc.) if (auto err = jit.initialize(jit.getMainJITDylib())) { @@ -1611,7 +1617,9 @@ int runJit(int argc, char **argv, mlir::ModuleOp module, CompileOptions &compile } auto llvmContext = std::make_unique(); + auto translationTiming = compileTiming().nest("Translation to LLVM IR"); auto llvmModule = mlir::translateModuleToLLVMIR(module, *llvmContext); + translationTiming.stop(); if (!llvmModule) { llvm::WithColor::error(llvm::errs(), "tslang") << "failed to emit LLVM IR\n"; @@ -1641,8 +1649,11 @@ int runJit(int argc, char **argv, mlir::ModuleOp module, CompileOptions &compile // the host's machine, which is what the JIT compiles with: the optimizer's cost model has to // be for the CPU the code is generated for + auto optimizationTiming = compileTiming().nest("LLVM optimization"); auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions, tmOrError->get()); - if (auto err = optPipeline(llvmModule.get())) + auto optimizationError = optPipeline(llvmModule.get()); + optimizationTiming.stop(); + if (auto err = std::move(optimizationError)) { llvm::WithColor::error(llvm::errs(), "tslang") << "failed to optimize LLVM IR, error: " << err << "\n"; llvm::consumeError(std::move(err)); diff --git a/tslang/tslang/jitcache.cpp b/tslang/tslang/jitcache.cpp index d8c95c8b9..57d888a70 100644 --- a/tslang/tslang/jitcache.cpp +++ b/tslang/tslang/jitcache.cpp @@ -24,6 +24,7 @@ #include "mlir/IR/MLIRContext.h" #include "mlir/IR/OwningOpRef.h" #include "mlir/Target/LLVMIR/Export.h" +#include "mlir/Support/Timing.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" @@ -67,6 +68,7 @@ extern cl::opt jitCacheDir; std::unique_ptr createMLIRContext(); int compileTypeScriptFileIntoMLIR(mlir::MLIRContext &, llvm::StringRef, llvm::SourceMgr &, mlir::OwningOpRef &, CompileOptions &); int runMLIRPasses(mlir::MLIRContext &, llvm::SourceMgr &, mlir::OwningOpRef &, CompileOptions &); +mlir::TimingScope &compileTiming(); int registerMLIRDialects(mlir::ModuleOp); std::function getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *); llvm::Error addEntryThunk(llvm::Module &, llvm::StringRef); @@ -509,6 +511,7 @@ class JitUnitBuilder module = nullptr; context = createMLIRContext(); sourceMgr = std::make_unique(); + auto generationTiming = compileTiming().nest("MLIR generation"); return compileTypeScriptFileIntoMLIR(*context, path, *sourceMgr, module, options); }; @@ -590,7 +593,9 @@ class JitUnitBuilder registerMLIRDialects(*module); llvm::LLVMContext llvmContext; + auto translationTiming = compileTiming().nest("Translation to LLVM IR"); auto llvmModule = mlir::translateModuleToLLVMIR(*module, llvmContext); + translationTiming.stop(); if (!llvmModule) { llvm::WithColor::error(llvm::errs(), "tslang") << "failed to emit LLVM IR\n"; @@ -600,8 +605,11 @@ class JitUnitBuilder llvmModule->setDataLayout(tm->createDataLayout()); llvmModule->setTargetTriple(tm->getTargetTriple()); + auto optimizationTiming = compileTiming().nest("LLVM optimization"); auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, options, tm.get()); - if (auto err = optPipeline(llvmModule.get())) + auto optimizationError = optPipeline(llvmModule.get()); + optimizationTiming.stop(); + if (auto err = std::move(optimizationError)) { llvm::WithColor::error(llvm::errs(), "tslang") << "failed to optimize LLVM IR, error: " << err << "\n"; llvm::consumeError(std::move(err)); diff --git a/tslang/tslang/obj.cpp b/tslang/tslang/obj.cpp index ae11dad8a..5fb29a255 100644 --- a/tslang/tslang/obj.cpp +++ b/tslang/tslang/obj.cpp @@ -1,6 +1,7 @@ #include "mlir/ExecutionEngine/ExecutionEngine.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/Target/LLVMIR/Export.h" +#include "mlir/Support/Timing.h" // for dump obj #include "llvm/IR/LegacyPassManager.h" @@ -422,13 +423,17 @@ static void replaceWasmBulkMemory(llvm::Module &module) } } +mlir::TimingScope &compileTiming(); + int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string outputFile, mlir::ModuleOp module, CompileOptions &compileOptions) { registerMLIRDialects(module); // Convert the module to LLVM IR in a new LLVM IR context. llvm::LLVMContext llvmContext; + auto translationTiming = compileTiming().nest("Translation to LLVM IR"); auto llvmModule = mlir::translateModuleToLLVMIR(module, llvmContext); + translationTiming.stop(); if (!llvmModule) { llvm::WithColor::error(llvm::errs(), "tslang") << "Failed to emit LLVM IR\n"; @@ -464,6 +469,7 @@ int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string } } + auto optimizationTiming = compileTiming().nest("LLVM optimization"); auto optPipeline = getTransformer(enableOpt, optLevel, sizeLevel, compileOptions, Target.get()); if (auto err = optPipeline(llvmModule.get())) { @@ -471,6 +477,8 @@ int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string return -1; } + optimizationTiming.stop(); + // a wasm program that starts a thread (Thread.spawn, new Worker) is linked with a shared memory if (compileOptions.wasmExceptions && llvmModule->getFunction("tslang_thread_spawn")) { @@ -561,9 +569,12 @@ int dumpObjOrAssembly(int argc, char **argv, enum Action emitAction, std::string // Before executing passes, print the final values of the LLVM options. //llvm::cl::PrintOptionValues(); - PM.run(*llvmModule.get()); + { + auto emissionTiming = compileTiming().nest("Code emission"); + PM.run(*llvmModule.get()); + } - auto HasError = ((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError; + auto HasError =((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError; if (*HasError) { llvm::WithColor::error(llvm::errs(), "tslang") << "diagnostic error(s)\n"; diff --git a/tslang/tslang/transform.cpp b/tslang/tslang/transform.cpp index 324f7c91e..6a9f78a49 100644 --- a/tslang/tslang/transform.cpp +++ b/tslang/tslang/transform.cpp @@ -26,6 +26,7 @@ #include "mlir/IR/MLIRContext.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/Pass/PassManager.h" +#include "mlir/Support/Timing.h" #include "mlir/InitAllDialects.h" #include "mlir/InitAllPasses.h" #include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h" @@ -98,6 +99,8 @@ extern cl::opt stackPromotionMaxObject; extern cl::opt stackPromotionMaxFrame; extern cl::opt stackPromotionReport; +mlir::TimingScope &compileTiming(); + int runMLIRPasses(mlir::MLIRContext &context, llvm::SourceMgr &sourceMgr, mlir::OwningOpRef &module, CompileOptions &compileOptions) { mlir::SmallVector> postponedMessages; @@ -107,6 +110,7 @@ int runMLIRPasses(mlir::MLIRContext &context, llvm::SourceMgr &sourceMgr, mlir:: mlir::PassManager pm(&context); // Apply any generic pass manager command line options and run the pipeline. applyPassManagerCLOptions(pm); + pm.enableTiming(compileTiming()); // Check to see what granularity of MLIR we are compiling to. bool isLoweringToAffine = emitAction >= Action::DumpMLIRAffine; diff --git a/tslang/tslang/tslang.cpp b/tslang/tslang/tslang.cpp index 94b32af28..0d9c3823e 100644 --- a/tslang/tslang/tslang.cpp +++ b/tslang/tslang/tslang.cpp @@ -14,6 +14,7 @@ #include "mlir/IR/AsmState.h" #include "mlir/Pass/PassManager.h" #include "mlir/Debug/Counter.h" +#include "mlir/Support/Timing.h" #include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Math/IR/Math.h" @@ -82,6 +83,32 @@ int buildExe(int, char **, std::string, std::string, CompileOptions&); int runJit(int, char **, mlir::ModuleOp, CompileOptions&); int runJitCached(int, char **, CompileOptions&); +// --mlir-timing: where a compilation spends its time - MLIR generation, each MLIR pass, the +// translation to LLVM IR, LLVM's optimizations and code emission - printed when the compiler +// exits. Without the option the scopes are empty and cost nothing. First called after the command +// line is parsed. +static mlir::DefaultTimingManager &compileTimingManager() +{ + static mlir::DefaultTimingManager manager; + return manager; +} + +mlir::TimingScope &compileTiming() +{ + static mlir::TimingScope root = + (mlir::applyDefaultTimingManagerCLOptions(compileTimingManager()), compileTimingManager().getRootScope()); + return root; +} + +// Prints the report now. The JIT calls it before it runs the program: the program's time is not +// the compiler's, and the JIT ends the process without running destructors. +void reportCompileTiming() +{ + compileTiming().stop(); + compileTimingManager().print(); + compileTimingManager().setEnabled(false); +} + extern cl::OptionCategory ObjOrAssemblyCategory; extern cl::opt TargetTriple; cl::OptionCategory TypeScriptCompilerCategory("Compiler Options"); @@ -605,9 +632,12 @@ int main(int argc, char **argv) llvm::SourceMgr sourceMgr; mlir::OwningOpRef module; - if (auto error = compileTypeScriptFileIntoMLIR(mlirContext, sourceMgr, module, compileOptions)) { - return error; + auto generationTiming = compileTiming().nest("MLIR generation"); + if (auto error = compileTypeScriptFileIntoMLIR(mlirContext, sourceMgr, module, compileOptions)) + { + return error; + } } // Read before the passes lower it away: a program that loads a tslang shared library has to