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: 10 additions & 1 deletion tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ class CastLogicHelper
}

// TODO: should be in LLVM cast?
// not to a float: index.casts only make integers (to f32 it lowered to a trunc); a float
// falls through to the sitofp in castLLVMTypesLogic, as `number` always did
if (inType.isIndex())
{
if (resType.isSignedInteger() || isFloat(resType))
if (resType.isSignedInteger())
{
return rewriter.create<mlir::index::CastSOp>(loc, resLLVMType, in);
}
Expand Down Expand Up @@ -702,6 +704,13 @@ class CastLogicHelper
return rewriter.create<mlir::arith::CmpFOp>(loc, arith::CmpFPredicate::ONE, in, clh.createFConstantOf(inLLVMType.getIntOrFloatBitWidth(), 0.0));
}

// true -> 1.0, false -> 0.0 at any width; only boolean -> number had a path of its own
// (castBoolToNumber), so boolean -> f32 fell through to a trunc - `___unbox<f32>` has one
if (isBool(inLLVMType) && isFloat(resLLVMType))
{
return rewriter.create<mlir::arith::UIToFPOp>(loc, resLLVMType, in);
}

if (isa<LLVM::LLVMPointerType>(inLLVMType) && isBool(resLLVMType))
{
// target-width: a ptrtoint of a real pointer - the integer it produces is as wide as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace typescript
namespace linux
{

// `float` - it used to be `_ZTId`, so a thrown f32 was read as a double
namespace F32Type
{
constexpr const auto *typeName = "_ZTId";
constexpr const auto *typeName = "_ZTIf";
} // namespace F32Type

namespace F64Type
Expand Down
11 changes: 3 additions & 8 deletions tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,8 @@ class LLVMRTTIHelperVCWin32

void setF32AsCatchType()
{
// only the ThrowInfo name is used from here; MLIRRTTIHelperVCWin32 builds the records
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 Down Expand Up @@ -205,9 +200,9 @@ class LLVMRTTIHelperVCWin32
})
.Case<mlir_ts::NumberType>([&](auto numberType) {
#ifdef NUMBER_F64
setF32AsCatchType();
#else
setF64AsCatchType();
#else
setF32AsCatchType();
#endif
})
.Case<mlir_ts::BooleanType>([&](auto boolType) { setBoolAsCatchType(); })
Expand Down
25 changes: 16 additions & 9 deletions tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,27 @@ constexpr const auto *copyThunkPrefix = ".eh.copy.";
constexpr int copyFunctionProperties = 0;
constexpr int simpleTypeProperties = 1;

// `float`. It used to describe itself as `.N` (double) with a size of 8 - the F64Type records
// under the F64Type names - so `catch (e: number)` caught a thrown f32 and read 8 bytes of a
// 4-byte value, and a module throwing both kept whichever thunk came first.
namespace F32Type
{
constexpr const auto *typeName = ".N";
constexpr const auto *typeName = ".M";
constexpr const auto *typeName2 = ".PEAX";
constexpr const auto *typeInfoRef = "??_R0N@8";
constexpr const auto *typeInfoRef = "??_R0M@8";
constexpr const auto *typeInfoRef2 = "??_R0PEAX@8";
constexpr const auto *catchableTypeInfoRef = "_CT??_R0N@88";
constexpr const auto *catchableTypeInfoRef = "_CT??_R0M@84";
// own record, not the pointer-shaped `_CT??_R0PEAX@88` - see the block comment above
constexpr const auto *catchableTypeInfoRef2 = "_CT??_R0PEAX@N88.box";
constexpr const auto *copyThunk2 = ".eh.copy.box.N";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA2N.box";
constexpr const auto *throwInfoRef = "_TI2N.box";
// describes `.N` (double), like F64Type - see setF32AsCatchType
constexpr int catchableTypeSize = 8;
constexpr const auto *catchableTypeInfoRef2 = "_CT??_R0PEAX@M84.box";
constexpr const auto *copyThunk2 = ".eh.copy.box.M";
// the third entry: `.N` (double), reached through a widening thunk, as for an int
constexpr const auto *typeName3 = ".N";
constexpr const auto *typeInfoRef3 = "??_R0N@8";
constexpr const auto *catchableTypeInfoRef3 = "_CT??_R0N@88.fromM";
constexpr const auto *copyThunk3 = ".eh.copy.num.M";
constexpr const auto *catchableTypeInfoArrayRef = "_CTA3M";
constexpr const auto *throwInfoRef = "_TI3M";
constexpr int catchableTypeSize = 4;
} // namespace F32Type

namespace F64Type
Expand Down
6 changes: 4 additions & 2 deletions tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ class MLIRRTTIHelperVCLinux
#else
setF32AsCatchType();
#endif
// a `catch (e: number)` also takes a thrown int (`throw 1`), and binding it
// compares against the int type_info - see TryOpLowering, SaveCatchVarOpLowering
// a `catch (e: number)` also takes a thrown int (`throw 1`) or f32, and binding
// it compares against their type_infos - see TryOpLowering, SaveCatchVarOpLowering
setI32AsCatchType();
setF32AsCatchType();
})
.Case<mlir_ts::BooleanType>([&](auto boolType) { setBoolAsCatchType(); })
.Case<mlir_ts::BigIntType>([&](auto bigIntType) { setI64AsCatchType(); })
Expand All @@ -199,6 +200,7 @@ class MLIRRTTIHelperVCLinux
// caught value throws again.
setI32AsCatchType();
setF64AsCatchType();
setF32AsCatchType();
setBoolAsCatchType();
setI64AsCatchType();
setStringTypeAsCatchType();
Expand Down
8 changes: 6 additions & 2 deletions tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ 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(boxEntry(windows::F32Type::catchableTypeInfoRef2, windows::F32Type::copyThunk2));

