diff --git a/tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h b/tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h index abf49986c..bc9f4c928 100644 --- a/tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h @@ -13,6 +13,8 @@ #include "TypeScript/LowerToLLVM/CodeLogicHelper.h" #include "TypeScript/LowerToLLVM/LLVMCodeHelperBase.h" +#include + using namespace mlir; namespace mlir_ts = mlir::typescript; @@ -28,10 +30,12 @@ class ConvertLogic LLVMCodeHelperBase ch; CodeLogicHelper clh; Location loc; + CompileOptions &compileOptions; public: ConvertLogic(Operation *op, PatternRewriter &rewriter, TypeConverterHelper &tch, Location loc, CompileOptions &compileOptions) - : op(op), rewriter(rewriter), tch(tch), th(rewriter), ch(op, rewriter, tch.typeConverter, compileOptions), clh(op, rewriter), loc(loc) + : op(op), rewriter(rewriter), tch(tch), th(rewriter), ch(op, rewriter, tch.typeConverter, compileOptions), clh(op, rewriter), loc(loc), + compileOptions(compileOptions) { } @@ -53,14 +57,211 @@ class ConvertLogic return newVal; } + // A number as JS writes it (Number::toString, ECMA-262 6.1.6.1.20): the fewest digits that read + // back as the same double, plain decimal for a decimal exponent n in -6 < n <= 21 and `d.ddde+N` + // otherwise; NaN, Infinity, -Infinity, and "0" for -0. `%g` kept 6 digits (`0.1 + 0.2` printed + // 0.3, `2 ** 53 + 2` 9.0072e+015), and the MSVC CRT writes 3-digit exponents. The work is done + // by a routine of the module's own (getOrCreateNumberToStringRoutine), over the CRT's + // snprintf/sprintf_s and strtod, which every target already links for the other conversions and + // parseFloat. mlir::Value sprintfOfF64(mlir::Value value) { #ifdef NUMBER_F64 - auto doubleValue = value; + mlir::Value doubleValue = value; #else - auto doubleValue = rewriter.create(loc, rewriter.getF64Type(), value); + mlir::Value doubleValue = rewriter.create(loc, rewriter.getF64Type(), value); #endif - return sprintf(50, "%g", doubleValue); + auto llvmIndexType = tch.convertType(th.getIndexType()); + auto bufferSizeValue = clh.createIndexConstantOf(llvmIndexType, NUMBER_TO_STRING_BUFFER_SIZE); + + auto routine = getOrCreateNumberToStringRoutine(); + + auto buffer = ch.MemoryAlloc(bufferSizeValue, MemoryAllocSet::Atomic); + rewriter.create(loc, routine, ValueRange{buffer, doubleValue}); + return buffer; + } + + // the longest text is 25 characters: "-0.00000" and 17 digits, or "-d.dddddddddddddddde-308" + static constexpr int NUMBER_TO_STRING_BUFFER_SIZE = 32; + + // `void __ts_number_to_string(ptr out, f64 v)`, internal to the module (a public one in every + // module collides when the modules link together), writing at most + // NUMBER_TO_STRING_BUFFER_SIZE bytes. Digits: for p = 0..16, `%.*e` with p decimals until + // strtod reads the same double back, which gives the shortest correctly rounded digit string + // (a shorter one ending in 0 would have matched first, so it has no trailing zeros). The + // digits and the exponent are then laid out as JS does, by snprintf with `%.*s` pieces. + LLVM::LLVMFuncOp getOrCreateNumberToStringRoutine() + { + constexpr auto name = "__ts_number_to_string"; + auto parentModule = op->getParentOfType(); + if (auto existing = parentModule.lookupSymbol(name)) + { + return existing; + } + + auto ptrTy = th.getPtrType(); + auto i8Ty = th.getI8Type(); + auto i32Ty = rewriter.getI32Type(); + auto f64Ty = rewriter.getF64Type(); + auto llvmIndexType = tch.convertType(th.getIndexType()); + auto noLoc = mlir::UnknownLoc::get(rewriter.getContext()); + + // the target's CRT, not the host's, as ConvertF: sprintf_s is MSVC only + auto printFunc = ch.getOrInsertFunction(compileOptions.isWindows ? "sprintf_s" : "snprintf", + th.getFunctionType(i32Ty, {ptrTy, llvmIndexType, ptrTy}, true)); + auto strtodFunc = ch.getOrInsertFunction("strtod", th.getFunctionType(f64Ty, {ptrTy, ptrTy})); + + OpBuilder::InsertionGuard insertGuard(rewriter); + rewriter.setInsertionPointToStart(parentModule.getBody()); + + auto funcOp = rewriter.create(noLoc, name, th.getFunctionType(th.getVoidType(), {ptrTy, f64Ty}), + LLVM::Linkage::Internal); + + auto *entry = funcOp.addEntryBlock(rewriter); + auto &body = funcOp.getBody(); + auto out = entry->getArgument(0); + auto value = entry->getArgument(1); + rewriter.setInsertionPointToStart(entry); + + auto newBlock = [&](TypeRange types = {}) { + SmallVector locs(types.size(), noLoc); + return rewriter.createBlock(&body, body.end(), types, locs); + }; + + auto i32Const = [&](int32_t v) { return clh.createI32ConstantOf(v); }; + auto text = [&](std::string s) { return ch.getOrCreateGlobalString(s); }; + auto bufferSize = clh.createIndexConstantOf(llvmIndexType, NUMBER_TO_STRING_BUFFER_SIZE); + + auto printTo = [&](mlir::Value dest, std::string format, ArrayRef args) { + SmallVector values{dest, bufferSize, text(format)}; + values.append(args.begin(), args.end()); + rewriter.create(noLoc, printFunc, values); + }; + auto print = [&](std::string format, ArrayRef args) { + printTo(out, format, args); + rewriter.create(noLoc, ValueRange{}); + }; + auto strtod = [&](mlir::Value str) { + auto nullPtr = rewriter.create(noLoc, ptrTy); + return rewriter.create(noLoc, strtodFunc, ValueRange{str, nullPtr}).getResult(); + }; + auto at = [&](mlir::Value base, mlir::Value index) -> mlir::Value { + return rewriter.create(noLoc, ptrTy, i8Ty, base, ArrayRef{index}); + }; + auto icmp = [&](LLVM::ICmpPredicate predicate, mlir::Value l, mlir::Value r) -> mlir::Value { + return rewriter.create(noLoc, predicate, l, r); + }; + auto fcmp = [&](LLVM::FCmpPredicate predicate, mlir::Value l, mlir::Value r) -> mlir::Value { + return rewriter.create(noLoc, predicate, l, r); + }; + + // entry: the scratch buffers, then NaN + auto scratchSize = i32Const(NUMBER_TO_STRING_BUFFER_SIZE); + auto scientific = rewriter.create(noLoc, ptrTy, i8Ty, scratchSize); + auto digits = rewriter.create(noLoc, ptrTy, i8Ty, scratchSize); + auto zero = clh.createF64ConstantOf(0.0); + auto isNegative = fcmp(LLVM::FCmpPredicate::olt, value, zero); + + auto *nanBlock = newBlock(); + auto *notNanBlock = newBlock(); + auto *infinityBlock = newBlock(); + auto *finiteBlock = newBlock(); + auto *zeroBlock = newBlock(); + auto *searchBlock = newBlock(); + auto *loopBlock = newBlock({i32Ty}); + auto *foundBlock = newBlock(); + auto *plainIntegerBlock = newBlock(); + auto *notPlainIntegerBlock = newBlock(); + auto *plainFractionBlock = newBlock(); + auto *notPlainFractionBlock = newBlock(); + auto *smallFractionBlock = newBlock(); + auto *exponentBlock = newBlock(); + + rewriter.setInsertionPointToEnd(entry); + rewriter.create(noLoc, fcmp(LLVM::FCmpPredicate::uno, value, value), nanBlock, notNanBlock); + + rewriter.setInsertionPointToEnd(nanBlock); + print("NaN", {}); + + rewriter.setInsertionPointToEnd(notNanBlock); + mlir::Value magnitude = rewriter.create(noLoc, f64Ty, value); + auto infinity = clh.createF64ConstantOf(std::numeric_limits::infinity()); + rewriter.create(noLoc, fcmp(LLVM::FCmpPredicate::oeq, magnitude, infinity), infinityBlock, + finiteBlock); + + rewriter.setInsertionPointToEnd(infinityBlock); + print("%s", {rewriter.create(noLoc, isNegative, text("-Infinity"), text("Infinity"))}); + + // -0 is "0" as well + rewriter.setInsertionPointToEnd(finiteBlock); + rewriter.create(noLoc, fcmp(LLVM::FCmpPredicate::oeq, value, zero), zeroBlock, searchBlock); + + rewriter.setInsertionPointToEnd(zeroBlock); + print("0", {}); + + rewriter.setInsertionPointToEnd(searchBlock); + mlir::Value sign = rewriter.create(noLoc, isNegative, text("-"), text("")); + rewriter.create(noLoc, ValueRange{i32Const(0)}, loopBlock); + + // loop(p): `d.ddd...e+NN` with p decimals; 17 digits (p = 16) always read back + rewriter.setInsertionPointToEnd(loopBlock); + mlir::Value decimals = loopBlock->getArgument(0); + printTo(scientific, "%.*e", {decimals, magnitude}); + auto readBack = strtod(scientific); + auto exact = fcmp(LLVM::FCmpPredicate::oeq, readBack, magnitude); + auto last = icmp(LLVM::ICmpPredicate::sge, decimals, i32Const(16)); + auto done = rewriter.create(noLoc, exact, last); + auto nextDecimals = rewriter.create(noLoc, decimals, i32Const(1)); + rewriter.create(noLoc, done, foundBlock, ValueRange{}, loopBlock, ValueRange{nextDecimals}); + + // found: k digits, the first at [0] and the rest from [2]; the exponent after the 'e' at + // [1] (no decimals) or [p + 2], read by strtod whatever its width ("e+08", MSVC's "e+008") + rewriter.setInsertionPointToEnd(foundBlock); + auto digitCount = rewriter.create(noLoc, decimals, i32Const(1)); + auto noDecimals = icmp(LLVM::ICmpPredicate::eq, decimals, i32Const(0)); + auto exponentIndex = rewriter.create(noLoc, noDecimals, i32Const(2), + rewriter.create(noLoc, decimals, i32Const(3))); + auto exponent = rewriter.create(noLoc, i32Ty, strtod(at(scientific, exponentIndex))); + // n: the position of the decimal point relative to the digits + mlir::Value n = rewriter.create(noLoc, exponent, i32Const(1)); + mlir::Value firstDigit = rewriter.create(noLoc, i32Ty, rewriter.create(noLoc, i8Ty, scientific)); + printTo(digits, "%c%.*s", {firstDigit, decimals, at(scientific, i32Const(2))}); + auto zeros = text("000000000000000000000"); + auto upTo21 = icmp(LLVM::ICmpPredicate::sle, n, i32Const(21)); + auto plainInteger = rewriter.create(noLoc, icmp(LLVM::ICmpPredicate::sle, digitCount, n), upTo21); + rewriter.create(noLoc, plainInteger, plainIntegerBlock, notPlainIntegerBlock); + + // k <= n <= 21: the digits, then n - k zeros + rewriter.setInsertionPointToEnd(plainIntegerBlock); + print("%s%s%.*s", {sign, digits, rewriter.create(noLoc, n, digitCount), zeros}); + + rewriter.setInsertionPointToEnd(notPlainIntegerBlock); + auto plainFraction = rewriter.create(noLoc, icmp(LLVM::ICmpPredicate::sgt, n, i32Const(0)), upTo21); + rewriter.create(noLoc, plainFraction, plainFractionBlock, notPlainFractionBlock); + + // 0 < n < k: the first n digits, '.', the rest + rewriter.setInsertionPointToEnd(plainFractionBlock); + print("%s%.*s.%s", {sign, n, digits, at(digits, n)}); + + rewriter.setInsertionPointToEnd(notPlainFractionBlock); + auto smallFraction = rewriter.create(noLoc, icmp(LLVM::ICmpPredicate::sgt, n, i32Const(-6)), + icmp(LLVM::ICmpPredicate::sle, n, i32Const(0))); + rewriter.create(noLoc, smallFraction, smallFractionBlock, exponentBlock); + + // -6 < n <= 0: "0.", -n zeros, the digits + rewriter.setInsertionPointToEnd(smallFractionBlock); + print("%s0.%.*s%s", {sign, rewriter.create(noLoc, i32Const(0), n), zeros, digits}); + + // otherwise d[.ddd]e+N / e-N + rewriter.setInsertionPointToEnd(exponentBlock); + auto dot = rewriter.create(noLoc, icmp(LLVM::ICmpPredicate::sgt, digitCount, i32Const(1)), text("."), text("")); + print("%s%c%s%se%+d", {sign, firstDigit, dot, at(digits, i32Const(1)), rewriter.create(noLoc, n, i32Const(1))}); + + // the helpers above take the user's location for the constants and the string addresses; + // a routine of its own has no debug scope to name + funcOp->walk([&](mlir::Operation *nested) { nested->setLoc(noLoc); }); + + return funcOp; } mlir::Value sprintfOfInt(mlir::Value valueIn, int width, bool isSigned) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 3cdb687e9..af036baac 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -452,6 +452,7 @@ tslang_add_test(NAME test-compile-00-cond_expr COMMAND test-runner "${PROJECT_SO tslang_add_test(NAME test-compile-00-switch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00switch.ts") tslang_add_test(NAME test-compile-01-switch COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01switch.ts") tslang_add_test(NAME test-compile-00-strings COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00strings.ts") +tslang_add_test(NAME test-compile-00-number-to-string COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00number_to_string.ts") tslang_add_test(NAME test-compile-00-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00tuple.ts") tslang_add_test(NAME test-compile-01-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/01tuple.ts") tslang_add_test(NAME test-compile-00-tuple-named COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00tuple_named.ts") @@ -1119,6 +1120,7 @@ tslang_add_test(NAME test-jit-00-cond_expr COMMAND test-runner -jit "${PROJECT_S tslang_add_test(NAME test-jit-00-switch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00switch.ts") tslang_add_test(NAME test-jit-01-switch COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01switch.ts") tslang_add_test(NAME test-jit-00-strings COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00strings.ts") +tslang_add_test(NAME test-jit-00-number-to-string COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00number_to_string.ts") tslang_add_test(NAME test-jit-00-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00tuple.ts") tslang_add_test(NAME test-jit-01-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/01tuple.ts") tslang_add_test(NAME test-jit-00-tuple-named COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00tuple_named.ts") diff --git a/tslang/test/tester/tests/00number_to_string.ts b/tslang/test/tester/tests/00number_to_string.ts new file mode 100644 index 000000000..ee3c30117 --- /dev/null +++ b/tslang/test/tester/tests/00number_to_string.ts @@ -0,0 +1,83 @@ +// A number becomes text as JS writes it (Number::toString): the fewest digits that read back as +// the same double, plain decimal while the decimal exponent n is in -6 < n <= 21, `d.ddde+N` / +// `d.ddde-N` otherwise, NaN / Infinity / -Infinity, and "0" for -0. `%g` kept 6 digits (#780). +// (String(x) is the default library's, over the same cast; these tests build without it.) +function concat(x: number) { + return "" + x; +} + +function template(x: number) { + return `${x}`; +} + +function converted(x: number) { + return x; +} + +function check(x: number, expected: string) { + const viaConcat = concat(x); + const viaTemplate = template(x); + const viaCast = converted(x); + if (viaConcat != expected || viaTemplate != expected || viaCast != expected) { + print("expected", expected, "got", viaConcat, viaTemplate, viaCast); + assert(false, "number to string"); + } +} + +function main() { + const two: number = 2; + const zero: number = 0; + const one: number = 1; + const three: number = 3; + + check(0.1 + 0.2, "0.30000000000000004"); + check(one / three, "0.3333333333333333"); + check(two / three, "0.6666666666666666"); + check(123456789.123456789, "123456789.12345679"); + check(two ** 53 + 2, "9007199254740994"); + check(two ** 53, "9007199254740992"); + check(two ** 60, "1152921504606847000"); + check(1e-7, "1e-7"); + check(1.5e-7, "1.5e-7"); + check(0.000001, "0.000001"); + check(0.0000012345, "0.0000012345"); + check(123e-20, "1.23e-18"); + check(5e-324, "5e-324"); + check(-5e-324, "-5e-324"); + check(2.2250738585072014e-308, "2.2250738585072014e-308"); + check(1.7976931348623157e308, "1.7976931348623157e+308"); + check(1e21, "1e+21"); + check(1e20, "100000000000000000000"); + check(1.2345678901234568e20, "123456789012345680000"); + check(1e23, "1e+23"); + check(8.41e21, "8.41e+21"); + check(4.35, "4.35"); + check(0.1, "0.1"); + check(-0.1, "-0.1"); + check(1.5, "1.5"); + check(-2.5, "-2.5"); + check(100, "100"); + check(-100, "-100"); + check(1, "1"); + check(123.456, "123.456"); + check(0.5, "0.5"); + check(1e100, "1e+100"); + check(-1.5e-10, "-1.5e-10"); + check(zero, "0"); + check(-zero, "0"); + check(zero / zero, "NaN"); + check(one / zero, "Infinity"); + check(-one / zero, "-Infinity"); + + // a literal, not only a value computed at run time + assert(`${0.1}` == "0.1", "literal in a template"); + assert("" + 1e21 == "1e+21", "literal concatenated"); + assert(1e-7 == "1e-7", "literal cast to string"); + + print(0.1 + 0.2); + print(one / three); + print(1e21, 1e-7, -zero); + print(zero / zero, one / zero, -one / zero); + + print("done."); +} diff --git a/tslang/tslang/jit.cpp b/tslang/tslang/jit.cpp index f4d345abb..7b68489ff 100644 --- a/tslang/tslang/jit.cpp +++ b/tslang/tslang/jit.cpp @@ -641,6 +641,19 @@ static void *jitTypeInfoVftable() } #endif +#ifdef _WIN32 +// sprintf_s and strtod write and read a number as text (`"" + x`, parseFloat). Left to the process +// resolver they bind to another CRT DLL than tslang.exe's static UCRT: one whose `%.*e` is not +// correctly rounded and writes 3-digit exponents, so the JIT printed `155280381172789.87` where +// an executable prints `155280381172789.88` (#780). sprintf_s is inline in the UCRT headers, with +// C++ template overloads beside it: name the C one by its type. +static void *jitSprintfS() +{ + int (*sprintfS)(char *, size_t, const char *, ...) = &sprintf_s; + return (void *)sprintfS; +} +#endif + using JitEntryThunkFn = int (*)(int, char **); #ifdef _WIN32 @@ -1016,6 +1029,9 @@ static int prepareJitProcess(CompileOptions &compileOptions, bool nativeImage) #ifdef _WIN32 // the bounded copy a Windows target emits (LLVMCodeHelperBase::MemoryCopy) addSym("memcpy_s", (void*)&memcpy_s); + // number to string (ConvertLogic) and parseFloat; see jitSprintfS + addSym("sprintf_s", jitSprintfS()); + addSym("strtod", (void*)&strtod); #endif addSym("fflush", (void*)&fflush); // see jitAssertFailed above: bound to ucrtbase this is a modal message box, which an @@ -1180,6 +1196,9 @@ static std::unique_ptr createJit(llvm::orc::JITTargetMachineBu addOverride("memcpy", (void *)&memcpy); #ifdef _WIN32 addOverride("memcpy_s", (void *)&memcpy_s); + // see jitSprintfS + addOverride("sprintf_s", jitSprintfS()); + addOverride("strtod", (void *)&strtod); #endif addOverride("fflush", (void *)&fflush); // see jitAssertFailed above: bound to ucrtbase this is a modal message box, which an