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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tslang/include/TypeScript/DataStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
8 changes: 4 additions & 4 deletions tslang/include/TypeScript/LowerToLLVM/ArrayLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class ArrayLayout : public LLVMCodeHelperBase
auto withWord = compileOptions.tracksOwnership();
auto headerName = ("ah_" + dataGlobalName).str();

auto global = parentModule.lookupSymbol<LLVM::GlobalOp>(headerName);
auto global = lookupGlobal(parentModule, headerName);
if (!global)
{
OpBuilder::InsertionGuard guard(rewriter);
Expand All @@ -313,7 +313,7 @@ class ArrayLayout : public LLVMCodeHelperBase
/*isPacked=*/true);
}

global = rewriter.create<LLVM::GlobalOp>(loc, globalType, /*isConstant=*/true, LLVM::Linkage::Internal,
global = createGlobal(loc, globalType, /*isConstant=*/true, LLVM::Linkage::Internal,
headerName, mlir::Attribute{});
global.setAlignment(getHeapBlockHeaderSize());

Expand Down Expand Up @@ -389,11 +389,11 @@ class ArrayLayout : public LLVMCodeHelperBase
TypeHelper th(rewriter);
auto loc = op->getLoc();
auto parentModule = op->getParentOfType<mlir::ModuleOp>();
if (!parentModule.lookupSymbol<LLVM::GlobalOp>(name))
if (!lookupGlobal(parentModule, name))
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());
auto global = rewriter.create<LLVM::GlobalOp>(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<LLVM::ZeroOp>(rewriter.getUnknownLoc(), headerType());
Expand Down
32 changes: 16 additions & 16 deletions tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ class LLVMCodeHelper : public LLVMCodeHelperBase

// Create the global at the entry of the module.
LLVM::GlobalOp global;
if (!(global = parentModule.lookupSymbol<LLVM::GlobalOp>(name)))
if (!(global = lookupGlobal(parentModule, name)))
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());

seekLast(parentModule.getBody());

global = rewriter.create<LLVM::GlobalOp>(loc, type, isConst, linkage, name, value);
global = createGlobal(loc, type, isConst, linkage, name, value);

{
setStructWritingPoint(global);
Expand Down Expand Up @@ -116,7 +116,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase
TypeHelper th(rewriter);

// Create the global at the entry of the module.
if (!(global = parentModule.lookupSymbol<LLVM::GlobalOp>(name)))
if (!(global = lookupGlobal(parentModule, name)))
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());
Expand All @@ -129,7 +129,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase
effectiveValue = rewriter.getArrayAttr({effectiveValue});
}

global = rewriter.create<LLVM::GlobalOp>(loc, type, isConst, linkage, name, effectiveValue);
global = createGlobal(loc, type, isConst, linkage, name, effectiveValue);
if (section.size() > 0)
{
global.setSection(section);
Expand Down Expand Up @@ -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<LLVM::GlobalOp>(name)))
if (!(global = lookupGlobal(parentModule, name)))
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());
Expand All @@ -180,7 +180,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase
TypeHelper th(rewriter);
auto arrayPtrType = LLVM::LLVMArrayType::get(ptrType, 1);

global = rewriter.create<LLVM::GlobalOp>(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);
Expand Down Expand Up @@ -310,15 +310,15 @@ class LLVMCodeHelper : public LLVMCodeHelperBase
TypeHelper th(rewriter);

// Create the global at the entry of the module.
LLVM::GlobalOp global = parentModule.lookupSymbol<LLVM::GlobalOp>(name);
LLVM::GlobalOp global = lookupGlobal(parentModule, name);
if (!global)
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());

seekLast(parentModule.getBody());

global = rewriter.create<LLVM::GlobalOp>(loc, type, true, linkage, name, mlir::Attribute());
global = createGlobal(loc, type, true, linkage, name, mlir::Attribute());

{
setStructWritingPoint(global);
Expand Down Expand Up @@ -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<LLVM::GlobalOp>(varName.str())))
if (!(global = lookupGlobal(parentModule, varName.str())))
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());

seekLast(parentModule.getBody());

global = rewriter.create<LLVM::GlobalOp>(loc, descriptorType, true, LLVM::Linkage::Internal, varName.str(),
global = createGlobal(loc, descriptorType, true, LLVM::Linkage::Internal, varName.str(),
mlir::Attribute{});

setStructWritingPoint(global);
Expand Down Expand Up @@ -571,7 +571,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase

// Create the global at the entry of the module.
LLVM::GlobalOp global;
if (!(global = parentModule.lookupSymbol<LLVM::GlobalOp>(name)))
if (!(global = lookupGlobal(parentModule, name)))
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());
Expand Down Expand Up @@ -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<LLVM::GlobalOp>(loc, blockType, true, LLVM::Linkage::Internal, name, mlir::Attribute{});
global = createGlobal(loc, blockType, true, LLVM::Linkage::Internal, name, mlir::Attribute{});

setStructWritingPoint(global);

