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
209 changes: 205 additions & 4 deletions tslang/include/TypeScript/LowerToLLVM/ConvertLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "TypeScript/LowerToLLVM/CodeLogicHelper.h"
#include "TypeScript/LowerToLLVM/LLVMCodeHelperBase.h"

#include <limits>

using namespace mlir;
namespace mlir_ts = mlir::typescript;

Expand All @@ -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)
{
}

Expand All @@ -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<LLVM::FPExtOp>(loc, rewriter.getF64Type(), value);
mlir::Value doubleValue = rewriter.create<LLVM::FPExtOp>(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<LLVM::CallOp>(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<ModuleOp>();
if (auto existing = parentModule.lookupSymbol<LLVM::LLVMFuncOp>(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<LLVM::LLVMFuncOp>(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<mlir::Location> 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<mlir::Value> args) {
SmallVector<mlir::Value> values{dest, bufferSize, text(format)};
values.append(args.begin(), args.end());
rewriter.create<LLVM::CallOp>(noLoc, printFunc, values);
};
auto print = [&](std::string format, ArrayRef<mlir::Value> args) {
printTo(out, format, args);
rewriter.create<LLVM::ReturnOp>(noLoc, ValueRange{});
};
auto strtod = [&](mlir::Value str) {
auto nullPtr = rewriter.create<LLVM::ZeroOp>(noLoc, ptrTy);
return rewriter.create<LLVM::CallOp>(noLoc, strtodFunc, ValueRange{str, nullPtr}).getResult();
};
auto at = [&](mlir::Value base, mlir::Value index) -> mlir::Value {
return rewriter.create<LLVM::GEPOp>(noLoc, ptrTy, i8Ty, base, ArrayRef<LLVM::GEPArg>{index});
};
auto icmp = [&](LLVM::ICmpPredicate predicate, mlir::Value l, mlir::Value r) -> mlir::Value {
return rewriter.create<LLVM::ICmpOp>(noLoc, predicate, l, r);
};
auto fcmp = [&](LLVM::FCmpPredicate predicate, mlir::Value l, mlir::Value r) -> mlir::Value {
return rewriter.create<LLVM::FCmpOp>(noLoc, predicate, l, r);
};

// entry: the scratch buffers, then NaN
auto scratchSize = i32Const(NUMBER_TO_STRING_BUFFER_SIZE);
auto scientific = rewriter.create<LLVM::AllocaOp>(noLoc, ptrTy, i8Ty, scratchSize);
auto digits = rewriter.create<LLVM::AllocaOp>(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<LLVM::CondBrOp>(noLoc, fcmp(LLVM::FCmpPredicate::uno, value, value), nanBlock, notNanBlock);

rewriter.setInsertionPointToEnd(nanBlock);
print("NaN", {});

rewriter.setInsertionPointToEnd(notNanBlock);
mlir::Value magnitude = rewriter.create<LLVM::FAbsOp>(noLoc, f64Ty, value);
auto infinity = clh.createF64ConstantOf(std::numeric_limits<double>::infinity());
rewriter.create<LLVM::CondBrOp>(noLoc, fcmp(LLVM::FCmpPredicate::oeq, magnitude, infinity), infinityBlock,
finiteBlock);

rewriter.setInsertionPointToEnd(infinityBlock);
print("%s", {rewriter.create<LLVM::SelectOp>(noLoc, isNegative, text("-Infinity"), text("Infinity"))});

// -0 is "0" as well
rewriter.setInsertionPointToEnd(finiteBlock);
rewriter.create<LLVM::CondBrOp>(noLoc, fcmp(LLVM::FCmpPredicate::oeq, value, zero), zeroBlock, searchBlock);

rewriter.setInsertionPointToEnd(zeroBlock);
print("0", {});

rewriter.setInsertionPointToEnd(searchBlock);
mlir::Value sign = rewriter.create<LLVM::SelectOp>(noLoc, isNegative, text("-"), text(""));
rewriter.create<LLVM::BrOp>(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<LLVM::OrOp>(noLoc, exact, last);
auto nextDecimals = rewriter.create<LLVM::AddOp>(noLoc, decimals, i32Const(1));
rewriter.create<LLVM::CondBrOp>(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<LLVM::AddOp>(noLoc, decimals, i32Const(1));
auto noDecimals = icmp(LLVM::ICmpPredicate::eq, decimals, i32Const(0));
auto exponentIndex = rewriter.create<LLVM::SelectOp>(noLoc, noDecimals, i32Const(2),
rewriter.create<LLVM::AddOp>(noLoc, decimals, i32Const(3)));
auto exponent = rewriter.create<LLVM::FPToSIOp>(noLoc, i32Ty, strtod(at(scientific, exponentIndex)));
// n: the position of the decimal point relative to the digits
mlir::Value n = rewriter.create<LLVM::AddOp>(noLoc, exponent, i32Const(1));
mlir::Value firstDigit = rewriter.create<LLVM::SExtOp>(noLoc, i32Ty, rewriter.create<LLVM::LoadOp>(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<LLVM::AndOp>(noLoc, icmp(LLVM::ICmpPredicate::sle, digitCount, n), upTo21);
rewriter.create<LLVM::CondBrOp>(noLoc, plainInteger, plainIntegerBlock, notPlainIntegerBlock);

// k <= n <= 21: the digits, then n - k zeros
rewriter.setInsertionPointToEnd(plainIntegerBlock);
print("%s%s%.*s", {sign, digits, rewriter.create<LLVM::SubOp>(noLoc, n, digitCount), zeros});

rewriter.setInsertionPointToEnd(notPlainIntegerBlock);
auto plainFraction = rewriter.create<LLVM::AndOp>(noLoc, icmp(LLVM::ICmpPredicate::sgt, n, i32Const(0)), upTo21);
rewriter.create<LLVM::CondBrOp>(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<LLVM::AndOp>(noLoc, icmp(LLVM::ICmpPredicate::sgt, n, i32Const(-6)),
icmp(LLVM::ICmpPredicate::sle, n, i32Const(0)));
rewriter.create<LLVM::CondBrOp>(noLoc, smallFraction, smallFractionBlock, exponentBlock);

// -6 < n <= 0: "0.", -n zeros, the digits
rewriter.setInsertionPointToEnd(smallFractionBlock);
print("%s0.%.*s%s", {sign, rewriter.create<LLVM::SubOp>(noLoc, i32Const(0), n), zeros, digits});

// otherwise d[.ddd]e+N / e-N
rewriter.setInsertionPointToEnd(exponentBlock);
auto dot = rewriter.create<LLVM::SelectOp>(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<LLVM::SubOp>(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)
Expand Down
2 changes: 2 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
83 changes: 83 additions & 0 deletions tslang/test/tester/tests/00number_to_string.ts
Original file line number Diff line number Diff line change
@@ -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 <string>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(<string>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.");
}
19 changes: 19 additions & 0 deletions tslang/tslang/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1180,6 +1196,9 @@ static std::unique_ptr<llvm::orc::LLJIT> 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
Expand Down
Loading