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
5 changes: 5 additions & 0 deletions docs/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ __jit/hello.ts.<hash>.o the object
__jit/hello.ts.<hash>.o.deps what it was compiled from
```

The files that have to be compiled are compiled at the same time, one thread each: after the
program's own file, whose compile finds every module it reaches, all of them start together.

- `--jit-cache-dir=<folder>` keeps all the objects in one folder instead;
- `--jit-jobs=<n>` compiles at most `n` files at the same time (default: the number of hardware
threads, at most 12; `--jit-jobs=1` compiles them one after another);
- `--jit-cache=false` turns the cache off: everything is compiled into one module on every run.

A program whose imports form a cycle is compiled into one object. Its modules can't be compiled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace typescript

static llvm::LLVMContext &getGlobalContext()
{
static llvm::LLVMContext GlobalContext;
// one per thread: the JIT cache compiles several files at the same time, and an LLVMContext is not shared between threads
static thread_local llvm::LLVMContext GlobalContext;
return GlobalContext;
}

Expand Down
9 changes: 6 additions & 3 deletions tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ class MLIRCustomMethods
static bool isInternalObjectName (StringRef objectName)
{
static std::map<std::string, bool> o { {"Symbol", true}};
return o[objectName.str()];
auto it = o.find(objectName.str());
return it != o.end() && it->second;
}

static bool isInternalFunctionName (CompileOptions &compileOptions, StringRef functionName)
Expand All @@ -422,7 +423,8 @@ class MLIRCustomMethods
{"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }, { "ReferenceOf", true }, { "Deref", true }, { "Ref", true },
{"atomicrmw", true}, {"cmpxchg", true}, {"atomicload", true}, {"atomicstore", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true}, {"gpuLaunch", true}, {"gpuReduce", true}, {"gpuMin", true}, {"gpuMax", true}, {"gpuBatch", true}, {"gpuThreadIndex", true}, {"gpuBlockIndex", true}, {"gpuBlockSize", true}, {"gpuBarrier", true}, {"gpuShared", true}, {"gpuLocal", true}, {"gpuSync", true}, {"gpuSeed", true}, {"gpuScan", true}, {"gpuSort", true}, {"gpuTime", true}, {"gpuArray", true}, {"gpuRead", true}, {"gpuWrite", true}, {"gpuFree", true}, {"gpuLaneIndex", true}, {"gpuWarpSize", true}, {"gpuShuffle", true}, {"gpuShuffleDown", true}, {"gpuShuffleUp", true}, {"gpuShuffleXor", true}, {"gpuBallot", true}, {"gpuAll", true}, {"gpuAny", true}, {".gpuBack", true}
};
return m[functionName.str()];
auto it = m.find(functionName.str());
return it != m.end() && it->second;
}

static bool isInternalFunctionNameNoBuiltin (StringRef functionName)
Expand All @@ -432,7 +434,8 @@ class MLIRCustomMethods
{"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }, { "ReferenceOf", true }, { "Deref", true }, { "Ref", true },
{"atomicrmw", true}, {"cmpxchg", true}, {"atomicload", true}, {"atomicstore", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true}, {"gpuLaunch", true}, {"gpuReduce", true}, {"gpuMin", true}, {"gpuMax", true}, {"gpuBatch", true}, {"gpuThreadIndex", true}, {"gpuBlockIndex", true}, {"gpuBlockSize", true}, {"gpuBarrier", true}, {"gpuShared", true}, {"gpuLocal", true}, {"gpuSync", true}, {"gpuSeed", true}, {"gpuScan", true}, {"gpuSort", true}, {"gpuTime", true}, {"gpuArray", true}, {"gpuRead", true}, {"gpuWrite", true}, {"gpuFree", true}, {"gpuLaneIndex", true}, {"gpuWarpSize", true}, {"gpuShuffle", true}, {"gpuShuffleDown", true}, {"gpuShuffleUp", true}, {"gpuShuffleXor", true}, {"gpuBallot", true}, {"gpuAll", true}, {"gpuAny", true}, {".gpuBack", true}
};
return m[functionName.str()];
auto it = m.find(functionName.str());
return it != m.end() && it->second;
}

ValueOrLogicalResult callMethod(StringRef functionName, mlir::SmallVector<mlir::Type> typeArgs, ArrayRef<mlir::Value> operands, std::function<ValueOrLogicalResult(mlir::Location, mlir::Type, mlir::Value, const GenContext &, bool)> castFn, const GenContext &genContext)
Expand Down
17 changes: 4 additions & 13 deletions tslang/include/TypeScript/VisitorAST.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,21 @@ class VisitorASTBase
public:
void visit(Node node)
{
// No callback for arrays: forEachChild then walks a node's arrays where they are, where one
// made a NodeArray<Node> copy of every array it passed - the copies were the most of a walk
ts::FuncT<> visitNode;
ts::ArrayFuncT<> visitArray;

visitNode = [&](ts::Node child) -> ts::Node {
if (isFiltered(child))
{
return undefined;
}

visitTree(child);
ts::forEachChild(child, visitNode, visitArray);
return undefined;
};

visitArray = [&](ts::NodeArray<ts::Node> array) -> ts::Node {
for (auto node : array)
{
visitNode(node);
}

ts::forEachChild(child, visitNode);
return undefined;
};

auto result = ts::forEachChild(node, visitNode, visitArray);
ts::forEachChild(node, visitNode);
}
};

Expand Down
34 changes: 29 additions & 5 deletions tslang/lib/TypeScript/MLIRGen.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
#include "MLIRGenImpl.h"

#include "llvm/ADT/DenseMap.h"

#include <atomic>
#include <shared_mutex>


// TODO: optimize of amount of calls to detect return types and if it is was calculated before then do not run it all
// the time
static CompileOptions *compileOptionsPtr = nullptr;
CompileOptions& getCompileOptions()
//
// The options a module is compiled with, by its context: an op's verifier has only its context to go by.
// The JIT cache compiles several files at the same time, each in a context of its own and with
// options of its own, so one pointer for the process would give a file the options of another -
// options that may be gone by then.
static std::shared_mutex compileOptionsMutex;
static llvm::DenseMap<mlir::MLIRContext *, CompileOptions *> compileOptionsByContext;
static std::atomic<CompileOptions *> lastCompileOptions{nullptr};

CompileOptions &getCompileOptions(mlir::MLIRContext *context)
{
return *compileOptionsPtr;
{
std::shared_lock<std::shared_mutex> lock(compileOptionsMutex);
auto it = compileOptionsByContext.find(context);
if (it != compileOptionsByContext.end())
{
return *it->second;
}
}

return *lastCompileOptions.load();
}

void setCompileOptions(CompileOptions &compileOptions)
void setCompileOptions(mlir::MLIRContext *context, CompileOptions &compileOptions)
{
compileOptionsPtr = &compileOptions;
std::unique_lock<std::shared_mutex> lock(compileOptionsMutex);
compileOptionsByContext[context] = &compileOptions;
lastCompileOptions = &compileOptions;
}

SourceMgrDiagnosticHandlerEx::SourceMgrDiagnosticHandlerEx(llvm::SourceMgr &mgr, mlir::MLIRContext *ctx) : mlir::SourceMgrDiagnosticHandler(mgr, ctx)
Expand Down
3 changes: 2 additions & 1 deletion tslang/lib/TypeScript/MLIRGenGenerics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2123,7 +2123,8 @@ namespace mlirgen
return {mlir::success(), deferredType};
}

static auto count = 0;
// how deep the specializations go on this thread: the JIT cache compiles files on several
static thread_local auto count = 0;
count++;
if (count > 99)
{
Expand Down
18 changes: 9 additions & 9 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ using llvm::Twine;
using DITableScopeT = llvm::ScopedHashTableScope<StringRef, mlir::LLVM::DIScopeAttr>;

// Defined in MLIRGen.cpp; also used by TypeScriptOps.cpp.
CompileOptions &getCompileOptions();
void setCompileOptions(CompileOptions &compileOptions);
CompileOptions &getCompileOptions(mlir::MLIRContext *context);
void setCompileOptions(mlir::MLIRContext *context, CompileOptions &compileOptions);

namespace typescript
{
Expand Down Expand Up @@ -178,7 +178,7 @@ class MLIRGenImpl
overwriteLoc(mlir::UnknownLoc::get(builder.getContext())),
generatedNodesAnchor(mlir::UnknownLoc::get(builder.getContext()))
{
setCompileOptions(compileOptions);
setCompileOptions(builder.getContext(), compileOptions);

rootNamespace = currentNamespace = std::make_shared<NamespaceInfo>();

Expand Down Expand Up @@ -4825,7 +4825,7 @@ class MLIRGenImpl
{DLL_EXPORT, true },
};

return funcAttrs[name];
return funcAttrs.lookup(name);
}

bool isFuncAttr(StringRef name)
Expand All @@ -4837,7 +4837,7 @@ class MLIRGenImpl
{DLL_EXPORT, true },
};

return funcAttrs[name];
return funcAttrs.lookup(name);
}

void processFunctionAttributes(SmallVector<mlir::NamedAttribute> &attrs, const GenContext &genContext)
Expand Down Expand Up @@ -13254,12 +13254,12 @@ class MLIRGenImpl
{
ss << S("if (r instanceof TYPE_INST_RIGHT_ALIAS");
ss << index;
ss << S(") return ") << S("l ") << Scanner::tokenStrings[opCode] << S(" r;\n");
ss << S(") return ") << S("l ") << lookupOrDefault(Scanner::tokenStrings, opCode) << S(" r;\n");
}
ss << S("}\n");
}
else
ss << S("return ") << S("l ") << Scanner::tokenStrings[opCode] << S(" r;\n");
ss << S("return ") << S("l ") << lookupOrDefault(Scanner::tokenStrings, opCode) << S(" r;\n");
}
};

Expand Down Expand Up @@ -13536,12 +13536,12 @@ class MLIRGenImpl
if (typeOfName == "class")
{
ss << S("{ if (l instanceof TYPE_INST_ALIAS") << classInstances.size() << S(") return l ")
<< Scanner::tokenStrings[opCode] << S(" r; }\n");
<< lookupOrDefault(Scanner::tokenStrings, opCode) << S(" r; }\n");
classInstances.push_back(member);
}
else
{
ss << S("return l ") << Scanner::tokenStrings[opCode] << S(" r;\n");
ss << S("return l ") << lookupOrDefault(Scanner::tokenStrings, opCode) << S(" r;\n");
}
}

Expand Down
6 changes: 2 additions & 4 deletions tslang/lib/TypeScript/MLIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,7 @@ namespace mlirgen
{"Record", true },
};

auto type = embeddedTypes[name];
return type;
return embeddedTypes.lookup(name);
}

bool MLIRGenImpl::isEmbededTypeWithNoBuiltins(mlir::StringRef name)
Expand Down Expand Up @@ -1151,8 +1150,7 @@ namespace mlirgen
//{"Array", true }
};

auto type = embeddedTypes[name];
return type;
return embeddedTypes.lookup(name);
}

mlir::Type MLIRGenImpl::getEmbeddedType(mlir::StringRef name)
Expand Down
75 changes: 41 additions & 34 deletions tslang/lib/TypeScript/MLIRGenVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "MLIRGenImpl.h"

#include <unordered_set>

namespace typescript
{
namespace mlirgen
Expand Down Expand Up @@ -2654,6 +2656,39 @@ namespace mlirgen
if (body == SyntaxKind::Block)
{
auto &statements = body.as<ts::Block>()->statements;

// The names the statements before the one at hand use - anywhere, and inside a function - gathered once
// as the loop moves on: walking every earlier statement again for each declaration took a body of n
// declarations n * n walks.
std::unordered_set<std::wstring> namesBefore;
std::unordered_set<std::wstring> namesInFunctionsBefore;
size_t namesGathered = 0;
auto gatherNamesBefore = [&](size_t index) {
for (; namesGathered < index; namesGathered++)
{
ts::VisitorAST names([&](ts::Node node) {
if (node == SyntaxKind::Identifier)
{
namesBefore.insert(node.as<Identifier>()->escapedText);
}

if (node == SyntaxKind::ArrowFunction || node == SyntaxKind::FunctionExpression
|| node == SyntaxKind::FunctionDeclaration || node == SyntaxKind::MethodDeclaration
|| node == SyntaxKind::GetAccessor || node == SyntaxKind::SetAccessor)
{
ts::VisitorAST inFunction([&](ts::Node inner) {
if (inner == SyntaxKind::Identifier)
{
namesInFunctionsBefore.insert(inner.as<Identifier>()->escapedText);
}
});
inFunction.visit(node);
}
});
names.visit(statements[namesGathered]);
}
};

for (auto [index, statement] : enumerate(statements))
{
if (statement != SyntaxKind::VariableStatement)
Expand Down Expand Up @@ -2696,20 +2731,8 @@ namespace mlirgen
continue;
}

auto name = declaration->name.as<Identifier>()->escapedText;
auto usedBefore = false;
for (size_t earlier = 0; earlier < index && !usedBefore; earlier++)
{
ts::VisitorAST names([&](ts::Node inner) {
if (inner == SyntaxKind::Identifier && inner.as<Identifier>()->escapedText == name)
{
usedBefore = true;
}
});
names.visit(statements[earlier]);
}

if (usedBefore)
gatherNamesBefore(index);
if (namesBefore.count(declaration->name.as<Identifier>()->escapedText))
{
forwardReferenced.insert(MLIRHelper::getName(declaration->name));
}
Expand All @@ -2731,27 +2754,11 @@ namespace mlirgen
continue;
}

auto name = declaration->name.as<Identifier>()->escapedText;
for (size_t earlier = 0; earlier < index && !readBefore; earlier++)
gatherNamesBefore(index);
if (namesInFunctionsBefore.count(declaration->name.as<Identifier>()->escapedText))
{
ts::VisitorAST functions([&](ts::Node node) {
if (readBefore
|| !(node == SyntaxKind::ArrowFunction || node == SyntaxKind::FunctionExpression
|| node == SyntaxKind::FunctionDeclaration || node == SyntaxKind::MethodDeclaration
|| node == SyntaxKind::GetAccessor || node == SyntaxKind::SetAccessor))
{
return;
}

ts::VisitorAST names([&](ts::Node inner) {
if (inner == SyntaxKind::Identifier && inner.as<Identifier>()->escapedText == name)
{
readBefore = true;
}
});
names.visit(node);
});
functions.visit(statements[earlier]);
readBefore = true;
break;
}
}

Expand Down
14 changes: 7 additions & 7 deletions tslang/lib/TypeScript/TypeScriptOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using namespace mlir;
namespace mlir_ts = mlir::typescript;

CompileOptions& getCompileOptions();
CompileOptions &getCompileOptions(mlir::MLIRContext *context);

/// Default callback for IfOp builders. Inserts a yield without arguments.
void mlir_ts::buildTerminatedBody(OpBuilder &builder, Location loc)
Expand Down Expand Up @@ -630,7 +630,7 @@ LogicalResult mlir_ts::CastOp::verify()
auto resFuncType = dyn_cast<mlir_ts::FunctionType>(resType);
if (inFuncType && resFuncType)
{
::typescript::MLIRTypeHelper mth(getContext(), getCompileOptions());
::typescript::MLIRTypeHelper mth(getContext(), getCompileOptions(getContext()));
auto result = mth.TestFunctionTypesMatchWithObjectMethods(getLoc(), inFuncType, resFuncType);
if (::typescript::MatchResultType::Match != result.result)
{
Expand Down Expand Up @@ -669,7 +669,7 @@ LogicalResult mlir_ts::CastOp::verify()
auto resUnionType = dyn_cast<mlir_ts::UnionType>(resType);
if (inUnionType || resUnionType)
{
::typescript::MLIRTypeHelper mth(getContext(), getCompileOptions());
::typescript::MLIRTypeHelper mth(getContext(), getCompileOptions(getContext()));
auto cmpTypes = [&](mlir::Type t1, mlir::Type t2) { return mth.canCastFromTo(getLoc(), t1, t2); };

if (inUnionType && !resUnionType)
Expand All @@ -680,7 +680,7 @@ LogicalResult mlir_ts::CastOp::verify()
auto types = inUnionType.getTypes();
if (!std::all_of(types.begin(), types.end(), pred))
{
::typescript::MLIRTypeHelper mth(getContext(), getCompileOptions());
::typescript::MLIRTypeHelper mth(getContext(), getCompileOptions(getContext()));
mlir::Type baseType;
if (!mth.isUnionTypeNeedsTag(getLoc(), inUnionType, baseType)/* && mth.canCastFromTo(baseType, resType)*/)
{
Expand All @@ -696,7 +696,7 @@ LogicalResult mlir_ts::CastOp::verify()

if (!inUnionType && resUnionType)
{
::typescript::MLIRTypeHelper mth(getContext(), getCompileOptions());
::typescript::MLIRTypeHelper mth(getContext(), getCompileOptions(getContext()));
mlir::Type baseType;
if (!mth.isUnionTypeNeedsTag(getLoc(), resUnionType, baseType)/* && mth.canCastFromTo(baseType, resType)*/)
{
Expand Down Expand Up @@ -805,7 +805,7 @@ struct NormalizeCast : public OpRewritePattern<mlir_ts::CastOp>
auto inUnionType = dyn_cast<mlir_ts::UnionType>(in.getType());
if (resUnionType && !inUnionType)
{
::typescript::MLIRTypeHelper mth(rewriter.getContext(), getCompileOptions());
::typescript::MLIRTypeHelper mth(rewriter.getContext(), getCompileOptions(rewriter.getContext()));
if (mth.isUnionTypeNeedsTag(location, resUnionType))
{
// TODO: boxing, finish it, need to send TypeOf
Expand All @@ -821,7 +821,7 @@ struct NormalizeCast : public OpRewritePattern<mlir_ts::CastOp>
// TODO: review it, if you still need it as we are should be using "safeCast"
if (inUnionType && !resUnionType)
{
::typescript::MLIRTypeHelper mth(rewriter.getContext(), getCompileOptions());
::typescript::MLIRTypeHelper mth(rewriter.getContext(), getCompileOptions(rewriter.getContext()));
if (mth.isUnionTypeNeedsTag(location, inUnionType))
{
auto value = rewriter.create<mlir_ts::GetValueFromUnionOp>(loc, res.getType(), in);
Expand Down
Loading
Loading