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 597834435..70240459e 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,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(array.getType())) + { + return; + } + + MLIRCodeLogic mcl(builder, compileOptions); + auto arrayRef = mcl.GetReferenceFromValue(location, array); + if (!arrayRef || !isa(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(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); + + // 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) { GenContext emptyContext{}; 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/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 471a7a6dd..5041260d7 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,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") 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..9313e626b --- /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. (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."); +} 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