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
8 changes: 7 additions & 1 deletion TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,13 @@ Still open, looked at:
- A spread of a dictionary into one of another value type is an error for now: inferring `f<T, S>(a: Box<T>, b:
Box<S>)` from `f(Box<number>, Box<string>)` gives T = number | string and S = any, and the call then reads the
second argument with the wrong layout (a crash) - a generic-inference bug of its own.
- `a[a.length] = x` on an array does not grow it (gc writes past the end; rc and own crash).
- `a[a.length] = x` on an array did not grow it (gc wrote past the end; rc and own crashed). Fixed under gc, rc and
none: the element store grows the array to `i + 1` first when `i` is at or past the length
(mlirGenGrowArrayForWrite, SetLengthOf: the slots in between read as the element type's default; a negative
index grows nothing). Test `00array_write_grows.ts`. Still open under -mm=own: growing through the array's
slot - a parameter's, or a `let`'s over one - reads to its passes as keeping the parameter ("'src' is a
parameter, kept here"), and a field's extra read of the array as a second reference; under own the write is
as before.

Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT, rc, own).

Expand Down
98 changes: 98 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8730,6 +8730,7 @@ class MLIRGenImpl
// rc). The array and the index are the ones the left side evaluated, as JavaScript has it.
if (auto elementRefOp = storeReference.getDefiningOp<mlir_ts::ElementRefOp>())
{
mlirGenGrowArrayForWrite(location, elementRefOp);
storeReference = builder.clone(*elementRefOp)->getResult(0);
}

Expand Down Expand Up @@ -20171,6 +20172,103 @@ class MLIRGenImpl
return loc(loc_);
}

// `a[i] = v` with `i` at or past the end of the array grows it to `i + 1`, as JavaScript does (`a[a.length] =
// v` appends): the slots in between read as the element type's default (SetLengthOf zeroes them). Before, the
// write went past the end of the block - under gc into whatever lay there, under rc and own a crash - and the
// length stayed. A negative index grows nothing. Only an array with a place to update (a local, a field):
// one made for the statement only has nobody to see it grown.
void mlirGenGrowArrayForWrite(mlir::Location location, mlir_ts::ElementRefOp elementRefOp)
{
auto array = elementRefOp.getArray();
if (!isa<mlir_ts::ArrayType>(array.getType()))
{
return;
}

MLIRCodeLogic mcl(builder, compileOptions);
auto arrayRef = mcl.GetReferenceFromValue(location, array);
if (!arrayRef || !isa<mlir_ts::RefType>(arrayRef.getType()))
{
return;
}

// Not under -mm=own yet: the array's slot written - a parameter's, a `let` over one ("'src' is a parameter,
// kept here") - and the array read once more before the store (a field's: "a value takes a second
// reference") are ways its passes do not take yet (TSLANG 152)
if (compileOptions.memoryModel == MemoryModelOwn)
{
return;
}

// index >= length, signed: a negative index is never past the end
auto index32 = elementRefOp.getIndex();
auto i32Type = builder.getI32Type();
auto length = builder.create<mlir_ts::LengthOfOp>(location, builder.getIndexType(), array);
auto length32 = builder.create<mlir_ts::CastOp>(location, i32Type, length);
auto condition = builder.create<mlir_ts::LogicalBinaryOp>(
location, getBooleanType(), builder.getI32IntegerAttr((int)SyntaxKind::GreaterThanEqualsToken), index32, length32);

auto ifOp = builder.create<mlir_ts::IfOp>(location, condition, false);
mlir::OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
auto one = builder.create<mlir_ts::ConstantOp>(location, i32Type, builder.getI32IntegerAttr(1));
auto newLength32 = builder.create<mlir_ts::ArithmeticBinaryOp>(
location, i32Type, builder.getI32IntegerAttr((int)SyntaxKind::PlusToken), index32, one);
auto newLength = builder.create<mlir_ts::CastOp>(location, builder.getIndexType(), newLength32);

// the growth itself in a function of its own, one for each array type: SetLengthOf lowers to a capacity
// check, a reallocation and the zeroing of the new slots, and made at every write, a function of many of
// them (the sweep's largeControlFlowGraph, 10000 `data[0] = 0`) took a minute to lower to LLVM
auto growFunc = arrayGrowFunction(location, mlir::cast<mlir_ts::RefType>(arrayRef.getType()));
builder.create<mlir_ts::CallIndirectOp>(location, growFunc, mlir::ValueRange{arrayRef, newLength});
}

