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
Original file line number Diff line number Diff line change
Expand Up @@ -5398,5 +5398,35 @@ and rc no reference of the box's own for it that the literal gives back: the box
to an object-type parameter and reads the objects after, churning between steps, under own, rc, gc and
none.

Not here: a literal given to an interface (`apply(q)` for `apply(s: Scaler)`): the cast copies it into
the interface's object, which keeps it, and the object stays moved into the box.
Not here: a literal given to an interface (`apply(q)` for `apply(s: Scaler)`); see spec 68.

## 68. An object literal whose arrows capture an object, given to an interface, 2026-10-29

`const b = new Box(); use({ greet: () => "hi" + b.n }); b.n` for `use(g: Greeter)` was `'this value' is used
here after its value was moved`: the interface is a view of the literal's box (`ts.NewInterface`), whose
release - the interface's at the end - frees the box and the `.captured` box with every copy in it, so
the copies could not borrow (spec 67 only covers a literal that is never released).

- **A borrowing record's box** (spec 32, `capturedBoxBorrows` in the signature pass): the `.captured` field
is one more field the record borrows when the box is stored there once, each copy in it is a value with
an owner of its own (`hasOwnerOfItsOwn`, spec 67's test) or holds no block, none is a cell, and every
interface made of the record is only retained, released, or given to calls that keep it not and return
nothing borrowed of it. Never returnable.
- **Its release** (`OwnershipRoutineLogic::releaseFields`): a borrowed field that is a capture box is
freed with `tsfrecb_` - the box, not what it holds - where any other borrowed field is skipped.
- **Inference** (`classifyLiteralCaptures`): the record's uses (`walkRecord` from its interfaces) are the
box's, in place of `walkBorrowed` from the object; the copies borrow as in spec 67. `classifyRecords`
leaves the `.captured` store to it. A record the signature pass marked that the walk finds kept or
returned is an error, not a silent free.
- **Bounded results that hold nothing**: `checkBoundedResults` skips a call whose result owns no block.
A literal capturing a parameter is state-shaped (`findBorrowingStates`), which bounded `viaParam`'s
number result by `item`, and `print(viaParam(c))` was an error.

`own_literal_interface_captures` gives literals capturing a local object, two objects, a string beside an
object, and a parameter to interface parameters and reads the objects after; a literal returned as an
interface and one an array keeps still take their copies. It churns between steps, under own, rc, gc and
none.

Not here: a literal capturing a `let` (a cell), which errors as `captured by a closure that escapes`
with or without an interface; and `return applyO({ w: (x) => x + item.n })` for an object-type parameter,
`a closure that escapes owns what it captures`.
12 changes: 9 additions & 3 deletions tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,11 @@ class OwnershipRoutineLogic

for (auto [index, fieldType] : llvm::enumerate(getFieldTypes(recordType)))
{
if (llvm::is_contained(left, static_cast<int32_t>(index)))
// ...but for an object literal's `.captured` box that borrows its copies (own spec 68): the
// box is the record's, what it holds is not
auto leftBox = mlir::dyn_cast<mlir_ts::RefType>(fieldType);
if (llvm::is_contained(left, static_cast<int32_t>(index)) &&
!(leftBox && isa<mlir_ts::TupleType>(leftBox.getElementType())))
{
continue;
}
Expand Down Expand Up @@ -897,8 +901,10 @@ class OwnershipRoutineLogic
{
if (isa<mlir_ts::TupleType>(refFieldType.getElementType()))
{
routineName = borrows ? getOrCreateCaptureBoxCellsRoutine(refFieldType)
: getOrCreateCaptureBoxReleaseRoutine(refFieldType);
routineName = llvm::is_contained(left, static_cast<int32_t>(index))
? getOrCreateCaptureBoxFreeRoutine(refFieldType)
: borrows ? getOrCreateCaptureBoxCellsRoutine(refFieldType)
: getOrCreateCaptureBoxReleaseRoutine(refFieldType);
}
}
else
Expand Down
14 changes: 14 additions & 0 deletions tslang/lib/TypeScript/OwnershipFacts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,20 @@ inline RecordFieldSource recordFieldSource(mlir::Value value)
return RecordFieldSource::Unknown;
}

// A copy an object literal's `.captured` box may borrow (specs 67, 68): a value with an owner of its
// own - a parameter, a local's read, a named value released here; not a cell (a captured `let`)
inline bool hasOwnerOfItsOwn(mlir::Value value)
{
auto root = rootOf(value);
if (mlir::isa<mlir::BlockArgument>(root) || root.getDefiningOp<mlir_ts::LoadOp>())
{
return true;
}

return isFresh(root) &&
llvm::any_of(root.getUsers(), [](mlir::Operation *rootUser) { return mlir::isa<mlir_ts::ReleaseOp>(rootUser); });
}

// The box of the object literal built in `varOp`: its one read, copied into a block `ts.New` made
// (recordBoxOf). Null for anything else.
inline mlir_ts::NewOp recordBoxOfLocal(mlir_ts::VariableOp varOp)
Expand Down
58 changes: 41 additions & 17 deletions tslang/lib/TypeScript/OwnershipInferenceClosures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace own_inference
// builds the literal in a local, then copies it into the object `ts.New` made
llvm::SmallVector<mlir_ts::StoreOp> fills;
mlir_ts::VariableOp literalOp;
int32_t capturedPosition = -1;
for (auto &use : boxOp->getUses())
{
auto *user = use.getOwner();
Expand Down Expand Up @@ -173,6 +174,7 @@ namespace own_inference
}

literalOp = ownerOp;
capturedPosition = static_cast<int32_t>(fieldOp.getPosition());
}

