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
15 changes: 14 additions & 1 deletion tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
// the call returns what the adapter returns, owned when every adapter here returns owned (OwnedReturnConsumptionPass)
#define ANY_CALL_ADAPTER_ATTR_NAME "__any_call_adapter"
#define ANY_CALL_ADAPTER_CALL_ATTR_NAME "__any_call_adapter_call"
// the call reading `x[k]` of `x: any` (MLIRGenImpl::mlirGenAnyKey): a value, not a place to write
#define ANY_KEY_READ_ATTR_NAME "__any_key_read"

// On a `ts.ValueOrDefault`: an explicit cast in the source (`<T>data` of a `T | undefined`) made
// it, so its lowering does not warn that an optional value is read as it is.
Expand Down Expand Up @@ -313,6 +315,9 @@
// module attribute: the adapter made for each function type boxed in the module, by the type's
// MLIRHelper::hashOfType (TYPE_DESCR_CALL)
#define ANY_CALL_ADAPTERS_ATTR_NAME "ts.any_call_adapters"
// module attribute: the adapter made for each tuple, class, interface or array type boxed in the module,
// by the type's MLIRHelper::hashOfType (TYPE_DESCR_KEYS)
#define ANY_KEYS_ADAPTERS_ATTR_NAME "ts.any_keys_adapters"
#define NEW_METHOD_NAME ".new"
#define NEW_CTOR_METHOD_NAME ".new_ctor"
// the fields of a class value's storage tuple (ClassValueType), beside NEW_METHOD_NAME
Expand Down Expand Up @@ -678,12 +683,20 @@
// `any` - `(box: any, args: any[]) => any`, made when the function is boxed (user ruling 11-18,
// MLIRGenImpl::ensureAnyCallAdapter) - and null for any other type, or when none was made.
#define TYPE_DESCR_CALL 5
// For a tuple, class, interface or array type, the address of the adapter that reads a key of a value of
// the type held in an `any` - `(box: any, op: number, key: any, value: any) => any`, made when the value
// is boxed (user ruling 11-19, MLIRGenImpl::ensureAnyKeysAdapter): op ANY_KEYS_OP_GET is `box[key]`,
// undefined for a key the type does not have, and ANY_KEYS_OP_HAS is `key in box`, a boolean. `value` is
// for an op that writes, none yet. Null for any other type, or when none was made.
#define TYPE_DESCR_KEYS 6
#define ANY_KEYS_OP_GET 0
#define ANY_KEYS_OP_HAS 1
// The block header, last so that it sits immediately in front of the name bytes. A tag is a
// `typeof` result, and `typeof x` can be assigned to a `string` and released like any other
// string - so a tag has to look like a payload with an immortal block header, exactly as a
// string literal does, on top of being a name preceded by a descriptor. Both reads work off
// the same pointer: `tag - sizeof(header)` is the marker, `tag - sizeof(record)` the record.
#define TYPE_DESCR_BLOCK_HEADER 6
#define TYPE_DESCR_BLOCK_HEADER 7

