From c5165b8254acfc5acd260b1b7ff63a280aadec3c Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 17:46:36 +0000 Subject: [PATCH 1/4] a[i] = v past the end of an array grows it to i + 1, as in JavaScript `a[a.length] = v` and `s[i] = ...` in a loop over an empty array left the length as it was and wrote past the end of the block: under gc into whatever lay there, under rc and own a crash (TSLANG 152). The element store, which takes the element's address anew after the right side ran, now grows the array first when the index is at or past the length (mlirGenGrowArrayForWrite: SetLengthOf, which zeroes the slots in between - a hole reads as the element type's default). A negative index grows nothing; an array with no place to update (made for the statement) is left as it was. Test 00array_write_grows.ts (compile, JIT, rc, own). Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenImpl.h | 39 ++++++++++++++++++ tslang/test/tester/CMakeLists.txt | 4 ++ .../test/tester/tests/00array_write_grows.ts | 40 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 tslang/test/tester/tests/00array_write_grows.ts diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 597834435..c1cb5f831 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -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()) { + mlirGenGrowArrayForWrite(location, elementRefOp); storeReference = builder.clone(*elementRefOp)->getResult(0); } @@ -20171,6 +20172,44 @@ 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(array.getType())) + { + return; + } + + MLIRCodeLogic mcl(builder, compileOptions); + auto arrayRef = mcl.GetReferenceFromValue(location, array); + if (!arrayRef || !isa(arrayRef.getType())) + { + return; + } + + // index >= length, signed: a negative index is never past the end + auto index32 = elementRefOp.getIndex(); + auto i32Type = builder.getI32Type(); + auto length = builder.create(location, builder.getIndexType(), array); + auto length32 = builder.create(location, i32Type, length); + auto condition = builder.create( + location, getBooleanType(), builder.getI32IntegerAttr((int)SyntaxKind::GreaterThanEqualsToken), index32, length32); + + auto ifOp = builder.create(location, condition, false); + mlir::OpBuilder::InsertionGuard guard(builder); + builder.setInsertionPointToStart(&ifOp.getThenRegion().front()); + auto one = builder.create(location, i32Type, builder.getI32IntegerAttr(1)); + auto newLength32 = builder.create( + location, i32Type, builder.getI32IntegerAttr((int)SyntaxKind::PlusToken), index32, one); + auto newLength = builder.create(location, builder.getIndexType(), newLength32); + builder.create(location, arrayRef, newLength); + } + mlir::LogicalResult parsePartialStatements(string src) { GenContext emptyContext{}; diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 471a7a6dd..06d9fea73 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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") @@ -1316,6 +1317,9 @@ 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-own-00-array-write-grows COMMAND test-runner -jit -mm=own "${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") diff --git a/tslang/test/tester/tests/00array_write_grows.ts b/tslang/test/tester/tests/00array_write_grows.ts new file mode 100644 index 000000000..2db99840d --- /dev/null +++ b/tslang/test/tester/tests/00array_write_grows.ts @@ -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. +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."); +} From cd89283003ec68fad578b2cd2c4f507f499ae897 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 17:57:36 +0000 Subject: [PATCH 2/4] Array growth on a write: not under -mm=own yet (its passes read the grown slot as a keep); TSLANG 152 Co-Authored-By: Claude Opus 5.5 --- TSLANG_ISSUES.md | 8 +++++++- tslang/lib/TypeScript/MLIRGenImpl.h | 8 ++++++++ tslang/test/tester/CMakeLists.txt | 1 - tslang/test/tester/tests/00array_write_grows.ts | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 548984e5e..f53366849 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -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(a: Box, b: Box)` from `f(Box, Box)` 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). diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index c1cb5f831..05007a9b3 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -20192,6 +20192,14 @@ class MLIRGenImpl 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(); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 06d9fea73..5041260d7 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -1319,7 +1319,6 @@ tslang_add_test(NAME test-jit-rc-00-any-undefined-field COMMAND test-runner -jit 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-own-00-array-write-grows COMMAND test-runner -jit -mm=own "${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") diff --git a/tslang/test/tester/tests/00array_write_grows.ts b/tslang/test/tester/tests/00array_write_grows.ts index 2db99840d..9313e626b 100644 --- a/tslang/test/tester/tests/00array_write_grows.ts +++ b/tslang/test/tester/tests/00array_write_grows.ts @@ -1,7 +1,7 @@ // `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. +// rc and own a crash. (Not under -mm=own yet: TSLANG 152.) class Item { constructor(public n: number) {} } From 4295bf0fb75eb959c23344d0b86093c92787d998 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 18:10:19 +0000 Subject: [PATCH 3/4] Array growth on a write in a function of its own for each array type SetLengthOf lowers to a capacity check, a reallocation and the zeroing of the new slots; made at every element write, the sweep's largeControlFlowGraph (10000 `data[0] = 0`) took 71 s instead of 5 s, all of it lowering to LLVM. The length check stays at the write; the growth is a call of `.array_grow.(array, length)`. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenImpl.h | 53 ++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 05007a9b3..70240459e 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -20215,7 +20215,58 @@ class MLIRGenImpl auto newLength32 = builder.create( location, i32Type, builder.getI32IntegerAttr((int)SyntaxKind::PlusToken), index32, one); auto newLength = builder.create(location, builder.getIndexType(), newLength32); - builder.create(location, arrayRef, newLength); + + // 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(arrayRef.getType())); + builder.create(location, growFunc, mlir::ValueRange{arrayRef, newLength}); + } + + // `.array_grow.(array: ref, 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(noLocation, mlir::Type()); + builder.create(noLocation, entryBlock.getArgument(0), entryBlock.getArgument(1)); + builder.create(noLocation, mlir::Value()); + (void)entryOp; + + pushFunction(funcOp); + } + + return builder.create(location, funcType, + mlir::FlatSymbolRefAttr::get(builder.getContext(), name)); } mlir::LogicalResult parsePartialStatements(string src) From e3446f91131dca5586a128675683e51b86d65ba6 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Sat, 21 Nov 2026 18:48:35 +0000 Subject: [PATCH 4/4] wasm and Android runners: skip 00array_write_grows.ts under -mm=own (growth not there yet, TSLANG 152) Co-Authored-By: Claude Opus 5.5 --- tslang/test/android/android_skips.txt | 1 + tslang/test/wasm/wasm_skips.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/tslang/test/android/android_skips.txt b/tslang/test/android/android_skips.txt index f078b2ec1..e725aca69 100644 --- a/tslang/test/android/android_skips.txt +++ b/tslang/test/android/android_skips.txt @@ -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 diff --git a/tslang/test/wasm/wasm_skips.txt b/tslang/test/wasm/wasm_skips.txt index f67ff03c5..a687ed434 100644 --- a/tslang/test/wasm/wasm_skips.txt +++ b/tslang/test/wasm/wasm_skips.txt @@ -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