From c3d3e087584cee54d760d95dfe5f5aa5845de5fc Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 15 Nov 2026 14:01:19 +0000 Subject: [PATCH 1/2] Compile time: find the last global without walking the whole module Every new global (a vtable, an RTTI record, a class's size field, a string literal) walked every operation of the module, function bodies included, to find the last global to insert after. Compile time grew with globals x module size: a file of 2,000 small classes took 100 s to compile to an object without optimisation, 16 s with --opt_level=3. The operations looked for (globals, global constructors) only sit at the top level of the module, so the module's body is now scanned backwards and the scan stops at the first match. The same file takes 5.4 s and 2.0 s. The insertion point is the same one, so the emitted IR does not change. Co-Authored-By: Claude Fable 5.2 --- .../LowerToLLVM/LLVMCodeHelperBase.h | 49 +++++++++---------- .../TypeScript/MLIRLogic/MLIRCodeLogic.h | 15 +++--- .../MLIRLogic/MLIRRTTIHelperVCLinux.h | 13 ++--- .../MLIRLogic/MLIRRTTIHelperVCWin32.h | 15 +++--- tslang/lib/TypeScript/MLIRGenImpl.h | 13 ++--- 5 files changed, 52 insertions(+), 53 deletions(-) diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h index a317c0f74..536c5d6c1 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h @@ -36,63 +36,58 @@ class LLVMCodeHelperBase { } - template + // The seekLast family puts the insertion point after the last operation of a kind in the + // module's body. Those operations (globals, global constructors) only sit at the top level of + // the module, so the body is scanned backwards and the function bodies are never entered: a + // walk of the whole module for every new global made compile time grow with globals x module + // size. + template void seekLast(mlir::Block *block) { // find last string - auto lastUse = [&](Operation *op) { - if (auto globalOp = dyn_cast_or_null(op)) + for (auto &blockOp : llvm::reverse(*block)) + { + if (auto globalOp = dyn_cast(&blockOp)) { if (globalOp.getValueAttr() && isa(globalOp.getValueAttr())) { rewriter.setInsertionPointAfter(globalOp); + return; } } - }; - - block->walk(lastUse); + } } void seekLast(mlir::Block *block) { - // find last string - auto lastUse = [&](mlir::Operation *op) { - if (auto globalOp = dyn_cast_or_null(op)) - { - rewriter.setInsertionPointAfter(globalOp); - } - }; - - block->walk(lastUse); + seekLastOp(block); } void seekLastWithBody(mlir::Block *block) { - // find last string - auto lastUse = [&](Operation *op) { - if (auto globalOp = dyn_cast_or_null(op)) + for (auto &blockOp : llvm::reverse(*block)) + { + if (auto globalOp = dyn_cast(&blockOp)) { if (globalOp.getInitializerBlock()) { rewriter.setInsertionPointAfter(globalOp); + return; } } - }; - - block->walk(lastUse); + } } template void seekLastOp(mlir::Block *block) { - // find last string - auto lastUse = [&](Operation *op) { - if (auto opT = dyn_cast_or_null(op)) + for (auto &blockOp : llvm::reverse(*block)) + { + if (auto opT = dyn_cast(&blockOp)) { rewriter.setInsertionPointAfter(opT); + return; } - }; - - block->walk(lastUse); + } } template Operation *seekFirstNonConstantOp(T funcOp) diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h index a31de5b15..0db27d96f 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h @@ -367,16 +367,17 @@ class MLIRCodeLogicHelper template void seekLastOp(mlir::Block *block) { - // find last string - auto lastUse = [&](mlir::Operation *op) { - if (auto globalOp = dyn_cast(op)) + // the last one at the top level of the block: these operations do not nest, and walking + // into every function body for each new global made compile time quadratic + for (auto &blockOp : llvm::reverse(*block)) + { + if (auto globalOp = dyn_cast(&blockOp)) { builder.setInsertionPointAfter(globalOp); + return; } - }; - - block->walk(lastUse); - } + } + } void seekLast(mlir::Block *block) { diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h index 0cd798672..63e4385aa 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h @@ -324,15 +324,16 @@ class MLIRRTTIHelperVCLinux void seekLast(mlir::Block *block) { - // find last string - auto lastUse = [&](mlir::Operation *op) { - if (auto globalOp = dyn_cast_or_null(op)) + // the last one at the top level of the block: globals do not nest, and walking into + // every function body for each new global made compile time quadratic + for (auto &blockOp : llvm::reverse(*block)) + { + if (auto globalOp = dyn_cast(&blockOp)) { rewriter.setInsertionPointAfter(globalOp); + return; } - }; - - block->walk(lastUse); + } } bool setRTTIForType(mlir::Location loc, mlir::Type type, std::function resolveClassInfo) diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h index 231330f55..f1152b403 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h @@ -397,16 +397,17 @@ class MLIRRTTIHelperVCWin32 template void seekLastOp(mlir::Block *block) { - // find last string - auto lastUse = [&](mlir::Operation *op) { - if (auto globalOp = dyn_cast_or_null(op)) + // the last one at the top level of the block: these operations do not nest, and walking + // into every function body for each new global made compile time quadratic + for (auto &blockOp : llvm::reverse(*block)) + { + if (auto globalOp = dyn_cast(&blockOp)) { rewriter.setInsertionPointAfter(globalOp); + return; } - }; - - block->walk(lastUse); - } + } + } void seekLast(mlir::Block *block) { diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index fc65160e3..4a0aa7e58 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -2809,15 +2809,16 @@ class MLIRGenImpl { mlir::OpBuilder::InsertionGuard insertGuard(builder); builder.setInsertionPointToStart(theModule.getBody()); - // find last string - auto lastUse = [&](mlir::Operation *op) { - if (auto globalOp = dyn_cast(op)) + // after the last global: globals only sit at the top level of the module, and walking + // into every function body for each new global made compile time quadratic + for (auto &moduleOp : llvm::reverse(*theModule.getBody())) + { + if (auto globalOp = dyn_cast(&moduleOp)) { builder.setInsertionPointAfter(globalOp); + break; } - }; - - theModule.getBody()->walk(lastUse); + } SmallVector attrs; From 39ac0a8c0259b1fccbbc1edcd35c702868ae993c Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sun, 15 Nov 2026 14:20:03 +0000 Subject: [PATCH 2/2] Compile time: an index of the LLVM globals for the lowering pass Lowering asked two questions for every global it made - is one of this name there yet, and where does the next one go - and answered each by scanning the module's top-level operations. With thousands of globals that was quadratic: a file of 4,000 small classes took 20 s to compile to an object without optimisation. LLVMGlobalsIndex keeps the names and the last global (of any kind, holding a string, holding dense elements) for the one conversion the LLVM lowering pass runs, and gives the answers the scans gave. The helpers reach it through CompileOptions::llvmGlobalsIndex; every LLVM global made during the conversion goes through LLVMCodeHelperBase::createGlobal, which reports it. The index is the conversion's listener and forgets everything when a pattern that made a global fails, since the driver erases what a failed pattern created; the next question rebuilds it with one scan. The same file now takes 8 s; 1,000 classes under -mm=rc go from 8.8 s to 1.6 s. The emitted IR does not change. TSLANG_VERIFY_LLVM_GLOBALS_INDEX= checks every answer against the scan it replaces and appends any difference to the file: the suite run that way leaves no file. Co-Authored-By: Claude Fable 5.2 --- tslang/include/TypeScript/DataStructs.h | 6 + .../TypeScript/LowerToLLVM/ArrayLayout.h | 8 +- .../TypeScript/LowerToLLVM/LLVMCodeHelper.h | 32 +-- .../LowerToLLVM/LLVMCodeHelperBase.h | 59 +++- .../TypeScript/LowerToLLVM/LLVMGlobalsIndex.h | 271 ++++++++++++++++++ .../LowerToLLVM/LLVMRTTIHelperVCWin32.h | 24 +- tslang/lib/TypeScript/LowerToLLVM.cpp | 24 +- 7 files changed, 384 insertions(+), 40 deletions(-) create mode 100644 tslang/include/TypeScript/LowerToLLVM/LLVMGlobalsIndex.h diff --git a/tslang/include/TypeScript/DataStructs.h b/tslang/include/TypeScript/DataStructs.h index fe7722afc..48b1b418d 100644 --- a/tslang/include/TypeScript/DataStructs.h +++ b/tslang/include/TypeScript/DataStructs.h @@ -135,6 +135,12 @@ struct CompileOptions { return memoryModel == MemoryModelRC || memoryModel == MemoryModelOwn; } + + // Not an option: the LLVM globals of the module the LLVM lowering pass is converting, there + // only while it converts (LowerToLLVM/LLVMGlobalsIndex.h). The lowering helpers are built + // from these options everywhere, so this is how they reach it. A typescript::LLVMGlobalsIndex, + // untyped because this header declares no namespace: read it with LLVMGlobalsIndex::of. + void *llvmGlobalsIndex = nullptr; }; #endif // TYPESCRIPT_DATASTRUCT_H_ \ No newline at end of file diff --git a/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h b/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h index 8213577ea..319365167 100644 --- a/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h +++ b/tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h @@ -301,7 +301,7 @@ class ArrayLayout : public LLVMCodeHelperBase auto withWord = compileOptions.tracksOwnership(); auto headerName = ("ah_" + dataGlobalName).str(); - auto global = parentModule.lookupSymbol(headerName); + auto global = lookupGlobal(parentModule, headerName); if (!global) { OpBuilder::InsertionGuard guard(rewriter); @@ -313,7 +313,7 @@ class ArrayLayout : public LLVMCodeHelperBase /*isPacked=*/true); } - global = rewriter.create(loc, globalType, /*isConstant=*/true, LLVM::Linkage::Internal, + global = createGlobal(loc, globalType, /*isConstant=*/true, LLVM::Linkage::Internal, headerName, mlir::Attribute{}); global.setAlignment(getHeapBlockHeaderSize()); @@ -389,11 +389,11 @@ class ArrayLayout : public LLVMCodeHelperBase TypeHelper th(rewriter); auto loc = op->getLoc(); auto parentModule = op->getParentOfType(); - if (!parentModule.lookupSymbol(name)) + if (!lookupGlobal(parentModule, name)) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); - auto global = rewriter.create(rewriter.getUnknownLoc(), headerType(), /*isConstant=*/true, + auto global = createGlobal(rewriter.getUnknownLoc(), headerType(), /*isConstant=*/true, LLVM::Linkage::Internal, name, mlir::Attribute{}); rewriter.setInsertionPointToStart(rewriter.createBlock(&global.getInitializerRegion())); mlir::Value zero = rewriter.create(rewriter.getUnknownLoc(), headerType()); diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h index 2a638002a..4029a013b 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h @@ -78,14 +78,14 @@ class LLVMCodeHelper : public LLVMCodeHelperBase // Create the global at the entry of the module. LLVM::GlobalOp global; - if (!(global = parentModule.lookupSymbol(name))) + if (!(global = lookupGlobal(parentModule, name))) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); seekLast(parentModule.getBody()); - global = rewriter.create(loc, type, isConst, linkage, name, value); + global = createGlobal(loc, type, isConst, linkage, name, value); { setStructWritingPoint(global); @@ -116,7 +116,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase TypeHelper th(rewriter); // Create the global at the entry of the module. - if (!(global = parentModule.lookupSymbol(name))) + if (!(global = lookupGlobal(parentModule, name))) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); @@ -129,7 +129,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase effectiveValue = rewriter.getArrayAttr({effectiveValue}); } - global = rewriter.create(loc, type, isConst, linkage, name, effectiveValue); + global = createGlobal(loc, type, isConst, linkage, name, effectiveValue); if (section.size() > 0) { global.setSection(section); @@ -170,7 +170,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase auto ptrType = th.getPtrType(); // Create the global at the entry of the module. - if (!(global = parentModule.lookupSymbol(name))) + if (!(global = lookupGlobal(parentModule, name))) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); @@ -180,7 +180,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase TypeHelper th(rewriter); auto arrayPtrType = LLVM::LLVMArrayType::get(ptrType, 1); - global = rewriter.create(loc, arrayPtrType, false, LLVM::Linkage::Appending, name, mlir::Attribute{}); + global = createGlobal(loc, arrayPtrType, false, LLVM::Linkage::Appending, name, mlir::Attribute{}); if (section.size() > 0) { global.setSection(section); @@ -310,7 +310,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase TypeHelper th(rewriter); // Create the global at the entry of the module. - LLVM::GlobalOp global = parentModule.lookupSymbol(name); + LLVM::GlobalOp global = lookupGlobal(parentModule, name); if (!global) { OpBuilder::InsertionGuard insertGuard(rewriter); @@ -318,7 +318,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase seekLast(parentModule.getBody()); - global = rewriter.create(loc, type, true, linkage, name, mlir::Attribute()); + global = createGlobal(loc, type, true, linkage, name, mlir::Attribute()); { setStructWritingPoint(global); @@ -375,14 +375,14 @@ class LLVMCodeHelper : public LLVMCodeHelperBase auto descriptorType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {recordType, nameArrayType}, false); LLVM::GlobalOp global; - if (!(global = parentModule.lookupSymbol(varName.str()))) + if (!(global = lookupGlobal(parentModule, varName.str()))) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); seekLast(parentModule.getBody()); - global = rewriter.create(loc, descriptorType, true, LLVM::Linkage::Internal, varName.str(), + global = createGlobal(loc, descriptorType, true, LLVM::Linkage::Internal, varName.str(), mlir::Attribute{}); setStructWritingPoint(global); @@ -571,7 +571,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase // Create the global at the entry of the module. LLVM::GlobalOp global; - if (!(global = parentModule.lookupSymbol(name))) + if (!(global = lookupGlobal(parentModule, name))) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); @@ -625,7 +625,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase // i32 wants sixteen bytes, which would put the header two words back instead auto blockType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {getHeapBlockWordType(), dataType}, /*isPacked=*/true); - global = rewriter.create(loc, blockType, true, LLVM::Linkage::Internal, name, mlir::Attribute{}); + global = createGlobal(loc, blockType, true, LLVM::Linkage::Internal, name, mlir::Attribute{}); setStructWritingPoint(global); @@ -650,7 +650,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase { seekLast(parentModule.getBody()); - global = rewriter.create(loc, /*arrayType*/dataType, true, LLVM::Linkage::Internal, name, denseAttr); + global = createGlobal(loc, /*arrayType*/dataType, true, LLVM::Linkage::Internal, name, denseAttr); } else { @@ -658,7 +658,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase OpBuilder::InsertionGuard guard(rewriter); - global = rewriter.create(loc, arrayType, true, LLVM::Linkage::Internal, name, mlir::Attribute{}); + global = createGlobal(loc, arrayType, true, LLVM::Linkage::Internal, name, mlir::Attribute{}); setStructWritingPoint(global); @@ -843,14 +843,14 @@ class LLVMCodeHelper : public LLVMCodeHelperBase // Create the global at the entry of the module. LLVM::GlobalOp global; - if (!(global = parentModule.lookupSymbol(name))) + if (!(global = lookupGlobal(parentModule, name))) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); seekLast(parentModule.getBody()); - global = rewriter.create(loc, llvmStructType, true, LLVM::Linkage::Internal, name, mlir::Attribute{}); + global = createGlobal(loc, llvmStructType, true, LLVM::Linkage::Internal, name, mlir::Attribute{}); setStructWritingPoint(global); diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h index 536c5d6c1..63f02c861 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h @@ -3,9 +3,12 @@ #include "TypeScript/LowerToLLVM/TypeConverterHelper.h" #include "TypeScript/LowerToLLVM/TypeHelper.h" +#include "TypeScript/LowerToLLVM/LLVMGlobalsIndex.h" #include "mlir/Dialect/Arith/IR/Arith.h" +#include + using namespace mlir; namespace mlir_ts = mlir::typescript; @@ -41,9 +44,50 @@ class LLVMCodeHelperBase // the module, so the body is scanned backwards and the function bodies are never entered: a // walk of the whole module for every new global made compile time grow with globals x module // size. + // + // While the LLVM lowering pass converts the module, its LLVMGlobalsIndex knows the LLVM globals + // and no scan is made at all: lookupGlobal, createGlobal and seekLast go through it. Every + // LLVM global made during the conversion has to be made with createGlobal. + + // module.lookupSymbol(name) + LLVM::GlobalOp lookupGlobal(mlir::ModuleOp module, StringRef name) + { + auto *index = LLVMGlobalsIndex::of(compileOptions); + if (index && index->isFor(module)) + { + return index->lookup(name); + } + + return module.lookupSymbol(name); + } + + // rewriter.create(...), told to the index + template LLVM::GlobalOp createGlobal(Args &&...args) + { + auto globalOp = rewriter.create(std::forward(args)...); + if (auto *index = LLVMGlobalsIndex::of(compileOptions)) + { + index->created(globalOp); + } + + return globalOp; + } + template void seekLast(mlir::Block *block) { + auto *index = LLVMGlobalsIndex::of(compileOptions); + if (index && index->isFor(block) && (std::is_same_v || std::is_same_v)) + { + auto kind = std::is_same_v ? LLVMGlobalsIndex::Kind::String : LLVMGlobalsIndex::Kind::Dense; + if (auto *last = index->last(kind)) + { + rewriter.setInsertionPointAfter(last); + } + + return; + } + // find last string for (auto &blockOp : llvm::reverse(*block)) { @@ -60,6 +104,17 @@ class LLVMCodeHelperBase void seekLast(mlir::Block *block) { + auto *index = LLVMGlobalsIndex::of(compileOptions); + if (index && index->isFor(block)) + { + if (auto *last = index->last(LLVMGlobalsIndex::Kind::Any)) + { + rewriter.setInsertionPointAfter(last); + } + + return; + } + seekLastOp(block); } @@ -151,7 +206,7 @@ class LLVMCodeHelperBase // Create the global at the entry of the module. LLVM::GlobalOp global; - if (!(global = parentModule.lookupSymbol(name))) + if (!(global = lookupGlobal(parentModule, name))) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); @@ -159,7 +214,7 @@ class LLVMCodeHelperBase seekLast(parentModule.getBody()); auto type = th.getArrayType(th.getI8Type(), blockBytes.size()); - global = rewriter.create(loc, type, true, LLVM::Linkage::Internal, name, rewriter.getStringAttr(blockBytes)); + global = createGlobal(loc, type, true, LLVM::Linkage::Internal, name, rewriter.getStringAttr(blockBytes)); // the header is read as a whole word, so the block base has to be word-aligned global.setAlignment(headerSize); } diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMGlobalsIndex.h b/tslang/include/TypeScript/LowerToLLVM/LLVMGlobalsIndex.h new file mode 100644 index 000000000..a1d5156af --- /dev/null +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMGlobalsIndex.h @@ -0,0 +1,271 @@ +#ifndef MLIR_TYPESCRIPT_LOWERTOLLVMLOGIC_LLVMGLOBALSINDEX_H_ +#define MLIR_TYPESCRIPT_LOWERTOLLVMLOGIC_LLVMGLOBALSINDEX_H_ + +#include "TypeScript/DataStructs.h" + +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/PatternMatch.h" + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/raw_ostream.h" + +#include +#include +#include + +namespace typescript +{ + +// The LLVM globals of the module being lowered: which names are taken, and which global is the +// last one - of any kind, and of those holding a string or dense elements - at the top level of +// the module. +// +// Lowering asks both questions for every global it makes ("is it there yet?", then "where does +// the next one go?"), and answering each by scanning the module's operations made the lowering +// of a module with thousands of classes quadratic. The index answers them from a table, and +// gives the answers the scans gave: the same global, the same insertion point. +// +// It belongs to one run of the LLVM lowering pass, which puts it in +// CompileOptions::llvmGlobalsIndex for the conversion and takes it out after. Two rules keep it +// true: +// - every LLVM global made during the conversion is reported with created() (the helpers' +// createGlobal does it). One that is not reported and is not where the rules of created() +// can place it would be missed by lookup(); +// - the conversion driver erases what a failed pattern created. The index is the conversion's +// listener, and forgets everything when a pattern that reported a global fails; the next +// question rebuilds it with one scan. +// +// Set TSLANG_VERIFY_LLVM_GLOBALS_INDEX to a file name to have every answer checked against the +// scan it replaces: a difference is appended to the file, and the scan's answer is used. +class LLVMGlobalsIndex : public mlir::RewriterBase::Listener +{ + public: + enum class Kind + { + Any, + String, + Dense, + Count + }; + + explicit LLVMGlobalsIndex(mlir::ModuleOp module) : module(module) + { + if (auto *file = std::getenv("TSLANG_VERIFY_LLVM_GLOBALS_INDEX")) + { + verifyFile = file; + } + } + + // the index of the conversion under way, if any + static LLVMGlobalsIndex *of(CompileOptions &compileOptions) + { + return static_cast(compileOptions.llvmGlobalsIndex); + } + + bool isFor(mlir::ModuleOp otherModule) + { + return otherModule == module; + } + + bool isFor(mlir::Block *block) + { + return block == module.getBody(); + } + + // module.lookupSymbol(name) + mlir::LLVM::GlobalOp lookup(llvm::StringRef name) + { + build(); + + auto it = byName.find(name); + auto *found = it == byName.end() ? nullptr : it->second; + if (!verifyFile.empty()) + { + auto scanned = module.lookupSymbol(name); + if (scanned.getOperation() != found) + { + report("lookup", name); + return scanned; + } + } + + return found ? llvm::cast(found) : mlir::LLVM::GlobalOp(); + } + + // the last global of the kind at the top level of the module, or null + mlir::Operation *last(Kind kind) + { + build(); + + auto *found = lastOf[static_cast(kind)]; + if (!verifyFile.empty()) + { + auto *scanned = scanLast(kind); + if (scanned != found) + { + report("last", kind == Kind::Any ? "any" : kind == Kind::String ? "string" : "dense"); + return scanned; + } + } + + return found; + } + + // call right after the global is created, before anything else is inserted beside it + void created(mlir::LLVM::GlobalOp globalOp) + { + created_++; + + if (!built || globalOp->getParentOp() != module.getOperation()) + { + return; + } + + if (!byName.try_emplace(globalOp.getSymName(), globalOp.getOperation()).second) + { + // a second symbol of the name: the scan's first-one-wins is what a rebuild gives + built = false; + return; + } + + // Where it stands among the others is read off the operation before it, never asked of + // the block's order: inserting again and again at one point invalidates that order, and + // each question would then renumber the block. + auto kind = kindOf(globalOp); + auto *previous = globalOp->getPrevNode(); + auto &lastAny = lastOf[static_cast(Kind::Any)]; + if (previous == lastAny) + { + // after the last global (or the first operation of a module with no global): the + // last of all now, so the last of its kind too + lastAny = globalOp; + if (kind != Kind::Any) + { + lastOf[static_cast(kind)] = globalOp; + } + } + else if (!previous) + { + // the first operation of the module, in front of every other global + if (kind != Kind::Any && !lastOf[static_cast(kind)]) + { + lastOf[static_cast(kind)] = globalOp; + } + } + else if (kind != Kind::Any && previous == lastOf[static_cast(kind)]) + { + // after the last of its kind, which is not the last of all: still in front of that one + lastOf[static_cast(kind)] = globalOp; + } + else + { + // somewhere else: only a scan can tell + built = false; + } + } + + void notifyPatternBegin(const mlir::Pattern &, mlir::Operation *) override + { + createdAtPatternBegin.push_back(created_); + } + + void notifyPatternEnd(const mlir::Pattern &, mlir::LogicalResult status) override + { + auto createdBefore = createdAtPatternBegin.empty() ? 0 : createdAtPatternBegin.pop_back_val(); + if (mlir::failed(status) && createdBefore != created_) + { + // 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; + } + } + + private: + static Kind kindOf(mlir::LLVM::GlobalOp globalOp) + { + auto value = globalOp.getValueAttr(); + if (value && llvm::isa(value)) + { + return Kind::String; + } + + if (value && llvm::isa(value)) + { + return Kind::Dense; + } + + return Kind::Any; + } + + void build() + { + if (built) + { + return; + } + + byName.clear(); + for (auto &last : lastOf) + { + last = nullptr; + } + + for (auto &moduleOp : *module.getBody()) + { + if (auto globalOp = llvm::dyn_cast(&moduleOp)) + { + byName.try_emplace(globalOp.getSymName(), &moduleOp); + lastOf[static_cast(Kind::Any)] = &moduleOp; + auto kind = kindOf(globalOp); + if (kind != Kind::Any) + { + lastOf[static_cast(kind)] = &moduleOp; + } + } + } + + built = true; + } + + mlir::Operation *scanLast(Kind kind) + { + for (auto &moduleOp : llvm::reverse(*module.getBody())) + { + if (auto globalOp = llvm::dyn_cast(&moduleOp)) + { + if (kind == Kind::Any || kindOf(globalOp) == kind) + { + return &moduleOp; + } + } + } + + return nullptr; + } + + void report(llvm::StringRef question, llvm::StringRef subject) + { + std::error_code ec; + llvm::raw_fd_ostream out(verifyFile, ec, llvm::sys::fs::OF_Append); + if (!ec) + { + out << "LLVM globals index: " << question << " '" << subject << "' differs from the scan\n"; + } + } + + mlir::ModuleOp module; + bool built = false; + llvm::StringMap byName; + mlir::Operation *lastOf[static_cast(Kind::Count)] = {}; + // globals reported so far, and the count when each pattern now being applied began + unsigned created_ = 0; + llvm::SmallVector createdAtPatternBegin; + std::string verifyFile; +}; + +} // namespace typescript + +#endif // MLIR_TYPESCRIPT_LOWERTOLLVMLOGIC_LLVMGLOBALSINDEX_H_ diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h index 4a2d5329d..2a337a24c 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h @@ -261,12 +261,12 @@ class LLVMRTTIHelperVCWin32 LogicalResult typeInfo(mlir::Location loc) { auto name = typeInfoExtRef; - if (parentModule.lookupSymbol(name)) + if (ch.lookupGlobal(parentModule, name)) { return failure(); } - rewriter.create(loc, th.getPtrType(), true, LLVM::Linkage::External, name, mlir::Attribute{}); + ch.createGlobal(loc, th.getPtrType(), true, LLVM::Linkage::External, name, mlir::Attribute{}); return success(); } @@ -286,14 +286,14 @@ class LLVMRTTIHelperVCWin32 LogicalResult typeDescriptor(mlir::Location loc, StringRef typeInfoRefName, StringRef typeName) { auto name = typeInfoRefName; - if (parentModule.lookupSymbol(name)) + if (ch.lookupGlobal(parentModule, name)) { return failure(); } auto rttiTypeDescriptor2Ty = getRttiTypeDescriptor2Ty(StringRef(typeName).size()); auto _r0n_Value = - rewriter.create(loc, rttiTypeDescriptor2Ty, false, LLVM::Linkage::LinkonceODR, name, mlir::Attribute{}); + ch.createGlobal(loc, rttiTypeDescriptor2Ty, false, LLVM::Linkage::LinkonceODR, name, mlir::Attribute{}); { ch.setStructWritingPoint(_r0n_Value); @@ -331,12 +331,12 @@ class LLVMRTTIHelperVCWin32 } auto name = imageBaseRef; - if (parentModule.lookupSymbol(name)) + if (ch.lookupGlobal(parentModule, name)) { return failure(); } - rewriter.create(loc, th.getI8Type(), true, LLVM::Linkage::External, name, mlir::Attribute{}); + ch.createGlobal(loc, th.getI8Type(), true, LLVM::Linkage::External, name, mlir::Attribute{}); return success(); } @@ -381,7 +381,7 @@ class LLVMRTTIHelperVCWin32 int catchableTypeSize) { auto name = catchableTypeInfoRefName; - if (parentModule.lookupSymbol(name)) + if (ch.lookupGlobal(parentModule, name)) { return failure(); } @@ -389,7 +389,7 @@ class LLVMRTTIHelperVCWin32 // _CT??_R0N@88 auto ehCatchableTypeTy = getCatchableTypeTy(); auto _ct_r0n_Value = - rewriter.create(loc, ehCatchableTypeTy, true, LLVM::Linkage::LinkonceODR, name, mlir::Attribute{}); + ch.createGlobal(loc, ehCatchableTypeTy, true, LLVM::Linkage::LinkonceODR, name, mlir::Attribute{}); { ch.setStructWritingPoint(_ct_r0n_Value); @@ -441,7 +441,7 @@ class LLVMRTTIHelperVCWin32 LogicalResult catchableArrayType(mlir::Location loc) { auto name = catchableTypeInfoArrayRef; - if (parentModule.lookupSymbol(name)) + if (ch.lookupGlobal(parentModule, name)) { return failure(); } @@ -451,7 +451,7 @@ class LLVMRTTIHelperVCWin32 // _CT??_R0N@88 auto ehCatchableArrayTypeTy = getCatchableArrayTypeTy(arraySize); auto _cta1nValue = - rewriter.create(loc, ehCatchableArrayTypeTy, true, LLVM::Linkage::LinkonceODR, name, mlir::Attribute{}); + ch.createGlobal(loc, ehCatchableArrayTypeTy, true, LLVM::Linkage::LinkonceODR, name, mlir::Attribute{}); { ch.setStructWritingPoint(_cta1nValue); @@ -493,13 +493,13 @@ class LLVMRTTIHelperVCWin32 LogicalResult throwInfo(mlir::Location loc) { auto name = throwInfoRef; - if (parentModule.lookupSymbol(name)) + if (ch.lookupGlobal(parentModule, name)) { return failure(); } auto throwInfoTy = getThrowInfoTy(); - auto _TI1NValue = rewriter.create(loc, throwInfoTy, true, LLVM::Linkage::LinkonceODR, name, mlir::Attribute{}); + auto _TI1NValue = ch.createGlobal(loc, throwInfoTy, true, LLVM::Linkage::LinkonceODR, name, mlir::Attribute{}); // Throw Info ch.setStructWritingPoint(_TI1NValue); diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 9a2fcafb7..00b847165 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -3307,8 +3307,9 @@ static LLVM::LLVMFuncOp getOrCreateRemapVTableFunction(Operation *op, Conversion rewriter.setInsertionPointToStart(parentModule.getBody()); // the first vtable built; each starts with the next one and its slot count: {next, count, slots...} - auto list = rewriter.create(loc, ptrType, /*isConstant=*/false, LLVM::Linkage::Internal, listName, - mlir::Attribute{}); + LLVMCodeHelper globals(op, rewriter, typeConverter, compileOptions); + auto list = globals.createGlobal(loc, ptrType, /*isConstant=*/false, LLVM::Linkage::Internal, listName, + mlir::Attribute{}); { OpBuilder::InsertionGuard initGuard(rewriter); rewriter.setInsertionPointToStart(rewriter.createBlock(&list.getInitializerRegion())); @@ -3474,7 +3475,8 @@ static mlir::Value remapInterfaceVTable(Operation *op, mlir::Value interfaceValu // the map, one constant per content auto mapName = "ts_vtable_map_" + std::to_string((size_t)llvm::hash_combine_range(vtableMap.begin(), vtableMap.end())); auto parentModule = op->getParentOfType(); - if (!parentModule.lookupSymbol(mapName)) + LLVMCodeHelper globals(op, rewriter, typeConverter, compileOptions); + if (!globals.lookupGlobal(parentModule, mapName)) { OpBuilder::InsertionGuard insertGuard(rewriter); rewriter.setInsertionPointToStart(parentModule.getBody()); @@ -3482,7 +3484,7 @@ static mlir::Value remapInterfaceVTable(Operation *op, mlir::Value interfaceValu auto mapType = LLVM::LLVMArrayType::get(i32Type, vtableMap.size()); auto mapValue = mlir::DenseIntElementsAttr::get( mlir::RankedTensorType::get({(int64_t)vtableMap.size()}, i32Type), vtableMap); - rewriter.create(loc, mapType, /*isConstant=*/true, LLVM::Linkage::Internal, mapName, mapValue); + globals.createGlobal(loc, mapType, /*isConstant=*/true, LLVM::Linkage::Internal, mapName, mapValue); } mlir::Value vtable = rewriter.create(loc, interfaceValue, ArrayRef{DATA_VALUE_INDEX}); @@ -6030,7 +6032,7 @@ struct AddressOfOpLowering : public TsLlvmPattern // load type from symbol auto module = addressOfOp->getParentOfType(); assert(module); - auto globalOp = module.lookupSymbol(addressOfOp.getGlobalName()); + auto globalOp = lch.lookupGlobal(module, addressOfOp.getGlobalName()); if (!globalOp) { LLVM_DEBUG(llvm::dbgs() << "\n!! NOT found symbol: " << addressOfOp.getGlobalName() << "\n";); @@ -9944,7 +9946,17 @@ void TypeScriptToLLVMLoweringPass::runOnOperation() } } - if (failed(applyFullConversion(m, target, std::move(patterns)))) + // The LLVM globals of the module, for the helpers that ask whether one exists and where the + // next one goes (LLVMGlobalsIndex.h). It listens to the conversion, to forget what a failed + // pattern created. + LLVMGlobalsIndex globalsIndex(m); + ConversionConfig conversionConfig; + conversionConfig.listener = &globalsIndex; + tsContext.compileOptions.llvmGlobalsIndex = &globalsIndex; + auto converted = applyFullConversion(m, target, std::move(patterns), conversionConfig); + tsContext.compileOptions.llvmGlobalsIndex = nullptr; + + if (failed(converted)) { signalPassFailure(); }