// Coarse category of the described type. These correspond one-to-one with the names
// TypeOfOpHelper::typeOfAsString reports, and are derived from that name so the two cannot
Expand Down
7 changes: 6 additions & 1 deletion tslang/include/TypeScript/LowerToLLVM/LLVMCodeHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class LLVMCodeHelper : public LLVMCodeHelperBase
mlir::Value getOrCreateTypeDescriptorName(mlir::Type type, std::string name, int kind,
StringRef releaseRoutineName, StringRef retainRoutineName,
StringRef shareRoutineName, StringRef variant = "", uint32_t layout = 0,
StringRef callRoutineName = "")
StringRef callRoutineName = "", StringRef keysRoutineName = "")
{
auto loc = op->getLoc();
auto parentModule = op->getParentOfType<ModuleOp>();
Expand Down Expand Up @@ -419,6 +419,11 @@ class LLVMCodeHelper : public LLVMCodeHelperBase
? (mlir::Value)rewriter.create<LLVM::ZeroOp>(loc, th.getPtrType())
: (mlir::Value)rewriter.create<LLVM::AddressOfOp>(loc, th.getPtrType(), callRoutineName);
setStructValue(loc, recordValue, callValue, TYPE_DESCR_CALL);
mlir::Value keysValue =
keysRoutineName.empty()
? (mlir::Value)rewriter.create<LLVM::ZeroOp>(loc, th.getPtrType())
: (mlir::Value)rewriter.create<LLVM::AddressOfOp>(loc, th.getPtrType(), keysRoutineName);
setStructValue(loc, recordValue, keysValue, TYPE_DESCR_KEYS);
// a tag doubles as a string payload, so what precedes the name has to read as an
// immortal block header - see TYPE_DESCR_BLOCK_HEADER
setStructValue(loc, recordValue,
Expand Down
16 changes: 13 additions & 3 deletions tslang/include/TypeScript/LowerToLLVM/TypeDescriptorLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ class TypeDescriptorLogic
{
}

// { i32 kind, i32 layout, ptr release, ptr retain, ptr share, ptr call, blockHeader }: the block header
// an integer of CompileOptions::heapBlockHeaderBytes, the index width but 8 bytes on wasm32
// { i32 kind, i32 layout, ptr release, ptr retain, ptr share, ptr call, ptr keys, blockHeader }: the block
// header an integer of CompileOptions::heapBlockHeaderBytes, the index width but 8 bytes on wasm32
static LLVM::LLVMStructType getRecordType(mlir::OpBuilder &builder, mlir::Type blockWordType)
{
auto i32Ty = builder.getI32Type();
auto ptrTy = LLVM::LLVMPointerType::get(builder.getContext());
return LLVM::LLVMStructType::getLiteral(builder.getContext(), {i32Ty, i32Ty, ptrTy, ptrTy, ptrTy, ptrTy, blockWordType}, false);
return LLVM::LLVMStructType::getLiteral(builder.getContext(),
{i32Ty, i32Ty, ptrTy, ptrTy, ptrTy, ptrTy, ptrTy, blockWordType}, false);
}

LLVM::LLVMStructType getRecordType()
Expand Down Expand Up @@ -99,6 +100,15 @@ class TypeDescriptorLogic
return rewriter.create<LLVM::LoadOp>(loc, th.getPtrType(), callPtr);
}

// TYPE_DESCR_KEYS of the type this tag names: the adapter that reads a key of a boxed value, or null
mlir::Value getKeysFromTag(mlir::Value tagValue)
{
auto recordPtr = getRecordPtrFromTag(tagValue);
auto keysPtr = rewriter.create<LLVM::GEPOp>(loc, th.getPtrType(), getRecordType(), recordPtr,
ArrayRef<LLVM::GEPArg>{0, TYPE_DESCR_KEYS});
return rewriter.create<LLVM::LoadOp>(loc, th.getPtrType(), keysPtr);
}

mlir::Value isKind(mlir::Value tagValue, int kind)
{
auto kindValue = getKindFromTag(tagValue);
Expand Down
3 changes: 3 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ struct GenContext
// storage declared in the body does not dominate it; hoisting is what lets the unwind leg
// dispose and release what the body's scope owes.
bool allocateScopeOwnedVarsOutsideOfOperation = false;
// a catch clause's variable is being declared: the landing pad writes it, not an initializer, so it
// starts with nothing in it (MLIRGenImpl::adjustLocalVariableType)
bool declaringCatchVariable = false;
bool forceDiscover = false;
bool discoverParamsOnly = false;
bool insertIntoParentScope = false;
Expand Down
12 changes: 12 additions & 0 deletions tslang/include/TypeScript/TypeScriptOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ def TypeScript_IsTupleOfLayoutOp : TypeScript_Op<"IsTupleOfLayout", [Pure]> {
let results = (outs TypeScript_Boolean:$res);
}

def TypeScript_AnyKeysAdapterOp : TypeScript_Op<"AnyKeysAdapter", [Pure]> {
let summary = "the adapter that reads a key of the value an any holds";
let description = [{
The TYPE_DESCR_KEYS of the tag of the box $value: `(box: any, op: number, key: any, value: any) => any`,
made for the type of the value (a tuple, class, interface or array) when it was boxed
(MLIRGenImpl::ensureAnyKeysAdapter), or null when none was made for it.
}];

let arguments = (ins TypeScript_Any:$value);
let results = (outs TypeScript_Opaque:$res);
}

def TypeScript_AnyCallAdapterOp : TypeScript_Op<"AnyCallAdapter", [Pure]> {
let summary = "the adapter that calls the function an any holds";
let description = [{
Expand Down
2 changes: 1 addition & 1 deletion tslang/lib/TypeScript/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3316,7 +3316,7 @@ void AddTsAffineLegalOps(ConversionTarget &target)
mlir_ts::EndCatchOp, mlir_ts::TryEntryOp, mlir_ts::BeginCleanupOp, mlir_ts::EndCleanupOp, mlir_ts::ThrowUnwindOp,
mlir_ts::ThrowCallOp, mlir_ts::SymbolCallInternalOp, mlir_ts::CallInternalOp, mlir_ts::ReturnInternalOp,
mlir_ts::NoOp, mlir_ts::SwitchStateInternalOp, mlir_ts::UnreachableOp, mlir_ts::GlobalConstructorOp, mlir_ts::AppendToUsedOp,
mlir_ts::CreateBoundFunctionOp, mlir_ts::TypeOfAnyOp, mlir_ts::IsTupleOfLayoutOp, mlir_ts::AnyCallAdapterOp, mlir_ts::IsTypeTagOfOp, mlir_ts::BoxOp, mlir_ts::UnboxOp,
mlir_ts::CreateBoundFunctionOp, mlir_ts::TypeOfAnyOp, mlir_ts::IsTupleOfLayoutOp, mlir_ts::AnyCallAdapterOp, mlir_ts::AnyKeysAdapterOp, mlir_ts::IsTypeTagOfOp, mlir_ts::BoxOp, mlir_ts::UnboxOp,
mlir_ts::CreateUnionInstanceOp, mlir_ts::GetValueFromUnionOp, mlir_ts::GetTypeInfoFromUnionOp,
mlir_ts::OptionalOp, mlir_ts::OptionalValueOp, mlir_ts::OptionalUndefOp,
mlir_ts::LoadLibraryPermanentlyOp, mlir_ts::SearchForAddressOfSymbolOp,
Expand Down
37 changes: 35 additions & 2 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,26 @@ class TypeDescriptorOpLowering : public TsLlvmPattern<mlir_ts::TypeDescriptorOp>
}
}

// ...and the adapter reading its keys through `any` (TYPE_DESCR_KEYS), on the same terms
std::string keysRoutineName;
if (auto parentModule = op->getParentOfType<mlir::ModuleOp>())
{
if (auto adapters = parentModule->getAttrOfType<mlir::DictionaryAttr>(ANY_KEYS_ADAPTERS_ATTR_NAME))
{
if (auto adapterName = adapters.getAs<mlir::StringAttr>(std::to_string(MLIRHelper::hashOfType(descriptorType)));
adapterName && parentModule.lookupSymbol(adapterName.getValue()))
{
keysRoutineName = adapterName.getValue().str();
}
}
}

rewriter.replaceOp(op, ch.getOrCreateTypeDescriptorName(descriptorType, name,
TypeOfOpHelper::typeKindFromName(name),
releaseRoutineName, retainRoutineName,
shareRoutineName, variant,
TypeOfOpHelper::layoutIdOf(descriptorType),
callRoutineName));
callRoutineName, keysRoutineName));

return success();
}
Expand Down Expand Up @@ -8535,6 +8549,25 @@ struct AnyCallAdapterOpLowering : public TsLlvmPattern<mlir_ts::AnyCallAdapterOp
}
};