Expand All @@ -650,15 +650,15 @@ class LLVMCodeHelper : public LLVMCodeHelperBase
{
seekLast<DenseElementsAttr>(parentModule.getBody());

global = rewriter.create<LLVM::GlobalOp>(loc, /*arrayType*/dataType, true, LLVM::Linkage::Internal, name, denseAttr);
global = createGlobal(loc, /*arrayType*/dataType, true, LLVM::Linkage::Internal, name, denseAttr);
}
else
{
seekLast(parentModule.getBody());

OpBuilder::InsertionGuard guard(rewriter);

global = rewriter.create<LLVM::GlobalOp>(loc, arrayType, true, LLVM::Linkage::Internal, name, mlir::Attribute{});
global = createGlobal(loc, arrayType, true, LLVM::Linkage::Internal, name, mlir::Attribute{});

setStructWritingPoint(global);

Expand Down Expand Up @@ -843,14 +843,14 @@ class LLVMCodeHelper : public LLVMCodeHelperBase

// Create the global at the entry of the module.
LLVM::GlobalOp global;
if (!(global = parentModule.lookupSymbol<LLVM::GlobalOp>(name)))
if (!(global = lookupGlobal(parentModule, name)))
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());

seekLast(parentModule.getBody());

global = rewriter.create<LLVM::GlobalOp>(loc, llvmStructType, true, LLVM::Linkage::Internal, name, mlir::Attribute{});
global = createGlobal(loc, llvmStructType, true, LLVM::Linkage::Internal, name, mlir::Attribute{});

setStructWritingPoint(global);

Expand Down
102 changes: 76 additions & 26 deletions tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelperBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type_traits>

using namespace mlir;
namespace mlir_ts = mlir::typescript;

Expand Down Expand Up @@ -36,63 +39,110 @@ class LLVMCodeHelperBase
{
}

template <typename T>
// 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.
//
// 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<LLVM::GlobalOp>(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<LLVM::GlobalOp>(name);
}

// rewriter.create<LLVM::GlobalOp>(...), told to the index
template <typename... Args> LLVM::GlobalOp createGlobal(Args &&...args)
{
auto globalOp = rewriter.create<LLVM::GlobalOp>(std::forward<Args>(args)...);
if (auto *index = LLVMGlobalsIndex::of(compileOptions))
{
index->created(globalOp);
}

return globalOp;
}

template <typename T>
void seekLast(mlir::Block *block)
{
auto *index = LLVMGlobalsIndex::of(compileOptions);
if (index && index->isFor(block) && (std::is_same_v<T, StringAttr> || std::is_same_v<T, DenseElementsAttr>))
{
auto kind = std::is_same_v<T, StringAttr> ? LLVMGlobalsIndex::Kind::String : LLVMGlobalsIndex::Kind::Dense;
if (auto *last = index->last(kind))
{
rewriter.setInsertionPointAfter(last);
}

return;
}

// find last string
auto lastUse = [&](Operation *op) {
if (auto globalOp = dyn_cast_or_null<LLVM::GlobalOp>(op))
for (auto &blockOp : llvm::reverse(*block))
{
if (auto globalOp = dyn_cast<LLVM::GlobalOp>(&blockOp))
{
if (globalOp.getValueAttr() && isa<T>(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<LLVM::GlobalOp>(op))
auto *index = LLVMGlobalsIndex::of(compileOptions);
if (index && index->isFor(block))
{
if (auto *last = index->last(LLVMGlobalsIndex::Kind::Any))
{
rewriter.setInsertionPointAfter(globalOp);
rewriter.setInsertionPointAfter(last);
}
};

block->walk(lastUse);
return;
}

seekLastOp<LLVM::GlobalOp>(block);
}

void seekLastWithBody(mlir::Block *block)
{
// find last string
auto lastUse = [&](Operation *op) {
if (auto globalOp = dyn_cast_or_null<LLVM::GlobalOp>(op))
for (auto &blockOp : llvm::reverse(*block))
{
if (auto globalOp = dyn_cast<LLVM::GlobalOp>(&blockOp))
{
if (globalOp.getInitializerBlock())
{
rewriter.setInsertionPointAfter(globalOp);
return;
}
}
};

block->walk(lastUse);
}
}

template <typename T> void seekLastOp(mlir::Block *block)
{
// find last string
auto lastUse = [&](Operation *op) {
if (auto opT = dyn_cast_or_null<T>(op))
for (auto &blockOp : llvm::reverse(*block))
{
if (auto opT = dyn_cast<T>(&blockOp))
{
rewriter.setInsertionPointAfter(opT);
return;
}
};

block->walk(lastUse);
}
}

template <typename T> Operation *seekFirstNonConstantOp(T funcOp)
Expand Down Expand Up @@ -156,15 +206,15 @@ class LLVMCodeHelperBase

// Create the global at the entry of the module.
LLVM::GlobalOp global;
if (!(global = parentModule.lookupSymbol<LLVM::GlobalOp>(name)))
if (!(global = lookupGlobal(parentModule, name)))
{
OpBuilder::InsertionGuard insertGuard(rewriter);
rewriter.setInsertionPointToStart(parentModule.getBody());

seekLast<StringAttr>(parentModule.getBody());

auto type = th.getArrayType(th.getI8Type(), blockBytes.size());
global = rewriter.create<LLVM::GlobalOp>(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);
}
Expand Down
Loading
Loading