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
29 changes: 28 additions & 1 deletion docs/performance.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
37 changes: 35 additions & 2 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2495,8 +2495,12 @@ struct FuncOpLowering : public TsLlvmPattern<mlir_ts::FuncOp>

const char *comdatName = "__llvm_comdat";
mlir::LLVM::ComdatOp comdatOp = module.lookupSymbol<mlir::LLVM::ComdatOp>(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<mlir::LLVM::ComdatOp>(module.getLoc(), comdatName);
}

Expand Down Expand Up @@ -5986,8 +5990,12 @@ struct GlobalOpLowering : public TsLlvmPattern<mlir_ts::GlobalOp>
const char *comdatName = "__llvm_comdat";
mlir::LLVM::ComdatOp comdatOp =
module.lookupSymbol<mlir::LLVM::ComdatOp>(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<mlir::LLVM::ComdatOp>(module.getLoc(), comdatName);
}

Expand Down Expand Up @@ -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<mlir::LLVM::ComdatOp>(&moduleOp))
{
if (&moduleOp != &body->front())
{
moduleOp.moveBefore(&body->front());
}

return;
}
}
}

void TypeScriptToLLVMLoweringPass::runOnOperation()
{
auto m = getOperation();
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tslang/tslang/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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";
Expand All @@ -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()))
Expand All @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion tslang/tslang/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *);

Expand Down Expand Up @@ -1426,6 +1429,9 @@ static int runProgram(ProgramSymbolLookup lookup, llvm::ArrayRef<std::string> in
// Runs what was added to the JIT: the platform initializers, then the program (runProgram).
static int runJitProgram(llvm::orc::LLJIT &jit, llvm::ArrayRef<std::string> 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()))
{
Expand Down Expand Up @@ -1611,7 +1617,9 @@ int runJit(int argc, char **argv, mlir::ModuleOp module, CompileOptions &compile
}

auto llvmContext = std::make_unique<llvm::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";
Expand Down Expand Up @@ -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));
Expand Down
10 changes: 9 additions & 1 deletion tslang/tslang/jitcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -67,6 +68,7 @@ extern cl::opt<std::string> jitCacheDir;
std::unique_ptr<mlir::MLIRContext> createMLIRContext();
int compileTypeScriptFileIntoMLIR(mlir::MLIRContext &, llvm::StringRef, llvm::SourceMgr &, mlir::OwningOpRef<mlir::ModuleOp> &, CompileOptions &);
int runMLIRPasses(mlir::MLIRContext &, llvm::SourceMgr &, mlir::OwningOpRef<mlir::ModuleOp> &, CompileOptions &);
mlir::TimingScope &compileTiming();
int registerMLIRDialects(mlir::ModuleOp);
std::function<llvm::Error(llvm::Module *)> getTransformer(bool, int, int, CompileOptions &, llvm::TargetMachine *);
llvm::Error addEntryThunk(llvm::Module &, llvm::StringRef);
Expand Down Expand Up @@ -509,6 +511,7 @@ class JitUnitBuilder
module = nullptr;
context = createMLIRContext();
sourceMgr = std::make_unique<llvm::SourceMgr>();
auto generationTiming = compileTiming().nest("MLIR generation");
return compileTypeScriptFileIntoMLIR(*context, path, *sourceMgr, module, options);
};

Expand Down Expand Up @@ -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";
Expand All @@ -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));
Expand Down
15 changes: 13 additions & 2 deletions tslang/tslang/obj.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -464,13 +469,16 @@ 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()))
{
llvm::WithColor::error(llvm::errs(), "tslang") << "Failed to optimize LLVM IR " << err << "\n";
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"))
{
Expand Down Expand Up @@ -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";
Expand Down
4 changes: 4 additions & 0 deletions tslang/tslang/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -98,6 +99,8 @@ extern cl::opt<unsigned> stackPromotionMaxObject;
extern cl::opt<unsigned> stackPromotionMaxFrame;
extern cl::opt<bool> stackPromotionReport;

mlir::TimingScope &compileTiming();

int runMLIRPasses(mlir::MLIRContext &context, llvm::SourceMgr &sourceMgr, mlir::OwningOpRef<mlir::ModuleOp> &module, CompileOptions &compileOptions)
{
mlir::SmallVector<std::unique_ptr<mlir::Diagnostic>> postponedMessages;
Expand All @@ -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;
Expand Down
34 changes: 32 additions & 2 deletions tslang/tslang/tslang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<std::string> TargetTriple;
cl::OptionCategory TypeScriptCompilerCategory("Compiler Options");
Expand Down Expand Up @@ -605,9 +632,12 @@ int main(int argc, char **argv)

llvm::SourceMgr sourceMgr;
mlir::OwningOpRef<mlir::ModuleOp> 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
Expand Down
Loading