// TYPE_DESCR_KEYS of the tag of a box
struct AnyKeysAdapterOpLowering : public TsLlvmPattern<mlir_ts::AnyKeysAdapterOp>
{
using TsLlvmPattern<mlir_ts::AnyKeysAdapterOp>::TsLlvmPattern;

LogicalResult matchAndRewrite(mlir_ts::AnyKeysAdapterOp op, Adaptor transformed,
ConversionPatternRewriter &rewriter) const final
{
auto loc = op.getLoc();

TypeConverterHelper tch(getTypeConverter());

AnyLogic al(op, rewriter, tch, loc, tsLlvmContext->compileOptions);
TypeDescriptorLogic tdl(rewriter, tch, loc, tsLlvmContext->compileOptions);
rewriter.replaceOp(op, tdl.getKeysFromTag(al.getTypeOfAny(transformed.getValue())));
return success();
}
};

struct IsTypeTagOfOpLowering : public TsLlvmPattern<mlir_ts::IsTypeTagOfOp>
{
using TsLlvmPattern<mlir_ts::IsTypeTagOfOp>::TsLlvmPattern;
Expand Down Expand Up @@ -10383,7 +10416,7 @@ void TypeScriptToLLVMLoweringPass::runOnOperation()
LoadSaveValueLowering, ThrowUnwindOpLowering, ThrowCallOpLowering, TryEntryOpLowering,VariableOpLowering, DebugVariableOpLowering, AllocaOpLowering, InvokeOpLowering, AccessorUnwindEdgeOpLowering,
InvokeHybridOpLowering, VirtualSymbolRefOpLowering, ThisVirtualSymbolRefOpLowering, ThisSymbolRefOpLowering, InterfaceSymbolRefOpLowering,
NewInterfaceOpLowering, VTableOffsetRefOpLowering, LoadBoundRefOpLowering, StoreBoundRefOpLowering, CreateBoundRefOpLowering,
CreateBoundFunctionOpLowering, GetThisOpLowering, GetMethodOpLowering, TypeOfOpLowering, TypeOfAnyOpLowering, IsTupleOfLayoutOpLowering, AnyCallAdapterOpLowering, IsTypeTagOfOpLowering, DebuggerOpLowering,
CreateBoundFunctionOpLowering, GetThisOpLowering, GetMethodOpLowering, TypeOfOpLowering, TypeOfAnyOpLowering, IsTupleOfLayoutOpLowering, AnyCallAdapterOpLowering, AnyKeysAdapterOpLowering, IsTypeTagOfOpLowering, DebuggerOpLowering,
UnreachableOpLowering, SymbolCallInternalOpLowering, CallInternalOpLowering, CallHybridInternalOpLowering,
ReturnInternalOpLowering, NoOpLowering, GlobalConstructorOpLowering, AppendToUsedOpLowering, ExtractInterfaceThisOpLowering,
ExtractInterfaceVTableOpLowering, BoxOpLowering, UnboxOpLowering, DialectCastOpLowering, CreateUnionInstanceOpLowering,
Expand Down
6 changes: 3 additions & 3 deletions tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1683,9 +1683,9 @@ namespace mlirgen
}
else if (auto anyType = dyn_cast<mlir_ts::AnyType>(arrayType))
{
emitError(location, "element access of a value of type 'any' ('x[k]') is not supported: cast it to its type first");
return mlir::failure();
}
// looked up at run time (user ruling 11-19)
return mlirGenAnyKey(location, expression, argumentExpression, ANY_KEYS_OP_GET, genContext);
}
else
{
LLVM_DEBUG(llvm::dbgs() << "\n!! ElementAccessExpression: " << arrayType
Expand Down
Loading
Loading