if (!literalOp)
Expand Down Expand Up @@ -213,7 +215,40 @@ namespace own_inference
// the literal stays here: nothing stores, returns, captures or keeps it
auto box = std::make_shared<Closure>();
llvm::SmallVector<BorrowUse> uses;
if (walkBorrowed(objects.front(), {}, uses))
auto newOp = objects.front().getDefiningOp<mlir_ts::NewOp>();
if (llvm::is_contained(recordBorrowedFields(newOp), capturedPosition))
{
// ...or a borrowing record's box, seen only as interfaces given to calls that keep them
// not (spec 68): their uses are the box's, and its release frees the box alone
llvm::SmallVector<mlir::Value> interfaces;
for (auto *user : newOp->getUsers())
{
if (mlir::isa<mlir_ts::CastOp>(user))
{
for (auto *castUser : user->getUsers())
{
if (mlir::isa<mlir_ts::NewInterfaceOp>(castUser))
{
interfaces.push_back(castUser->getResult(0));
}
}
}
}

RecordWalk walk;
llvm::SmallVector<mlir::Operation *, 2> kills{newOp.getOperation(), literalOp.getOperation()};
walkRecord(interfaces, kills, walk);
if (walk.kept || !walk.returns.empty())
{
auto *at = walk.kept ? walk.kept : walk.returns.front();
at->emitError("-mm=own: an object literal that borrows what its arrows capture can't outlive this function");
signalPassFailure();
return;
}

uses.append(walk.uses.begin(), walk.uses.end());
}
else if (walkBorrowed(objects.front(), {}, uses))
{
return;
}
Expand All @@ -223,20 +258,7 @@ namespace own_inference
box->uses.push_back(use.op);
}

// copies of values with an owner of their own: a named value released here, a parameter, a
// local's; not a cell (a captured `let`), and not one rc retained for the box
auto owned = [&](mlir::Value value) {
auto root = rootOf(value);
if (mlir::isa<mlir::BlockArgument>(root) || root.getDefiningOp<mlir_ts::LoadOp>())
{
return true;
}

return isFresh(root) && llvm::any_of(root.getUsers(), [](mlir::Operation *rootUser) {
return mlir::isa<mlir_ts::ReleaseOp>(rootUser);
});
};

// copies of values with an owner of their own, not one rc retained for the box
llvm::SmallVector<mlir_ts::StoreOp> borrowed;
for (auto storeOp : fills)
{
Expand All @@ -246,7 +268,7 @@ namespace own_inference
continue;
}

