diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index c38d284b3..f8ba7d746 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -467,6 +467,18 @@ namespace mlirgen mlir::Value resultSlot; if (resultType) { + // A slot that will own the result is released by the scope, on the unwind leg too: in + // the body of a TryOp - a `try`, or a block wrapped for its cleanup (on Itanium, any + // block with a typed `catch`) - its storage goes in front of the TryOp, as an owned + // local's does (allocateScopeOwnedVarsOutsideOfOperation), or the cleanup region + // names a slot the body declares + mlir::OpBuilder::InsertionGuard insertGuard(builder); + if (genContext.allocateScopeOwnedVarsOutsideOfOperation && genContext.currentOperation && + compileOptions.tracksOwnership() && mth.ownsHeapMemory(location, resultType)) + { + builder.setInsertionPoint(genContext.currentOperation); + } + resultSlot = builder.create(location, mlir_ts::RefType::get(resultType), mlir::Value(), builder.getBoolAttr(false), builder.getIndexAttr(0)); diff --git a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp index e9d7b0402..255b398ba 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp @@ -1166,6 +1166,17 @@ namespace own_inference { if (auto retainSlotOp = mlir::dyn_cast(retain)) { + // an owned local hoisted in front of a TryOp (allocateScopeOwnedVarsOutsideOfOperation) + // is declared with no value and given it by the store right before the retain: that + // store is the move, not the declaration, which ran before the value was made + auto varOp = retainSlotOp.getSlot().getDefiningOp(); + auto storeOp = mlir::dyn_cast_or_null(retain->getPrevNode()); + if (varOp && !varOp.getInitializer() && storeOp && storeOp.getReference() == retainSlotOp.getSlot() && + storeOp.getValue() == value) + { + return storeOp; + } + return retainSlotOp.getSlot().getDefiningOp(); } diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index 414e585f0..56f8afafc 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -868,6 +868,9 @@ class OwnershipInferencePass // Does `store` put `value`, a string read out of a place, into another place with no retain of // rc's for it, so that the place can take a copy instead (callCopies)? bool storesCopyOf(mlir::Operation *store, mlir::Value value); + // Is `storeOp` an `await`'s store of its value, from the function the async lowering outlined + // its region into, through the reference to the caller's local the code after the await reads? + bool isAwaitResultStore(mlir_ts::StoreOp storeOp); // A local that holds a borrow through `user`, and the field path under it that holds it (empty: // the local's value itself): a store of it into the local, or into a field of the local's value diff --git a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp index 43f988945..40567e916 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp @@ -774,11 +774,23 @@ namespace own_inference llvm::count(call->getOperands(), value) == 1; } + bool OwnershipInferencePass::isAwaitResultStore(mlir_ts::StoreOp storeOp) + { + // `await f(x)` runs the call in an `async.execute` region, which the async lowering outlines + // into a `func.func` given a reference to the caller's local, and stores the value through + // it for the code after the await to read. The outlined function's temporaries (`x`) are + // released when it ends, so what borrows them cannot be given back as it is. + auto argument = mlir::dyn_cast(storeOp.getReference()); + auto *owner = argument ? argument.getOwner() : nullptr; + return owner && owner->isEntryBlock() && owner->getParentOp() && + owner->getParentOp()->getName().getStringRef() == "func.func"; + } + bool OwnershipInferencePass::storesCopyOf(mlir::Operation *store, mlir::Value value) { auto storeOp = mlir::dyn_cast(store); if (!storeOp || storeOp.getValue() != value || !(isCopyableValueAt(store, value.getType()) || isHandle(value)) || - !isPlace(storeOp.getReference())) + !(isPlace(storeOp.getReference()) || isAwaitResultStore(storeOp))) { return false; } diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index f0ffd622f..09d94e69d 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -910,6 +910,13 @@ namespace own_signature return llvm::is_contained(pushOp.getItems(), used); } + // `throw reason` takes the value with the exception: the catch owns it, not the caller + // (releaseKeptParamsAtExits places no release at such a throw) + if (mlir::isa(user)) + { + return user->getOperand(0) == used; + } + // `new Shared(x)` moves `x` into the block, as a store into a place does (spec 23.1) if (auto sharedNewOp = mlir::dyn_cast(user)) { diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 3b35cca7c..f8d9173c8 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -923,6 +923,8 @@ tslang_add_test(NAME test-compile-00-new-alias-and-ctor-overloads COMMAND test-r tslang_add_test(NAME test-compile-own-00-atomic-builtins COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00atomic_builtins.ts") tslang_add_test(NAME test-compile-own-00-generic-literal-inference COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_literal_inference.ts") tslang_add_test(NAME test-compile-own-00-new-alias-and-ctor-overloads COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") +tslang_add_test(NAME test-compile-00-own-throw-param-await-borrow COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") +tslang_add_test(NAME test-compile-own-00-own-throw-param-await-borrow COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") tslang_add_test(NAME test-compile-load-store-decorators COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/load_store_decorators.ts") tslang_add_test(NAME test-compile-reference-null-bug COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_null_bug.ts") tslang_add_test(NAME test-compile-reference-index-bug COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_index_bug.ts") @@ -1574,6 +1576,8 @@ tslang_add_test(NAME test-jit-00-new-alias-and-ctor-overloads COMMAND test-runne tslang_add_test(NAME test-jit-own-00-atomic-builtins COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00atomic_builtins.ts") tslang_add_test(NAME test-jit-own-00-generic-literal-inference COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_literal_inference.ts") tslang_add_test(NAME test-jit-own-00-new-alias-and-ctor-overloads COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") +tslang_add_test(NAME test-jit-00-own-throw-param-await-borrow COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") +tslang_add_test(NAME test-jit-own-00-own-throw-param-await-borrow COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00own_throw_param_await_borrow.ts") tslang_add_test(NAME test-jit-load-store-decorators COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/load_store_decorators.ts") tslang_add_test(NAME test-jit-reference-null-bug COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_null_bug.ts") tslang_add_test(NAME test-jit-reference-index-bug COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_index_bug.ts") @@ -2168,6 +2172,7 @@ set(TSLANG_CORPUS 00atomic_builtins.ts 00generic_literal_inference.ts 00new_alias_and_ctor_overloads.ts + 00own_throw_param_await_borrow.ts 00any_truthiness_objects.ts 00add_promotes_both_operands.ts 00conditional_owned_result.ts diff --git a/tslang/test/tester/tests/00own_throw_param_await_borrow.ts b/tslang/test/tester/tests/00own_throw_param_await_borrow.ts new file mode 100644 index 000000000..dcf5cded3 --- /dev/null +++ b/tslang/test/tester/tests/00own_throw_param_await_borrow.ts @@ -0,0 +1,111 @@ +// -mm=own: a function that throws its parameter keeps it (the exception takes it), and an awaited +// call whose string result borrows its argument gives back a copy (the argument is released when +// the await's region ends). + +class E { + constructor(public m: string) {} +} + +function reject(reason: R): T { + throw reason; +} + +function rejectE(reason: E): int { + throw reason; +} + +// throws on one path only: the other releases the parameter +function maybeThrow(reason: E, really: boolean): int { + if (really) { + throw reason; + } + + return 1; +} + +function identity(value: T): T { + return value; +} + +async function greet(name: string): Promise { + return "hello " + name; +} + +function main() { + let caught = ""; + try { + rejectE(new E("plain")); + } catch (e: E) { + caught = e.m; + } + + assert(caught == "plain", "a thrown parameter"); + + try { + reject(new E("generic")); + } catch (e: E) { + caught = e.m; + } + + assert(caught == "generic", "a thrown generic parameter"); + + const named = new E("named"); + try { + rejectE(named); + } catch (e: E) { + caught = e.m; + } + + assert(caught == "named", "a named local given up to the throw"); + + let r = 0; + for (let i = 0; i < 100; i++) { + try { + r += maybeThrow(new E("x"), i % 2 == 0); + } catch (e: E) { + r += 10; + } + } + + assert(r == 550, "a throw on one path"); + + awaitBorrowed(); + awaitInTry(); + + print("done."); +} + +// Its own function: on Itanium (Linux) a block with a typed `catch` is wrapped in a cleanup, and +// under -mm=own a value moved out of an await's slot at that block's top level is still "moved on +// some paths only" (the wrapper's cleanup releases the slot too). +function awaitBorrowed() { + let total = 0; + for (let i = 0; i < 100; i++) { + const s = await identity(greet("you")); + total += s.length; + } + + assert(total == 900, "an awaited borrowed string, copied"); + + const t = await identity("lit"); + assert(t == "lit", "an awaited borrowed literal"); +} + +// an await in a try body: the slot it gives its value back in is released on the unwind path +// too, so it is declared in front of the try, as an owned local is +function awaitInTry() { + let inTry = 0; + for (let i = 0; i < 100; i++) { + try { + const u = await identity(greet("again")); + inTry += u.length; + if (i == 99) { + throw new E("last"); + } + } catch (e: E) { + inTry += e.m.length; + } + } + + assert(inTry == 1104, "an await in a try body"); +}