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
5 changes: 3 additions & 2 deletions docs/compile-time-work.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ Every table answers a question a scan used to answer, and must give the same ans
`TSLANG_LIB_PATH`, `TSLANG_HOME`; set `TSLANG_VERIFY_LLVM_GLOBALS_INDEX`; `ctest -C Release -j 10
--timeout 300`. Then the suite without it.
4. Compare LLVM IR with a compiler from before the change (`--emit=llvm`, default options,
`--opt --opt_level=3`, `--mm=rc`) on the largest tests. `00param_assigned_owned.ts` differs
from run to run with any compiler (the same lines in another order) and is not a finding.
`--opt --opt_level=3`, `--mm=rc`) on the largest tests. The output is the same from run to run;
a compiler before RelocateConstantPass kept program order put the constants of a function with more than 16 of them (and the
globals made for them) in another order on each run - `00param_assigned_owned.ts` showed it.

Known on the development machine: `test-borrow-facts-*` (5 tests) fail because the build cache's
`CMAKE_AR` names an MSVC toolset that is no longer installed. They pass when their commands are
Expand Down
40 changes: 40 additions & 0 deletions docs/own-memory-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,46 @@ stores a parameter keeps it, so its callers have to move the argument in; a func
returns a field of its argument returns a borrow, so its callers may only borrow the result.
Callers in other modules cannot see the body, which is what the [decorators](#decorators) are for.

## Assigned parameters and `delete`

A parameter is lent by the caller. A function that assigns one keeps it instead: the parameter
then owns each value it is given, so the caller moves the argument in and may not use it after
the call.

```typescript
class B { constructor(public b: number) {} }

function pick(x: B, fresh: boolean) {
if (fresh) {
x = new B(7); // pick assigns x, so it keeps it
}

return x.b;
}

function main() {
print(pick(new B(1), true)); // a new object moves in

const b = new B(2);
print(pick(b, false)); // b moves in
// print(b.b); // error: 'b' is used here after its value was moved
}
```

An exported function, or a method reached through an interface, says so in its signature with
[`@keeps("x")`](#keeps). A parameter captured or assigned by a closure, one a generator assigns,
and `x ??= value` are not accepted yet.

`delete` ends an owner. `delete c` of a local or a field that owns its value frees the value now
and leaves null there, so reading it afterwards fails as a null read does. If the value was moved
out before (`h.child = c; delete c`), nothing is freed. A `const` holding a new object is freed
where its block ends. `delete` of a parameter, or of anything else the function does not own, is
an error:

```text
error: 'delete' of a value this function does not own: under -mm=own only a local or a field that owns its value can be deleted
```

## Reading the errors

A value is named `'this value'` in the messages below; the position points at it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ place, so all four places that report it get the same names. Each message has a
| a function returns a borrow of a parameter on one path and something else on another | `'f' returns a borrow of its argument on some paths and another value on others; -mm=own cannot tell its callers whether they own the result`, with a note at the other return | `own_err_mixed_return`; `own_err_unbox_string` until §22, which copies the string and deleted it |
| a parameter's value, returned, where the callers cannot learn the fact (§15) | `'x' is a parameter, returned here, but -mm=own cannot tell the callers of 'f' that the result borrows it`, with §15's facts-lost note | `own_err_param_returned_exported` |
| a parameter's value, kept (stored, pushed, given away) | `'x' is a parameter, kept here, but -mm=own cannot make the callers of 'f' give it up`, with the note | `own_err_param_kept_exported` |
| a store into a parameter's slot | `'x' is a parameter and cannot be assigned under -mm=own yet` | `own_err_param_assigned` |
| a store into a parameter's slot (one with no local of its own, §81) | `'x' is a parameter and cannot be assigned under -mm=own yet` | - |
| a store into a local that owns nothing | `'x' owns nothing under -mm=own (it is declared without a value, or in a catch or finally clause), so it cannot be given a new value here` | `own_err_local_owns_nothing` |
| a value merged from branches or loop iterations (a conditional, `&&`, `\|\|`, `??`, a loop) | 'x' is merged from several branches (the operators, a loop); -mm=own cannot prove which one owns it yet | `own_err_merged_result` |
| a global's value, stored, returned or boxed | `'x' borrows the global 'g' and cannot be stored, returned or captured` | `own_err_global_returned` |
Expand Down Expand Up @@ -5763,3 +5763,35 @@ it does not read.
Not here: an array written (`push`, an element stored), stored, returned, captured or given to a
call (the call's array of §49 aside); a literal with a fresh element beside borrowed ones; a local's
value as an element (`[background, tile]`), whose bound is the local's own ends.

## 81. An assigned parameter is kept; `delete` ends an owner, 2026-11-15

Two refusals that said "yet". Of the 621 corpus files, nine stopped at the first and five at the second.

**A parameter the body assigns.** rc gives such a parameter a local of its own (#512), and own did so
only for a string (§22.3) and a `Shared<T>` handle; any other was "a parameter and cannot be assigned
under -mm=own yet". `mlirGenFunctionOwnAssignedParams` now starts the local for every parameter that owns
a block. The local owns what it holds, so the caller's value moves into it: the parameter is a kept one,
by the rules a parameter stored in a field already follows (`__own_params`, `@keeps("x")` for a function
another module may call), and no pass changed. What follows from that:
- a caller that reads the argument after the call gets "is used here after its value was moved", with the
call as the place it moved (`own_err_param_assigned`);
- a function whose callers cannot be told - exported, reached through an interface - gets "is a parameter,
kept here, but -mm=own cannot make the callers ... give it up" (`own_err_param_assigned_exported`);
- `x = n` of a local moves `n` into the parameter's local, where it was the error #752 added
(`own_err_param_assigned_local`, now a case of `own_param_assigned`).

`p.length = n` starts a local only for a string; an array is resized where it is, and its callers keep it.
`assignsParameter` stays for a parameter with no local (a binding pattern). Still refused, by other rules:
a parameter a closure captures or assigns, one a generator assigns, and `x ??= v` (a merge).

**`delete`.** Own has no `ts.Delete`. MLIRGen writes `delete place` as the assignment that clears the place
when the place owns its value (`isOwningSlot`: a local, a field, an element, a global): the release and the
store an assignment makes, which the passes follow already - a local whose value moved frees nothing, a
borrow of it ends (`own_err_delete_borrowed`), the function may drop. A value no storage holds (`delete new
C()`, a `const` kept as a value) is released where its block ends, as before, and a value that owns no
block is left alone. Anything else - a parameter, a local that owns nothing - is "'delete' of a value this
function does not own" (`own_err_delete_param`). A local read after its `delete` holds null.

Tests: `own_param_assigned` (own, rc, gc), `own_delete` (own, rc), and the seven corpus files only these
two stopped, under own (`test-*-own-flipped-*`).
36 changes: 36 additions & 0 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,8 @@ namespace mlirgen
auto result = mlirGen(deleteExpression->expression, genContext);
EXIT_IF_FAILED_OR_NO_VALUE(result)
auto expr = V(result);
// what the expression named, before an array is turned into a reference to its elements
auto place = expr;

if (!isa<mlir_ts::RefType>(expr.getType()) && !isa<mlir_ts::ValueRefType>(expr.getType()) &&
!isa<mlir_ts::ClassType>(expr.getType()))
Expand Down Expand Up @@ -1936,6 +1938,40 @@ namespace mlirgen
}
}

// -mm=own: `delete` ends the owner the expression names, and only an owner can be ended. There is no
// ts.Delete: a local or a field that owns its value is released and cleared the way an assignment
// releases what it replaces, which the ownership passes already follow - what was moved out of the local
// is not freed, a borrow of it ends, and a caller learns the function may drop. A value no storage holds
// (`delete new C()`, or a `const` the compiler kept as a value) is released where its block ends, as it
// would have been. Anything else - a parameter, a local that owns nothing, a value read out of another
// owner - belongs to someone who would free it again.
if (compileOptions.memoryModel == MemoryModelOwn)
{
// a discovery run marks no value as carrying a reference, so it cannot tell whose the value is
// ...and a value that owns no block (`new` of a number) has no owner to end: own frees it no more than
// reference counting does
if (genContext.dummyRun || !mth.ownsHeapMemory(location, place.getType()) || producesOwnedReferenceAtLastUse(place))
{
return mlir::success();
}

MLIRCodeLogic mcl(builder, compileOptions);
auto reference = mcl.GetReferenceFromValue(location, place);
if (!reference || !isOwningSlot(location, reference))
{
emitError(location, "'delete' of a value this function does not own: under -mm=own only a local or a field "
"that owns its value can be deleted");
return mlir::failure();
}

auto nullValue = builder.create<mlir_ts::NullOp>(location, getNullType());
auto clearedResult = cast(location, place.getType(), nullValue, genContext, /*disableStrictNullCheck*/ true);
EXIT_IF_FAILED_OR_NO_VALUE(clearedResult)
auto saved = mlirGenSaveLogicOneItem(location, place, V(clearedResult), genContext);
EXIT_IF_FAILED(saved)
return mlir::success();
}

builder.create<mlir_ts::DeleteOp>(location, expr);

// Under reference counting `delete` gives up the reference the expression named, so the
Expand Down
53 changes: 48 additions & 5 deletions tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,27 @@ namespace mlirgen
visitor.visit(body);
}

// Every name a function nested in this body mentions: what a closure there may capture. A name too many only
// keeps a parameter as it was.
static void collectNestedFunctionNames(ts::Node body, llvm::StringSet<> &names)
{
ts::VisitorAST visitor([&](ts::Node node) {
if (node == SyntaxKind::ArrowFunction || node == SyntaxKind::FunctionExpression ||
node == SyntaxKind::FunctionDeclaration || node == SyntaxKind::MethodDeclaration ||
node == SyntaxKind::GetAccessor || node == SyntaxKind::SetAccessor || node == SyntaxKind::Constructor)
{
ts::VisitorAST nested([&](ts::Node inner) {
if (inner == SyntaxKind::Identifier)
{
names.insert(MLIRHelper::getName(inner.as<ts::Identifier>()));
}
});
nested.visit(node);
}
});
visitor.visit(body);
}

// -mm=rc: a parameter is borrowed - the caller owns its value (takeOwnershipOfLocal), so its slot neither
// retains nor releases. One the body assigns holds values the frame made, though: `x = new B(1)` stored the
// instance with no reference of its own, the block it was made in released it, and `x` pointed at freed
Expand All @@ -1794,8 +1815,15 @@ namespace mlirgen
// -mm=own: a Shared<T> parameter the body assigns is copied too. Own steps aside for Shared handles - their
// counts are rc's - so it accepts the assignment, and the slot held a handle that only borrowed the new
// value: assigned in a loop, the handle was released at the end of the loop body and a later `p.value` read
// freed memory. Any other parameter own refuses to assign (OwnershipInferencePass), and a collector needs
// none of this.
// freed memory.
//
// -mm=own: any other parameter the body assigns - an object, an array - starts a local as well. The local
// owns what it holds, so the caller's value has to become its own: the function keeps the parameter, as one
// that stores it in a field does, and its callers give the argument up (the ownership passes' kept
// parameters; '@keeps' says it for a function another module may call). A caller that reads the argument
// after the call is told it was moved. A parameter left without a local own still refuses to assign
// (OwnershipInferencePass): one with a binding pattern, and one a nested function mentions - a closure's
// cell borrows the caller's value, and the error for that names the capture. A collector needs none of this.
mlir::LogicalResult MLIRGenImpl::mlirGenFunctionOwnAssignedParams(mlir::Location location,
FunctionLikeDeclarationBase functionLikeDeclarationBaseAST,
FunctionPrototypeDOM::TypePtr funcProto,
Expand All @@ -1809,22 +1837,37 @@ namespace mlirgen

llvm::StringSet<> assignedNames;
llvm::StringSet<> lengthNames;
llvm::StringSet<> nestedNames;
auto scanned = false;
for (const auto &param : funcProto->getParams())
{
if (param->getBindingPattern() || !mth.ownsHeapMemory(location, param->getType()) ||
(own && !mlir::isa<mlir_ts::StringType, mlir_ts::SharedType>(param->getType())))
if (param->getBindingPattern() || !mth.ownsHeapMemory(location, param->getType()))
{
continue;
}

if (!scanned)
{
collectAssignedNames(functionLikeDeclarationBaseAST->body, assignedNames, own ? &lengthNames : nullptr);
if (own)
{
collectNestedFunctionNames(functionLikeDeclarationBaseAST->body, nestedNames);
}

scanned = true;
}

if (!assignedNames.contains(param->getName()) && !lengthNames.contains(param->getName()))
// own: a string or a Shared<T> handle is a copy whoever captures it; any other parameter a closure may
// capture stays the caller's
if (own && !mlir::isa<mlir_ts::StringType, mlir_ts::SharedType>(param->getType()) &&
nestedNames.contains(param->getName()))
{
continue;
}

// `p.length = n` makes a new value only of a string: an array is resized where it is
if (!assignedNames.contains(param->getName()) &&
!(lengthNames.contains(param->getName()) && mlir::isa<mlir_ts::StringType>(param->getType())))
{
continue;
}
Expand Down
13 changes: 8 additions & 5 deletions tslang/lib/TypeScript/RelocateConstantPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "mlir/Dialect/LLVMIR/LLVMDialect.h"

#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

Expand All @@ -30,7 +30,10 @@ class RelocateConstantPass : public mlir::PassWrapper<RelocateConstantPass, Type
// TODO: get rid of using it
auto f = getFunction();

SmallPtrSet<Operation *, 16> workSetConst;
// in the order the function holds them: a set of pointers gave them back in the order of their addresses
// once there were more than 16, so the same source compiled to constants - and the globals made for
// them - in another order on every run
SmallVector<Operation *, 16> workSetConst;
getOps<mlir_ts::ConstantOp>(f, workSetConst);
/*auto lastConstOp =*/relocateConst(f, workSetConst);

Expand All @@ -48,7 +51,7 @@ class RelocateConstantPass : public mlir::PassWrapper<RelocateConstantPass, Type
});
}

Operation *relocateConst(mlir_ts::FuncOp &f, SmallPtrSet<Operation *, 16> &workSet)
Operation *relocateConst(mlir_ts::FuncOp &f, SmallVector<Operation *, 16> &workSet)
{
// find first non-constant op
auto firstNonConstOp = seekFirstNonConstOp(f);
Expand All @@ -75,7 +78,7 @@ class RelocateConstantPass : public mlir::PassWrapper<RelocateConstantPass, Type
return firstNonConstOp;
}

template <typename T> void getOps(mlir_ts::FuncOp &f, SmallPtrSet<Operation *, 16> &workSet, Operation *startFrom = nullptr)
template <typename T> void getOps(mlir_ts::FuncOp &f, SmallVector<Operation *, 16> &workSet, Operation *startFrom = nullptr)
{
auto startFromStage = startFrom != nullptr;
auto skipFirstOps = true;
Expand All @@ -101,7 +104,7 @@ class RelocateConstantPass : public mlir::PassWrapper<RelocateConstantPass, Type
if (typedOp->getParentOp() == f)
{
// select only those consts which are not at the beginning
workSet.insert(typedOp);
workSet.push_back(typedOp);
}
}
}
Expand Down
Loading
Loading