TypeNames toNumber{windows::F32Type::typeName3, windows::F32Type::typeInfoRef3, windows::F32Type::catchableTypeInfoRef3,
windows::F64Type::catchableTypeSize};
toNumber.copyThunk = windows::F32Type::copyThunk3;
toNumber.copyThunkTarget = mlir_ts::NumberType::get(rewriter.getContext());
types.push_back(toNumber);

catchableTypeInfoArrayRef = windows::F32Type::catchableTypeInfoArrayRef;
throwInfoRef = windows::F32Type::throwInfoRef;
}
Expand Down
40 changes: 20 additions & 20 deletions tslang/lib/TypeScript/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,16 +1653,19 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
? (mlir::Value)rttih.typeInfoPtrValue(loc)
: /*catch all*/ (mlir::Value)rewriter.create<mlir_ts::NullOp>(loc, mth.getNullType());

// `catch (e: number)` has to take `throw 1` too - an integer literal is an int here. On
// Windows the int's ThrowInfo lists a `.N` entry that converts it (see copyThunkPrefix in
// LLVMRTTIHelperVCWin32Const.h); on the Itanium path the catch lists the int type_info as
// a second clause, accepts either, and linux::SaveCatchVarOpLowering converts.
mlir::Value intTypeInfo;
// `catch (e: number)` has to take `throw 1` too - an integer literal is an int here - and
// a thrown f32. On Windows their ThrowInfos list a `.N` entry that converts (see
// copyThunkPrefix in LLVMRTTIHelperVCWin32Const.h); on the Itanium path the catch lists
// their type_infos as further clauses, accepts any of them, and
// linux::SaveCatchVarOpLowering converts.
SmallVector<mlir::Value> widenedTypeInfos;
if (!tsContext->compileOptions.isWindows && catchVarType && isa<mlir_ts::NumberType>(catchVarType))
{
intTypeInfo = rewriter.create<mlir_ts::ConstantOp>(
loc, mth.getRefType(mth.getOpaqueType()),
mlir::FlatSymbolRefAttr::get(rewriter.getContext(), ::typescript::linux::I32Type::typeName));
for (auto typeInfoName : {::typescript::linux::I32Type::typeName, ::typescript::linux::F32Type::typeName})
{
widenedTypeInfos.push_back(rewriter.create<mlir_ts::ConstantOp>(
loc, mth.getRefType(mth.getOpaqueType()), mlir::FlatSymbolRefAttr::get(rewriter.getContext(), typeInfoName)));
}
}

// A cleanup-only try (a `using` scope) nested inside another try in the same function
Expand Down Expand Up @@ -1746,17 +1749,14 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
}

