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
4 changes: 4 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion tslang/include/TypeScript/LowerToLLVM/CastLogicHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir::IntegerType>(inType))
{
if (!inIntType.isUnsigned() && inIntType.getWidth() > 1 && (isa<mlir::IntegerType>(resType) || resType.isIndex())
if (!inIntType.isUnsigned() && inIntType.getWidth() > 1
&& (isa<mlir::IntegerType>(resType) || resType.isIndex() || isa<mlir_ts::BigIntType>(resType))
&& resLLVMType.getIntOrFloatBitWidth() > inLLVMType.getIntOrFloatBitWidth())
{
return rewriter.create<LLVM::SExtOp>(loc, resLLVMType, in);
Expand Down
7 changes: 4 additions & 3 deletions tslang/include/TypeScript/TypeScriptOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemoryEffectsOpInterface>]> {
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<TypeScript_RefOrBoundRefOrValueRef, "the reference to load from", [MemRead]>:$reference);
let arguments = (ins TypeScript_RefOrBoundRefOrValueRef:$reference);
let results = (outs AnyType:$result);


Expand Down
4 changes: 2 additions & 2 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5210,8 +5210,8 @@ LogicalResult NegativeOpValue(mlir_ts::ArithmeticUnaryOp &unaryOp, mlir::Value o
}
else if (!type.isIntOrIndex() && type.isIntOrIndexOrFloat())
{
builder.replaceOpWithNewOp<arith::SubFOp>(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<arith::NegFOp>(unaryOp, type, oper);
return mlir::success();
}
else
Expand Down
20 changes: 18 additions & 2 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6547,6 +6547,20 @@ class MLIRGenImpl
auto intType = mlir::cast<mlir::IntegerType>(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<mlir_ts::LiteralType>(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<mlir_ts::ConstantOp>(
location, mlir_ts::LiteralType::get(negZero, getNumberType()), negZero);
}

if (intType.isSignless())
{
intType = builder.getIntegerType(intType.getWidth(), true);
Expand Down Expand Up @@ -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)
Expand All @@ -9291,7 +9307,7 @@ class MLIRGenImpl
return false;
}

return isa<mlir_ts::NumberType>(type) || type.isIntOrIndexOrFloat();
return isa<mlir_ts::NumberType, mlir_ts::BigIntType>(type) || type.isIntOrIndexOrFloat();
}

bool syncTypes(mlir::Location location, mlir::Type type, mlir::Value &leftExpressionValue, mlir::Value &rightExpressionValue, const GenContext &genContext)
Expand Down
26 changes: 26 additions & 0 deletions tslang/lib/TypeScript/MLIRGenStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir_ts::AccessorOp, mlir_ts::ThisAccessorOp, mlir_ts::ThisIndirectAccessorOp,
mlir_ts::ThisIndexAccessorOp, mlir_ts::ThisIndirectIndexAccessorOp,
mlir_ts::BoundIndirectAccessorOp, mlir_ts::BoundIndirectIndexAccessorOp>(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<mlir_ts::LoadOp>(defOp))
{
if (loadOp.getReference().getDefiningOp<mlir_ts::InterfaceSymbolRefOp>())
{
loadOp->setAttr(GETTER_STATEMENT_ATTR, builder.getUnitAttr());
}
}
}
}

return mlir::success();
}

Expand Down
23 changes: 19 additions & 4 deletions tslang/lib/TypeScript/TypeScriptOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ template <typename T> struct RemoveUnused : public OpRewritePattern<T>

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();
}
Expand Down Expand Up @@ -376,8 +377,9 @@ template <typename T> struct RemoveUnusedAccessor : public OpRewritePattern<T>

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();
}
Expand Down Expand Up @@ -519,6 +521,19 @@ void mlir_ts::LoadOp::getCanonicalizationPatterns(RewritePatternSet &results, ML
results.insert<RemoveUnused<mlir_ts::LoadOp>>(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<SideEffects::EffectInstance<MemoryEffects::Effect>> &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
//===----------------------------------------------------------------------===//
Expand Down
18 changes: 18 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<T> (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).
Expand Down
27 changes: 27 additions & 0 deletions tslang/test/tester/tests/00bigint_mixed_arith.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
75 changes: 75 additions & 0 deletions tslang/test/tester/tests/00getter_statement.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
19 changes: 19 additions & 0 deletions tslang/test/tester/tests/00negative_zero.ts
Original file line number Diff line number Diff line change
@@ -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.");
}
Loading