// `.array_grow.<type>(array: ref<T[]>, length: index)`: SetLengthOf (mlirGenGrowArrayForWrite)
mlir::Value arrayGrowFunction(mlir::Location location, mlir_ts::RefType arrayRefType)
{
auto funcType = mlir_ts::FunctionType::get(builder.getContext(), {arrayRefType, builder.getIndexType()}, {}, false);

std::string typeText;
llvm::raw_string_ostream typeStream(typeText);
typeStream << arrayRefType;
auto name = ".array_grow." + llvm::utohexstr(llvm::xxh3_64bits(typeText));
if (!lookupModuleSymbol(name))
{
mlir::OpBuilder::InsertionGuard guard(builder);
builder.setInsertionPointToStart(theModule.getBody());

// compiler glue, as fewerParametersThunk's: without --di the place it is made for, with --di a
// subprogram of its own there (its lowering's calls need a location)
mlir::Location noLocation = compileOptions.generateDebugInfo
? mlir::Location(mlir::UnknownLoc::get(builder.getContext()))
: stripMetadata(location);
auto funcOp = mlir_ts::FuncOp::create(noLocation, name, funcType);
funcOp.setPrivate();
funcOp->setAttr("internal_linkage", builder.getUnitAttr());

DITableScopeT debugFuncScope(debugScope);
if (compileOptions.generateDebugInfo)
{
MLIRDebugInfoHelper mdi(builder, debugScope);
auto plainLocation = stripMetadata(location);
funcOp->setLoc(mdi.getSubprogram(plainLocation, name, name, plainLocation));
noLocation = locFuseWithScope(plainLocation);
}

auto &entryBlock = *funcOp.addEntryBlock();
builder.setInsertionPointToStart(&entryBlock);
auto entryOp = builder.create<mlir_ts::EntryOp>(noLocation, mlir::Type());
builder.create<mlir_ts::SetLengthOfOp>(noLocation, entryBlock.getArgument(0), entryBlock.getArgument(1));
builder.create<mlir_ts::ExitOp>(noLocation, mlir::Value());
(void)entryOp;

pushFunction(funcOp);
}

return builder.create<mlir_ts::SymbolRefOp>(location, funcType,
mlir::FlatSymbolRefAttr::get(builder.getContext(), name));
}

mlir::LogicalResult parsePartialStatements(string src)
{
GenContext emptyContext{};
Expand Down
1 change: 1 addition & 0 deletions tslang/test/android/android_skips.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ json.ts none # (default library) builds a 100000-character st
# refused by design under the model, as in ctest (which registers them under the other models only)
00weak_field.ts rc # '@weak' is not supported under -mm=rc (own spec 93): ctest runs it under gc, none and own
00let_without_initializer_owns.ts own # copies an `any` holding an object, which -mm=own refuses at run time: ctest runs it under gc, rc and none
00array_write_grows.ts own # a[i] = v past the end does not grow the array under -mm=own yet (TSLANG 152): ctest runs it under gc and rc
3 changes: 3 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ tslang_add_test(NAME test-compile-00-object-method-arity COMMAND test-runner "${
tslang_add_test(NAME test-compile-00-generator-next-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts")
tslang_add_test(NAME test-compile-00-generator-return-throw COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_return_throw.ts")
tslang_add_test(NAME test-compile-00-any-undefined-field COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
tslang_add_test(NAME test-compile-00-array-write-grows COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts")
tslang_add_test(NAME test-compile-00-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
tslang_add_test(NAME test-compile-00-string-concat-chain COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00string_concat_chain.ts")
tslang_add_test(NAME test-compile-00-arrays COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array.ts")
Expand Down Expand Up @@ -1316,6 +1317,8 @@ tslang_add_test(NAME test-jit-own-00-generator-return-throw COMMAND test-runner
tslang_add_test(NAME test-jit-00-any-undefined-field COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
tslang_add_test(NAME test-jit-rc-00-any-undefined-field COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
tslang_add_test(NAME test-jit-own-00-any-undefined-field COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00any_undefined_field.ts")
tslang_add_test(NAME test-jit-00-array-write-grows COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts")
tslang_add_test(NAME test-jit-rc-00-array-write-grows COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_grows.ts")
tslang_add_test(NAME test-jit-00-constructor-type-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
tslang_add_test(NAME test-jit-rc-00-constructor-type-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
tslang_add_test(NAME test-jit-own-00-constructor-type-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
Expand Down
40 changes: 40 additions & 0 deletions tslang/test/tester/tests/00array_write_grows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// `a[i] = v` with `i` at or past the end of the array grows it to `i + 1`, as in JavaScript
// (`a[a.length] = v` appends); the slots in between read as the element type's default. Before, the
// length stayed and the write went past the end of the block: under gc into whatever lay there, under
// rc and own a crash. (Not under -mm=own yet: TSLANG 152.)
class Item {
constructor(public n: number) {}
}

class Holder {
items: Item[] = [];
}

function main() {
const a: number[] = [1, 2];
a[a.length] = 3;
assert(a.length == 3 && a[2] == 3, "append");

// a hole: the slots between read as the default
a[6] = 7;
assert(a.length == 7 && a[3] == 0 && a[5] == 0 && a[6] == 7, "hole");

// strings, built at run time, under rc and own owned by the array; one overwritten
const s: string[] = [];
for (let i = 0; i < 100; i++) s[i] = "v" + i;
s[50] = "x" + 50;
assert(s.length == 100 && s[99] == "v99" && s[50] == "x50", "strings");

// objects, in a field
const h = new Holder();
for (let i = 0; i < 50; i++) h.items[h.items.length] = new Item(i);
let total: number = 0;
for (const item of h.items) total += item.n;
assert(h.items.length == 50 && total == 1225, "objects in a field");

// within the length: no change
a[0] = 10;
assert(a.length == 7 && a[0] == 10, "inside");

print("done.");
}
1 change: 1 addition & 0 deletions tslang/test/wasm/wasm_skips.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ json.ts none # (default library) builds a 100000-character st
01tuple.ts own # frees a block it does not own (native own: the program does not finish either)
00global_interface_initializer.ts own # asserts "a field read" (native own: the same assert)
00let_without_initializer_owns.ts own # copies an `any` holding an object, which -mm=own refuses at run time by design (native own: the same assert)
00array_write_grows.ts own # a[i] = v past the end does not grow the array under -mm=own yet (TSLANG 152): the write goes past the block
Loading