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
7 changes: 5 additions & 2 deletions docs/own-memory-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,11 @@ a reference gives it back on every path out of a function.

`-mm=own` is still in development, and some valid TypeScript does not compile under it yet.

- The default library has an `-mm=own` build, and arrays, strings and their methods work, but
`Map` and `Set` do not compile under `-mm=own` yet.
- The default library has an `-mm=own` build, and arrays, strings and their methods work. `Map`
and `Set` work with number, boolean and string keys and values (`new Map<string, number>()`,
`new Set<string>(["a", "b"])`): their iterators borrow the container, and strings go in as
copies. With `any` or object keys or values they do not compile yet, and neither does a `Map`
or `Set` made without type arguments (`new Set([1, 2])` also makes a `Set<any>`).
- Array methods that keep elements of the source in a new array (`filter` over class instances)
give each such element a second owner, which is an error by design. Use `Shared<T>` elements,
or build the result from copies.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# -mm=own: generator methods over `this`, and the default library's `Map` and `Set`

Status: implemented 2026-10-26 (branch `own-generator-this-borrows`; DefaultLib branch
`own-generic-iterators`).

## 1. Why

`lib.generics.ts` - `Map`, `Set`, the array generators - is compiled into each program, with its
bodies, so it was never part of the own build of the default library (`lib.ts`). Under `-mm=own`
every `map_*` and `set_*` test of the DefaultLib suite failed to compile. The errors came in a few
kinds:

