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
1 change: 1 addition & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
#define SHARED_LIB_MEMORY_MODEL "__tsmm_"
#define DLL_EXPORT "dllexport"
#define DLL_IMPORT "dllimport"
#define DLL_NAME "dllname"

#if __LP64__
#define TRAMPOLINE_SIZE 48
Expand Down
6 changes: 3 additions & 3 deletions tslang/include/TypeScript/MLIRLogic/MLIRDeclarationPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace typescript

void printTypeDeclaration(StringRef, NamespaceInfo::TypePtr, mlir::Type);
void printEnum(StringRef, NamespaceInfo::TypePtr, mlir::DictionaryAttr);
void printVariableDeclaration(StringRef, NamespaceInfo::TypePtr, mlir::Type, bool);
void print(StringRef, NamespaceInfo::TypePtr, mlir_ts::FunctionType);
void printVariableDeclaration(StringRef, NamespaceInfo::TypePtr, mlir::Type, bool, StringRef = StringRef());
void print(StringRef, NamespaceInfo::TypePtr, mlir_ts::FunctionType, StringRef = StringRef());
void print(ClassInfo::TypePtr);
void print(InterfaceInfo::TypePtr);
void printGenericClass(NamespaceInfo::TypePtr, StringRef);
Expand All @@ -35,7 +35,7 @@ namespace typescript
void printParams(ArrayRef<mlir::Type>, mlir::Type);
void printFunction(StringRef, ArrayRef<mlir::Type>, mlir::Type);
void printMethod(bool, StringRef, ArrayRef<mlir::Type>, mlir::Type, mlir::Type);
void printAccessor(bool, StringRef, StringRef, mlir_ts::AccessLevel, ArrayRef<mlir::Type>, mlir::Type, mlir::Type);
void printAccessor(bool, StringRef, StringRef, mlir_ts::AccessLevel, ArrayRef<mlir::Type>, mlir::Type, mlir::Type, StringRef);
void printIndexer(mlir::Type, mlir::Type);
void printNamespaceBegin(NamespaceInfo::TypePtr);
void printNamespaceEnd(NamespaceInfo::TypePtr);
Expand Down
6 changes: 6 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ struct VariableClass
// DeclarationPrinter.cpp's printVariableDeclaration and
// MLIRGenVariables.cpp's isDynamicImport load branch.
bool isBoxed;
// @dllname: exported symbol name, empty when the variable keeps its own name
StringRef dllName;

inline VariableClass& operator=(VariableType type_) { type = type_; return *this; }

Expand All @@ -124,6 +126,8 @@ struct FunctionEntry
{
std::string name;
mlir_ts::FunctionType funcType;
// @dllname: exported symbol name, empty when the function keeps name
std::string dllName;

explicit operator bool() const
{
Expand All @@ -144,6 +148,8 @@ struct MethodInfo
int virtualIndex;
int orderWeight;
mlir_ts::AccessLevel accessLevel;
// @dllname: exported symbol name, empty when the method keeps funcName
std::string dllName;
};

struct GenericMethodInfo
Expand Down
38 changes: 33 additions & 5 deletions tslang/lib/TypeScript/DeclarationPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,15 @@ namespace typescript
}

void MLIRDeclarationPrinter::printAccessor(bool isStatic, StringRef keyword, StringRef name, mlir_ts::AccessLevel accessLevel,
ArrayRef<mlir::Type> params, mlir::Type returnType, mlir::Type thisType)
ArrayRef<mlir::Type> params, mlir::Type returnType, mlir::Type thisType, StringRef dllName)
{
if (!dllName.empty())
{
os.indent(4);
os << "@dllname(\"" << dllName << "\")";
newline();
}

os.indent(4);

if (accessLevel == mlir_ts::AccessLevel::Protected)
Expand Down Expand Up @@ -347,12 +354,18 @@ namespace typescript
printNamespaceEnd(elementNamespace);
}

void MLIRDeclarationPrinter::printVariableDeclaration(StringRef name, NamespaceInfo::TypePtr elementNamespace, mlir::Type type, bool isConst)
void MLIRDeclarationPrinter::printVariableDeclaration(StringRef name, NamespaceInfo::TypePtr elementNamespace, mlir::Type type, bool isConst, StringRef dllName)
{
printNamespaceBegin(elementNamespace);

printBeforeDeclaration();

if (!dllName.empty())
{
os << "@dllname(\"" << dllName << "\")";
newline();
}

// no TS source syntax expresses "this symbol's storage is a single
// boxed pointer to the real data, not the data inline" - an inferred
// (untyped) object-literal export is boxed as ObjectType, but its
Expand Down Expand Up @@ -387,12 +400,18 @@ namespace typescript
printNamespaceEnd(elementNamespace);
}

