diff --git a/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h b/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h index edbadd096..09bdb1aee 100644 --- a/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h @@ -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(loc, resLLVMType, in); } @@ -702,6 +704,13 @@ class CastLogicHelper return rewriter.create(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` has one + if (isBool(inLLVMType) && isFloat(resLLVMType)) + { + return rewriter.create(loc, resLLVMType, in); + } + if (isa(inLLVMType) && isBool(resLLVMType)) { // target-width: a ptrtoint of a real pointer - the integer it produces is as wide as the diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCLinuxConst.h b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCLinuxConst.h index 4b36993bb..2dae3ce2d 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCLinuxConst.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCLinuxConst.h @@ -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 diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h index e5f512565..f3982f25a 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h @@ -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; @@ -205,9 +200,9 @@ class LLVMRTTIHelperVCWin32 }) .Case([&](auto numberType) { #ifdef NUMBER_F64 - setF32AsCatchType(); -#else setF64AsCatchType(); +#else + setF32AsCatchType(); #endif }) .Case([&](auto boolType) { setBoolAsCatchType(); }) diff --git a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32Const.h b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32Const.h index cc4f6e4f4..5c701c46d 100644 --- a/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32Const.h +++ b/tslang/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32Const.h @@ -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 diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h index 32be28ffc..e4a028003 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h @@ -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([&](auto boolType) { setBoolAsCatchType(); }) .Case([&](auto bigIntType) { setI64AsCatchType(); }) @@ -199,6 +200,7 @@ class MLIRRTTIHelperVCLinux // caught value throws again. setI32AsCatchType(); setF64AsCatchType(); + setF32AsCatchType(); setBoolAsCatchType(); setI64AsCatchType(); setStringTypeAsCatchType(); diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h index b579a598a..0eedaf857 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h @@ -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; } diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index d7461fd13..b20d19f25 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -1653,16 +1653,19 @@ struct TryOpLowering : public TsPattern ? (mlir::Value)rttih.typeInfoPtrValue(loc) : /*catch all*/ (mlir::Value)rewriter.create(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 widenedTypeInfos; if (!tsContext->compileOptions.isWindows && catchVarType && isa(catchVarType)) { - intTypeInfo = rewriter.create( - 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( + 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 @@ -1746,17 +1749,14 @@ struct TryOpLowering : public TsPattern } mlir::Value cmpValue; - mlir::Value cmpIntValue; + SmallVector cmpWidenedValues; if (catchHasOps) { // catches:landingpad rewriter.setInsertionPointToStart(linuxHasCleanups ? cleanupBlock : catchesBlock); SmallVector catchTypes{catch1}; - if (intTypeInfo) - { - catchTypes.push_back(intTypeInfo); - } + catchTypes.append(widenedTypeInfos.begin(), widenedTypeInfos.end()); if (linuxHasCleanups && rttih.hasType() || linuxTypedCatchChains) { @@ -1793,10 +1793,10 @@ struct TryOpLowering : public TsPattern cmpValue = rewriter.create(loc, mth.getBooleanType(), landingPadOp, rttih.throwInfoPtrValue(loc)); - if (intTypeInfo) + for (auto widenedTypeInfo : widenedTypeInfos) { - cmpIntValue = - rewriter.create(loc, mth.getBooleanType(), landingPadOp, intTypeInfo); + cmpWidenedValues.push_back( + rewriter.create(loc, mth.getBooleanType(), landingPadOp, widenedTypeInfo)); } } @@ -1937,7 +1937,7 @@ struct TryOpLowering : public TsPattern if (cmpValue) { // condbr - auto lastCmpValue = cmpIntValue ? cmpIntValue : cmpValue; + auto lastCmpValue = cmpWidenedValues.empty() ? cmpValue : cmpWidenedValues.back(); rewriter.setInsertionPointAfterValue(lastCmpValue); mlir::Block *currentBlockBrCmp = rewriter.getInsertionBlock(); @@ -1975,11 +1975,11 @@ struct TryOpLowering : public TsPattern rewriter.setInsertionPointAfterValue(lastCmpValue); mlir::Value castToI1 = rewriter.create(loc, rewriter.getI1Type(), cmpValue); - if (cmpIntValue) + for (auto cmpWidenedValue : cmpWidenedValues) { - auto intToI1 = rewriter.create(loc, rewriter.getI1Type(), cmpIntValue); + auto widenedToI1 = rewriter.create(loc, rewriter.getI1Type(), cmpWidenedValue); castToI1 = rewriter.create( - loc, rewriter.getI1Type(), rewriter.getI32IntegerAttr((int)SyntaxKind::BarToken), castToI1, intToI1); + loc, rewriter.getI1Type(), rewriter.getI32IntegerAttr((int)SyntaxKind::BarToken), castToI1, widenedToI1); } rewriter.create(loc, castToI1, continuationBrCmp, rethrowBlock); diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 45cc196e0..183e00fed 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -276,12 +276,17 @@ class ParseFloatOpLowering : public TsLlvmPattern auto i8PtrTy = th.getPtrType(); auto parseFloatFuncOp = ch.getOrInsertFunction("atof", th.getFunctionType(rewriter.getF64Type(), {i8PtrTy})); -#ifdef NUMBER_F64 - auto funcCall = rewriter.replaceOpWithNewOp(op, parseFloatFuncOp, ValueRange{transformed.getArg()}); -#else + // atof gives a double whatever the result is: `"2.5"` wants a float auto funcCall = rewriter.create(loc, parseFloatFuncOp, ValueRange{transformed.getArg()}); - rewriter.replaceOpWithNewOp(op, rewriter.getF32Type(), funcCall.getResult()); -#endif + auto resultType = getTypeConverter()->convertType(op.getType()); + if (resultType == rewriter.getF64Type()) + { + rewriter.replaceOp(op, funcCall.getResult()); + } + else + { + rewriter.replaceOpWithNewOp(op, resultType, funcCall.getResult()); + } return success(); } @@ -5070,7 +5075,17 @@ struct SaveCatchVarOpLowering : public TsLlvmPattern return rewriter.create(loc, llvmNumberType, intValue); }, [&](OpBuilder &, Location) -> mlir::Value { - return rewriter.create(loc, llvmNumberType, exceptionInfo); + auto floatTypeInfo = rewriter.create(loc, ptrTy, ::typescript::linux::F32Type::typeName); + auto isFloat = rewriter.create(loc, LLVM::ICmpPredicate::eq, typeInfo, floatTypeInfo); + return clh.conditionalExpressionLowering( + loc, llvmNumberType, isFloat, + [&](OpBuilder &, Location) -> mlir::Value { + auto floatValue = rewriter.create(loc, rewriter.getF32Type(), exceptionInfo); + return rewriter.create(loc, llvmNumberType, floatValue); + }, + [&](OpBuilder &, Location) -> mlir::Value { + return rewriter.create(loc, llvmNumberType, exceptionInfo); + }); }); } @@ -5115,6 +5130,7 @@ struct SaveCatchVarOpLowering : public TsLlvmPattern SmallVector> 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)}, diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index fab86becc..7d7c14a50 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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") @@ -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") @@ -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 @@ -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 diff --git a/tslang/test/tester/tests/00cast_to_f32.ts b/tslang/test/tester/tests/00cast_to_f32.ts new file mode 100644 index 000000000..fdc2db693 --- /dev/null +++ b/tslang/test/tester/tests/00cast_to_f32.ts @@ -0,0 +1,36 @@ +// Casts to f32 that failed to compile, all reached by unboxing an `any` as an f32 (`e` in +// an untyped catch): `___unbox` 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 b; +} + +function fromIndex(i: index) { + return i; +} + +function fromString(s: string) { + return s; +} + +function fromAny(a: any) { + return a; +} + +function main() { + assert(fromBoolean(true) == 1, "true is 1"); + assert(fromBoolean(false) == 0, "false is 0"); + assert(fromIndex(7) == 7, " of an index"); + assert(fromString("2.5") == 2.5, " of a string"); + + let f: f32 = 2.5; + assert(fromAny(f) == 2.5, " of an any holding an f32"); + + let n: number = 1.5; + assert(fromAny(n) == 1.5, " of an any holding a number"); + + print("done."); +} diff --git a/tslang/test/tester/tests/00throw_f32.ts b/tslang/test/tester/tests/00throw_f32.ts new file mode 100644 index 000000000..ee718613b --- /dev/null +++ b/tslang/test/tester/tests/00throw_f32.ts @@ -0,0 +1,66 @@ +// Throwing and catching an f32. +// +// A thrown f32 described itself as a double: on Windows it used the `.N` records with a size of +// 8, on Linux the `_ZTId` type_info. So `catch (e: number)` took a thrown f32 and read eight +// bytes of a four-byte value - 1.5f came back as 0.125. It is now thrown as a C++ `float` (`.M` +// / `_ZTIf`), and a `number` catch takes it by widening, the same way it takes a thrown int. + +function typedF32() { + let f: f32 = 1.5; + let r: f32 = 0; + try { + throw f; + } catch (e: f32) { + r = e; + } + + return r; +} + +function untypedF32() { + let f: f32 = 2.5; + let r: f32 = 0; + try { + throw f; + } catch (e) { + r = e; + } + + return r; +} + +function f32AsNumber() { + let f: f32 = 1.5; + let r: number = 0; + try { + throw f; + } catch (e: number) { + r = e; + } + + return r; +} + +function numberPassesF32Catch() { + let r: number = 0; + try { + try { + throw 2.25; + } catch (e: f32) { + r = -1; + } + } catch (e2: number) { + r = e2; + } + + return r; +} + +function main() { + assert(typedF32() == 1.5, "catch (e: f32) catches a thrown f32"); + assert(untypedF32() == 2.5, "an untyped catch reads a thrown f32"); + assert(f32AsNumber() == 1.5, "catch (e: number) widens a thrown f32"); + assert(numberPassesF32Catch() == 2.25, "a thrown number passes an f32 catch"); + + print("done."); +}