- `private *iter()` keeps `this` in its state, and `entries()`, `keys()`, `values()`, `forEach`,
`[Symbol.iterator]` and every set operation call it on `this`, which they do not own.
- `@borrows("this")` (spec of the param markers) helped only for an exported or `@final` class: a
method of any other class is called through its vtable (`ts.GetThis(ts.ThisVirtualSymbolRef(o))`),
which the forwarder check and the bound check did not see through. And a generator method with
the marker still kept `this`: a call moved the object into the generator, so a caller could not
use it after the loop (`for (const c of w.iter()) ...; w.x` was "used after its value was moved").
- The insert paths store a key and a value that are the caller's: a string read out of an element
(`for (const [k, v] of values) this.tryInsert(k, v)`), a borrowed string put into a yielded tuple
(`yield [key, value]`), a string lent to a callback (`forEach`'s `f(entry.value, entry.key)`).
- `resizeHelper` moved entries out of one array into another, which nothing under own can do.

## 2. Rules

1. **A generator method's state borrows `this`.** When the signature pass finds a borrowing state
(phase 7b), the box's store of `this` (`.this`) keeps nothing (`OWN_STATE_BORROW_ATTR_NAME`), so
the method does not keep `this` and its callers keep their object. The states are found before
the facts now; they need none of them.
2. **Through the vtable.** `parameterOf` (the forwarder check) and `checkBoundedResults` see the
object a method is called on through `ts.GetThis` (`boundThis`). A reassignment of the object
while its generator is read is the error a function generator's argument gets
(`own_err_generator_method_reassigned`).
3. **A yielded tuple.** A state field read only into a tuple that is yielded - also cast to a named
tuple, which reads the fields out into a new one - moves into the yield, as a field read straight
into it does (`onlyIntoYield`).
4. **Strings as values, three more places (spec 22).** A `ts.CreateTuple` of borrowed strings that
rc retains starts from copies (`copyIntoTuple`). A string read out of a place and given to a call
that may free it is copied for the call, and the copy released after it, when the call is a
callback (a function value nothing can mark `@nodrops`); a call that keeps it takes the copy. A
direct call that may free it stays an error, so `@nodrops` keeps its meaning
(`own_err_declared_drops`). A string given twice is copied twice (it was
`own_err_string_call_keeps_twice`, an error for want of a retain to turn into a copy).
5. **A parameter kept on some paths only** is released on the others (spec 33's exit releases and
spec 30's edge releases): `takenParams` is the parameters a move reaches every return from, not
any kept one. It was `own_err_param_kept_some_paths`. The move must be in the function's top
region (`placeOnUnmovedEdges`): a move inside a loop is still the error.
6. **A read is read again around a loop.** `findBetween` does not search past the read's own block:
a path back into it (a `continue`) reads again, and the call is given that read
(`for (const v of it) { if (other.has(v)) continue; result.add(v); }`).
7. **Length writes drop elements of their type.** A call's drops for `length =`, `pop`, `shift`,
`splice` on an array it reaches are the elements of arrays of that element type (`-1`, the
element's ref type), as `dropsChain` judges them in a body, not anything.
8. **Callbacks of methods.** A method no other method overrides gets a callback family (spec 33)
from its calls through the vtable and through its bound method: its users may be its class's
vtable and `ts.ThisVirtualSymbolRef`/`ts.ThisSymbolRef` whose methods are only called with the
object first. A family holds every method of a slot's name, of any class; another member
overrides this one when its class extends this one's (a base is the first field of a class's
storage). A closed family with no members - a method no one calls but through the vtable - calls
nothing through the parameter.

## 3. The library (DefaultLib `own-generic-iterators`)

- `@borrows("this")` on `iter`, `iterKey`, `iterValue` and their forwarders; the iterators read
`this._entries[i]` at each step instead of keeping the array in their state, so an insert during
iteration that gives the map a new array is not a dangling borrow. (A state that keeps a field of
`this` is "borrows a field and cannot be stored": phase 7b's borrowing fields are read straight
out of the box.)
- `forEach` is a loop over the entries, not over `iter()`.
- `resizeHelper` grows `_entries` in place (`length =`) instead of moving each entry into a new
array.
- An entry is stored as a tuple cast to the entry type, not an object literal (a literal that holds
what it owns moves only into a local, spec 36), in one place after the search loop (rule 5).
`Set`'s overwrite branch was dead (a set never overwrites an equal value) and is gone.
- `hashCodeGeneral` hashes the parameter's own bytes (`Ref(key)`) instead of a local copy, which
took the key under own.
- Bug fix, every model: `size` was `count`, which still counts deleted entries on the free list.

## 4. Not here

- `any` or object keys and values. The constructor from an array (`new Map([[k, v]])`) cannot take
the elements out of the caller's array, and moving an object parameter into a tuple
(`Retain` of a tuple made of kept parameters) is "takes a second reference". This waits on the
ruling on containers of owned values (two sets sharing an element by `union` is two owners).
Tried and dropped: a parameter kept through a tuple field (`entries[i] = <Entry>[key, value]`)
counted as kept. It made `set` and `add` keeping calls, and a string given to two of them
(`m1.set(k, 1); m2.set(k, 2)`) has no retain to copy (spec 22.4): an error that the copy into the
tuple (rule 4) avoids, while no object case compiled with it either.
- `new Set([1, 2])` instantiates `Set<any>` beside `Set<si32>` (MLIRGen tries the default type
argument first, in every model), so an untyped container fails under own for the `any` case.
- A callback that assigns a captured variable (`(s) => { r += s.n; }`) has no family, for a function
as much as a method: what it lends is still an error.
- A module-level `Date` with a method that changes it (`event.setDate(24)`) is "a value read out of
the global is lent to a call that may release or overwrite it", though `setDate` writes a number.

## 5. Tests

Positive, under own (AOT and JIT): `own_generator_method_this`, `own_string_copy_call`,
`own_param_kept_some_paths`, `own_method_callbacks`, `own_loop_read_again`,
`own_length_drops_elements`, `own_tuple_param_kept`; each also prints the same under gc, rc and none.
Errors: `own_err_generator_method_reassigned`, `own_err_callback_method_overridden`.
Retired: `own_err_param_kept_some_paths` (rule 5), `own_err_string_call_keeps_twice` (rule 4).
11 changes: 8 additions & 3 deletions tslang/lib/TypeScript/OwnershipFacts.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ constexpr int64_t OWN_INTERFACE_FIELD_POSITION = -3;
// result is the caller's to own, but no use of it may come after any of these arguments is given
// back, and it may not be kept anywhere that outlives them.
#define OWN_RESULT_BOUNDED_ATTR_NAME "__own_result_bounded"
// A generator method's store of `this` into its box (`.this`), when the state borrows it: the store
// keeps nothing, so the method does not keep `this` (OwnershipSignaturePass::keeps).
#define OWN_STATE_BORROW_ATTR_NAME "__own_state_borrow"
// The globals a call may give a new value, by name: a store to one, or its release, in a callee or
// in a call it makes. A call that may drop and whose callees are not known may assign any of them
// (OWN_ASSIGNS_ANY_GLOBAL_ATTR_NAME). A read of a global ends at such a call (checkGlobalRead).
Expand Down Expand Up @@ -747,13 +750,15 @@ inline mlir::Operation *findBetween(mlir::Operation *read, mlir::Operation *call
scan(op);
}

// the blocks on a path: reached from the read's, and reaching the call's
// the blocks on a path: reached from the read's, and reaching the call's. Not through the read's
// block again: a path back into it reads again (a loop's `continue`), and the call is given that
// read
llvm::SmallPtrSet<mlir::Block *, 16> after;
llvm::SmallVector<mlir::Block *> work(fromBlock->getSuccessors().begin(), fromBlock->getSuccessors().end());
while (!work.empty())
{
auto *block = work.pop_back_val();
if (after.insert(block).second)
if (block != fromBlock && after.insert(block).second)
{
work.append(block->getSuccessors().begin(), block->getSuccessors().end());
}
Expand All @@ -764,7 +769,7 @@ inline mlir::Operation *findBetween(mlir::Operation *read, mlir::Operation *call
while (!work.empty())
{
auto *block = work.pop_back_val();
if (before.insert(block).second)
if (block != fromBlock && before.insert(block).second)
{
work.append(block->getPredecessors().begin(), block->getPredecessors().end());
}
Expand Down
6 changes: 6 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferenceClosures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ namespace own_inference
// a closure's box (`.map(f)` calls its maker with it): it lives while the closures on
// it do, and what it holds while their owners do. A reference to storage, so first.
auto root = rootOf(args[index]);
// the object a method is called on (`w.iter()` of a class that is not `@final`)
if (auto object = boundThis(root))
{
root = rootOf(object);
}

if (auto boxOp = root.getDefiningOp<mlir_ts::VariableOp>(); boxOp && boxOp->hasAttr(CAPTURE_BOX_ATTR_NAME))
{
if (!checkBoundedByBox(call, root, uses, name))
Expand Down
83 changes: 82 additions & 1 deletion tslang/lib/TypeScript/OwnershipInferencePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace own_inference
mergeBorrowRetains.clear();
captureStores.clear();
captureRetains.clear();
callCopies.clear();
returnedParams.clear();
returnsBorrowOf = resultBorrows(f);
returnsRecordOf = isRecordResult(f) ? resultBorrowsAll(f) : llvm::SmallVector<int>();
Expand Down Expand Up @@ -490,7 +491,7 @@ namespace own_inference
OWN_NO_DROPS_ATTR_NAME, OWN_DROP_FIELDS_ATTR_NAME, OWN_FACTS_LOST_ATTR_NAME,
OWN_FRESH_RESULT_ATTR_NAME,
OWN_ASSIGNS_CAPTURES_ATTR_NAME,
OWN_RESULT_BOUNDED_ATTR_NAME,
OWN_RESULT_BOUNDED_ATTR_NAME, OWN_STATE_BORROW_ATTR_NAME,
OWN_CAPTURE_BORROW_ATTR_NAME})
{
op->removeAttr(name);
Expand Down Expand Up @@ -540,6 +541,37 @@ namespace own_inference
{
copyForRetain(retain);
}

for (auto [read, call] : callCopies)
{
auto value = read->getResult(0);
if (!llvm::is_contained(call->getOperands(), value))
{
continue;
}

// a callee that keeps it takes the copy; else it is released after the call
auto args = callArgs(call);
auto kept = llvm::any_of(ownedParams(call), [&](int32_t index) {
return static_cast<size_t>(index) < args.size() && args[index] == value;
});

mlir::OpBuilder builder(call);
auto copy = builder.create<mlir_ts::StringCopyOp>(call->getLoc(), value.getType(), value);
for (auto &operand : call->getOpOperands())
{
if (operand.get() == value)
{
operand.set(copy);
}
}

if (!kept)
{
builder.setInsertionPointAfter(call);
builder.create<mlir_ts::ReleaseOp>(call->getLoc(), copy);
}
}
}

bool OwnershipInferencePass::borrowsKnownParam(mlir::Value value)
Expand Down Expand Up @@ -569,6 +601,11 @@ namespace own_inference

auto retainOp = mlir::cast<mlir_ts::RetainOp>(retain);
auto value = retainOp.getReference();
if (copyIntoTuple(retainOp))
{
return true;
}

if (!isCopyableString(value.getType()) || borrowsKnownParam(value))
{
return false;
Expand Down Expand Up @@ -628,6 +665,50 @@ namespace own_inference
return true;
}

bool OwnershipInferencePass::copyIntoTuple(mlir_ts::RetainOp retainOp)
{
// `[s, n]` of a borrowed string: rc retains the tuple, which retains the string in it. The
// tuple starts from a copy instead, which is its own (spec 22). Only when strings are all it
// holds blocks of: a borrowed object in it cannot be copied.
auto createTupleOp = retainOp.getReference().getDefiningOp<mlir_ts::CreateTupleOp>();
if (!createTupleOp || createTupleOp->getBlock() != retainOp->getBlock() ||
!createTupleOp->isBeforeInBlock(retainOp))
{
return false;
}

llvm::SmallVector<mlir::OpOperand *> borrowed;
for (auto &operand : createTupleOp->getOpOperands())
{
auto field = operand.get();
if (holdsNoBlock(field) || isConstantData(field) || isFresh(rootOf(field)))
{
continue;
}

if (!isCopyableString(field.getType()) || borrowsKnownParam(field))
{
return false;
}

borrowed.push_back(&operand);
}

if (borrowed.empty())
{
return false;
}

mlir::OpBuilder builder(createTupleOp);
for (auto *operand : borrowed)
{
operand->set(builder.create<mlir_ts::StringCopyOp>(retainOp->getLoc(), operand->get().getType(), operand->get()));
}

retainOp->erase();
return true;
}

} // namespace own_inference

#undef DEBUG_TYPE
Expand Down
8 changes: 8 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferencePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ class OwnershipInferencePass
// without the twinReadAfter fallback: a retain whose value has no use of its own is not copied,
// so its error stays.
bool copyForRetain(mlir::Operation *retain);
// A retain of a tuple `ts.CreateTuple` made from borrowed strings (`[s, n]`): the strings are
// copied into it instead, and the retain goes.
bool copyIntoTuple(mlir_ts::RetainOp retainOp);

// The parameter this function's result borrows (`__own_result_borrows`), or -1; and the
// parameters (or views of them) whose rc reference for the caller was already decided.
Expand Down Expand Up @@ -222,6 +225,9 @@ class OwnershipInferencePass
llvm::DenseMap<mlir::Operation *, Closure *> captureStores;
// rc's references for boxes, which the closures decide
llvm::DenseSet<mlir::Operation *> captureRetains;
// A string read out of a place, given to a call that may release it (the dry run's): the call
// gets a copy instead, released after it (spec 22), read -> call
llvm::SmallVector<std::pair<mlir::Operation *, mlir::Operation *>> callCopies;
// each closure over a cell, by its `ts.CreateBoundFunction`
llvm::DenseMap<mlir::Operation *, Closure *> closureOf;
// each generator's capture box, which escapes with its state object (classifyStateBoxes)
Expand Down Expand Up @@ -785,6 +791,8 @@ class OwnershipInferencePass
// Reports and returns false when the read is kept, or used after something that may destroy
// it.
bool checkPlaceRead(mlir::Operation *read, llvm::SetVector<mlir::Operation *> &toErase);
// Can `call` be given a copy of `value`, a string read out of a place, in its place (callCopies)?
static bool copiesInto(mlir::Operation *call, mlir::Value value);

// 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
28 changes: 28 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,14 @@ namespace own_inference
});
}

