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
5 changes: 0 additions & 5 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2299,11 +2299,6 @@ namespace mlirgen
ValueOrLogicalResult MLIRGenImpl::mlirGen(NumericLiteral numericLiteral, const GenContext &genContext)
{
auto attrVal = getNumericLiteralAttribute(numericLiteral);
if (!attrVal)
{
emitError(loc(numericLiteral), "integer literal is too large to represent (more than 128 bits)");
return mlir::failure();
}

auto attrType = mlir::cast<mlir::TypedAttr>(attrVal).getType();
auto valueType = isa<mlir::FloatType>(attrType) ? getNumberType() : attrType;
Expand Down
33 changes: 23 additions & 10 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11161,6 +11161,28 @@ class MLIRGenImpl
{
APSInt newVal(wstos(text));

// a literal past 64 bits is a `number`, the nearest double, as in JS: taken as an integer it
// was cut to its low 64 bits (123456789012345680000 read -5670419503621181000), and past 128
// bits it was an error
if (newVal.getActiveBits() > 64)
{
#ifdef NUMBER_F64
auto floatType = builder.getF64Type();
#else
auto floatType = builder.getF32Type();
#endif
llvm::APFloat val(floatType.getFloatSemantics());
val.convertFromAPInt(newVal, newVal.isSigned(), llvm::APFloat::rmNearestTiesToEven);
return builder.getFloatAttr(floatType, val);
}

// [2^63, 2^64): a u64, exactly (`let max: u64 = 18446744073709551615`). The signed string
// parse makes it 65 bits wide, which took a 128-bit type and read back only its low word.
if (newVal.getActiveBits() == 64)
{
return mlir::IntegerAttr::get(builder.getIntegerType(64, false), llvm::APInt(newVal.trunc(64)));
}

auto unsignedVal = false;
auto width = newVal.getBitWidth();
switch (width)
Expand All @@ -11170,22 +11192,13 @@ class MLIRGenImpl
// a literal in [2^31, 2^32) does not fit s32; it takes s64 (default branch), not u32,
// so arithmetic with other integer literals does not wrap
case 64:
case 128:
unsignedVal = true;
break;
default:
//if (width < 8) width = 8; else
//if (width < 16) width = 16; else
if (width < 32) width = 32; else
if (width < 64) width = 64; else
if (width < 128) width = 128;
else
{
// no mlir::Location available at this layer to emitError with;
// return a null Attribute and let the caller (which has the
// literal's location) report it cleanly instead of crashing.
return mlir::Attribute();
}
width = 64;
}

auto type = builder.getIntegerType(width, !unsignedVal);
Expand Down
5 changes: 4 additions & 1 deletion tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ tslang_add_test(NAME test-compile-00-switch COMMAND test-runner "${PROJECT_SOURC
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-integer-literal-past-64-bits COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00integer_literal_past_64_bits.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 @@ -1122,6 +1123,7 @@ tslang_add_test(NAME test-jit-00-switch COMMAND test-runner -jit "${PROJECT_SOUR
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-integer-literal-past-64-bits COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00integer_literal_past_64_bits.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 Expand Up @@ -5819,7 +5821,8 @@ if (WIN32)
set(uncaught_expected_throw_string "error: uncaught exception: not caught")
set(uncaught_expected_throw_class "error: uncaught exception: Failure")
set(uncaught_expected_throw_number "error: uncaught exception: 2.5")
foreach(uncaught_name throw_string throw_class throw_number)
set(uncaught_expected_throw_number_shortest "error: uncaught exception: 1e-7")
foreach(uncaught_name throw_string throw_class throw_number throw_number_shortest)
foreach(uncaught_model gc rc none)
add_test(NAME test-jit-${uncaught_model}-uncaught-${uncaught_name}
COMMAND $<TARGET_FILE:tslang> --emit=jit --no-default-lib -mm=${uncaught_model}
Expand Down
33 changes: 33 additions & 0 deletions tslang/test/tester/tests/00integer_literal_past_64_bits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// An integer literal past 64 bits is a `number`, the nearest double, as in JS: it was cut to its
// low 64 bits (123456789012345680000 read -5670419503621181000), and past 128 bits it was an error.
function main() {
const big = 123456789012345680000;
assert(`${big}` == "123456789012345680000", "past 64 bits");
assert(big > 0, "past 64 bits: positive");
assert(big / 1e20 == 1.234567890123457, "past 64 bits: arithmetic as a number");

// up to 2^64 - 1 a literal is still a u64, exactly
const twoTo63 = 9223372036854775808;
assert(`${twoTo63}` == "9223372036854775808", "2^63: u64");
const maxU64 = 18446744073709551615;
assert(`${maxU64}` == "18446744073709551615", "2^64 - 1: u64");

const twoTo64 = 18446744073709551616;
assert(`${twoTo64}` == "18446744073709552000", "2^64");

const negative = -123456789012345680000;
assert(`${negative}` == "-123456789012345680000", "negative past 64 bits");

const past128 = 1000000000000000000000000000000000000000000;
assert(`${past128}` == "1e+42", "past 128 bits");

const pastDouble = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
assert(`${pastDouble}` == "Infinity", "past the largest double");

// still an integer below 2^63
const fits = 9007199254740993;
assert(`${fits}` == "9007199254740993", "fits s64: stays an integer");

print(big, twoTo63);
print("done.");
}
7 changes: 7 additions & 0 deletions tslang/test/tester/uncaught/throw_number_shortest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// nothing catches it: the error says the number as the program prints it, `1e-7`, not `%.17g`'s
// 9.9999999999999995e-08
function main() {
print("before");
const tenth: number = 0.1;
throw tenth * 1e-6;
}
69 changes: 68 additions & 1 deletion tslang/tslang/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include "llvm/Support/Process.h"

#include <cstdio>
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <typeinfo>
#ifdef _WIN32
Expand Down Expand Up @@ -660,6 +662,71 @@ using JitEntryThunkFn = int (*)(int, char **);
// What an uncaught exception threw, said after "uncaught exception: ", or empty
static char uncaughtDescription[512];

// A thrown number as the program would print it (Number::toString, as ConvertLogic's
// __ts_number_to_string): the fewest digits that read back as the same double, plain decimal for
// a decimal exponent n in -6 < n <= 21, `d.ddde+N` / `e-N` otherwise. `%.17g` said `0.1` as
// 0.10000000000000001.
static void numberText(char *out, size_t size, double value)
{
if (value != value)
{
snprintf(out, size, "NaN");
return;
}

if (value == 0)
{
snprintf(out, size, "0");
return;
}

auto *sign = value < 0 ? "-" : "";
auto magnitude = value < 0 ? -value : value;
if (magnitude > DBL_MAX)
{
snprintf(out, size, "%sInfinity", sign);
return;
}

char scientific[32];
int decimals = 0;
for (; decimals < 16; decimals++)
{
snprintf(scientific, sizeof(scientific), "%.*e", decimals, magnitude);
if (strtod(scientific, nullptr) == magnitude)
{
break;
}
}

if (decimals == 16)
{
snprintf(scientific, sizeof(scientific), "%.*e", decimals, magnitude);
}

char digits[32];
snprintf(digits, sizeof(digits), "%c%.*s", scientific[0], decimals, scientific + 2);
int k = decimals + 1;
int n = atoi(strchr(scientific, 'e') + 1) + 1;
static const char zeros[] = "000000000000000000000";
if (k <= n && n <= 21)
{
snprintf(out, size, "%s%s%.*s", sign, digits, n - k, zeros);
}
else if (0 < n && n <= 21)
{
snprintf(out, size, "%s%.*s.%s", sign, n, digits, digits + n);
}
else if (-6 < n && n <= 0)
{
snprintf(out, size, "%s0.%.*s%s", sign, -n, zeros, digits);
}
else
{
snprintf(out, size, "%s%c%s%se%+d", sign, digits[0], k > 1 ? "." : "", digits + 1, n - 1);
}
}

// The thrown value, read from the exception record of a throw (_CxxThrowException's): the object, and its
// ThrowInfo, whose first catchable type names its type (`.N` a number, `.PEAD` a string, `.PEAVName@@` a class).
// Read while the object is alive, in the filter; a record not shaped so is left undescribed.
Expand Down Expand Up @@ -704,7 +771,7 @@ static void describeUncaught(EXCEPTION_POINTERS *pointers)
}
else if (strcmp(name, ".N") == 0)
{
snprintf(uncaughtDescription, size, "%.17g", *reinterpret_cast<double *>(object));
numberText(uncaughtDescription, size, *reinterpret_cast<double *>(object));
}
else if (strcmp(name, ".H") == 0)
{
Expand Down
Loading