mlir::Value cmpValue;
mlir::Value cmpIntValue;
SmallVector<mlir::Value> cmpWidenedValues;
if (catchHasOps)
{
// catches:landingpad
rewriter.setInsertionPointToStart(linuxHasCleanups ? cleanupBlock : catchesBlock);

SmallVector<mlir::Value> catchTypes{catch1};
if (intTypeInfo)
{
catchTypes.push_back(intTypeInfo);
}
catchTypes.append(widenedTypeInfos.begin(), widenedTypeInfos.end());

if (linuxHasCleanups && rttih.hasType() || linuxTypedCatchChains)
{
Expand Down Expand Up @@ -1793,10 +1793,10 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>

cmpValue = rewriter.create<mlir_ts::CompareCatchTypeOp>(loc, mth.getBooleanType(), landingPadOp,
rttih.throwInfoPtrValue(loc));
if (intTypeInfo)
for (auto widenedTypeInfo : widenedTypeInfos)
{
cmpIntValue =
rewriter.create<mlir_ts::CompareCatchTypeOp>(loc, mth.getBooleanType(), landingPadOp, intTypeInfo);
cmpWidenedValues.push_back(
rewriter.create<mlir_ts::CompareCatchTypeOp>(loc, mth.getBooleanType(), landingPadOp, widenedTypeInfo));
}
}

Expand Down Expand Up @@ -1937,7 +1937,7 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
if (cmpValue)
{
// condbr
auto lastCmpValue = cmpIntValue ? cmpIntValue : cmpValue;
auto lastCmpValue = cmpWidenedValues.empty() ? cmpValue : cmpWidenedValues.back();
rewriter.setInsertionPointAfterValue(lastCmpValue);

mlir::Block *currentBlockBrCmp = rewriter.getInsertionBlock();
Expand Down Expand Up @@ -1975,11 +1975,11 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>

rewriter.setInsertionPointAfterValue(lastCmpValue);
mlir::Value castToI1 = rewriter.create<mlir_ts::CastOp>(loc, rewriter.getI1Type(), cmpValue);
if (cmpIntValue)
for (auto cmpWidenedValue : cmpWidenedValues)
{
auto intToI1 = rewriter.create<mlir_ts::CastOp>(loc, rewriter.getI1Type(), cmpIntValue);
auto widenedToI1 = rewriter.create<mlir_ts::CastOp>(loc, rewriter.getI1Type(), cmpWidenedValue);
castToI1 = rewriter.create<mlir_ts::ArithmeticBinaryOp>(
loc, rewriter.getI1Type(), rewriter.getI32IntegerAttr((int)SyntaxKind::BarToken), castToI1, intToI1);
loc, rewriter.getI1Type(), rewriter.getI32IntegerAttr((int)SyntaxKind::BarToken), castToI1, widenedToI1);
}

rewriter.create<mlir::cf::CondBranchOp>(loc, castToI1, continuationBrCmp, rethrowBlock);
Expand Down
28 changes: 22 additions & 6 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,17 @@ class ParseFloatOpLowering : public TsLlvmPattern<mlir_ts::ParseFloatOp>
auto i8PtrTy = th.getPtrType();
auto parseFloatFuncOp = ch.getOrInsertFunction("atof", th.getFunctionType(rewriter.getF64Type(), {i8PtrTy}));

#ifdef NUMBER_F64
auto funcCall = rewriter.replaceOpWithNewOp<LLVM::CallOp>(op, parseFloatFuncOp, ValueRange{transformed.getArg()});
#else
// atof gives a double whatever the result is: `<f32>"2.5"` wants a float
auto funcCall = rewriter.create<LLVM::CallOp>(loc, parseFloatFuncOp, ValueRange{transformed.getArg()});
rewriter.replaceOpWithNewOp<LLVM::FPTruncOp>(op, rewriter.getF32Type(), funcCall.getResult());
#endif
auto resultType = getTypeConverter()->convertType(op.getType());
if (resultType == rewriter.getF64Type())
{
rewriter.replaceOp(op, funcCall.getResult());
}
else
{
rewriter.replaceOpWithNewOp<LLVM::FPTruncOp>(op, resultType, funcCall.getResult());
}

return success();
}
Expand Down Expand Up @@ -5070,7 +5075,17 @@ struct SaveCatchVarOpLowering : public TsLlvmPattern<mlir_ts::SaveCatchVarOp>
return rewriter.create<LLVM::SIToFPOp>(loc, llvmNumberType, intValue);
},
[&](OpBuilder &, Location) -> mlir::Value {
return rewriter.create<LLVM::LoadOp>(loc, llvmNumberType, exceptionInfo);
auto floatTypeInfo = rewriter.create<LLVM::AddressOfOp>(loc, ptrTy, ::typescript::linux::F32Type::typeName);
auto isFloat = rewriter.create<LLVM::ICmpOp>(loc, LLVM::ICmpPredicate::eq, typeInfo, floatTypeInfo);
return clh.conditionalExpressionLowering(
loc, llvmNumberType, isFloat,
[&](OpBuilder &, Location) -> mlir::Value {
auto floatValue = rewriter.create<LLVM::LoadOp>(loc, rewriter.getF32Type(), exceptionInfo);
return rewriter.create<LLVM::FPExtOp>(loc, llvmNumberType, floatValue);
},
[&](OpBuilder &, Location) -> mlir::Value {
return rewriter.create<LLVM::LoadOp>(loc, llvmNumberType, exceptionInfo);
});
});
}