if (!owned(value))
if (!hasOwnerOfItsOwn(value))
{
return;
}
Expand Down Expand Up @@ -494,7 +516,9 @@ namespace own_inference
{
getFunction().walk([&](mlir::Operation *call) {
auto bounded = call->getAttrOfType<mlir::DenseI32ArrayAttr>(OWN_RESULT_BOUNDED_ATTR_NAME);
if (!bounded || !isCall(call) || call->getNumResults() != 1)
// a number holds nothing a bound could outlive: `return apply({ w: (x) => x + item.n })`
// makes a state-shaped literal, not a result that borrows `item`
if (!bounded || !isCall(call) || call->getNumResults() != 1 || !ownsHeap(call->getResult(0)))
{
return;
}
Expand Down
7 changes: 7 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferenceRecords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ namespace own_inference
continue;
}

// the `.captured` box: what it borrows is the literal's box's (classifyLiteralCaptures)
if (auto boxOp = storeOp.getValue().getDefiningOp<mlir_ts::VariableOp>();
borrows && boxOp && boxOp->hasAttr(CAPTURE_BOX_ATTR_NAME))
{
continue;
}

auto source = recordFieldSource(storeOp.getValue());
if (source == RecordFieldSource::NoBlock)
{
Expand Down
4 changes: 4 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignaturePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,10 @@ class OwnershipSignaturePass : public mlir::PassWrapper<OwnershipSignaturePass,
void giveBorrowingRecordsTheirVTables(mlir::ModuleOp module);

void classifyRecordBox(mlir_ts::NewOp newOp);
// Is `field` the `.captured` box of a literal's arrows that borrows its copies (spec 68): every one
// a value with an owner of its own (hasOwnerOfItsOwn), not a cell, and the record seen only as
// `interfaces` given to calls that keep them not? Its release then frees the box alone.
bool capturedBoxBorrows(mlir::Value field, llvm::ArrayRef<mlir::Operation *> interfaces);

// An object literal of tuple type (a `@value` value, a type literal) that is never boxed and holds
// a borrow beside nothing it owns - every field that holds a block borrows it - is an unboxed
Expand Down
87 changes: 87 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignatureRecords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,85 @@ namespace own_signature
}
}

bool OwnershipSignaturePass::capturedBoxBorrows(mlir::Value field, llvm::ArrayRef<mlir::Operation *> interfaces)
{
// the box, stored once
mlir_ts::VariableOp boxOp;
for (auto *user : field.getUsers())
{
if (mlir::isa<mlir_ts::LoadOp>(user))
{
continue;
}

auto storeOp = mlir::dyn_cast<mlir_ts::StoreOp>(user);
auto varOp = storeOp && storeOp.getReference() == field ? storeOp.getValue().getDefiningOp<mlir_ts::VariableOp>()
: mlir_ts::VariableOp();
if (!varOp || !varOp->hasAttr(CAPTURE_BOX_ATTR_NAME) || boxOp)
{
return false;
}

boxOp = varOp;
}

if (!boxOp)
{
return false;
}

// ...filled with copies of values that have owners of their own
for (auto *user : boxOp->getUsers())
{
if (mlir::isa<mlir_ts::RetainCellOp, mlir_ts::ReleaseCellOp>(user))
{
continue;
}

if (auto storeOp = mlir::dyn_cast<mlir_ts::StoreOp>(user); storeOp && storeOp.getReference() == field)
{
continue;
}

auto propertyRefOp = mlir::dyn_cast<mlir_ts::PropertyRefOp>(user);
if (!propertyRefOp)
{
return false;
}

for (auto *fieldUser : propertyRefOp->getUsers())
{
auto fillOp = mlir::dyn_cast<mlir_ts::StoreOp>(fieldUser);
if (!fillOp || fillOp.getReference() != propertyRefOp.getResult())
{
return false;
}

auto value = fillOp.getValue();
if (mlir::isa<mlir_ts::RefType>(value.getType()) || (!holdsNoBlock(value) && !hasOwnerOfItsOwn(value)))
{
return false;
}
}
}

// ...and nothing keeps the record: its retains and releases, and calls that only borrow it
for (auto *newInterfaceOp : interfaces)
{
auto stays = true;
forEachUse(newInterfaceOp->getResult(0), [&](mlir::Operation *use, mlir::Value used) {
stays = stays && (mlir::isa<mlir_ts::RetainOp, mlir_ts::ReleaseOp>(use) ||
(isCall(use) && !keeps(use, used) && resultBorrows(use) < 0));
});
if (!stays)
{
return false;
}
}

return true;
}

void OwnershipSignaturePass::classifyRecordBox(mlir_ts::NewOp newOp)
{
auto boxOp = recordBoxOf(newOp.getResult());
Expand Down Expand Up @@ -294,6 +373,14 @@ namespace own_signature
}

auto field = propertyRefOp.getResult();
if (capturedBoxBorrows(field, interfaces))
{
// never returned: what the box borrows is this function's
info.returnable = false;
borrowed.push_back(propertyRefOp.getPosition());
continue;
}

auto refType = mlir::dyn_cast<mlir_ts::RefType>(field.getType());
if (!refType || !mth.ownsHeapMemory(field.getLoc(), refType.getElementType()))
{
Expand Down
4 changes: 2 additions & 2 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4019,7 +4019,7 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o
own_generator_this_param own_copy_within_returned_param own_swap_elements
own_callback_forwarded own_optional_record_read own_param_default_kept own_literal_into_array
own_generator_take own_rest_array_borrows own_string_copies
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures
own_callback_shapes own_callback_extended own_callback_interface
own_generic_specializations own_callback_cast own_callback_interface_slot)
tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts")
Expand Down Expand Up @@ -4057,7 +4057,7 @@ foreach(keeps_mm rc gc none)
own_copy_within_returned_param own_swap_elements own_callback_forwarded
own_optional_record_read own_param_default_kept own_literal_into_array
own_generator_take own_rest_array_borrows own_string_copies
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures
own_captured_string_callback own_callback_and_call own_callback_captured own_literal_closures own_literal_variable_closures own_temporary_field_take own_union_narrowed_calls own_named_field_take own_let_field_take own_literal_captures own_literal_object_captures own_literal_interface_captures
own_callback_shapes own_callback_extended own_callback_interface
own_generic_specializations own_callback_cast own_callback_interface_slot)
tslang_add_test(NAME test-jit-${keeps_mm}-${keeps_test} COMMAND test-runner -jit -mm=${keeps_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${keeps_test}.ts")
Expand Down
Loading
Loading