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
12 changes: 12 additions & 0 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir_ts::VariableOp>(location, mlir_ts::RefType::get(resultType),
mlir::Value(), builder.getBoolAttr(false),
builder.getIndexAttr(0));
Expand Down
11 changes: 11 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferenceMoves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,17 @@ namespace own_inference
{
if (auto retainSlotOp = mlir::dyn_cast<mlir_ts::RetainSlotOp>(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<mlir_ts::VariableOp>();
auto storeOp = mlir::dyn_cast_or_null<mlir_ts::StoreOp>(retain->getPrevNode());
if (varOp && !varOp.getInitializer() && storeOp && storeOp.getReference() == retainSlotOp.getSlot() &&
storeOp.getValue() == value)
{
return storeOp;
}

return retainSlotOp.getSlot().getDefiningOp();
}

Expand Down
3 changes: 3 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferencePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir::BlockArgument>(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<mlir_ts::StoreOp>(store);
if (!storeOp || storeOp.getValue() != value || !(isCopyableValueAt(store, value.getType()) || isHandle(value)) ||
!isPlace(storeOp.getReference()))
!(isPlace(storeOp.getReference()) || isAwaitResultStore(storeOp)))
{
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignaturePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir_ts::ThrowOp, mlir_ts::ThrowCallOp, mlir_ts::ThrowUnwindOp>(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<mlir_ts::SharedNewOp>(user))
{
Expand Down
5 changes: 5 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
111 changes: 111 additions & 0 deletions tslang/test/tester/tests/00own_throw_param_await_borrow.ts
Original file line number Diff line number Diff line change
@@ -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<T, R>(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<T>(value: T): T {
return value;
}

async function greet(name: string): Promise<string> {
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<int, E>(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");
}
Loading