diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index d3ec52793..2bf47dfe0 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2171,7 +2171,11 @@ Still open, looked at: the elements a borrow reads as dropped; in a branch between the value stored and its store, the store as a second reference. Not through a local over a parameter's array (`let src = order; src[i] = v`): changed in place through it - a push too - the local owns what it holds and the parameter is kept, so there the write is as before - (open). A parameter's array, a field's of a parameter, arrays of objects and of arrays: flat at 6 MB over 200000 + (open). Nor through a handle's payload (`s.value[i] = v`): a push there may move the elements another handle's + borrow reads (spec 23.3), so the library's TypedArray.set (and MessageQueue.close, the workers) were taken for + methods that release what their callers borrow, and 8 library tests stopped compiling under -mm=own ("a value that + borrows 'this.elements' is used here after it may be released"); the write there is as before (open). Test + `00array_write_shared.ts`. A parameter's array, a field's of a parameter, arrays of objects and of arrays: flat at 6 MB over 200000 rounds (gc, rc, own), clean on the wasm checked heap (own, rc). Not when the right side gave the variable another array (`a[1] = (a = [5], 9)`): the store goes into the one the left side read; a store of another through another place (`h.a[0] = (h.a = [5], 9)`, or in a call) is not seen, and grows the new one (open). @@ -2210,9 +2214,16 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT, done: true }`, throw(e) throws e. The generator object holds a mode its yields check where they resume (addGeneratorResumeMethods, mlirGenGeneratorResumeChecks). Under -mm=own return() only for a T that owns nothing (a number, a boolean): its result would hold the value the caller lends. The library's `Generator` - declares them optional (`return?`, `throw?`: a generator declared otherwise has neither) once a compiler with - them is released. Under -mm=own a call through an optional one is read as moving the generator, so there a - program declaring them required calls them. + declares them optional (`return?`, `throw?`: a generator declared otherwise has neither), and its next() takes no + value: declared `next: (value?: TNext) =>`, a Generator given to an Iterable (`next: () =>`) was let through + as it is - castInterfaceToInterface reports a member of another type only when the vtable must be remapped, and + `next` is the first slot of both - so an Iterable's next() called a function of another signature: wasm's + "function signature mismatch" (the library's iteration_types.ts), natively a yield reading a value never given + (open: such a cast is not refused; next(value) through the library's Generator). Under -mm=own a call of an optional function-typed member of an interface (`g.return(v)`) + was read as moving the object: the method value bound to it is wrapped in an optional, given out of the If that + tests the member (lowered by then: a branch's block argument), tested and unwrapped for the call - all of which + only calls it now lends the object (isBoundForCall, boundOnlyCalled), as a call of a member that is not optional + does. Test `00interface_optional_member_call.ts` (compile, JIT, rc, own). - -mm=own: an object literal's field of `undefined` or `null` (`{ t: undefined, n: 1 }`) was released twice: own_facts::isConstantData took null or undefined seen through its casts for data nothing owns, a box into `any` included - so the store of the box into the object was not checked as a move, and the diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index f911901b0..ff1543bdf 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -20295,6 +20295,14 @@ class MLIRGenImpl } } + // Not through a handle's payload (`s.value[i] = v`, the library's TypedArray.set): a growth there, as a + // push, may move the elements another handle's borrow reads (spec 23.3), so a method that writes one + // element would be taken for one that releases what its callers borrow. As before there. + if (own_facts::reachesSharedValue(arrayRef)) + { + return; + } + auto one = builder.create(location, i32Type, builder.getI32IntegerAttr(1)); auto newLength32 = builder.create( location, i32Type, builder.getI32IntegerAttr((int)SyntaxKind::PlusToken), index32, one); diff --git a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp index 1ee761cce..b775fa00a 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp @@ -899,15 +899,56 @@ namespace own_inference return false; } - return llvm::all_of(boundOp->getUsers(), [](mlir::Operation *boundUser) { - if (mlir::isa(boundUser)) - { - return true; - } + return boundOnlyCalled(boundOp.getResult(), 0); + }); + } - return mlir::isa(boundUser) && - llvm::all_of(boundUser->getUsers(), [](mlir::Operation *thisUser) { return isCall(thisUser); }); - }); + // A bound function only called: its method and object taken for a call, or - a call of an optional member, + // `g.return?.(v)`, `g.return(v)` of `return?: ...` - wrapped in an optional, given out of an If, tested and + // unwrapped for the call. Its object is lent to the call, not moved into a value that outlives it. + bool OwnershipInferencePass::boundOnlyCalled(mlir::Value bound, int depth) + { + if (depth > 6) + { + return false; + } + + return llvm::all_of(bound.getUses(), [&](mlir::OpOperand &use) { + auto *boundUser = use.getOwner(); + if (mlir::isa(boundUser)) + { + return true; + } + + if (mlir::isa(boundUser)) + { + return llvm::all_of(boundUser->getUsers(), [](mlir::Operation *thisUser) { return isCall(thisUser); }); + } + + if (auto castOp = mlir::dyn_cast(boundUser)) + { + return mlir::isa(castOp.getType()); + } + + if (mlir::isa(boundUser)) + { + return boundOnlyCalled(boundUser->getResult(0), depth + 1); + } + + if (auto resultOp = mlir::dyn_cast(boundUser)) + { + auto ifOp = mlir::dyn_cast_or_null(resultOp->getParentOp()); + return ifOp && use.getOperandNumber() < ifOp->getNumResults() && + boundOnlyCalled(ifOp->getResult(use.getOperandNumber()), depth + 1); + } + + // the If lowered: its result a block argument + if (auto brOp = mlir::dyn_cast(boundUser)) + { + return boundOnlyCalled(brOp.getDest()->getArgument(use.getOperandNumber()), depth + 1); + } + + return false; }); } diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index d5f28cb97..b8eb83e85 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -1132,6 +1132,7 @@ class OwnershipInferencePass // interface's `this` (`ts.ExtractInterfaceThis`) is bound so for a call of a function-typed // field (`i.toString()` where `toString: () => string`). static bool isBoundForCall(mlir::Operation *thisOp); + static bool boundOnlyCalled(mlir::Value bound, int depth); // Is `value` an argument the call's callee keeps? static bool isKeptArgument(mlir::Operation *call, mlir::Value value); diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 725a8d780..296babe6a 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -480,6 +480,8 @@ tslang_add_test(NAME test-compile-00-generator-symbol-iterator COMMAND test-runn tslang_add_test(NAME test-compile-00-generic-shadowed-type-param COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_shadowed_type_param.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-array-write-shared COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") +tslang_add_test(NAME test-compile-00-interface-optional-member-call COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_optional_member_call.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") @@ -1347,6 +1349,12 @@ tslang_add_test(NAME test-jit-own-00-any-undefined-field COMMAND test-runner -ji 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-array-write-shared COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") +tslang_add_test(NAME test-jit-rc-00-array-write-shared COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") +tslang_add_test(NAME test-jit-own-00-array-write-shared COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00array_write_shared.ts") +tslang_add_test(NAME test-jit-00-interface-optional-member-call COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_optional_member_call.ts") +tslang_add_test(NAME test-jit-rc-00-interface-optional-member-call COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_optional_member_call.ts") +tslang_add_test(NAME test-jit-own-00-interface-optional-member-call COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00interface_optional_member_call.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_shared.ts b/tslang/test/tester/tests/00array_write_shared.ts new file mode 100644 index 000000000..764a9c0f4 --- /dev/null +++ b/tslang/test/tester/tests/00array_write_shared.ts @@ -0,0 +1,37 @@ +// A write of an element of an array held by a handle (`s.value[i] = v`, as the library's TypedArray.set +// does): under -mm=own it does not grow the array there, and so a method that writes one element is +// not taken for one that releases what its callers borrow ("a value that borrows 'this.sync' is used +// here after it may be released"). + +class Cells { + private store = new Shared([0, 0, 0]); + public set(index: int, value: int) { + this.store.value[index] = value; + } + + public get(index: int) { + return this.store.value[index]; + } +} + +class Queue { + sync = new Cells(); + close() { + this.sync.set(2, 1); + } +} + +class Worker1 { + q = new Shared(new Queue()); + end() { + this.q.value.close(); + this.q.value.close(); + } +} + +function main() { + const w = new Worker1(); + w.end(); + assert(w.q.value.sync.get(2) == 1, "written"); + print("done."); +} diff --git a/tslang/test/tester/tests/00interface_optional_member_call.ts b/tslang/test/tester/tests/00interface_optional_member_call.ts new file mode 100644 index 000000000..eb56d150b --- /dev/null +++ b/tslang/test/tester/tests/00interface_optional_member_call.ts @@ -0,0 +1,33 @@ +// A call of an interface's optional function-typed member (`undo?: ...`) lends the object to the call, as a +// call of a member that is not optional does: under -mm=own the object is used again after it (the library's +// Generator `return?` / `throw?`, `g.return(v)` and then `g.next()`), not moved into the method value. + +interface Counter { + n: number; + add: (x: number) => number; + undo?: (x: number) => number; +} + +function make(): Counter { + return { + n: 0, + add(x: number): number { + this.n += x; + return this.n; + }, + undo(x: number): number { + this.n -= x; + return this.n; + }, + }; +} + +function main() { + const c = make(); + c.add(5); + const r = c.undo(2); + assert(r == 3, "the optional member called"); + assert(c.add(1) == 4, "the object used again after it"); + assert(c.n == 4, "its field"); + print("done."); +}