bool OwnershipInferencePass::copiesInto(mlir::Operation *call, mlir::Value value)
{
// given once: a copy given twice would have two owners. Not an invoke, whose release after
// it would go on the normal path only.
return isCopyableString(value.getType()) && !mlir::isa<mlir_ts::InvokeOp>(call) &&
llvm::count(call->getOperands(), value) == 1;
}

bool OwnershipInferencePass::checkPlaceRead(mlir::Operation *read, llvm::SetVector<mlir::Operation *> &toErase)
{
auto loadOp = mlir::dyn_cast<mlir_ts::LoadOp>(read);
Expand Down Expand Up @@ -736,6 +744,12 @@ namespace own_inference

if (kept || letKept)
{
// a string given to a call that keeps it: the call takes a copy (spec 22)
if (dryRun && kept && isCall(kept) && copiesInto(kept, result))
{
callCopies.push_back({read, kept});
}

if (!kept && isAssignmentOf(letKept, letKeeps))
{
reportPlaceBorrowerAssigned(letKept, varName(letKeeps), place);
Expand All @@ -757,6 +771,20 @@ namespace own_inference
{
if ((use.op != drop || !use.callee) && reachableAfter(drop, use.op, use.kills))
{
// a string given to the very call that may release it, when the call keeps it, or
// is a callback - a function value, which nothing can say `@nodrops` of: the call
// gets a copy
auto callee = calleeValue(drop);
auto args = callArgs(drop);
auto keepsIt = llvm::any_of(ownedParams(drop), [&](int32_t index) {
return static_cast<size_t>(index) < args.size() && args[index] == result;
});
if (dryRun && use.op == drop && (keepsIt || (callee && callee.getDefiningOp<mlir_ts::LoadOp>())) &&
copiesInto(drop, result))
{
callCopies.push_back({read, drop});
}

reportPlaceBorrowOutlives(use.op, drop, name, place);
return true;
}
Expand Down
Loading
Loading