diff --git a/docs/compile-time-work.md b/docs/compile-time-work.md new file mode 100644 index 000000000..4317dd378 --- /dev/null +++ b/docs/compile-time-work.md @@ -0,0 +1,155 @@ +# Compile-time work: state and how to carry on + +Working notes for the compile-time optimisation of tslang itself, started 2026-11-15. Read this to +pick the work up in a new session. User-facing numbers are in [performance.md](performance.md#compile-time). + +## 1. Where it stands + +The compiler took a time that grew much faster than the source for several kinds of program. Each +case had the same root: something done once per global, call or class that scanned the whole +module. The fixes replace the scan with a table, and every table is checked against the scan it +replaces (section 4). + +Merged: + +| PR | What | +| --- | --- | +| #862 | The last global is found by scanning the module's top level, not every function body. `LLVMGlobalsIndex` for the LLVM lowering pass: names of LLVM globals and the last global of each kind. | +| #863 | The comdat table is first in the module. `--mlir-timing` works. | +| #864 | MLIR generation remembers each module's last global (`MLIRGenImpl::lastGlobalOfModule`). | + +Open: PR #865, branch `perf-compile-time-4`. + +| Commit | What | +| --- | --- | +| `8f303db6` | `StackEscapeFacts` looks functions up in a table. | +| `5254b577` | `LLVMGlobalsIndex::lookupSymbol`: the callee of a call is found from a table of the symbols the module has before the conversion. | +| `fea78525` | An enum's member dictionary is made once, not after every member. | + +Also on `perf-compile-time-4`, after those three (see `git log`; all went through section 5): + +- **Bounded comdat tables** (`addComdatSelector`, `COMDAT_TABLE_SELECTORS` in `LowerToLLVM.cpp`; + `comdatTable` in `LLVMGlobalsIndex.h`). Translation to LLVM IR for 2,000 generic instances: + 1.8 s -> 0.5-0.7 s. +- **Symbols the lowering makes are tabled** (`createSymbol`, `lookupOwnSymbolAs`, `createdSymbols` + in `LLVMGlobalsIndex.h`; 14 lookups and 17 creations routed in `OwnershipRoutineLogic.h`, + `ConvertLogic.h`, `LLVMCodeHelperBase.h`, `LLVMCodeHelper.h`, `LowerToLLVM.cpp`). A first version + treated every name missing from the tables as absent from the module; the check caught `puts`, + which MLIR's own `cf.assert` lowering declares. The rule is now: a miss is final only for names + the lowering itself invents (`ownName`), otherwise the scan decides. LLVM lowering of 2,000 + interfaces: 2.0 s -> 1.4 s. +- `tslang/benchmarks/compile-time/` (the scripts of section 2) and this document. + +Figures, object file, no optimisation, `--no-default-lib`, Windows x64, Release compiler: + +| Input | At the start | Now | +| --- | --- | --- | +| 2,000 classes of 5 lines | 99.9 s | 2.5 s | +| 8,000 classes | not measured (4,000: 19.7 s after #862) | 11.9 s | +| 2,000 instances of a generic class | 16.4 s | 6.0-7.0 s | +| 2,000 interfaces with a class and a use each | 10.9 s | 7.3-8.7 s | +| 2,000 classes overriding a base method | 3.3 s | 2.4 s | +| an enum of 2,000 members and a switch | 1.8 s | 0.3 s | +| 2,000 functions of 5 lines | 1.8 s | 1.7 s | + +## 2. How to measure + +**`--mlir-timing`** prints the time of each stage (MLIR generation, every MLIR pass, translation +to LLVM IR, LLVM optimisation, code emission) for any `--emit`. This is the number to trust. + +**`tslang/benchmarks/compile-time/shapes.ps1`** generates 14 program shapes at growing sizes and +prints the time and the growth from one size to the next. Linear is about 2.0x for twice the +source; 3x and more means something quadratic. + +``` +pwsh tslang/benchmarks/compile-time/shapes.ps1 # all shapes, n = 500, 1000, 2000 +pwsh tslang/benchmarks/compile-time/shapes.ps1 -Only interfaces -Stages # which stage grows +``` + +**`tslang/benchmarks/compile-time/profile.ps1 `** samples call stacks of the Release +compiler (relinked with symbols into `out/`, the build directory untouched) and prints the hot +functions. Use it to find the code inside a stage, not to compare stages: it takes fewer samples +where stacks are deep, and it once showed a lookup at 31% that was 5% of the wall time. A +per-function pass runs on a worker thread; pass `-Flags --mlir-disable-threading` to see it. + +The suite's largest test is 1,500 lines and shows none of this. Only generated inputs do. + +## 3. What is left, with the cause of each + +Measured with all of the above, n = 1,000 -> 2,000. Timings of the larger inputs moved by 10-15% +between runs on the development machine, and at n = 2,000 even code emission (LLVM's, linear) +came out at 2.8x on these two shapes, so read 2.5-2.9x here as "about linear, plus memory": + +| Shape | Total | Growth | Where | +| --- | --- | --- | --- | +| interfaces | 2.8 -> 8.7 s | 3.1x | MLIR generation 0.9 -> 3.2 s (3.6x) is the one clear case; LLVM lowering 0.46 -> 1.36 s (2.9x); `OwnedReturnConsumptionPass` and `StackPromotionPass` 0.04 -> 0.2 s (4-5x, small) | +| one function of n `if` statements | 0.65 -> 1.7 s | 2.7x | `TypeScriptToAffineLoweringTSFuncPass` 0.23 -> 0.89 s (3.9x) | +| generics | 2.8 -> 7.0 s | 2.5x | nothing stands out; every stage 2.1-2.8x | +| inheritance, classes | | 2.3x | nothing stands out | + +**Interfaces, MLIR generation.** `mlirGenNullInterfaceVTable` (`MLIRGenInterfaces.cpp`) makes one +throwing stub per interface method through `mlirGenThrowingMethodStub`, which asks +`theModule.lookupSymbol(stubName)` before and after: the first is a miss and scans the +module. `INSTANCEOF_NONE_NAME` is looked up for every vtable and the function is pushed at the +*end* of the module, so each lookup scans all of it. `createGlobalVariable` still scans after a +dummy run made a global (the entry of `lastGlobalOfModule` is dropped then). A fix needs a symbol +table for MLIR generation that survives its erases: `funcOp.erase()` in about eight places, +`GenContext::clean` for dummy runs, and the temp and discovery modules. Count the creation and +erase sites of `mlir_ts::FuncOp` first; if they are few, the pattern of `lastGlobalOfModule` (drop +the entry wherever it can go stale, check against the scan) applies. + +**One very large function.** The `IfOp` lowering in `LowerToAffineLoops.cpp` calls +`rewriter.splitBlock`. Under a conversion rewriter that moves the rest of the block one operation +at a time, each with a recorded rewrite, so n sequential `if`s move n^2/2 operations. Options, none +tried: lower the `if`s of a block last to first, so each split moves little; or build the +continuation block once per region instead of splitting at every `if`. Upstream's SCF-to-CF has +the same shape. + +**Not looked at:** `OwnedReturnConsumptionPass` and `BorrowFactsExportPass` showed 4-5x growth on +some shapes but small absolute times (0.2 s); the canonicalizer runs grow 2.3-2.6x; `-mm=rc` and +`-mm=own` were only timed on classes; no shape uses the default library; nothing was measured on +Linux. + +## 4. The rules that keep the tables honest + +Every table answers a question a scan used to answer, and must give the same answer. + +- **`TSLANG_VERIFY_LLVM_GLOBALS_INDEX=`** makes the compiler ask the scan too, append any + difference to the file and use the scan's answer. It covers `LLVMGlobalsIndex` (global lookup, + last global, symbol lookup) and `lastGlobalOfModule`. After a change to any of them: run the + whole suite with it set, and the file must not exist afterwards. It has caught two wrong + versions that the plain suite's IR comparison would also have shown, and one (`puts`) that it + would not have. +- **LLVM globals** made during the conversion go through `LLVMCodeHelperBase::createGlobal`; + other symbols through `LLVMGlobalsIndex::createSymbol`. A grep for `create`, + `create` and `create` under `LowerToLLVM` should find only the + wrappers and the code that runs after `applyFullConversion`. +- **Rollback.** The conversion driver erases what a failed pattern created. The index is the + conversion's listener (`notifyPatternEnd` arrives when a pattern fails, in MLIR 22.1.8) and + forgets its pointers when a pattern that created something fails. Pointers to symbols that + existed before the conversion are safe until it ends. +- **A miss is final only for the lowering's own names.** MLIR's patterns create symbols too. +- **In MLIR generation** the remembered global of a module is dropped where a global or a module + is erased, for dummy-run globals, and around `MLIRRTTIHelperVC` (`RttiGlobalsScope`), which + makes globals itself. +- A cheaper comparison inside a scan does not help: the cost is walking the operation list. + +## 5. Verifying a change + +1. Build Release: `cmake --build . --config Release --target tslang -- /m:6` in + `__build/tslang/windows-msbuild-2026-release`. +2. `shapes.ps1` for the shapes touched, before and after. +3. The suite with the check: clear `DEFAULT_LIB_PATH`, `GC_LIB_PATH`, `LLVM_LIB_PATH`, + `TSLANG_LIB_PATH`, `TSLANG_HOME`; set `TSLANG_VERIFY_LLVM_GLOBALS_INDEX`; `ctest -C Release -j 10 + --timeout 300`. Then the suite without it. +4. Compare LLVM IR with a compiler from before the change (`--emit=llvm`, default options, + `--opt --opt_level=3`, `--mm=rc`) on the largest tests. `00param_assigned_owned.ts` differs + from run to run with any compiler (the same lines in another order) and is not a finding. + +Known on the development machine: `test-borrow-facts-*` (5 tests) fail because the build cache's +`CMAKE_AR` names an MSVC toolset that is no longer installed. They pass when their commands are +rerun with the installed `lib.exe`; a CMake reconfigure fixes it. + +Not done for any of this work: a Linux build, and a run of the suite with a Debug-built compiler. +The test workflows do not run on pull requests, so nothing checks Linux until one is started by +hand. diff --git a/tslang/benchmarks/compile-time/.gitignore b/tslang/benchmarks/compile-time/.gitignore new file mode 100644 index 000000000..a62451d76 --- /dev/null +++ b/tslang/benchmarks/compile-time/.gitignore @@ -0,0 +1,5 @@ +out/ +*.exe +*.pdb +*.obj +*.ilk diff --git a/tslang/benchmarks/compile-time/profile.ps1 b/tslang/benchmarks/compile-time/profile.ps1 new file mode 100644 index 000000000..3ad136b7c --- /dev/null +++ b/tslang/benchmarks/compile-time/profile.ps1 @@ -0,0 +1,54 @@ +# Samples the call stacks of the Release compiler while it compiles one file, and prints the +# functions the time is in. Windows, no administrator rights needed. +# +# pwsh tslang/benchmarks/compile-time/profile.ps1 [-Flags --mm=rc] [-Interval 1] +# +# What it does: +# 1. relinks the Release tslang.exe with symbols into out/ (tslang_prof.exe + .pdb), from the link +# command MSBuild recorded for it: the build directory is not touched, and nothing is rebuilt; +# 2. builds sampler.cpp (cl, from the newest Visual Studio); +# 3. runs the compiler under the sampler and writes out/profile.txt: exclusive and inclusive +# percentages per function, and the commonest stack of the top leaves. +# +# The numbers are for locating code inside one stage, not for comparing stages: walking a deep +# stack takes longer, so the stages with deep stacks (MLIR generation, the passes) get fewer +# samples than their time. --mlir-timing is what says how long each stage takes. A per-function +# MLIR pass runs on a worker thread, which this does not sample: add -Flags --mlir-disable-threading. +param( + [Parameter(Mandatory)][string]$Source, + [string[]]$Flags = @(), + [int]$Interval = 1, + [string]$BuildDir = "$PSScriptRoot\..\..\..\__build\tslang\windows-msbuild-2026-release", + [switch]$Relink +) + +$ErrorActionPreference = "Stop" +$out = Join-Path $PSScriptRoot "out" +New-Item -ItemType Directory -Force $out | Out-Null +$vs = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -property installationPath +$vcvars = "$vs\VC\Auxiliary\Build\vcvars64.bat" + +$exe = Join-Path $out "tslang_prof.exe" +$built = Join-Path $BuildDir "bin\tslang.exe" +if ($Relink -or -not (Test-Path $exe) -or (Get-Item $exe).LastWriteTime -lt (Get-Item $built).LastWriteTime) { + $tlog = Join-Path $BuildDir "tslang\tslang.dir\Release\tslang.tlog\link.command.1.tlog" + $cmd = ((Get-Content $tlog) | Where-Object { $_ -notmatch '^\^' }) -join ' ' + $cmd = $cmd -replace '/OUT:"[^"]*"', "/OUT:`"$exe`"" + $cmd = $cmd -replace '/PDB:"[^"]*"', "/PDB:`"$out\tslang_prof.pdb`"" + $cmd = $cmd -replace '/IMPLIB:"[^"]*"', "/IMPLIB:`"$out\tslang_prof.lib`"" + $cmd = $cmd -replace '/ERRORREPORT:\S+', '' + $cmd += ' /DEBUG:FULL /OPT:REF /OPT:NOICF' + Set-Content "$out\link.rsp" $cmd -Encoding Unicode + Push-Location (Join-Path $BuildDir "tslang") + try { cmd /c "`"$vcvars`" >nul && link @`"$out\link.rsp`"" 2>&1 | Select-Object -Last 3 } finally { Pop-Location } +} + +$sampler = Join-Path $out "sampler.exe" +if (-not (Test-Path $sampler)) { + Push-Location $out + try { cmd /c "`"$vcvars`" >nul && cl /nologo /std:c++17 /O2 /EHsc `"$PSScriptRoot\sampler.cpp`" /Fe:sampler.exe dbghelp.lib" 2>&1 | Select-Object -Last 3 } finally { Pop-Location } +} + +& $sampler $Interval $exe --emit=obj --no-default-lib @Flags $Source -o "$out\profiled.obj" > "$out\profile.txt" 2>&1 +"written $out\profile.txt" +Get-Content "$out\profile.txt" -TotalCount 60 diff --git a/tslang/benchmarks/compile-time/sampler.cpp b/tslang/benchmarks/compile-time/sampler.cpp new file mode 100644 index 000000000..fad305f66 --- /dev/null +++ b/tslang/benchmarks/compile-time/sampler.cpp @@ -0,0 +1,126 @@ +// Stack sampler: runs a command, samples its main thread's call stack, prints exclusive and +// inclusive function counts. Usage: sampler.exe +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include +#include +#include +#include + +static std::string symName(HANDLE proc, DWORD64 addr) +{ + char buf[sizeof(SYMBOL_INFO) + 1024]; + auto *sym = (SYMBOL_INFO *)buf; + sym->SizeOfStruct = sizeof(SYMBOL_INFO); + sym->MaxNameLen = 1000; + DWORD64 disp = 0; + if (SymFromAddr(proc, addr, &disp, sym)) + { + std::string s(sym->Name); + if (s.size() > 170) s = s.substr(0, 170) + "..."; + return s; + } + char tmp[64]; + sprintf(tmp, "0x%llx", (unsigned long long)addr); + return tmp; +} + +int main(int argc, char **argv) +{ + if (argc < 3) return 2; + int interval = atoi(argv[1]); + std::string cmd; + for (int i = 2; i < argc; i++) + { + if (i > 2) cmd += " "; + cmd += std::string("\"") + argv[i] + "\""; + } + + STARTUPINFOA si{sizeof(si)}; + PROCESS_INFORMATION pi{}; + HANDLE nul = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); + SetHandleInformation(nul, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); + si.dwFlags = STARTF_USESTDHANDLES; + si.hStdOutput = nul; + si.hStdError = nul; + if (!CreateProcessA(nullptr, cmd.data(), nullptr, nullptr, TRUE, CREATE_SUSPENDED, nullptr, nullptr, &si, &pi)) + { + printf("CreateProcess failed %lu\n", GetLastError()); + return 1; + } + SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES); + ResumeThread(pi.hThread); + Sleep(150); + SymInitialize(pi.hProcess, nullptr, TRUE); + + std::vector> stacks; + while (WaitForSingleObject(pi.hProcess, interval) == WAIT_TIMEOUT) + { + if (SuspendThread(pi.hThread) == (DWORD)-1) break; + CONTEXT ctx{}; + ctx.ContextFlags = CONTEXT_FULL; + std::vector st; + if (GetThreadContext(pi.hThread, &ctx)) + { + STACKFRAME64 f{}; + f.AddrPC.Offset = ctx.Rip; f.AddrPC.Mode = AddrModeFlat; + f.AddrFrame.Offset = ctx.Rbp; f.AddrFrame.Mode = AddrModeFlat; + f.AddrStack.Offset = ctx.Rsp; f.AddrStack.Mode = AddrModeFlat; + for (int i = 0; i < 96; i++) + { + if (!StackWalk64(IMAGE_FILE_MACHINE_AMD64, pi.hProcess, pi.hThread, &f, &ctx, nullptr, + SymFunctionTableAccess64, SymGetModuleBase64, nullptr)) + break; + if (!f.AddrPC.Offset) break; + st.push_back(f.AddrPC.Offset); + } + } + ResumeThread(pi.hThread); + if (!st.empty()) stacks.push_back(std::move(st)); + } + + std::map names; + auto name = [&](DWORD64 a) -> const std::string & { + auto it = names.find(a); + if (it == names.end()) it = names.emplace(a, symName(pi.hProcess, a)).first; + return it->second; + }; + std::map excl, incl; + std::map> leafStacks; + for (auto &st : stacks) + { + excl[name(st[0])]++; + std::set seen; + for (auto a : st) seen.insert(name(a)); + for (auto &s : seen) incl[s]++; + std::string key; + for (size_t i = 1; i < st.size() && i < 14; i++) key += " <- " + name(st[i]) + "\n"; + leafStacks[name(st[0])][key]++; + } + auto top = [&](std::map &m, int n, const char *title) { + std::vector> v; + for (auto &kv : m) v.push_back({kv.second, kv.first}); + std::sort(v.rbegin(), v.rend()); + printf("\n== %s (%zu samples) ==\n", title, stacks.size()); + for (int i = 0; i < n && i < (int)v.size(); i++) + printf("%6.1f%% %s\n", 100.0 * v[i].first / stacks.size(), v[i].second.c_str()); + return v; + }; + auto ex = top(excl, 30, "exclusive"); + top(incl, 70, "inclusive"); + printf("\n== most common stack of the top 6 leaves ==\n"); + for (int i = 0; i < 6 && i < (int)ex.size(); i++) + { + auto &m = leafStacks[ex[i].second]; + auto best = std::max_element(m.begin(), m.end(), [](auto &a, auto &b) { return a.second < b.second; }); + printf("%s (%d of %d samples with this stack)\n%s\n", ex[i].second.c_str(), best->second, ex[i].first, best->first.c_str()); + } + DWORD code = 0; + GetExitCodeProcess(pi.hProcess, &code); + printf("exit code %lu\n", code); + SymCleanup(pi.hProcess); + return 0; +} diff --git a/tslang/benchmarks/compile-time/shapes.ps1 b/tslang/benchmarks/compile-time/shapes.ps1 new file mode 100644 index 000000000..729bc71a7 --- /dev/null +++ b/tslang/benchmarks/compile-time/shapes.ps1 @@ -0,0 +1,159 @@ +# Compile time against program size, for a set of program shapes. +# +# Generates each shape at each size (one .ts file of n units), compiles it to an object file and +# prints the wall time (best of two) and how it grows from one size to the next. A shape that grows +# much more than the size does (2x for twice the source) has something quadratic in it. +# +# pwsh tslang/benchmarks/compile-time/shapes.ps1 [-Tslang ] [-Sizes 500,1000,2000] +# [-Only generics,interfaces] [-Flags --opt,--opt_level=3] [-Stages] +# +# -Stages prints, instead, the per-stage times of the two largest sizes from --mlir-timing: which +# stage of the compiler the growth is in. +param( + [string]$Tslang = "$PSScriptRoot\..\..\..\__build\tslang\windows-msbuild-2026-release\bin\tslang.exe", + [int[]]$Sizes = @(500, 1000, 2000), + [string[]]$Only = @(), + [string[]]$Flags = @(), + [switch]$Stages +) + +$ErrorActionPreference = "Continue" +# `pwsh -File shapes.ps1 -Only a,b` hands the list over as one string +$Only = @($Only | ForEach-Object { $_ -split ',' } | Where-Object { $_ }) +$Flags = @($Flags | ForEach-Object { $_ -split ',' } | Where-Object { $_ }) +$out = Join-Path $PSScriptRoot "out" +New-Item -ItemType Directory -Force $out | Out-Null + +function Lines([scriptblock]$body) { + $sb = [Text.StringBuilder]::new() + & $body $sb + return $sb.ToString() +} + +$shapes = [ordered]@{ + 'functions' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { + [void]$sb.AppendLine("function f$i(a: number, b: number): number {") + [void]$sb.AppendLine(" let s = 0;") + [void]$sb.AppendLine(" for (let i = 0; i < a; i++) { if (i % 3 == 0) s += i * b; else s -= b; }") + [void]$sb.AppendLine(" return s + a;") + [void]$sb.AppendLine("}") + } + [void]$sb.AppendLine("function main() { print(f0(1, 2)); }") } } + 'classes' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { + [void]$sb.AppendLine("class C$i {") + [void]$sb.AppendLine(" x: number; y: number;") + [void]$sb.AppendLine(" constructor(x: number, y: number) { this.x = x; this.y = y; }") + [void]$sb.AppendLine(" len(): number { return this.x * this.x + this.y * this.y; }") + [void]$sb.AppendLine("}") + } + [void]$sb.AppendLine("function main() { print(1); }") } } + 'strings' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine("function s$i(): string { return `"text number $i of the program`"; }") } + [void]$sb.AppendLine("function main() { print(s0()); }") } } + 'interfaces' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { + [void]$sb.AppendLine("interface I$i { area(): number; }") + [void]$sb.AppendLine("class K$i implements I$i { w: number = $i; area(): number { return this.w * 2; } }") + [void]$sb.AppendLine("function use$i(): number { const v: I$i = new K$i(); return v.area(); }") + } + [void]$sb.AppendLine("function main() { print(use0()); }") } } + 'inheritance' = { param($n) Lines { param($sb) + [void]$sb.AppendLine("class B { v: number = 1; get(): number { return this.v; } }") + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine("class D$i extends B { get(): number { return this.v + $i; } }") } + [void]$sb.AppendLine("function main() { const b: B = new D0(); print(b.get()); }") } } + 'generics' = { param($n) Lines { param($sb) + [void]$sb.AppendLine("class Box { value: T; constructor(value: T) { this.value = value; } get(): T { return this.value; } }") + for ($i = 0; $i -lt $n; $i++) { + [void]$sb.AppendLine("class P$i { n: number = $i; }") + [void]$sb.AppendLine("function g$i(): number { const b = new Box(new P$i()); return b.get().n; }") + } + [void]$sb.AppendLine("function main() { print(g0()); }") } } + 'globals' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine("let v$i = $i * 2 + 1;") } + [void]$sb.AppendLine("function main() { print(v0 + v$($n - 1)); }") } } + 'big function' = { param($n) Lines { param($sb) + [void]$sb.AppendLine("function big(a: number): number {"); [void]$sb.AppendLine(" let s = a;") + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine(" if (s > $i) { s = s + $i; } else { s = s * 2 - $i; }") } + [void]$sb.AppendLine(" return s; }"); [void]$sb.AppendLine("function main() { print(big(3)); }") } } + 'closures' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine("function c$i(a: number): number { const f = (x: number) => x + a + $i; return f(1); }") } + [void]$sb.AppendLine("function main() { print(c0(1)); }") } } + 'object literals' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine("function o$i(): number { const p = { a$i`: $i, b: 2 }; return p.a$i + p.b; }") } + [void]$sb.AppendLine("function main() { print(o0()); }") } } + 'arrays' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine("function a$i(): number { const xs = [$i, 2, 3, 4]; let s = 0; for (const x of xs) s += x; return s; }") } + [void]$sb.AppendLine("function main() { print(a0()); }") } } + 'calls' = { param($n) Lines { param($sb) + [void]$sb.AppendLine("function f0(a: number): number { return a + 1; }") + for ($i = 1; $i -lt $n; $i++) { [void]$sb.AppendLine("function f$i(a: number): number { return f$($i - 1)(a) + f$([int]($i / 2))(a); }") } + [void]$sb.AppendLine("function main() { print(f5(1)); }") } } + 'try/catch' = { param($n) Lines { param($sb) + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine("function t$i(a: number): number { try { if (a > $i) throw $i; return a; } catch (e: number) { return e; } }") } + [void]$sb.AppendLine("function main() { print(t0(1)); }") } } + 'enums+switch' = { param($n) Lines { param($sb) + [void]$sb.Append("enum E {"); for ($i = 0; $i -lt $n; $i++) { [void]$sb.Append(" M$i,") }; [void]$sb.AppendLine(" }") + [void]$sb.AppendLine("function sw(e: E): number { switch (e) {") + for ($i = 0; $i -lt $n; $i++) { [void]$sb.AppendLine(" case E.M$i`: return $i;") } + [void]$sb.AppendLine(" } return -1; }"); [void]$sb.AppendLine("function main() { print(sw(E.M1)); }") } } +} + +function SourceOf([string]$name, [int]$n) { + $src = Join-Path $out "$($name -replace '[^a-z]', '_')_$n.ts" + Set-Content $src (& $shapes[$name] $n) + return $src +} + +function StagesOf([string]$src) { + $r = [ordered]@{} + $lines = & $Tslang --emit=obj --no-default-lib --mlir-timing @Flags $src -o "$out\x.obj" 2>&1 | ForEach-Object { "$_" } + foreach ($l in $lines) { + if ($l -match '^\s*[\d.]+ \(\s*[\d.]+%\)\s+([\d.]+) \(\s*[\d.]+%\)\s+(.+?)\s*$') { + $stage = $Matches[2] -replace "``anonymous-namespace'::", '' + $r[$stage] = [double]$Matches[1] + $(if ($r.Contains($stage)) { $r[$stage] } else { 0 }) + } + } + return $r +} + +"flags: --emit=obj --no-default-lib $($Flags -join ' ')" +if ($Stages) { + $small = $Sizes[-2]; $large = $Sizes[-1] + foreach ($name in $shapes.Keys) { + if ($Only.Count -and $Only -notcontains $name) { continue } + $a = StagesOf (SourceOf $name $small); $b = StagesOf (SourceOf $name $large) + "===== $name n=$small -> n=$large" + $b.Keys | Sort-Object { -$b[$_] } | Select-Object -First 10 | ForEach-Object { + $x = if ($a.Contains($_)) { $a[$_] } else { 0 } + "{0,8:N3} -> {1,8:N3} {2,5} {3}" -f $x, $b[$_], $(if ($x -gt 0.004) { "{0:N1}x" -f ($b[$_] / $x) } else { '' }), $_ + } + } + return +} + +$header = "{0,-16}" -f 'shape'; foreach ($n in $Sizes) { $header += "{0,10}" -f "n=$n" } +$header + " growth" +foreach ($name in $shapes.Keys) { + if ($Only.Count -and $Only -notcontains $name) { continue } + $row = "{0,-16}" -f $name; $times = @(); $failed = $false + foreach ($n in $Sizes) { + $src = SourceOf $name $n + $best = [double]::MaxValue + foreach ($i in 1..2) { + $sw = [Diagnostics.Stopwatch]::StartNew(); & $Tslang --emit=obj --no-default-lib @Flags $src -o "$out\x.obj" *> "$out\last.log"; $sw.Stop() + if ($LASTEXITCODE -ne 0) { $failed = $true; break } + $best = [Math]::Min($best, $sw.Elapsed.TotalSeconds) + if ($best -gt 60) { break } + } + if ($failed) { $row += "{0,10}" -f 'FAIL'; $row += " " + ((Get-Content "$out\last.log" -TotalCount 2) -join ' | '); break } + $times += $best; $row += "{0,10:N2}" -f $best + if ($best -gt 60) { $row += ' (stopped: over a minute)'; break } + } + if (-not $failed -and $times.Count -ge 2) { + $growth = @(); for ($k = 1; $k -lt $times.Count; $k++) { $growth += "{0:N1}x" -f ($times[$k] / $times[$k - 1]) } + $row += " " + ($growth -join ', ') + } + $row +} diff --git a/tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h b/tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h index bc9f4c928..f12067e9f 100644 --- a/tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h @@ -94,7 +94,7 @@ class ConvertLogic { constexpr auto name = "__ts_number_to_string"; auto parentModule = op->getParentOfType(); - if (auto existing = parentModule.lookupSymbol(name)) + if (auto existing = LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return existing; } @@ -114,7 +114,7 @@ class ConvertLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create(noLoc, name, th.getFunctionType(th.getVoidType(), {ptrTy, f64Ty}), + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, noLoc, name, th.getFunctionType(th.getVoidType(), {ptrTy, f64Ty}), LLVM::Linkage::Internal); auto *entry = funcOp.addEntryBlock(rewriter); diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h index 4029a013b..a6cbd33ae 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h @@ -242,8 +242,9 @@ class LLVMCodeHelper : public LLVMCodeHelperBase TypeHelper th(rewriter); // TODO: finish it - auto newFuncOp = rewriter.create( - location, name, mlir::FunctionType::get(rewriter.getContext(), llvm::ArrayRef(), llvm::ArrayRef())); + auto newFuncOp = LLVMGlobalsIndex::createSymbol( + compileOptions, rewriter, location, name, + mlir::FunctionType::get(rewriter.getContext(), llvm::ArrayRef(), llvm::ArrayRef())); // the per-symbol "__cctor" is only ever called from this module's own global ctors / __mlir_gctors. // keep it internal so it is not exported - otherwise an executable and a shared lib that both // define e.g. "A.Origin__cctor" interpose each other (ELF gives the executable's copy priority), diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h index 63f02c861..5c71da1a4 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h @@ -61,6 +61,18 @@ class LLVMCodeHelperBase return module.lookupSymbol(name); } + // module.lookupSymbol(name) + mlir::Operation *lookupSymbol(mlir::ModuleOp module, StringRef name) + { + auto *index = LLVMGlobalsIndex::of(compileOptions); + if (index && index->isFor(module)) + { + return index->lookupSymbol(name); + } + + return module.lookupSymbol(name); + } + // rewriter.create(...), told to the index template LLVM::GlobalOp createGlobal(Args &&...args) { @@ -238,14 +250,14 @@ class LLVMCodeHelperBase LLVM::LLVMFuncOp getOrInsertFunction(mlir::Location loc, ModuleOp parentModule, const StringRef &name, const LLVM::LLVMFunctionType &llvmFnType) { - if (auto funcOp = parentModule.lookupSymbol(name)) + if (auto funcOp = LLVMGlobalsIndex::lookupSymbolAs(compileOptions, parentModule, name)) { return funcOp; } PatternRewriter::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - return rewriter.create(loc, name, llvmFnType); + return LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, llvmFnType); } LLVM::LLVMFuncOp getOrInsertFunction(const StringRef &name, const LLVM::LLVMFunctionType &llvmFnType) diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMGlobalsIndex.h b/tslang/include/TypeScript/LowerToLLVM/LLVMGlobalsIndex.h index a1d5156af..300e1ad06 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMGlobalsIndex.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMGlobalsIndex.h @@ -95,6 +95,205 @@ class LLVMGlobalsIndex : public mlir::RewriterBase::Listener return found ? llvm::cast(found) : mlir::LLVM::GlobalOp(); } + // module.lookupSymbol(name): the first operation of the name at the top level of the module. + // + // The lookup scans the module, and lowering asks it for the callee of every call. Here the + // symbols the module has before the conversion are tabled (tableSymbols), and the answer is + // found from the tabled one: an operation that replaces a symbol during the conversion (a ts + // function by a func.func, that by an llvm.func) is created in front of the one it replaces, + // which stays where it was until the conversion ends, so the first of the name is reached by + // stepping back over the operations of that name. The conversion never erases those symbols + // before it ends. + // + // A symbol made during the conversion (a helper routine, a global, a comdat table) is in a + // second table, which createdSymbol fills; what a failed pattern created is erased, so that + // table is made again by a scan after one fails. + // + // A name in neither table is looked up by the scan, unless it is the lowering's own + // (`ownName`): a name only tslang's lowering gives a symbol - a release routine of a type, + // "__mlir_gctors". Such a name in neither table is not in the module. The helpers ask "is this + // routine there yet?" for every type, the answer is nearly always no, and a scan to learn it + // read the whole module each time. It holds because every symbol the lowering makes is + // reported (createSymbol, the helpers' createGlobal). It would not hold for any name: MLIR's + // own patterns make symbols too (the lowering of cf.assert declares "puts"). + void tableSymbols() + { + for (auto &moduleOp : *module.getBody()) + { + if (auto symbolName = symbolNameOf(&moduleOp)) + { + symbols.try_emplace(symbolName, &moduleOp); + } + } + } + + // a symbol the lowering made at the top level of the module + void createdSymbol(mlir::Operation *op) + { + created_++; + noteCreatedSymbol(op); + } + + // module.lookupSymbol(name), from the index while the module is being converted + static mlir::Operation *lookupSymbolIn(CompileOptions &compileOptions, mlir::ModuleOp module, llvm::StringRef name, + bool ownName = false) + { + auto *index = of(compileOptions); + if (index && index->isFor(module)) + { + return index->lookupSymbol(name, ownName); + } + + return module.lookupSymbol(name); + } + + // module.lookupSymbol(name) + template + static T lookupSymbolAs(CompileOptions &compileOptions, mlir::ModuleOp module, llvm::StringRef name) + { + return llvm::dyn_cast_or_null(lookupSymbolIn(compileOptions, module, name)); + } + + // module.lookupSymbol(name) for a name that only tslang's lowering gives a symbol + template + static T lookupOwnSymbolAs(CompileOptions &compileOptions, mlir::ModuleOp module, llvm::StringRef name) + { + return llvm::dyn_cast_or_null(lookupSymbolIn(compileOptions, module, name, /*ownName=*/true)); + } + + // builder.create(...) for a symbol at the top level of the module, told to the index + template + static OpTy createSymbol(CompileOptions &compileOptions, mlir::OpBuilder &builder, Args &&...args) + { + auto symbolOp = builder.create(std::forward(args)...); + if (auto *index = of(compileOptions)) + { + index->createdSymbol(symbolOp); + } + + return symbolOp; + } + + mlir::Operation *lookupSymbol(llvm::StringRef name, bool ownName = false) + { + auto nameAttr = mlir::StringAttr::get(module.getContext(), name); + mlir::Operation *found = nullptr; + auto it = symbols.find(nameAttr); + if (it != symbols.end()) + { + found = it->second; + } + else + { + if (!createdSymbolsKnown) + { + createdSymbols.clear(); + for (auto &moduleOp : *module.getBody()) + { + auto symbolName = symbolNameOf(&moduleOp); + if (symbolName && !symbols.contains(symbolName)) + { + createdSymbols.try_emplace(symbolName, &moduleOp); + } + } + + createdSymbolsKnown = true; + } + + auto created = createdSymbols.find(nameAttr); + if (created != createdSymbols.end()) + { + found = created->second; + } + else if (!ownName) + { + return module.lookupSymbol(name); + } + } + + if (found) + { + for (auto *previous = found->getPrevNode(); previous && symbolNameOf(previous) == nameAttr; + previous = previous->getPrevNode()) + { + found = previous; + } + } + + if (!verifyFile.empty()) + { + auto *scanned = module.lookupSymbol(name); + if (scanned != found) + { + report("symbol lookup", name); + return scanned; + } + } + + return found; + } + + // The comdat table that takes the next selector, with how many it holds and its number (the + // module's tables are numbered from 0 as they are made): null when the module has none. The + // lowering keeps a table small (COMDAT_TABLE_SELECTORS in LowerToLLVM.cpp) and starts another. + mlir::Operation *comdatTable(unsigned &selectors, unsigned &number) + { + if (!comdatKnown) + { + comdat = nullptr; + comdatSelectors = 0; + comdatNumber = 0; + for (auto &moduleOp : *module.getBody()) + { + auto comdatOp = llvm::dyn_cast(&moduleOp); + if (!comdatOp) + { + continue; + } + + // the newest is the one with the highest number, which its name ends with + unsigned tableNumber = 0; + auto name = comdatOp.getSymName(); + auto underscore = name.rfind('_'); + if (underscore == llvm::StringRef::npos || name.substr(underscore + 1).getAsInteger(10, tableNumber)) + { + // the first one's name has no number + tableNumber = 0; + } + + if (!comdat || tableNumber >= comdatNumber) + { + comdat = &moduleOp; + comdatNumber = tableNumber; + comdatSelectors = static_cast( + std::distance(comdatOp.getBody().front().begin(), comdatOp.getBody().front().end())); + } + } + + comdatKnown = true; + } + + selectors = comdatSelectors; + number = comdatNumber; + return comdat; + } + + void comdatTableCreated(mlir::Operation *table, unsigned number) + { + created_++; + noteCreatedSymbol(table); + comdat = table; + comdatSelectors = 0; + comdatNumber = number; + comdatKnown = true; + } + + void comdatSelectorAdded() + { + created_++; + comdatSelectors++; + } + // the last global of the kind at the top level of the module, or null mlir::Operation *last(Kind kind) { @@ -118,6 +317,7 @@ class LLVMGlobalsIndex : public mlir::RewriterBase::Listener void created(mlir::LLVM::GlobalOp globalOp) { created_++; + noteCreatedSymbol(globalOp); if (!built || globalOp->getParentOp() != module.getOperation()) { @@ -180,10 +380,31 @@ class LLVMGlobalsIndex : public mlir::RewriterBase::Listener // what the pattern created, and the patterns applied to what it created, is erased // (or about to be): no pointer taken since it began can be trusted built = false; + comdatKnown = false; + createdSymbolsKnown = false; } } private: + static mlir::StringAttr symbolNameOf(mlir::Operation *op) + { + return op->getAttrOfType(mlir::SymbolTable::getSymbolAttrName()); + } + + void noteCreatedSymbol(mlir::Operation *op) + { + if (!createdSymbolsKnown || op->getParentOp() != module.getOperation()) + { + return; + } + + auto symbolName = symbolNameOf(op); + if (symbolName && !symbols.contains(symbolName)) + { + createdSymbols.try_emplace(symbolName, op); + } + } + static Kind kindOf(mlir::LLVM::GlobalOp globalOp) { auto value = globalOp.getValueAttr(); @@ -257,6 +478,16 @@ class LLVMGlobalsIndex : public mlir::RewriterBase::Listener } mlir::ModuleOp module; + // the first operation of each symbol name before the conversion (tableSymbols) + llvm::DenseMap symbols; + // ...and of each name made since (createdSymbol); empty and known as the conversion begins + bool createdSymbolsKnown = true; + llvm::DenseMap createdSymbols; + // the comdat table taking selectors (comdatTable) + bool comdatKnown = false; + mlir::Operation *comdat = nullptr; + unsigned comdatSelectors = 0; + unsigned comdatNumber = 0; bool built = false; llvm::StringMap byName; mlir::Operation *lastOf[static_cast(Kind::Count)] = {}; diff --git a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h index 6dfe40740..559a5302b 100644 --- a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h @@ -169,7 +169,7 @@ class OwnershipRoutineLogic auto name = getRoutineName(type); auto parentModule = op->getParentOfType(); - if (parentModule.lookupSymbol(name)) + if (LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return name; } @@ -180,7 +180,7 @@ class OwnershipRoutineLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create( + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, th.getFunctionType(th.getVoidType(), {th.getPtrType()}), LLVM::Linkage::Internal); // the symbol must exist before the body is built: a recursive type (`class Node { @@ -217,7 +217,7 @@ class OwnershipRoutineLogic auto name = nameStream.str(); auto parentModule = op->getParentOfType(); - if (parentModule.lookupSymbol(name)) + if (LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return name; } @@ -228,7 +228,7 @@ class OwnershipRoutineLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create( + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, th.getFunctionType(th.getVoidType(), {th.getPtrType()}), LLVM::Linkage::Internal); auto *entryBlock = funcOp.addEntryBlock(rewriter); @@ -259,7 +259,7 @@ class OwnershipRoutineLogic auto name = getRetainRoutineName(type); auto parentModule = op->getParentOfType(); - if (parentModule.lookupSymbol(name)) + if (LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return name; } @@ -270,7 +270,7 @@ class OwnershipRoutineLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create( + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, th.getFunctionType(th.getVoidType(), {th.getPtrType()}), LLVM::Linkage::Internal); // as with release, the symbol must exist before the body is built, so that a @@ -590,7 +590,7 @@ class OwnershipRoutineLogic auto name = nameStream.str(); auto parentModule = op->getParentOfType(); - if (parentModule.lookupSymbol(name)) + if (LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return name; } @@ -601,7 +601,7 @@ class OwnershipRoutineLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create( + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, th.getFunctionType(th.getVoidType(), {th.getPtrType()}), LLVM::Linkage::Internal); // the symbol exists before the body, for a recursive type, as the release routine's does @@ -631,7 +631,7 @@ class OwnershipRoutineLogic auto name = nameStream.str(); auto parentModule = op->getParentOfType(); - if (parentModule.lookupSymbol(name)) + if (LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return name; } @@ -644,7 +644,7 @@ class OwnershipRoutineLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create(loc, name, th.getFunctionType(th.getVoidType(), {ptrTy}), + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, th.getFunctionType(th.getVoidType(), {ptrTy}), LLVM::Linkage::Internal); auto *entryBlock = funcOp.addEntryBlock(rewriter); @@ -770,7 +770,7 @@ class OwnershipRoutineLogic auto name = nameStream.str(); auto parentModule = op->getParentOfType(); - if (parentModule.lookupSymbol(name)) + if (LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return name; } @@ -785,7 +785,7 @@ class OwnershipRoutineLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create(loc, name, th.getFunctionType(th.getVoidType(), {llvmType}), + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, th.getFunctionType(th.getVoidType(), {llvmType}), LLVM::Linkage::Internal); auto *entryBlock = funcOp.addEntryBlock(rewriter); @@ -813,12 +813,12 @@ class OwnershipRoutineLogic auto parentModule = op->getParentOfType(); const char *helperName = "__tslang_free_block"; - if (!parentModule.lookupSymbol(helperName)) + if (!LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, helperName)) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto helper = rewriter.create( + auto helper = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, helperName, th.getFunctionType(th.getVoidType(), {th.getPtrType()}), LLVM::Linkage::Internal); auto *entryBlock = helper.addEntryBlock(rewriter); @@ -865,13 +865,13 @@ class OwnershipRoutineLogic const bool atomicAlways = compileOptions.atomicRefCounts(); const char *helperName = atomicAlways ? "__tslang_dec_ref_atomic" : "__tslang_dec_ref"; - if (!parentModule.lookupSymbol(helperName)) + if (!LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, helperName)) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); auto boolType = th.getLLVMBoolType(); - auto helper = rewriter.create( + auto helper = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, helperName, th.getFunctionType(boolType, {th.getPtrType()}), LLVM::Linkage::Internal); auto *entryBlock = helper.addEntryBlock(rewriter); @@ -955,12 +955,12 @@ class OwnershipRoutineLogic const bool atomicAlways = compileOptions.atomicRefCounts(); const char *helperName = atomicAlways ? "__tslang_inc_ref_atomic" : "__tslang_inc_ref"; - if (!parentModule.lookupSymbol(helperName)) + if (!LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, helperName)) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto helper = rewriter.create( + auto helper = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, helperName, th.getFunctionType(th.getVoidType(), {th.getPtrType()}), LLVM::Linkage::Internal); auto *entryBlock = helper.addEntryBlock(rewriter); @@ -1409,7 +1409,7 @@ class OwnershipRoutineLogic void ensurePlainBlockRetainRoutine() { auto parentModule = op->getParentOfType(); - if (parentModule.lookupSymbol(PLAIN_BLOCK_RETAIN_ROUTINE)) + if (LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, PLAIN_BLOCK_RETAIN_ROUTINE)) { return; } @@ -1420,7 +1420,7 @@ class OwnershipRoutineLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create( + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, PLAIN_BLOCK_RETAIN_ROUTINE, th.getFunctionType(th.getVoidType(), {th.getPtrType()}), LLVM::Linkage::Internal); auto *entryBlock = funcOp.addEntryBlock(rewriter); rewriter.setInsertionPointToStart(entryBlock); @@ -1522,7 +1522,7 @@ class OwnershipRoutineLogic auto name = nameStream.str(); auto parentModule = op->getParentOfType(); - if (parentModule.lookupSymbol(name)) + if (LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return name; } @@ -1534,7 +1534,7 @@ class OwnershipRoutineLogic OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto funcOp = rewriter.create( + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, th.getFunctionType(th.getVoidType(), {ptrTy}), LLVM::Linkage::Internal); auto *entryBlock = funcOp.addEntryBlock(rewriter); diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 7053d6689..98c98be38 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -2297,6 +2297,73 @@ static StringRef getIntegerExtensionAttrName(mlir::Type type) return {}; } +// A comdat selector named `name` for a function or a global, and the reference to it that goes +// in their `comdat` attribute. +// +// The selectors live in comdat tables at the start of the module: "__llvm_comdat", then +// "__llvm_comdat_1" and so on, each holding at most COMDAT_TABLE_SELECTORS. MLIR's verifier and its +// translation to LLVM IR find a selector by name for every function and global that has one, +// scanning the module's operations for the table and then the table for the selector. With one +// table for the module that was a scan of every selector for each of them: the translation of a +// module with 2,000 instances of a generic class took 1.8 s, and four times that for twice as +// many. In LLVM IR a comdat is named by its selector alone, so which table holds it changes +// nothing there. +constexpr unsigned COMDAT_TABLE_SELECTORS = 128; + +static mlir::SymbolRefAttr addComdatSelector(mlir::ModuleOp module, StringRef name, mlir::LLVM::comdat::Comdat comdat, + mlir::ConversionPatternRewriter &rewriter, CompileOptions &compileOptions) +{ + auto *index = LLVMGlobalsIndex::of(compileOptions); + if (index && !index->isFor(module)) + { + index = nullptr; + } + + mlir::LLVM::ComdatOp comdatOp; + unsigned selectors = 0; + unsigned number = 0; + auto haveTable = false; + if (index) + { + if (auto *table = index->comdatTable(selectors, number)) + { + comdatOp = cast(table); + haveTable = true; + } + } + else if ((comdatOp = module.lookupSymbol("__llvm_comdat"))) + { + // outside the LLVM lowering pass nothing counts the selectors: the one table takes them all + haveTable = true; + } + + if (!haveTable || (index && selectors >= COMDAT_TABLE_SELECTORS)) + { + auto newNumber = haveTable ? number + 1 : 0; + auto tableName = newNumber == 0 ? std::string("__llvm_comdat") : "__llvm_comdat_" + std::to_string(newNumber); + + // at the start of the module, where a lookup by name finds it soonest: see moveComdatsFirst + mlir::OpBuilder::InsertionGuard guard(rewriter); + rewriter.setInsertionPointToStart(module.getBody()); + comdatOp = rewriter.create(module.getLoc(), tableName); + if (index) + { + index->comdatTableCreated(comdatOp, newNumber); + } + } + + mlir::OpBuilder::InsertionGuard guard(rewriter); + rewriter.setInsertionPointToEnd(&comdatOp.getBody().back()); + auto selectorOp = rewriter.create(comdatOp.getLoc(), name, comdat); + if (index) + { + index->comdatSelectorAdded(); + } + + return mlir::SymbolRefAttr::get(rewriter.getContext(), comdatOp.getSymName(), + mlir::FlatSymbolRefAttr::get(selectorOp.getSymNameAttr())); +} + struct FuncOpLowering : public TsLlvmPattern { using TsLlvmPattern::TsLlvmPattern; @@ -2331,7 +2398,9 @@ struct FuncOpLowering : public TsLlvmPattern } auto convertedFuncType = rewriter.getFunctionType(signatureInputsConverter.getConvertedTypes(), signatureResultsConverter.getConvertedTypes()); - auto newFuncOp = rewriter.create(location, funcOp.getName(), convertedFuncType, ArrayRef{}, argDictAttrs); + auto newFuncOp = LLVMGlobalsIndex::createSymbol( + tsLlvmContext->compileOptions, rewriter, location, funcOp.getName(), convertedFuncType, ArrayRef{}, + argDictAttrs); // C expects an 8- or 16-bit integer or a bool extended to 32 bits by whoever hands it // over: the caller for an argument, the callee for a result, and clang-compiled code @@ -2417,7 +2486,7 @@ struct FuncOpLowering : public TsLlvmPattern //newFuncOp->setAttr("dso_local", rewriter.getUnitAttr()); newFuncOp.setPrivate(); - addComdat(newFuncOp, rewriter); + addComdat(newFuncOp, rewriter, tsLlvmContext->compileOptions); continue; } @@ -2490,28 +2559,9 @@ struct FuncOpLowering : public TsLlvmPattern } static void addComdat(mlir::func::FuncOp &func, - mlir::ConversionPatternRewriter &rewriter) { - auto module = func->getParentOfType(); - - const char *comdatName = "__llvm_comdat"; - mlir::LLVM::ComdatOp comdatOp = module.lookupSymbol(comdatName); - 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); - } - - mlir::OpBuilder::InsertionGuard guard(rewriter); - rewriter.setInsertionPointToEnd(&comdatOp.getBody().back()); - auto selectorOp = rewriter.create( - comdatOp.getLoc(), func.getSymName(), - mlir::LLVM::comdat::Comdat::Any); - func->setAttr("comdat", mlir::SymbolRefAttr::get( - rewriter.getContext(), comdatName, - mlir::FlatSymbolRefAttr::get(selectorOp.getSymNameAttr()))); + mlir::ConversionPatternRewriter &rewriter, CompileOptions &compileOptions) { + func->setAttr("comdat", addComdatSelector(func->getParentOfType(), func.getSymName(), + mlir::LLVM::comdat::Comdat::Any, rewriter, compileOptions)); } }; @@ -2528,7 +2578,8 @@ struct SymbolCallInternalOpLowering : public TsLlvmPatterngetParentOfType(); - auto funcOp = moduleOp.lookupSymbol(op.getCallee()); + LLVMCodeHelper symbolsHelper(op, rewriter, getTypeConverter(), tsLlvmContext->compileOptions); + auto funcOp = symbolsHelper.lookupSymbol(moduleOp, op.getCallee()); LLVM::LLVMFunctionType llvmFuncType; if (auto llvmFuncOp = dyn_cast_or_null(funcOp)) @@ -3293,7 +3344,7 @@ static LLVM::LLVMFuncOp getOrCreateRemapVTableFunction(Operation *op, Conversion StringRef listName = "ts_remap_vtable_list"; auto parentModule = op->getParentOfType(); - if (auto existing = parentModule.lookupSymbol(name)) + if (auto existing = LLVMGlobalsIndex::lookupOwnSymbolAs(compileOptions, parentModule, name)) { return existing; } @@ -3321,7 +3372,7 @@ static LLVM::LLVMFuncOp getOrCreateRemapVTableFunction(Operation *op, Conversion rewriter.create(loc, ValueRange{nullList}); } - auto funcOp = rewriter.create( + auto funcOp = LLVMGlobalsIndex::createSymbol(compileOptions, rewriter, loc, name, th.getFunctionType(ptrType, {ptrType, ptrType, wordType}), LLVM::Linkage::Internal); // anchored at the function, not at `op`: what it builds takes the anchor's location, and one in @@ -5964,7 +6015,7 @@ struct GlobalOpLowering : public TsLlvmPattern auto comdat = globalOp.getComdat().has_value() ? static_cast(globalOp.getComdat().value()) : mlir::LLVM::comdat::Comdat::Any; - addComdat(llvmGlobalOp, rewriter, comdat); + addComdat(llvmGlobalOp, rewriter, tsLlvmContext->compileOptions, comdat); } if (tsLlvmContext->compileOptions.generateDebugInfo) @@ -5984,29 +6035,11 @@ struct GlobalOpLowering : public TsLlvmPattern } static void addComdat(mlir::LLVM::GlobalOp &global, - mlir::ConversionPatternRewriter &rewriter, mlir::LLVM::comdat::Comdat comdat = mlir::LLVM::comdat::Comdat::Any) { - auto module = global->getParentOfType(); - - const char *comdatName = "__llvm_comdat"; - mlir::LLVM::ComdatOp comdatOp = - module.lookupSymbol(comdatName); - 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); - } - + mlir::ConversionPatternRewriter &rewriter, CompileOptions &compileOptions, + mlir::LLVM::comdat::Comdat comdat = mlir::LLVM::comdat::Comdat::Any) { // TODO: add check if name already added - mlir::OpBuilder::InsertionGuard guard(rewriter); - rewriter.setInsertionPointToEnd(&comdatOp.getBody().back()); - auto selectorOp = rewriter.create( - comdatOp.getLoc(), global.getSymName(), comdat); - global.setComdatAttr(mlir::SymbolRefAttr::get( - rewriter.getContext(), comdatName, - mlir::FlatSymbolRefAttr::get(selectorOp.getSymNameAttr()))); + global.setComdatAttr(addComdatSelector(global->getParentOfType(), global.getSymName(), comdat, + rewriter, compileOptions)); } }; @@ -8394,7 +8427,7 @@ struct GlobalConstructorOpLowering : public TsLlvmPatterngetParentOfType(); //auto mlirGCtors = parentModule.lookupSymbol(MLIR_GCTORS); - auto mlirGCtors = parentModule.lookupSymbol(MLIR_GCTORS); + auto mlirGCtors = LLVMGlobalsIndex::lookupSymbolIn(tsLlvmContext->compileOptions, parentModule, MLIR_GCTORS, /*ownName=*/true); if (!mlirGCtors) { OpBuilder::InsertionGuard insertGuard(rewriter); @@ -8402,7 +8435,8 @@ struct GlobalConstructorOpLowering : public TsLlvmPattern(loc, MLIR_GCTORS, llvmFnType); + auto initFunc = LLVMGlobalsIndex::createSymbol(tsLlvmContext->compileOptions, rewriter, loc, + MLIR_GCTORS, llvmFnType); initFunc.setPublic(); auto linkage = LLVM::LinkageAttr::get(rewriter.getContext(), LLVM::Linkage::External); initFunc->setAttr("llvm.linkage", linkage); @@ -9668,7 +9702,7 @@ static void giveEntryPointAnExitCode(mlir::ModuleOp m, CompileOptions &compileOp } } -// The module's comdat table becomes its first operation. MLIR's verifier and its translation to +// The module's comdat tables (addComdatSelector) become its first operations. 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 @@ -9677,17 +9711,32 @@ static void giveEntryPointAnExitCode(mlir::ModuleOp m, CompileOptions &compileOp static void moveComdatsFirst(mlir::ModuleOp m) { auto *body = m.getBody(); + SmallVector tables; for (auto &moduleOp : *body) { if (isa(&moduleOp)) { - if (&moduleOp != &body->front()) + tables.push_back(&moduleOp); + } + } + + // in the order they stand in, so the same module always comes out the same + mlir::Operation *after = nullptr; + for (auto *table : tables) + { + if (after) + { + if (after->getNextNode() != table) { - moduleOp.moveBefore(&body->front()); + table->moveAfter(after); } - - return; } + else if (table != &body->front()) + { + table->moveBefore(&body->front()); + } + + after = table; } } @@ -9981,6 +10030,7 @@ void TypeScriptToLLVMLoweringPass::runOnOperation() // next one goes (LLVMGlobalsIndex.h). It listens to the conversion, to forget what a failed // pattern created. LLVMGlobalsIndex globalsIndex(m); + globalsIndex.tableSymbols(); ConversionConfig conversionConfig; conversionConfig.listener = &globalsIndex; tsContext.compileOptions.llvmGlobalsIndex = &globalsIndex; diff --git a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp index 496da3317..fb9b6d561 100644 --- a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp +++ b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp @@ -44,6 +44,38 @@ namespace mlirgen llvm::APInt currentEnumValue(32, 0); // a member after a string member has no value to count on from auto previousIsString = false; + + // The enum's members so far, as the dictionary an initializer naming an earlier member + // reads them from. It is made again only where it can be read - before an initializer that + // is not a literal, and once all members are in: making it after every member took a time + // that grew with the square of their number. + auto membersListed = true; + auto listMembers = [&]() { + if (membersListed) + { + return; + } + + SmallVector namedEnumValues; + for (auto &key : enumValues) + { + namedEnumValues.push_back({builder.getStringAttr(key.first()), key.second}); + } + + enumInfo.second = mlir::DictionaryAttr::get(builder.getContext(), namedEnumValues /*adjustedEnumValues*/); + membersListed = true; + }; + + // also where a member fails: the declaration is tried again, against what is listed + struct ListMembersOnExit + { + std::function list; + ~ListMembersOnExit() + { + list(); + } + } listMembersOnExit{listMembers}; + for (auto enumMember : enumDeclarationAST->members) { auto location = loc(enumMember); @@ -57,6 +89,11 @@ namespace mlirgen mlir::Attribute enumValueAttr; if (enumMember->initializer) { + if (enumMember->initializer != SyntaxKind::NumericLiteral && enumMember->initializer != SyntaxKind::StringLiteral) + { + listMembers(); + } + GenContext enumValueGenContext(genContext); enumValueGenContext.allowConstEval = true; auto result = mlirGen(enumMember->initializer, enumValueGenContext); @@ -137,17 +174,13 @@ namespace mlirgen previousIsString = isa(enumValueAttr); // update enum to support req. access - SmallVector namedEnumValues; - for (auto &key : enumValues) - { - namedEnumValues.push_back({builder.getStringAttr(key.first()), key.second}); - } - - enumInfo.second = mlir::DictionaryAttr::get(builder.getContext(), namedEnumValues /*adjustedEnumValues*/); + membersListed = false; currentEnumValue++; } + listMembers(); + auto location = loc(enumDeclarationAST); auto storeType = mth.getUnionTypeWithMerge(location, enumLiteralTypes); diff --git a/tslang/lib/TypeScript/StackEscapeFacts.h b/tslang/lib/TypeScript/StackEscapeFacts.h index ed3f75354..5b651aa67 100644 --- a/tslang/lib/TypeScript/StackEscapeFacts.h +++ b/tslang/lib/TypeScript/StackEscapeFacts.h @@ -170,7 +170,7 @@ class StackEscapeFacts { for (auto candidate : candidates) { - auto funcOp = module.lookupSymbol(candidate); + auto funcOp = lookupFunction(candidate); // declared here, defined in a library linked statically that published its facts - by // the name it binds: one renamed by @dllname / @linkname binds another symbol if (funcOp && funcOp.getBody().empty() && !funcOp->hasAttr(DLL_NAME) && @@ -205,9 +205,33 @@ class StackEscapeFacts return it != imported->end() && llvm::is_contained(it->second, argIndex); } + // module.lookupSymbol(symbol), from a table of the module's symbols made on + // the first question: the lookup itself scans the module's operations, and it is asked for + // every call and every function reference of the module. The facts already hold the module's + // functions by address, so the symbols are no more allowed to change under them than those. + mlir_ts::FuncOp lookupFunction(mlir::FlatSymbolRefAttr symbol) + { + if (!symbols) + { + symbols.emplace(); + for (auto &moduleOp : *module.getBody()) + { + if (auto name = moduleOp.getAttrOfType(mlir::SymbolTable::getSymbolAttrName())) + { + // the first of a name, as the lookup finds it + symbols->try_emplace(name, &moduleOp); + } + } + } + + auto it = symbols->find(symbol.getAttr()); + return it == symbols->end() ? mlir_ts::FuncOp() : llvm::dyn_cast(it->second); + } + mlir::ModuleOp module; bool trustLocalVTables; const BorrowFactsMap *imported; + std::optional> symbols; std::optional> recursive; // Every function on a cycle of the call graph, in one pass (Tarjan's strongly connected @@ -232,7 +256,7 @@ class StackEscapeFacts llvm::SmallPtrSet seen; for (auto callee : calleesOfFunction(funcOp)) { - auto calleeOp = module.lookupSymbol(callee); + auto calleeOp = lookupFunction(callee); if (!calleeOp || !seen.insert(calleeOp).second) { continue; @@ -359,7 +383,7 @@ class StackEscapeFacts llvm::SmallPtrSet seen; module.walk([&](mlir_ts::FuncOp funcOp) { funcOp.walk([&](mlir_ts::SymbolRefOp symbolRefOp) { - auto target = module.lookupSymbol(symbolRefOp.getIdentifierAttr()); + auto target = lookupFunction(symbolRefOp.getIdentifierAttr()); if (target && seen.insert(target).second) { addressTakenFunctions->push_back(symbolRefOp.getIdentifierAttr());