void MLIRDeclarationPrinter::print(StringRef name, NamespaceInfo::TypePtr elementNamespace, mlir_ts::FunctionType funcType)
void MLIRDeclarationPrinter::print(StringRef name, NamespaceInfo::TypePtr elementNamespace, mlir_ts::FunctionType funcType, StringRef dllName)
{
printNamespaceBegin(elementNamespace);

printBeforeDeclaration();

if (!dllName.empty())
{
os << "@dllname(\"" << dllName << "\")";
newline();
}

printFunction(
name,
funcType.getParams(),
Expand Down Expand Up @@ -561,6 +580,13 @@ namespace typescript
continue;
}

if (!method.dllName.empty())
{
os.indent(4);
os << "@dllname(\"" << method.dllName << "\")";
newline();
}

os.indent(4);

if (method.accessLevel == mlir_ts::AccessLevel::Protected)
Expand Down Expand Up @@ -599,7 +625,8 @@ namespace typescript
accessor.isStatic, "get", accessor.name, accessor.getAccessLevel,
accessor.get.funcType.getParams(),
accessor.get.funcType.getNumResults() > 0 ? accessor.get.funcType.getResult(0) : mlir::Type(),
classType->classType);
classType->classType,
accessor.get.dllName);
}

if (accessor.set)
Expand All @@ -608,7 +635,8 @@ namespace typescript
accessor.isStatic, "set", accessor.name, accessor.setAccessLevel,
accessor.set.funcType.getParams(),
mlir::Type(),
classType->classType);
classType->classType,
accessor.set.dllName);
}
}

Expand Down
30 changes: 28 additions & 2 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1568,9 +1568,11 @@ struct FuncOpLowering : public TsLlvmPattern<mlir_ts::FuncOp>
continue;
}

auto addAttr =
auto addAttr =
std::find(skipAttrs.begin(), skipAttrs.end(), name) == skipAttrs.end();
if (addAttr)
if (addAttr && name == DLL_NAME)
funcAttrs.push_back(ArrayAttr::get(rewriter.getContext(), {name, attr.getValue()}));
else if (addAttr)
funcAttrs.push_back(name);
}

