diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index b03056929..33e5eed44 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -302,6 +302,10 @@ // the field's function as a hybrid): the address of the class field a tagged slot's cell names, or // null where the slot can't be written (a method's cell, or an object literal's field) #define INTERFACE_FUNCTION_FIELD_WRITE_ATTR "__function_field_write" +// on an accessor op whose getter is read as a statement of its own (`obj.prop;`), or a load of an +// interface's field read so (`iface.field;`, which may call a getter): kept though nothing uses its result, +// as the call is the point +#define GETTER_STATEMENT_ATTR "__getter_statement" #define INSTANCEOF_NONE_NAME ".instanceOf.none" #define MAIN_ENTRY_NAME "main" #define TS_NEST_ATTRIBUTE "ts.nest" diff --git a/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h b/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h index 87f58dd96..45485b69a 100644 --- a/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h +++ b/tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h @@ -266,9 +266,11 @@ class CastLogicHelper // a wider integer keeps the value: a signed or sign-neutral (iN) one sign-extends, only an unsigned one // zero-extends (castLLVMTypesLogic, which sees signless LLVM types only). `let c: i64 = -7` held // 4294967289, `let n = s.length; n = -1` held 4294967295, and `i16` -300 compared as 65236. + // A bigint is a signed i64 too: `const b: bigint = -5` held 4294967291, so `b < 0` was false. if (auto inIntType = dyn_cast(inType)) { - if (!inIntType.isUnsigned() && inIntType.getWidth() > 1 && (isa(resType) || resType.isIndex()) + if (!inIntType.isUnsigned() && inIntType.getWidth() > 1 + && (isa(resType) || resType.isIndex() || isa(resType)) && resLLVMType.getIntOrFloatBitWidth() > inLLVMType.getIntOrFloatBitWidth()) { return rewriter.create(loc, resLLVMType, in); diff --git a/tslang/include/TypeScript/TypeScriptOps.td b/tslang/include/TypeScript/TypeScriptOps.td index 9539ae8b1..87e6c7581 100644 --- a/tslang/include/TypeScript/TypeScriptOps.td +++ b/tslang/include/TypeScript/TypeScriptOps.td @@ -1461,13 +1461,14 @@ def TypeScript_SwapOp : TypeScript_Op<"Swap", [AllTypesMatch<["first", "second"] ); } -def TypeScript_LoadOp : TypeScript_Op<"Load", []> { +def TypeScript_LoadOp : TypeScript_Op<"Load", [DeclareOpInterfaceMethods]> { let summary = "loads the value from the reference type"; let description = [{ - The `load` operation loads the value from the reference value. + The `load` operation loads the value from the reference value. It reads the reference; one that is + a statement of its own (`iface.field;`, marked __getter_statement) may call a getter and is kept. }]; - let arguments = (ins Arg:$reference); + let arguments = (ins TypeScript_RefOrBoundRefOrValueRef:$reference); let results = (outs AnyType:$result); diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index f00c6db81..89e07581d 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -5210,8 +5210,8 @@ LogicalResult NegativeOpValue(mlir_ts::ArithmeticUnaryOp &unaryOp, mlir::Value o } else if (!type.isIntOrIndex() && type.isIntOrIndexOrFloat()) { - builder.replaceOpWithNewOp(unaryOp, type, clh.createFConstantOf(type.getIntOrFloatBitWidth(), 0.0), - oper); + // negf, not 0.0 - x: that is +0 for x = 0, where -x is -0 (1 / -x was Infinity) + builder.replaceOpWithNewOp(unaryOp, type, oper); return mlir::success(); } else diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index cc09151ab..7fe6342d5 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -6547,6 +6547,20 @@ class MLIRGenImpl auto intType = mlir::cast(intAttr.getType()); auto constType = constantOp.getType(); auto valAttr = intAttr; + // `-0` is the number -0, as in JavaScript: as an integer it was 0, and + // 1 / -0 was Infinity + if (intAttr.getValue().isZero() && isa(constType)) + { +#ifdef NUMBER_F64 + auto floatType = mlir::Float64Type::get(builder.getContext()); +#else + auto floatType = mlir::Float32Type::get(builder.getContext()); +#endif + auto negZero = builder.getFloatAttr(floatType, APFloat::getZero(floatType.getFloatSemantics(), true)); + return (mlir::Value) builder.create( + location, mlir_ts::LiteralType::get(negZero, getNumberType()), negZero); + } + if (intType.isSignless()) { intType = builder.getIntegerType(intType.getWidth(), true); @@ -9272,7 +9286,9 @@ class MLIRGenImpl // sits at this rung because that is where it promotes, not because it is 64 bits wide. // An unsigned type follows the signed and signless ones of its width. Missing from the // ladder, `u64 * 2` found only the literal's s32 and multiplied in 32 bits. - getNumberType(), builder.getF64Type(), builder.getI64Type(), SInt(64), UInt(64), builder.getIndexType(), + // A bigint is a 64-bit integer, and the rung above the others of its width: missing from the + // ladder, `n * 1000000000` found only the literal's s32 and multiplied in 32 bits. + getNumberType(), builder.getF64Type(), getBigIntType(), builder.getI64Type(), SInt(64), UInt(64), builder.getIndexType(), builder.getF32Type(), SInt(32), builder.getI32Type(), UInt(32), builder.getF16Type(), SInt(16), builder.getI16Type(), UInt(16), SInt(8), builder.getI8Type(), UInt(8) @@ -9291,7 +9307,7 @@ class MLIRGenImpl return false; } - return isa(type) || type.isIntOrIndexOrFloat(); + return isa(type) || type.isIntOrIndexOrFloat(); } bool syncTypes(mlir::Location location, mlir::Type type, mlir::Value &leftExpressionValue, mlir::Value &rightExpressionValue, const GenContext &genContext) diff --git a/tslang/lib/TypeScript/MLIRGenStatements.cpp b/tslang/lib/TypeScript/MLIRGenStatements.cpp index 365c11810..417b42802 100644 --- a/tslang/lib/TypeScript/MLIRGenStatements.cpp +++ b/tslang/lib/TypeScript/MLIRGenStatements.cpp @@ -734,6 +734,32 @@ namespace mlirgen } EXIT_IF_FAILED(result) + + // `obj.prop;` calls the getter, as in JavaScript, though nothing uses what it gives: the + // canonicalizer erased an unused getter, and with it whatever the getter does + if (result.value) + { + if (auto *defOp = result.value.getDefiningOp()) + { + if (mlir::isa(defOp)) + { + defOp->setAttr(GETTER_STATEMENT_ATTR, builder.getUnitAttr()); + } + + // an interface's field, which the class may give by a getter: read through a cell of + // accessors when the load is lowered + if (auto loadOp = mlir::dyn_cast(defOp)) + { + if (loadOp.getReference().getDefiningOp()) + { + loadOp->setAttr(GETTER_STATEMENT_ATTR, builder.getUnitAttr()); + } + } + } + } + return mlir::success(); } diff --git a/tslang/lib/TypeScript/TypeScriptOps.cpp b/tslang/lib/TypeScript/TypeScriptOps.cpp index bd2ac79cb..8b5ba24f2 100644 --- a/tslang/lib/TypeScript/TypeScriptOps.cpp +++ b/tslang/lib/TypeScript/TypeScriptOps.cpp @@ -320,8 +320,9 @@ template struct RemoveUnused : public OpRewritePattern LogicalResult matchAndRewrite(T op, PatternRewriter &rewriter) const override { - // report failure when nothing was erased: the greedy driver re-scans after every success - if (!op->getResult(0).use_empty()) + // report failure when nothing was erased: the greedy driver re-scans after every success. A read + // that is a statement of its own may call a getter (an interface's field): kept + if (!op->getResult(0).use_empty() || op->hasAttr(GETTER_STATEMENT_ATTR)) { return failure(); } @@ -376,8 +377,9 @@ template struct RemoveUnusedAccessor : public OpRewritePattern LogicalResult matchAndRewrite(T op, PatternRewriter &rewriter) const override { - // a setter call has side effects; otherwise erase only an unused getter - if (op.getSetValue() || !op.getValue().use_empty()) + // a setter call has side effects; otherwise erase only an unused getter, and not one that is a + // statement of its own (`obj.prop;`), whose call is the point + if (op.getSetValue() || !op.getValue().use_empty() || op->hasAttr(GETTER_STATEMENT_ATTR)) { return failure(); } @@ -519,6 +521,19 @@ void mlir_ts::LoadOp::getCanonicalizationPatterns(RewritePatternSet &results, ML results.insert>(context); } +// A load reads its reference. A field of an interface may be a cell of accessors, and then the load calls +// the getter: one that is a statement of its own (`iface.field;`, __getter_statement) says it writes as +// well, or a load nothing uses was dead and the getter never ran. +void mlir_ts::LoadOp::getEffects(SmallVectorImpl> &effects) +{ + effects.emplace_back(MemoryEffects::Read::get(), &getOperation()->getOpOperand(0), 0, false, + SideEffects::DefaultResource::get()); + if (getOperation()->hasAttr(GETTER_STATEMENT_ATTR)) + { + effects.emplace_back(MemoryEffects::Write::get(), SideEffects::DefaultResource::get()); + } +} + //===----------------------------------------------------------------------===// // ValueOp //===----------------------------------------------------------------------===// diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 1238a2e34..0c71728bf 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -2666,6 +2666,9 @@ tslang_add_test(NAME test-jit-none-strings COMMAND test-runner -jit -mm=none "${ # every single-file test the default model runs, in either tier set(TSLANG_CORPUS + 00negative_zero.ts + 00getter_statement.ts + 00bigint_mixed_arith.ts 00private_name_in.ts 00type_literal_accessors.ts 00array_conversions.ts @@ -5522,6 +5525,21 @@ tslang_add_test(NAME test-compile-own-00-in-union-of-classes COMMAND test-runner tslang_add_test(NAME test-jit-00-in-union-of-classes COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00in_union_of_classes.ts") tslang_add_test(NAME test-jit-own-00-in-union-of-classes COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00in_union_of_classes.ts") +# -0 kept by unary minus and the literal -0; a getter read as a statement runs; a bigint with a number literal or an +# int is done in 64 bits. rc and none run them from TSLANG_CORPUS. +tslang_add_test(NAME test-compile-00-negative-zero COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00negative_zero.ts") +tslang_add_test(NAME test-compile-own-00-negative-zero COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00negative_zero.ts") +tslang_add_test(NAME test-jit-00-negative-zero COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00negative_zero.ts") +tslang_add_test(NAME test-jit-own-00-negative-zero COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00negative_zero.ts") +tslang_add_test(NAME test-compile-00-getter-statement COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00getter_statement.ts") +tslang_add_test(NAME test-compile-own-00-getter-statement COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00getter_statement.ts") +tslang_add_test(NAME test-jit-00-getter-statement COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00getter_statement.ts") +tslang_add_test(NAME test-jit-own-00-getter-statement COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00getter_statement.ts") +tslang_add_test(NAME test-compile-00-bigint-mixed-arith COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00bigint_mixed_arith.ts") +tslang_add_test(NAME test-compile-own-00-bigint-mixed-arith COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00bigint_mixed_arith.ts") +tslang_add_test(NAME test-jit-00-bigint-mixed-arith COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00bigint_mixed_arith.ts") +tslang_add_test(NAME test-jit-own-00-bigint-mixed-arith COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00bigint_mixed_arith.ts") + # Shared (spec 23): counted handles. These are not in the own_test list, whose # test-own-no-counting check forbids __tslang_inc_ref: a handle is counted under own by design. # own_shared_user_named: a program's own class named Shared wins over the built-in (spec 23.1). diff --git a/tslang/test/tester/tests/00bigint_mixed_arith.ts b/tslang/test/tester/tests/00bigint_mixed_arith.ts new file mode 100644 index 000000000..9d2dbddab --- /dev/null +++ b/tslang/test/tester/tests/00bigint_mixed_arith.ts @@ -0,0 +1,27 @@ +// +, - and * of a bigint and a number literal or an int are done in 64 bits: bigint was not on the +// promotion ladder, so the operands met at the literal's s32 and the product was cut to 32 bits +const K = 1000000000; + +function main() { + const s: bigint = 1710505800; + assert(s * 1000000000 == 1710505800000000000, "bigint * literal"); + assert(1000000000 * s == 1710505800000000000, "literal * bigint"); + assert(s * K == 1710505800000000000, "bigint * const"); + const k: int = 1000000000; + assert(s * k == 1710505800000000000, "bigint * int"); + const big: bigint = 9000000000000000000; + assert(big + 1 == 9000000000000000001, "bigint + literal"); + assert(1 + big == 9000000000000000001, "literal + bigint"); + assert(big - 1 == 8999999999999999999, "bigint - literal"); + assert(big > 1, "a comparison keeps the bigint"); + + // an int becomes a bigint sign-extended: zero-extended, -1000000 was 4293967296 + const neg: bigint = -1000000; + assert(neg < 0, "a negative bigint is below 0"); + assert(neg * 2 == -2000000, "a negative bigint * literal"); + const m: int = -5; + const fromInt: bigint = m; + assert(fromInt == -5 && fromInt < 0, "a negative int as a bigint"); + assert(s * -1 == -1710505800, "bigint * a negative literal"); + print("done."); +} diff --git a/tslang/test/tester/tests/00getter_statement.ts b/tslang/test/tester/tests/00getter_statement.ts new file mode 100644 index 000000000..62babcd55 --- /dev/null +++ b/tslang/test/tester/tests/00getter_statement.ts @@ -0,0 +1,75 @@ +// obj.prop; on its own calls the getter, as in JavaScript: the getter was erased as unused, and with it +// what it does (here, count its calls or throw) +interface HasValue { + value: int; +} + +interface HasFailing { + failing: int; +} + +class Plain { + value = 3; +} + +class Counter { + public reads = 0; + + get value(): int { + this.reads++; + return 7; + } + + get failing(): int { + throw "read"; + } + + static count = 0; + + static get next(): int { + Counter.count++; + return Counter.count; + } +} + +function main() { + const c = new Counter(); + c.value; + c.value; + assert(c.reads == 2, "a getter read as a statement runs"); + (c.value); + assert(c.reads == 3, "in parentheses too"); + + Counter.next; + assert(Counter.count == 1, "a static getter"); + + // an interface's field, which the class gives by a getter + const h: HasValue = c; + h.value; + assert(c.reads == 4, "through an interface"); + + let threw = false; + try { + c.failing; + } catch (e) { + threw = true; + } + + assert(threw, "a getter that throws, throws"); + + let threwThroughInterface = false; + const f: HasFailing = c; + try { + f.failing; + } catch (e) { + threwThroughInterface = true; + } + + assert(threwThroughInterface, "through an interface, in a try"); + + // a plain field read so is only a read + const p: HasValue = new Plain(); + p.value; + assert(p.value == 3, "a field"); + print("done."); +} diff --git a/tslang/test/tester/tests/00negative_zero.ts b/tslang/test/tester/tests/00negative_zero.ts new file mode 100644 index 000000000..94152ecf2 --- /dev/null +++ b/tslang/test/tester/tests/00negative_zero.ts @@ -0,0 +1,19 @@ +// -x of a float 0 is -0, as in JavaScript, and so is the literal -0: the negation was 0.0 - x, which is +// +0 for 0, and -0 was the integer 0, so 1 / -0 and 1 / -v were Infinity +function negate(v: number): number { + return -v; +} + +function main() { + assert(1 / negate(0) == -Infinity, "-v of 0 is -0"); + assert(1 / negate(-0) == Infinity, "-v of -0 is 0"); + assert(negate(1.5) == -1.5, "-v of 1.5"); + assert(1 / -0 == -Infinity, "the literal -0"); + const z = -0; + assert(1 / z == -Infinity, "a constant of -0"); + const f: f32 = 0; + assert(1 / -f == -Infinity, "an f32"); + let i: int = 0; + assert(-i == 0, "an int stays an int"); + print("done."); +}