Expand Down Expand Up @@ -5115,6 +5130,7 @@ struct SaveCatchVarOpLowering : public TsLlvmPattern<mlir_ts::SaveCatchVarOp>
SmallVector<std::pair<std::string, mlir::Type>> candidates{
{::typescript::linux::I32Type::typeName, mlir::IntegerType::get(ctx, 32, mlir::IntegerType::Signed)},
{::typescript::linux::F64Type::typeName, mlir_ts::NumberType::get(ctx)},
{::typescript::linux::F32Type::typeName, mlir::Float32Type::get(ctx)},
{::typescript::linux::BoolType::typeName, mlir_ts::BooleanType::get(ctx)},
{::typescript::linux::I64Type::typeName, mlir_ts::BigIntType::get(ctx)},
{::typescript::linux::StringType::typeName, mlir_ts::StringType::get(ctx)},
Expand Down
6 changes: 6 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ tslang_add_test(NAME test-compile-00-try-in-catch-with-finally COMMAND test-runn
tslang_add_test(NAME test-compile-00-throw-number-untyped-catch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_number_untyped_catch.ts")
tslang_add_test(NAME test-compile-00-catch-untyped-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_untyped_value.ts")
tslang_add_test(NAME test-compile-00-throw-bool-bigint COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_bool_bigint.ts")
tslang_add_test(NAME test-compile-00-throw-f32 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_f32.ts")
tslang_add_test(NAME test-compile-00-cast-to-f32 COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00cast_to_f32.ts")
tslang_add_test(NAME test-compile-00-using-nested-scopes COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00using_nested_scopes.ts")
tslang_add_test(NAME test-compile-00-using-unwind-progress COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00using_unwind_progress.ts")
tslang_add_test(NAME test-compile-00-break-continue-scope-exit COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00break_continue_scope_exit.ts")
Expand Down Expand Up @@ -819,6 +821,8 @@ tslang_add_test(NAME test-jit-00-try-in-catch-with-finally COMMAND test-runner -
tslang_add_test(NAME test-jit-00-throw-number-untyped-catch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_number_untyped_catch.ts")
tslang_add_test(NAME test-jit-00-catch-untyped-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00catch_untyped_value.ts")
tslang_add_test(NAME test-jit-00-throw-bool-bigint COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_bool_bigint.ts")
tslang_add_test(NAME test-jit-00-throw-f32 COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00throw_f32.ts")
tslang_add_test(NAME test-jit-00-cast-to-f32 COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00cast_to_f32.ts")
tslang_add_test(NAME test-jit-00-using-nested-scopes COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00using_nested_scopes.ts")
tslang_add_test(NAME test-jit-00-using-unwind-progress COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00using_unwind_progress.ts")
tslang_add_test(NAME test-jit-00-break-continue-scope-exit COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00break_continue_scope_exit.ts")
Expand Down Expand Up @@ -1502,6 +1506,7 @@ set(TSLANG_CORPUS
00break_continue_scope_exit.ts
00break_continue.ts
00capture_in_new_arguments.ts
00cast_to_f32.ts
00catch_untyped_value.ts
00catch_value.ts
00catch_value_minimal.ts
Expand Down Expand Up @@ -1710,6 +1715,7 @@ set(TSLANG_CORPUS
00symbol.ts
00throw_in_catch.ts
00throw_bool_bigint.ts
00throw_f32.ts
00throw_inlined.ts
00throw_number_untyped_catch.ts
00to_primitive_ops.ts
Expand Down
36 changes: 36 additions & 0 deletions tslang/test/tester/tests/00cast_to_f32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Casts to f32 that failed to compile, all reached by unboxing an `any` as an f32 (`<f32>e` in
// an untyped catch): `___unbox<f32>` tries every boxed type, and three of the casts were bad.
// - boolean -> f32 had no path (only boolean -> number did) and became undef with a warning;
// - index -> f32 used index.casts, which only make integers, and lowered to an invalid trunc;
// - string -> f32 returned atof's double as the float.

function fromBoolean(b: boolean) {
return <f32>b;
}

function fromIndex(i: index) {
return <f32>i;
}

function fromString(s: string) {
return <f32>s;
}

function fromAny(a: any) {
return <f32>a;
}

function main() {
assert(fromBoolean(true) == 1, "<f32>true is 1");
assert(fromBoolean(false) == 0, "<f32>false is 0");
assert(fromIndex(7) == 7, "<f32> of an index");
assert(fromString("2.5") == 2.5, "<f32> of a string");

let f: f32 = 2.5;
assert(fromAny(f) == 2.5, "<f32> of an any holding an f32");

let n: number = 1.5;
assert(fromAny(n) == 1.5, "<f32> of an any holding a number");

print("done.");
}
Loading
Loading