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
11 changes: 11 additions & 0 deletions tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class LLVMRTTIHelperVCWin32
void setF32AsCatchType()
{
types.push_back({F32Type::typeName, F32Type::typeInfoRef, F32Type::catchableTypeInfoRef, F32Type::catchableTypeSize});
// Same catchableTypeSize as the primary entry, not pointerSize(): both entries describe
// the exact same in-memory value (a double), and a size mismatch here would tell the CRT
// to copy pointerSize() bytes out of a catchableTypeSize()-byte object when a generic
// `catch (e)`/`catch (e: any)` matches via this fallback entry - see the note on
// I32Type::typeName2 in LLVMRTTIHelperVCWin32Const.h.
types.push_back({F32Type::typeName2, F32Type::typeInfoRef2, F32Type::catchableTypeInfoRef2, F32Type::catchableTypeSize});

catchableTypeInfoArrayRef = F32Type::catchableTypeInfoArrayRef;
throwInfoRef = F32Type::throwInfoRef;
Expand All @@ -69,6 +75,8 @@ class LLVMRTTIHelperVCWin32
void setF64AsCatchType()
{
types.push_back({F64Type::typeName, F64Type::typeInfoRef, F64Type::catchableTypeInfoRef, F64Type::catchableTypeSize});
// see setF32AsCatchType for why this reuses the primary size instead of pointerSize()
types.push_back({F64Type::typeName2, F64Type::typeInfoRef2, F64Type::catchableTypeInfoRef2, F64Type::catchableTypeSize});

catchableTypeInfoArrayRef = F64Type::catchableTypeInfoArrayRef;
throwInfoRef = F64Type::throwInfoRef;
Expand All @@ -77,6 +85,9 @@ class LLVMRTTIHelperVCWin32
void setI32AsCatchType()
{
types.push_back({I32Type::typeName, I32Type::typeInfoRef, I32Type::catchableTypeInfoRef, I32Type::catchableTypeSize});
// `int` is 4 bytes on every target, unlike a pointer - see setF32AsCatchType for why this
// reuses the primary size instead of pointerSize()
types.push_back({I32Type::typeName2, I32Type::typeInfoRef2, I32Type::catchableTypeInfoRef2, I32Type::catchableTypeSize});

catchableTypeInfoArrayRef = I32Type::catchableTypeInfoArrayRef;
throwInfoRef = I32Type::throwInfoRef;
Expand Down
45 changes: 39 additions & 6 deletions tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,67 @@ namespace windows
constexpr const auto *typeInfoExtRef = "??_7type_info@@6B@";
constexpr const auto *imageBaseRef = "__ImageBase";

// F32Type/F64Type/I32Type each also carry a second, `.PEAX` (void*) catchable-type entry -
// see the comment on StringType::typeName2 below for why: it is what lets an untyped
// `catch (e)` or a `catch (e: any)` (which filters on `??_R0PEAX@8`, the same generic
// void*-shaped RTTI used for `any`) match a thrown primitive at all. Without it the
// CatchableTypeArray the CRT walks contains only the primitive's own descriptor, the
// filter never matches, and the exception propagates out of `main` uncaught - see
// throw-number-catchable-type-missing-any-fallback.md.
//
// That fallback entry's *type descriptor* (`typeInfoRef2`, `??_R0PEAX@8`) is safe to share
// with String/Class/I8PtrType below: a type descriptor carries no size, just a vtable pointer
// and the `.PEAX` name. Its *catchable-type* record is not safe to share, though - it carries
// `sizeOrOffset`, and that value is genuinely different per primitive (4 for an `int`, 8 for a
// `double`, `pointerSize()` - 4 or 8 depending on target - for a string/class/`any` pointer).
// `_CT??_R0PEAX@88` is a `linkonce_odr` global keyed only by name: whichever translation unit
// or, within one module, whichever type gets processed first "wins" and fixes that symbol's
// size for every later reference to the same name (see `catchableType()`'s
// `lookupSymbol(...) -> return failure()` idempotency guard). Giving `I32Type` and
// `F32Type`/`F64Type` their own distinctly-named fallback records below keeps a module that
// throws both an `int` and a `string` (say) from having one of them silently take on the
// other's copy size.

namespace F32Type
{
constexpr const auto *typeName = ".N";
constexpr const auto *typeName2 = ".PEAX";
constexpr const auto *typeInfoRef = "??_R0N@8";
constexpr const auto *typeInfoRef2 = "??_R0PEAX@8";
constexpr const auto *catchableTypeInfoRef = "_CT??_R0N@88";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA1N";
constexpr const auto *throwInfoRef = "_TI1N";
// own record, not the pointer-shaped `_CT??_R0PEAX@88` - see the block comment above
constexpr const auto *catchableTypeInfoRef2 = "_CT??_R0PEAX@N88";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA2N";
constexpr const auto *throwInfoRef = "_TI2N";
// describes `.N` (double), like F64Type - see setF32AsCatchType
constexpr int catchableTypeSize = 8;
} // namespace F32Type

namespace F64Type
{
constexpr const auto *typeName = ".N";
constexpr const auto *typeName2 = ".PEAX";
constexpr const auto *typeInfoRef = "??_R0N@8";
constexpr const auto *typeInfoRef2 = "??_R0PEAX@8";
constexpr const auto *catchableTypeInfoRef = "_CT??_R0N@88";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA1N";
constexpr const auto *throwInfoRef = "_TI1N";
// own record, not the pointer-shaped `_CT??_R0PEAX@88` - see the block comment above
constexpr const auto *catchableTypeInfoRef2 = "_CT??_R0PEAX@N88";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA2N";
constexpr const auto *throwInfoRef = "_TI2N";
constexpr int catchableTypeSize = 8;
} // namespace F64Type

namespace I32Type
{
constexpr const auto *typeName = ".H";
constexpr const auto *typeName2 = ".PEAX";
constexpr const auto *typeInfoRef = "??_R0H@8";
constexpr const auto *typeInfoRef2 = "??_R0PEAX@8";
constexpr const auto *catchableTypeInfoRef = "_CT??_R0H@84";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA1H";
constexpr const auto *throwInfoRef = "_TI1H";
// own record, not the pointer-shaped `_CT??_R0PEAX@88` - see the block comment above
constexpr const auto *catchableTypeInfoRef2 = "_CT??_R0PEAX@84";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA2H";
constexpr const auto *throwInfoRef = "_TI2H";
// 4, not the pointer size: `int` is 4 bytes on every target, and the `4` at the end of
// `_CT??_R0H@84` says so too
constexpr int catchableTypeSize = 4;
Expand Down
8 changes: 8 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class MLIRRTTIHelperVCWin32
{
types.push_back({windows::F32Type::typeName, windows::F32Type::typeInfoRef, windows::F32Type::catchableTypeInfoRef,
windows::F32Type::catchableTypeSize});
// Same catchableTypeSize as the primary entry, not pointerSize() - see the equivalent
// comment in LLVMRTTIHelperVCWin32.h::setF32AsCatchType.
types.push_back({windows::F32Type::typeName2, windows::F32Type::typeInfoRef2, windows::F32Type::catchableTypeInfoRef2,
windows::F32Type::catchableTypeSize});

catchableTypeInfoArrayRef = windows::F32Type::catchableTypeInfoArrayRef;
throwInfoRef = windows::F32Type::throwInfoRef;
Expand All @@ -71,6 +75,8 @@ class MLIRRTTIHelperVCWin32
{
types.push_back({windows::F64Type::typeName, windows::F64Type::typeInfoRef, windows::F64Type::catchableTypeInfoRef,
windows::F64Type::catchableTypeSize});
types.push_back({windows::F64Type::typeName2, windows::F64Type::typeInfoRef2, windows::F64Type::catchableTypeInfoRef2,
windows::F64Type::catchableTypeSize});

catchableTypeInfoArrayRef = windows::F64Type::catchableTypeInfoArrayRef;
throwInfoRef = windows::F64Type::throwInfoRef;
Expand All @@ -80,6 +86,8 @@ class MLIRRTTIHelperVCWin32
{
types.push_back({windows::I32Type::typeName, windows::I32Type::typeInfoRef, windows::I32Type::catchableTypeInfoRef,
windows::I32Type::catchableTypeSize});
types.push_back({windows::I32Type::typeName2, windows::I32Type::typeInfoRef2, windows::I32Type::catchableTypeInfoRef2,
windows::I32Type::catchableTypeSize});

catchableTypeInfoArrayRef = windows::I32Type::catchableTypeInfoArrayRef;
throwInfoRef = windows::I32Type::throwInfoRef;
Expand Down
63 changes: 60 additions & 3 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,71 @@ namespace mlirgen
// with only a warning. Such an array is built here from its elements, each cast to the element type.
std::optional<ValueOrLogicalResult> MLIRGenImpl::castConstArrayToArray(mlir::Location location, mlir::Type type, mlir::Value value, mlir::Type valueType, const GenContext &genContext)
{
auto constArrayType = dyn_cast<mlir_ts::ConstArrayType>(valueType);
auto arrayType = dyn_cast<mlir_ts::ArrayType>(type);
if (!constArrayType || !arrayType || constArrayType.getElementType() == arrayType.getElementType())
if (!arrayType)
{
return std::nullopt;
}

auto constArrayType = dyn_cast<mlir_ts::ConstArrayType>(valueType);

// A `const` array literal (`const c = [1, 2]`) now gets real identity
// storage so mutating methods like .sort() share one heap array instead of
// a fresh disposable copy per call (see processConstRef /
// adjustLocalVariableType / adjustGlobalVariableType in MLIRGenImpl.h) --
// which means reading `c` elsewhere yields the already-widened
// `!ts.array<T>`, not the original `!ts.const_array<T,N>` this function
// keys off. Trace back through whichever shape that widening introduced to
// recover the original literal, so the elementwise conversion below still
// applies to a `const` int-array literal read into e.g. `number[]`:
// - local: Load -> VariableOp -> its initializer (a Cast chain)
// - module-level: Load -> AddressOf(@global) -> that GlobalOp's
// initializer region, whose GlobalResultOp terminator yields the same
// kind of Cast chain (see createGlobalVariableInitialization)
mlir::Value literalValue = value;
if (!constArrayType)
{
if (auto loadOp = value.getDefiningOp<mlir_ts::LoadOp>())
{
if (auto varOp = loadOp.getReference().getDefiningOp<mlir_ts::VariableOp>())
{
if (auto init = varOp.getInitializer())
{
literalValue = init;
}
}
else if (auto addressOfOp = loadOp.getReference().getDefiningOp<mlir_ts::AddressOfOp>())
{
if (auto globalOp = theModule.lookupSymbol<mlir_ts::GlobalOp>(addressOfOp.getGlobalNameAttr()))
{
if (auto *initBlock = globalOp.getInitializerBlock())
{
if (auto globalResultOp = dyn_cast<mlir_ts::GlobalResultOp>(initBlock->getTerminator()))
{
if (globalResultOp.getResults().size() == 1)
{
literalValue = globalResultOp.getResults().front();
}
}
}
}
}
}

while (auto castOp = literalValue.getDefiningOp<mlir_ts::CastOp>())
{
literalValue = castOp.getIn();
}

constArrayType = dyn_cast<mlir_ts::ConstArrayType>(literalValue.getType());
}

if (!constArrayType || constArrayType.getElementType() == arrayType.getElementType())
{
return std::nullopt;
}

auto constOp = value.getDefiningOp<mlir_ts::ConstantOp>();
auto constOp = literalValue.getDefiningOp<mlir_ts::ConstantOp>();
auto elementAttrs = constOp ? dyn_cast<mlir::ArrayAttr>(constOp.getValue()) : mlir::ArrayAttr();
if (!elementAttrs)
{
Expand Down
64 changes: 63 additions & 1 deletion tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1424,8 +1424,22 @@ class MLIRGenImpl
// pointer-like representation -- it still needs real storage so that
// mutation through the bound method is visible across property accesses.
// See docs/const-let-storage-design.md.
//
// A `const` array LITERAL is the same shape of problem: its resolved type
// is `!ts.const_array<T,N>`, a compile-time-constant snapshot, not the
// mutable heap-backed `!ts.array<T>` that TS array semantics require (a
// `const` binding only blocks *reassigning* `arr`, not mutating its
// contents). `let`/`var` widen const_array -> array once at declaration
// (adjustLocalVariableType -> wideStorageType) and keep that single heap
// array alive via real storage; `const` skipped both steps, so every call
// site needing `!ts.array<T>` (e.g. `.sort()`/`.reverse()`) had to
// `ts.Cast` the pristine const_array into a fresh, disposable copy, while
// direct element access indexed the immutable original directly -
// mutating methods silently did nothing (they mutated a copy nobody kept)
// and `arr[i] = v` segfaulted (write into read-only constant data).
// Force the same real-storage + widening path as `let` for this case too.
MLIRTypeHelper mth(builder.getContext(), compileOptions);
needsIdentityStorage = mth.hasBoundMethodField(type);
needsIdentityStorage = mth.hasBoundMethodField(type) || isa<mlir_ts::ConstArrayType>(type);
if (needsIdentityStorage)
{
return mlir::success();
Expand Down Expand Up @@ -1629,6 +1643,29 @@ class MLIRGenImpl

if (variableDeclarationInfo.isConst)
{
// A const array literal (`!ts.const_array<T,N>`) that was routed into real
// storage by processConstRef's needsIdentityStorage check still needs the
// same const_array -> array widening `let` gets below - storage alone is
// not enough. Without it, the Variable slot would hold the immutable
// const_array value itself, and each mutating use (.sort(), element
// write, ...) would still `ts.Cast` a fresh, disposable !ts.array<T> copy
// out of it instead of sharing one heap array through the slot. Every
// other const type (generator wrapper tuples, plain values) keeps the
// early-return: this widening is only valid/needed for const_array. See
// docs/const-let-storage-design.md and the const-array note above
// processConstRef.
if (isa<mlir_ts::ConstArrayType>(type) && variableDeclarationInfo.needsIdentityStorage)
{
auto actualType = mth.removeConstType(type);
if (variableDeclarationInfo.initial && actualType != type)
{
CAST_A(castedValue, location, actualType, variableDeclarationInfo.initial, genContext);
variableDeclarationInfo.setInitial(castedValue);
}

variableDeclarationInfo.setType(actualType);
}

return mlir::success();
}

Expand All @@ -1655,6 +1692,31 @@ class MLIRGenImpl
{
if (variableDeclarationInfo.isConst)
{
// Same const-array widening as adjustLocalVariableType, for a module-level
// `const arr = [...]`: a global const binding never goes through
// processConstRef (that path is local-only), so needsIdentityStorage is
// never computed for it - but the GlobalOp itself is always real storage,
// so no extra identity check is needed here, just the type widening. The
// GlobalOp stays `isConst` (its own {ptr,len} header can't be
// reassigned - `arr = other` is still rejected) but the header's type must
// be the heap-backed `!ts.array<T>`, not the immutable `!ts.const_array
// <T,N>` snapshot, or every mutating method/element write still operates
// on a disposable ts.Cast copy or the read-only original. See the note
// above processConstRef / const-let-storage-design.md.
if (isa<mlir_ts::ConstArrayType>(variableDeclarationInfo.type))
{
auto type = variableDeclarationInfo.type;
auto actualType = mth.removeConstType(type);
if (variableDeclarationInfo.initial && actualType != type)
{
auto result = cast(location, actualType, variableDeclarationInfo.initial, genContext);
EXIT_IF_FAILED_OR_NO_VALUE(result)
variableDeclarationInfo.initial = V(result);
}

variableDeclarationInfo.setType(actualType);
}

return mlir::success();
}

Expand Down
Loading
Loading