Expand Down Expand Up @@ -7216,6 +7218,30 @@ void TypeScriptToLLVMLoweringPass::runOnOperation()
{
auto m = getOperation();

// @dllname on a global: unlike a function, an LLVM global carries no string attribute
// ExportFixPass could rename it by, so rename the symbol and its uses here instead
SmallVector<mlir_ts::GlobalOp> renamedGlobals;
m.walk([&](mlir_ts::GlobalOp globalOp) {
if (globalOp->hasAttr(DLL_NAME))
{
renamedGlobals.push_back(globalOp);
}
});

for (auto globalOp : renamedGlobals)
{
auto dllName = globalOp->getAttrOfType<mlir::StringAttr>(DLL_NAME);
globalOp->removeAttr(DLL_NAME);
if (failed(SymbolTable::replaceAllSymbolUses(globalOp, dllName, m)))
{
globalOp.emitError("can't rename global to @dllname '") << dllName.getValue() << "'";
signalPassFailure();
return;
}

SymbolTable::setSymbolName(globalOp, dllName);
}

// The first thing to define is the conversion target. This will define the
// final target for this lowering. For this lowering, we are only targeting
// the LLVM dialect.
Expand Down
4 changes: 2 additions & 2 deletions tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ namespace mlirgen
auto globalFuncVar = resolveFullNameIdentifier(location, funcName, false, genContext);
if (!globalFuncVar)
{
auto symbolNameValue = V(mlirGenStringValue(location, funcName.str(), true));
auto symbolNameValue = V(mlirGenStringValue(location, funcEntry.dllName.empty() ? funcName.str() : funcEntry.dllName, true));
auto referenceToFuncOpaque = builder.create<mlir_ts::SearchForAddressOfSymbolOp>(
location, getOpaqueType(), symbolNameValue);
auto castResult = cast(location, effectiveFuncType, referenceToFuncOpaque, genContext);
Expand Down Expand Up @@ -838,7 +838,7 @@ namespace mlirgen
auto globalFuncVar = resolveFullNameIdentifier(location, funcName, false, genContext);
if (!globalFuncVar)
{
auto symbolNameValue = V(mlirGenStringValue(location, funcName.str(), true));
auto symbolNameValue = V(mlirGenStringValue(location, funcEntry.dllName.empty() ? funcName.str() : funcEntry.dllName, true));
auto referenceToFuncOpaque = builder.create<mlir_ts::SearchForAddressOfSymbolOp>(
location, getOpaqueType(), symbolNameValue);
auto castResult = cast(location, effectiveFuncType, referenceToFuncOpaque, genContext);
Expand Down
11 changes: 8 additions & 3 deletions tslang/lib/TypeScript/MLIRGenClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,8 @@ genContext);
// initializer into a synthesized __cctor function via
// GlobalOpLowering, and a "live" (main-scoped) location on any op
// living there would attach the wrong DISubprogram to it.
auto symbolNameValue = V(mlirGenStringValue(location, methodOrField.methodInfo.funcName, true));
auto symbolNameValue = V(mlirGenStringValue(location,
methodOrField.methodInfo.dllName.empty() ? methodOrField.methodInfo.funcName : methodOrField.methodInfo.dllName, true));
auto referenceToSymbolOpaque = builder.create<mlir_ts::SearchForAddressOfSymbolOp>(
location, getOpaqueType(), symbolNameValue);
auto castResult = cast(location, methodOrField.methodInfo.funcType, referenceToSymbolOpaque, genContext);
Expand Down Expand Up @@ -2001,7 +2002,10 @@ genContext);

if (isOwnedByDynamicImport(symbolName, vtRecord.isStaticField))
{
auto symbolNameValue = V(mlirGenStringValue(location, symbolName.str(), true));
auto dllSymbolName = !vtRecord.isStaticField && !vtRecord.methodInfo.dllName.empty()
? StringRef(vtRecord.methodInfo.dllName)
: symbolName;
auto symbolNameValue = V(mlirGenStringValue(location, dllSymbolName.str(), true));
auto referenceToSymbolOpaque = builder.create<mlir_ts::SearchForAddressOfSymbolOp>(
location, getOpaqueType(), symbolNameValue);
auto castResult = cast(location, slotType, referenceToSymbolOpaque, genContext);
Expand Down Expand Up @@ -2280,8 +2284,9 @@ genContext);
classMethodMemberInfo.setFuncOp(funcOp);

auto location = loc(funcLikeDeclaration);
auto dllNameAttr = funcOp->getAttrOfType<mlir::StringAttr>(DLL_NAME);
if (mlir::succeeded(mlirGenFunctionLikeDeclarationDynamicImport(
location, funcOp.getName(), funcOp.getFunctionType(), funcOp.getName(), genContext)))
location, funcOp.getName(), funcOp.getFunctionType(), dllNameAttr ? dllNameAttr.getValue() : funcOp.getName(), genContext)))
{
// no need to generate method in code
funcLikeDeclaration->processed = true;
Expand Down
13 changes: 10 additions & 3 deletions tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ namespace mlirgen
if (functionLikeDeclarationBaseAST == SyntaxKind::FunctionDeclaration
|| functionLikeDeclarationBaseAST == SyntaxKind::ArrowFunction)
{
addFunctionDeclarationToExport(funcProto, currentNamespace);
auto dllNameAttr = funcOp->getAttrOfType<mlir::StringAttr>(DLL_NAME);
addFunctionDeclarationToExport(funcProto, currentNamespace, dllNameAttr ? dllNameAttr.getValue() : StringRef());
}
}
}
Expand Down Expand Up @@ -844,19 +845,25 @@ namespace mlirgen

// check decorator for class
auto dynamicImport = false;
StringRef dllFuncName = funcProto->getName();
iterateDecorators(functionLikeDeclarationBaseAST, genContext, [&](StringRef name, SmallVector<StringRef> args) {
if (name == DLL_IMPORT && args.size() > 0)
{
dynamicImport = true;
}

if (name == DLL_NAME && args.size() > 0)
{
dllFuncName = args.front();
}
});

if (dynamicImport)
{
// TODO: we do not need to register funcOp as we need to reference global variables
auto result = mlirGenFunctionLikeDeclarationDynamicImport(
location, funcProto->getNameWithoutNamespace(), funcOp.getFunctionType(),
funcProto->getName(), funcDeclGenContext, false);
location, funcProto->getNameWithoutNamespace(), funcOp.getFunctionType(),
dllFuncName, funcDeclGenContext, false);
return {result, funcOp, funcProto->getName().str(), false};
}

Expand Down
Loading
Loading