diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index e3ba368f4..d4cd53956 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -845,6 +845,12 @@ namespace mlirgen { CAST(numberValue, location, builder.getI32Type(), expressionValue, genContext); } + // a number literal above int32 (a `const` of one): ToInt32 of it, as in JavaScript (TSLANG 234) + else if (isIntegerLiteralValue(expressionValue)) + { + CAST(numberValue, location, mlir::IntegerType::get(builder.getContext(), 32, mlir::IntegerType::Signed), + expressionValue, genContext); + } return V(builder.create( location, numberValue.getType(), builder.getI32IntegerAttr((int)opCode), numberValue)); diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 22fe49401..0a0d58f73 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -1419,6 +1419,26 @@ class MLIRGenImpl // A parameter answers yes on the same terms a local does. Being captured is what makes // storage a cell, and a parameter's storage is the same `ts.Variable` by the time the // affine pass has run - ParamOpLowering builds one, carrying the marking across. + // A number literal too big for an s32 (or a `const` of one): an s64 for its size only, a JavaScript number to `^`, + // `|`, `~` (TSLANG 234). Its constant, read as it is or cast to the s64 it is stored as. + static bool isIntegerLiteralValue(mlir::Value value) + { + while (auto castOp = value.getDefiningOp()) + { + if (!isa(castOp.getIn().getType())) + { + return false; + } + + value = castOp.getIn(); + } + + auto constantOp = value.getDefiningOp(); + auto literalType = constantOp ? dyn_cast(constantOp.getType()) : mlir_ts::LiteralType(); + auto storageType = literalType ? dyn_cast(literalType.getElementType()) : mlir::IntegerType(); + return storageType && storageType.isSigned() && storageType.getWidth() == 64 && isa(constantOp.getValue()); + } + static bool isCapturedVariableCell(mlir::Value reference) { if (auto varOp = reference.getDefiningOp()) @@ -8428,7 +8448,16 @@ class MLIRGenImpl // check if you can use it on 64 bits, check JS code for it value = mlir::TypeSwitch(valueAttr) - .Case([&](auto intAttr) { + .Case([&](auto intAttr) -> mlir::Value { + // a number literal above int32 (an s64 for its size): `~` of ToInt32 of it, an s32, as in + // JavaScript (`~0x80000000` is 2147483647, TSLANG 234) + if (isIntegerLiteralValue(constantOp)) + { + auto s32Type = mlir::IntegerType::get(builder.getContext(), 32, mlir::IntegerType::Signed); + return builder.create( + location, s32Type, builder.getIntegerAttr(s32Type, ~intAttr.getValue().trunc(32))); + } + return builder.create( location, constantOp.getType(), builder.getIntegerAttr(intAttr.getType(), ~intAttr.getValue())); }) @@ -12716,12 +12745,22 @@ class MLIRGenImpl mlir::Type intType = opCode == SyntaxKind::GreaterThanGreaterThanGreaterThanToken ? builder.getI32Type() : mlir::IntegerType::get(builder.getContext(), 32, mlir::IntegerType::Signed); - if (auto leftIntType = dyn_cast(leftExpressionValue.getType())) + // ...but a number literal above int32 (an s64 for its size, or a `const` of one) is a JavaScript + // number: ToInt32 of it, as `0x9e3779b9 ^ 5` and `big | 0` are in JavaScript (USER RULING 2026-12-04, + // TSLANG 234; it kept 64 bits). A 64-bit variable keeps its width, and the literal goes to its type. + auto isWideVariable = [&](mlir::Value value) { + auto valueIntType = dyn_cast(value.getType()); + return valueIntType && valueIntType.getWidth() > 32 && !isIntegerLiteralValue(value); + }; + + if (isWideVariable(leftExpressionValue)) { - if (leftIntType.getWidth() > 32) - { - intType = leftIntType; - } + intType = leftExpressionValue.getType(); + } + else if ((opCode == SyntaxKind::AmpersandToken || opCode == SyntaxKind::BarToken || opCode == SyntaxKind::CaretToken) && + isWideVariable(rightExpressionValue) && isIntegerLiteralValue(leftExpressionValue)) + { + intType = rightExpressionValue.getType(); } if (leftExpressionValue.getType() != intType) @@ -22731,7 +22770,9 @@ class MLIRGenImpl if (showWarnings && symbolTable.count(name) && !genContext.allocateVarsInContextThis) { auto previousVariable = symbolTable.lookup(name).first; - if (previousVariable && previousVariable.getParentBlock() == value.getParentBlock()) + // (the same place named again is no other variable: the `.a` of two nested `for...of` over one array + // is that array's variable, TSLANG 234) + if (previousVariable && previousVariable != value && previousVariable.getParentBlock() == value.getParentBlock()) { LLVM_DEBUG(llvm::dbgs() << "\n!! WARNING redeclaration: " << name << " = [" << value << "]\n";); // TODO: find out why you have redeclared vars diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 6048c0b59..84913d5b7 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -2795,6 +2795,18 @@ foreach(issue235_mm gc rc none own) tslang_add_test(NAME test-jit-00tslang_issue_235_types-${issue235_mm} COMMAND test-runner -jit -mm=${issue235_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_235_types.ts") tslang_add_test(NAME test-compile-00tslang_issue_235_types-${issue235_mm} COMMAND test-runner -mm=${issue235_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_235_types.ts") endforeach() +# TSLANG 234: a number literal above int32 given to a bitwise operator is ToInt32'd; nested for...of over one array +foreach(issue234_mm gc rc none own) + tslang_add_test(NAME test-jit-00tslang_issue_234-${issue234_mm} COMMAND test-runner -jit -mm=${issue234_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_234.ts") + tslang_add_test(NAME test-compile-00tslang_issue_234-${issue234_mm} COMMAND test-runner -mm=${issue234_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_234.ts") +endforeach() +# ...with no warning ("variable .a redeclared" of the inner loop) +add_test(NAME test-compile-00tslang_issue_234-quiet + COMMAND $ --emit=obj --no-default-lib -mm=none + "${PROJECT_SOURCE_DIR}/test/tester/tests/00tslang_issue_234.ts" + -o "${CMAKE_CURRENT_BINARY_DIR}/00tslang_issue_234-quiet.obj") +set_tests_properties(test-compile-00tslang_issue_234-quiet + PROPERTIES FAIL_REGULAR_EXPRESSION "warning:|error:|Stack dump|Assertion failed") add_test(NAME test-compile-declared-method-overloads-warning COMMAND $ --emit=obj --no-default-lib -mm=none "${PROJECT_SOURCE_DIR}/test/tester/tests/declared_method_overloads_warning.ts" diff --git a/tslang/test/tester/tests/00tslang_issue_234.ts b/tslang/test/tester/tests/00tslang_issue_234.ts new file mode 100644 index 000000000..d8e638601 --- /dev/null +++ b/tslang/test/tester/tests/00tslang_issue_234.ts @@ -0,0 +1,52 @@ +// TSLANG 234: a number literal above int32 (an s64 for its size, or a `const` of one) given to `^`, `|`, `&`, `~`, `<<`, +// `>>` is a JavaScript number: ToInt32 of it, as JavaScript's hashes and PRNGs rely on (`x | 0`). It was done in 64 +// bits: `0x9e3779b9 ^ 5` was 2654435772, not -1640531524 (USER RULING 2026-12-04: literal operands only; a `u64`/`s64` +// variable keeps 64-bit bitwise math, 00int64_shift_bitwise.ts). Also: two nested `for...of` over one array warned +// "variable .a redeclared". + +function main() { + const k = 0x9e3779b9; + const big = 3663131626; + + assert(`${0x9e3779b9 ^ 5}` == "-1640531524", "literal ^"); + assert(`${k ^ 5}` == "-1640531524", "const ^"); + assert(`${k | 0}` == "-1640531527", "const | 0"); + assert(`${big | 0}` == "-631835670", "big | 0"); + assert(`${big ^ 0}` == "-631835670", "big ^ 0"); + assert(`${big >>> 0}` == "3663131626", "big >>> 0"); + assert(`${0xFFFFFFFF | 0}` == "-1", "0xFFFFFFFF | 0"); + assert(`${0xFFFFFFFF & 0xFF}` == "255", "0xFFFFFFFF & 0xFF"); + assert(`${k >> 1}` == "-820265764", "const >> 1"); + assert(`${k << 1}` == "1013904242", "const << 1"); + assert(`${0x80000000 >> 0}` == "-2147483648", "0x80000000 >> 0"); + assert(`${k >>> 0}` == "2654435769", "const >>> 0"); + + // `~`: folded, and of a const + assert(`${~0x80000000}` == "2147483647", "~literal"); + assert(`${~k}` == "1640531526", "~const"); + + // an int operand on the left was right already, and still is + let h = 7; + h ^= 0x9e3779b9; + assert(`${h}` == "-1640531522", "s32 ^= literal"); + + // a 64-bit variable keeps its width; the literal goes to its type + let m: u64 = 0xFFFFFFFFFF; + let one: s64 = 1; + assert(`${m & 0xFF00000000}` == "1095216660480", "u64 & literal"); + assert(`${0xFF00000000 & m}` == "1095216660480", "literal & u64"); + assert(`${m ^ 0x9e3779b9}` == "1096857192006", "u64 ^ literal"); + assert(`${one << 40}` == "1099511627776", "s64 << 40"); + + // nested for...of over one array: no warning, the right sum + const arr = [3, 1, 2]; + let c = 0; + for (const r of arr) { + for (const o of arr) { + c += r * o; + } + } + + assert(c == 36, "nested for...of"); + print("done."); +}