diff --git a/docs/memory-models.md b/docs/memory-models.md index ca4ff9aa8..d87ad298b 100644 --- a/docs/memory-models.md +++ b/docs/memory-models.md @@ -89,9 +89,11 @@ holds exactly as much as `-mm=none` — that is, reference counting reclaims non done with the structure, or store a key/index instead of a pointer back to the owner. 3. If neither fits, `-mm=rc` is not the right mode for that program. -A `WeakRef` that lets you declare a back-reference as non-owning is designed but not -implemented; see `tslang/docs/reference-counting-evaluation.md` §9.8. When it lands it will be -the fourth option here, and it does not change anything above. +`WeakRef` declares a back-reference that does not own what it refers to. It works under +`-mm=gc`, `-mm=own` and `-mm=none`; under `-mm=rc` it is a compile error, because rc's header word +is already the strong count and a weak count needs a second one (see +`tslang/docs/reference-counting-evaluation.md` §9.8). So for a program with parent links, `-mm=own` +with `WeakRef` is the fourth option here. ## The standard library diff --git a/docs/own-memory-model.md b/docs/own-memory-model.md index ae94fa178..4cc66bd21 100644 --- a/docs/own-memory-model.md +++ b/docs/own-memory-model.md @@ -905,7 +905,16 @@ a reference gives it back on every path out of a function. parameter (`while (n) { k++; n = n.next; }`) when every value it gives it is read out of the one it holds: the parameter then borrows the caller's list. A parameter given anything else (`n = new Node(0)` on some path) is kept, and the caller gives it up. -- `Shared` cycles are never freed. +- A back-reference is a `WeakRef` (`parent: WeakRef | null`, `c.parent = new WeakRef(this)`, + `this.parent?.deref()`). It does not own its target: when the target's owner frees it, `deref()` + gives `undefined` from then on. The object `deref()` gives is a borrow: it cannot be stored, + returned or captured, and anything that may free an object of its type (a release of a holder of + that type, a call that may drop) ends it, so read what you need right away. A `@final` target class + keeps more of those borrows. The target is a class object. A module that makes WeakRefs may not + share with another thread (`Thread.spawn`, a worker, `Shared.share`) anything that may hold their + target, since the count is not atomic; in practice no `Thread.spawn` of a closure in such a module. + Each object carries the count in the header word it already has: no object grows. +- `Shared` cycles are never freed; break them with a `WeakRef` where one side only refers back. - There are no lifetime annotations: a borrow that the compiler cannot follow is an error, even when the program would be correct. The fix is usually a move, a fresh value or `Shared`. diff --git a/docs/own-readiness.md b/docs/own-readiness.md index 113c34170..02052ffbe 100644 --- a/docs/own-readiness.md +++ b/docs/own-readiness.md @@ -104,8 +104,8 @@ Probed one by one under the JIT: | Tree insert by recursion on a parameter: `t.l = ins(t.l, v)` | Compiles and runs, when the function is in the same module and never throws | | Cutting while walking: `const nx = n.next; n.next = null; n = nx` | Rejected: the cut frees what `nx` names (accepted by mistake from 2026-11-16 until spec 83) | | Tree insert and sum as methods recursing on `this` | Compiles and runs | -| Parent link: `k.parent = this` | Rejected; the answer is `Shared`, whose cycles leak | -| `new WeakRef(a)` | Rejected: built on the collector | +| Parent link: `k.parent = new WeakRef(this)` | Compiles and runs (spec 84); `deref()` gives `undefined` once the parent is freed | +| `new WeakRef(a)` | Compiles and runs under own, gc and none; rc refuses it | | `new Map()`, `filter` over class instances | Rejected by design; `Shared` | | Swap through a local: `let t = a[i]; a[i] = a[j]; a[j] = t` | Rejected by design; `[a[i], a[j]] = [a[j], a[i]]` | | `x ??= new B()` on a parameter | Rejected ("merged from several branches") | @@ -148,8 +148,10 @@ assertions-enabled CI leg, and a soak run. The first holds today; the other two ## What is left, in order -1. **A non-owning back-reference.** `WeakRef` under own, or a declared borrowing field. Without - it a tree with parent links either does not compile or leaks. +1. **Back-references beyond WeakRef.** `WeakRef` works (spec 84): the target's header word holds + the weak count, and a freed target is a tombstone until its last WeakRef goes. Left: a module that + makes WeakRefs cannot spawn threads with closures, and a zero-cost borrowing field for shapes the + compiler can prove. 2. **The rest of linked structures.** A list, a node built from the old head, a walk that assigns a parameter and the recursive tree insert work (spec 83). A call that may throw, or one into another module, still cannot take a field this way, and the popped node cannot be used after the assignment that frees it. diff --git a/tslang/docs/superpowers/plans/2026-11-16-own-weakref.md b/tslang/docs/superpowers/plans/2026-11-16-own-weakref.md new file mode 100644 index 000000000..af8ef359c --- /dev/null +++ b/tslang/docs/superpowers/plans/2026-11-16-own-weakref.md @@ -0,0 +1,153 @@ +# `WeakRef` under `-mm=own` Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** A built-in `WeakRef` that works under own, gc and none: own keeps a weak count in the target's header word, freed targets become tombstones, and `deref()` gives a compile-time borrow. + +**Architecture:** A new type `!ts.weak_ref` with ops `ts.WeakRefNew` / `ts.WeakRefDeref`, resolved by name before any declaration. Under own a weak reference is a counted handle like `Shared` (own leaves rc's retains and releases, lowering counts on the target's header). The class release routine checks the count before freeing. Inference treats a deref result as a borrow of an unknown place (TSLANG 84's `anyPlace` chain). + +**Tech Stack:** C++17, MLIR (TableGen ops/types), LLVM lowering, the tslang test-runner and CTest. + +**Spec:** `tslang/docs/superpowers/specs/2026-11-16-own-weakref-design.md` + +Worktree: `I:\TypeScriptCompiler\.claude\worktrees\own-items-60`, branch `own-weakref`. Build: `cmake --build __build/tslang/windows-msbuild-2026-release --config Release --target tslang --parallel 8` (all targets before ctest); tests: `ctest -C Release -R -j 8` from that build dir. JIT probe: `bin/tslang.exe -mm= --emit=jit file.ts`. + +## Global Constraints + +- Models: own, gc, none have `WeakRef`; under rc any use is the error `WeakRef is not supported under -mm=rc; use -mm=gc, -mm=own or -mm=none`. +- Under own and none, `T` and the argument must be a class object; else `WeakRef needs a class object`. Under gc any object (`T extends object`) is accepted, as today. +- Own header word of a mortal class object: bits 0..61 weak count, bit 62 dead (tombstone), bit 63 unused, all bits set = immortal (tested first). On a 4-byte word (wasm32, 32-bit targets) the dead bit is bit 30. +- `deref()` returns `T | undefined`: the object, or `undefined` once freed (own: tombstone; gc: link cleared; none: never). +- Releasing a `WeakRef` is no drop: it does not remove `__own_no_drops` and ends no borrow. +- No object grows; a program without `WeakRef` gains one header test per free of a class object, nothing else. +- `WeakRef` resolves to the built-in type even where a class named `WeakRef` is declared (the default library declares one; it is removed in Task 7). +- Commit messages end with `Co-Authored-By: Claude Opus 5.5 `; commits are GPG-signed (never `--no-gpg-sign`). + +## Review Focus + +1. A `WeakRef` copied (`b.parent = a.parent`) and both released: the count goes back to zero, no early free of the tombstone, no leak. → Task 2 test `00weakref` step "copies". +2. A `WeakRef` field reassigned after its target died: the old tombstone is freed by that release. → Task 2, `00weakref` "retarget". +3. An object holding a `WeakRef` to itself, freed: its field release takes the count to zero before the free path tests it, so the block is freed once. → Task 2, `00weakref` "self". +4. `WeakRef`s held in an array that is popped, spliced and dropped. → Task 2, `00weakref` "elements". +5. A `WeakRef` to an immortal object (a global's, kept for good): count untouched, `deref()` always gives it. → Task 2, `00weakref` "immortal". + +--- + +### Task 1: The type, the surface, gc and none + +**Files:** +- Modify: `tslang/include/TypeScript/TypeScriptTypes.td` (after `TypeScript_Shared`, ~line 133): `TypeScript_WeakRef`, mnemonic `weak_ref`, one parameter `elementType`. +- Modify: `tslang/include/TypeScript/TypeScriptOps.td` (after `TypeScript_SharedCountOp`, ~line 2531): `TypeScript_WeakRefNewOp` (`ins AnyType:$target`, `outs TypeScript_WeakRef:$result`), `TypeScript_WeakRefDerefOp` (`ins TypeScript_WeakRef:$weakRef`, `outs AnyType:$result`). +- Modify: `tslang/lib/TypeScript/MLIRGenTypes.cpp` (`isEmbededTypeWithBuiltins`/`NoBuiltins` ~949/1043, `getEmbeddedTypeWithParam*` ~1207/1314, `getTypeByTypeReference` ~804): `WeakRef` checked before declared generics. +- Modify: `tslang/lib/TypeScript/MLIRGenExpressions.cpp` (`new` ~1666, beside `mlirGenNewShared` ~1851): `mlirGenNewWeakRef`, taken before normal resolution. +- Modify: `tslang/lib/TypeScript/MLIRGenAccessCall.cpp` (beside `s.value` ~350): `w.deref()` → `ts.WeakRefDeref`. +- Modify: `tslang/lib/TypeScript/LowerToLLVM.cpp` (beside Shared lowering ~1385, registration ~9990): type converter (a pointer), `WeakRefNew`, `WeakRefDeref` for gc and none. +- Test: `tslang/test/tester/tests/00weakref_basic.ts`, `tslang/test/tester/own/own_err_weakref_rc.ts`, `own_err_weakref_not_class.ts`; `tslang/test/tester/CMakeLists.txt`. + +**Interfaces:** +- Produces: `mlir_ts::WeakRefType` (`getElementType()`), `mlir_ts::WeakRefNewOp`, `mlir_ts::WeakRefDerefOp`; `MLIRTypeHelper::isWeakRefType(mlir::Type)` (also true for an optional of one, and a union of one with members owning no heap, mirroring `isSharedHandleType`). + +- [ ] **Step 1: Write `00weakref_basic.ts`**: a `Node` with `parent: WeakRef | null`; `add(c)` sets `c.parent = new WeakRef(this)`; `depth()` via `this.parent?.deref()`; asserts `leaf.depth() == 2`, `(new WeakRef(root)).deref() === root`; prints `done.`. Register it for `gc none` (jit + compile) beside the `shared_models` block (~line 5079). +- [ ] **Step 2: Write the errors**: `own_err_weakref_rc.ts` (any `new WeakRef(new C())`) expecting `WeakRef is not supported under -mm=rc` under `-mm=rc`; `own_err_weakref_not_class.ts` (`new WeakRef([1, 2])`) expecting `WeakRef needs a class object` under own. Register in the own error-cases list (rc case needs its own `add_test` with `-mm=rc`). +- [ ] **Step 3: Build and run `ctest -C Release -R "weakref"`**: expected FAIL (unknown type / GC_ call refused). +- [ ] **Step 4: Implement the type, ops, MLIRGen and the gc/none lowering.** gc: allocate an atomic (unscanned) gc block of one word, store `~ptr`, call `GC_general_register_disappearing_link(block, target)`; deref loads the word, 0 → `undefined`, else `~word`. none: the value is the target pointer; deref returns it. `deref()`'s result type is what a method declared `deref(): T | undefined` gets. Under own, until Task 2, emit the error `WeakRef under -mm=own is not implemented yet`. +- [ ] **Step 5: Run `ctest -C Release -R "weakref|shared"`**: expected PASS; also the default library's gc `WeakMap`/`WeakSet` tests still pass (they now use the built-in type): JIT-run `I:\dl_release_check\tests\weakmap_weakset.ts` and `weakref_basic.ts` under gc. +- [ ] **Step 6: Commit** `WeakRef: built-in type; gc and none`. + +### Task 2: Own run time: counts, tombstones, handles + +**Files:** +- Modify: `tslang/include/TypeScript/Defines.h` (~line 583): `HEAP_BLOCK_DEAD_BIT` comment and value (word's bit `bits - 2`), `HEAP_BLOCK_WEAK_COUNT_MASK`. +- Modify: `tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h`: `buildBody` class branch (~1707-1718) — after `releaseFields`, if `word & WEAK_COUNT_MASK` then `word |= DEAD` else `emitFreeBlock`; new `emitWeakIncRef(target)` / `emitWeakDecRef(target)` (skip immortal; dec frees the block when the result is exactly `DEAD`); the release routine of a `weak_ref` type calls `emitWeakDecRef`. +- Modify: `tslang/lib/TypeScript/LowerToLLVM.cpp`: own lowering of `WeakRefNew` (`emitWeakIncRef`), `WeakRefDeref` (immortal → target; dead → `undefined`; else target), Retain/Release of a `weak_ref` (allowed under own like Shared handles, ~510/746/834). +- Modify: `tslang/lib/TypeScript/OwnershipFacts.h` (`isHandle`/`touchesHandle` ~1041): also `isWeakRefType`; new `inline bool isWeakHandle(mlir::Value)`. +- Modify: `tslang/lib/TypeScript/OwnershipSignaturePass.cpp` (`dropsInBody` ~2136): a release of a weak handle is no drop. +- Modify: `tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp` / `OwnershipInferencePass.cpp`: `ts.WeakRefDeref`'s result is a borrowing read with `Chain{anyPlace = true}`, no roots; ended as TSLANG 84's chains are (`dropsChain`, `typeMayHold`). +- Test: `tslang/test/tester/tests/00weakref.ts`; CMakeLists (own, gc, none; `-noopt` under own in the debug-own list ~6686; not in `own_test`, whose no-counting check forbids counts). + +**Interfaces:** +- Consumes: Task 1's type, ops and `isWeakRefType`. +- Produces: `own_facts::isWeakHandle(mlir::Value) -> bool`; `OwnershipRoutineLogic::emitWeakIncRef(mlir::Value)`, `emitWeakDecRef(mlir::Value)`. + +- [ ] **Step 1: Write `00weakref.ts`** with sections, each in a 2000-iteration loop with same-size `new Node` churn between steps: + - "tree": parents, children; `leaf.depth() == 3`. + - "dropped": `const w = new WeakRef(sub)` kept while `root.children.pop()` frees `sub`; then `w.deref() === undefined`. + - "copies": `b.parent = a.parent`; free the parent; both `deref()` undefined; drop both. + - "retarget": a field's target dies, the field gets `new WeakRef(other)`; `deref() === other`. + - "self": `n.me = new WeakRef(n)`, `n` dropped. + - "elements": an array of `WeakRef`s, `pop`, `splice(0, 1)`, the array dropped. + - "immortal": `const G = new Node(7)` global; `new WeakRef(G).deref() === G` after locals churn. + - Ends `print("done.")`; enable `00weakref_basic` under own too. +- [ ] **Step 2: Run** `ctest -C Release -R weakref`: FAIL under own (Task 1's not-implemented error). +- [ ] **Step 3: Implement** the files above. +- [ ] **Step 4: Run** `ctest -C Release -R "weakref|shared|linked_list"`: PASS. `--verify-ownership` quiet on `00weakref` under own. +- [ ] **Step 5: Leak and double free**: AOT churn (`--emit=exe --no-default-lib`, 20M iterations of the "dropped" and "copies" sections, expected total as a float) prints `done.` with flat private memory under own; on WSL `~/tsc-size` (build `__build/tslang/ninja/release`) `GLIBC_TUNABLES=glibc.malloc.tcache_count=0 MALLOC_PERTURB_=165 ctest -R "weakref|own"` passes. +- [ ] **Step 6: Commit** `-mm=own: WeakRef counts in the target's header; freed targets are tombstones`. + +### Task 3: Own: what a deref result may not do + +**Files:** +- Modify: `tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp` (`describePlace` and the place-borrow reports): a deref result is described as `the object a WeakRef refers to`. +- Test: `tslang/test/tester/own/own_err_weakref_stored.ts`, `_returned.ts`, `_captured.ts`, `_after_call.ts`, `_after_release.ts`; `own_weakref_borrow.ts` (positive); CMakeLists. + +**Interfaces:** +- Consumes: Task 2's borrow chain for `ts.WeakRefDeref`. + +- [ ] **Step 1: Write the tests.** Errors (own error-cases list, exact line:col filled from the first run): + - `_stored`: `h.n = w.deref()!` → `borrows the object a WeakRef refers to and cannot be stored, returned or captured`. + - `_returned`: `return w.deref()`, same message. + - `_captured`: `const p = w.deref(); fns.push(() => p!.v)`, same message. + - `_after_call`: `const p = w.deref(); dropAll(); print(p!.v)` where `dropAll` releases a `Node` field → `borrows the object a WeakRef refers to but is used here after it may be freed`. + - `_after_release`: `const p = w.deref(); holder.child = null; print(p!.v)`, same message. + - Positive `own_weakref_borrow.ts`: `const p = this.parent?.deref(); if (p) return p.v + p.depth();` with `@final class Node` (own, gc, none). +- [ ] **Step 2: Run** `ctest -C Release -R weakref`: the messages differ (generic place text) → FAIL. +- [ ] **Step 3: Implement** the description; keep the existing message forms. +- [ ] **Step 4: Run** `ctest -C Release -R "weakref|own-err"`: PASS. +- [ ] **Step 5: Commit** `-mm=own: a WeakRef's object is a borrow`. + +### Task 4: No stack promotion of a weak target + +**Files:** +- Modify: `tslang/lib/TypeScript/StackPromotionPass.cpp` (`moduleUsesShared` ~85, `decide` ~390): `weakTargets` — class names `T` of every `weak_ref` type in the module; a `ts.New` of such a class or of one deriving from it gets reason `"weak-target"`, in every model. +- Test: `tslang/test/tester/tests/00weakref_frame.ts` (own, gc, none, and `-noopt` own). + +- [ ] **Step 1: Write the test**: `function make(l: Holder) { const n = new Node(5); l.w = new WeakRef(n); return n.v; }` called in a loop; after each call `l.w!.deref()` is `undefined` under own, and under gc/none either `undefined` or a node whose `v == 5`; churn between calls; `done.` +- [ ] **Step 2: Run**: FAIL (garbage or crash under own/none when `n` is promoted). +- [ ] **Step 3: Implement** the refusal; check the dump option the pass already has for reasons (`"shared-handle"` shows how) prints `weak-target`. +- [ ] **Step 4: Run** `ctest -C Release -R "weakref|stack-promotion"`: PASS. +- [ ] **Step 5: Commit** `WeakRef: a weak target stays on the heap`. + +### Task 5: Own: nothing a WeakRef module holds crosses threads + +The design refuses a weak-target object crossing threads. Thread hand-off reaches the compiler only as `Shared.share(this.start)` (`ts.MarkShared`) inside the default library's `ThreadHandle`/worker constructors, where a closure's captures are not visible. So the rule is: under own, in a module whose `weakTargets` is non-empty, a `ts.MarkShared` whose operand's type may hold a weak target (`typeMayHold`, which answers yes for a function's captures) is an error at the spawn's source position: `-mm=own cannot share this value with another thread: it may hold a 'Node', which a WeakRef refers to`. + +**Files:** +- Modify: `tslang/lib/TypeScript/OwnershipSignaturePass.cpp` (or inference, where `ts.MarkShared` is visible with the call stack position): the check. +- Test: `tslang/test/tester/own/own_err_weakref_thread.ts` (spawn of a closure capturing a `Node` in a module using `WeakRef`), `own_weakref_thread_ok.ts` (a module without `WeakRef` still spawns as before; reuse an existing threads test if one covers it). + +- [ ] **Step 1: Write the tests**; register the error with its line:col. +- [ ] **Step 2: Run**: the error test FAILs (compiles). +- [ ] **Step 3: Implement**; the position is the user's `Thread.spawn` call (the MarkShared is in an instantiated library generic: report at the outermost location in the op's call-site `CallSiteLoc`/`FusedLoc` chain that is in the user's file). +- [ ] **Step 4: Run** `ctest -C Release -R "weakref|thread|worker"`: PASS. +- [ ] **Step 5: Commit** `-mm=own: a module using WeakRef shares nothing that may hold a weak target`. + +### Task 6: Docs and validation, then the PR + +**Files:** +- Modify: `tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md` (append `## 84. WeakRef`), `docs/own-memory-model.md` (Current limits: back-references), `docs/memory-models.md` (~line 92, "the fourth option" now exists for own/gc/none), `docs/own-readiness.md` (shapes table rows "Parent link" and "`new WeakRef(a)`"; What is left item 1). + +- [ ] **Step 1: Write the docs**, including Task 5's coarse thread rule and the cross-module note (objects from an older own compiler's object files are unsupported). +- [ ] **Step 2: Full Windows ctest**: `ctest -C Release -j 8` — all pass. +- [ ] **Step 3: Linux**: apply the branch diff to WSL `~/tsc-size` (its `origin` is stale: fetch commits by sha from `/mnt/i/TypeScriptCompiler`), `ninja -j 4`, run own and weakref tests under `MALLOC_PERTURB_` as in Task 2 — all pass. +- [ ] **Step 4: Corpus**: compile every `tslang/test/tester/tests/*.ts` and `own/*.ts` under own with origin/main's compiler and this branch's; no file newly refused, no crash. +- [ ] **Step 5: Fuzzer**: `python tslang/test/fuzz/mm_fuzz.py --jobs 6 --out run --count 500 --seed 918000` — 0 failures. +- [ ] **Step 6: Default library**: build `I:\dl_release_check` with this compiler and run its tests for gc and own (scripts as in the previous round): no new failure; `weakref_basic`, `weakmap_weakset` pass under gc. +- [ ] **Step 7: Commit, push `own-weakref`, open the PR** (`gh auth switch --user ASDAlexander77` first), body: what works under which model, the header layout, the thread rule, the validation numbers. + +### Task 7: Default library follow-up (separate repository) + +In a clean clone of TypeScriptCompilerDefaultLib (`I:\dl_release_check`, never the user's checkout), branch `weakref-builtin`: + +- [ ] **Step 1**: remove `class WeakRef` (`src/generics/lib.generics.ts` ~3061-3081); give `WeakMapEntry` a private `class __GcWeakLink` with the same body, so `WeakMap`/`WeakSet` stay gc only. +- [ ] **Step 2**: build and run the library's tests under gc, own, rc and none: `weakref_basic` passes under gc (built-in), `weakmap_weakset` passes under gc and stays skipped elsewhere. +- [ ] **Step 3**: commit, push, open the PR, linked from Task 6's PR; it merges after it. diff --git a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md index 665ac2f08..1b27f9272 100644 --- a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md +++ b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md @@ -5911,3 +5911,51 @@ and a borrow is not returned from a parameter's local). Tests: `00linked_list_build` (own, rc, gc, none; also `-noopt` under own), and `own_err_list_unlink_walk`, `_cut_in_step`, `_cut_through_alias`, `_push_ctor_throws`, `_push_through_this`, `own_err_tree_field_given_twice`, `own_err_param_walk_mixed`. + +## 84. `WeakRef`, 2026-11-16 + +The design and its decisions are in `2026-11-16-own-weakref-design.md`; this records what was built. + +**The type.** `!ts.weak_ref`, built into the compiler and resolved before any declaration of the +name (the default library's gc-only class is shadowed). `new WeakRef(x)` is `ts.WeakRefNew`, `w.deref()` +and `w?.deref()` are `ts.WeakRefDeref`, whose result is an optional of `T`. gc lowers to a link block +the collector clears (`GC_general_register_disappearing_link`, as the library class did), none to the +target's pointer, and rc refuses it. Under own and none the target is a class object (narrowed out of +`C | null` is fine); gc takes any object. + +**The count.** Under own a class object's header word held only 0 or the immortal mark. It now holds +the number of WeakRefs to the object in the bits below the dead bit, `1 << (bits - 2)` (Defines.h). A +WeakRef is a handle (`isSharedHandleType` is true for it): own leaves rc's retains and releases of it, +and they become `emitWeakIncRef` / `emitWeakDecRef` on the target, which skip an immortal one. The +class release routine under own ends in `emitFreeOrBury`: with a weak count it sets the dead bit and +keeps the block (a tombstone, its fields released), else it frees it; the last WeakRef to a tombstone +frees it (`newWord == dead bit`). `deref()` checks for null, then immortal or not dead. + +**What a WeakRef ends.** Its release frees at most a tombstone nobody can reach, so `releasesNothing` +is true for a WeakRef slot and `dropsInBody` ignores its release: no borrow ends at it, no function +drops because of it. + +**The object `deref()` gives** is a place read (`ops.placeReads`) with an `anyPlace` chain and no +roots. A release of a slot or a value whose type may hold `T` (`typeMayHold`), anywhere in the +function, ends it, a local's included (`let n; p = w.deref(); n = new Node(); p.v` was accepted and +read freed memory before that rule); so does a call that may drop, unless all it overwrites are fields +that cannot hold `T` (a call judged against a chain with no roots had counted as "caught" and never +dropped). It cannot be stored, returned or captured: "borrows the object a WeakRef refers to". + +**Where it lives.** `new WeakRef(x)` is an escape of `x` for stack promotion (`"weak-target"`). + +**Threads.** The count is not atomic: the signature pass refuses a WeakRef to a class in a module +whose `ts.MarkShared` (Thread.spawn, a worker, `Shared.share`) shares a value whose type may hold +that class (`rejectWeakTargetsShared`), at the WeakRef with a note at the share. Thread.spawn shares a +library `ThreadStart` holding a closure, so in practice a module that makes WeakRefs spawns no thread +with a closure; a `@final` target and a shared `@final` class of numbers are fine. + +**Timing.** "Once its owner frees it" is when own frees: a discarded call result (`a.pop();`) is +released at the end of its function, so the object dies there, not at the statement. + +**Across modules.** Every own class release now reads the weak count; an object file from an older +own compiler frees without it. The default library is rebuilt with the compiler, as always. + +Tests: `00weakref_basic`, `00weakref`, `00weakref_frame` (own, gc, none; `-noopt` under own), +`own_weakref_borrow`, `own_weakref_share_unrelated`; errors `own_err_weakref_rc`, `_not_class`, +`_stored`, `_returned`, `_captured`, `_after_call`, `_after_release`, `_local_reassigned`, `_thread`. diff --git a/tslang/docs/superpowers/specs/2026-11-16-own-weakref-design.md b/tslang/docs/superpowers/specs/2026-11-16-own-weakref-design.md new file mode 100644 index 000000000..62a4c7a1d --- /dev/null +++ b/tslang/docs/superpowers/specs/2026-11-16-own-weakref-design.md @@ -0,0 +1,230 @@ +# `WeakRef` under `-mm=own`: design + +Date: 2026-11-16. Status: draft for approval. + +## Why + +Under `-mm=own` every heap object has one owner, and the owner frees it. A child cannot point back +at its parent: the field would be a second owner. Today such a program either does not compile or +uses `Shared`, whose cycles leak. The readiness page (`docs/own-readiness.md`) lists this first +among what is left: "Without it a tree with parent links either does not compile or leaks." + +The goal is a reference that does not own what it points to, that cannot read freed memory, and that +a TypeScript programmer already knows how to write. + +```ts +class Node { + parent: WeakRef | null = null; + children: Node[] = []; + constructor(public v: number) {} + + add(c: Node) { + c.parent = new WeakRef(this); + this.children.push(c); + } + + depth(): number { + const p = this.parent?.deref(); + return p ? 1 + p.depth() : 0; + } +} +``` + +## Decisions taken (user, 2026-11-16) + +| Question | Decision | +| --- | --- | +| Form | The standard `WeakRef`, with `new WeakRef(x)` and `deref(): T \| undefined`. Checked at run time. Not a compile-time "borrowing field" | +| What `deref()` gives | A plain borrow, checked at compile time like every other borrow under own. Not a pin count held at run time | + +## Goals + +- `WeakRef` of a class object compiles and runs under own, gc and none, with the same output. +- `deref()` returns the object while it lives, and `undefined` once its owner freed it, at once and + every time (stronger than JavaScript's "some time after", and compatible with it). +- No accepted program reads freed memory or leaks. Every block is freed exactly once. +- No object gets bigger. Programs that do not use `WeakRef` run no new code beyond one test per free + of a class object. + +## Non-goals + +- `-mm=rc`. Its header word is the strong count; a weak count needs a second word (rc evaluation + §9.8), which is an ABI change of its own. Under rc, `WeakRef` stays a compile error with a message + that names the models that have it. +- `WeakMap` and `WeakSet` under own (they stay gc only). +- Under own and none, a `WeakRef` to anything but a class object: arrays, strings, object literals, + closures, `Shared` handles, `@value` classes. These are compile errors there. Under gc, + `WeakRef` keeps accepting any object, as the library class does today, so no gc + program breaks. *(Builder-check call.)* +- Atomic weak counts. Instead, under own, an object of a weak-target class may not cross threads (see + "Threads" below). +- `FinalizationRegistry`. + +## Anti-goals + +- A `deref()` that can return an object which is freed before the borrow's last use. That is the one + failure that makes the feature worse than not having it. +- A tombstone or weak count that leaks: every object freed while weak references remain must have its + block freed when the last of them goes. + +## Constraints from the code as it is + +- **The header word.** Every heap block has one word in front of the payload. Under own, + `_MemoryAlloc` writes 0 there, meaning "mortal" (`LLVMCodeHelperBase.h:516-543`); an immortal + block holds `HEAP_BLOCK_IMMORTAL` (-1, all bits set), which `emitIsMortal` tests. A class object's + word under own holds nothing else today: no count, never the shared bit (only `Shared` blocks are + counted). Under none and gc nothing writes the word, and none never frees. +- **The free path.** The release routine of a class (`OwnershipRoutineLogic.h:1707-1718`) is: if the + object is mortal, release its fields, then free the block. Routines are generated per module, per + type. +- **Stack promotion** (`StackPromotionPass.cpp`) runs after own inference under every model and may + put an object on the stack, writing `HEAP_BLOCK_IMMORTAL` into its header. A `WeakRef` to it would + then never see it die, and read the dead frame. There is a precedent: under rc and own an object is + not promoted when the module uses `Shared` and the storage owns heap (`"shared-handle"`, + `:390-397`). +- **Name resolution.** The default library declares `class WeakRef` (gc only, built + on `GC_general_register_disappearing_link`, `lib.generics.ts:3061-3081`), and `WeakMap`/`WeakSet` on + it. A declared class is found before an embedded type (`MLIRGenTypes.cpp:804-814`), so a built-in + `WeakRef` is shadowed while that class exists. +- **`Shared` handles** are the model for a value own does not own but counts: `isHandle` values + keep rc's retains and releases, and lowering counts them. A `WeakRef` is that kind of value. +- **Borrows of an unknown place** already exist: TSLANG 84's chain with `anyPlace`, ended by any + release whose type may hold the borrowed value's type (`typeMayHold`) and by any call that may drop. + +## Approaches considered + +1. **`WeakRef`, weak count in the target's header word (chosen).** No new block, no growth. + Freeing an object that weak references still point to releases its fields and keeps the block as + a tombstone; the last weak reference frees it. +2. **`WeakRef` as a separate cell block** (the object points at a cell that outlives it). Needs a + header slot or a side table to find the cell from the object, which collapses into 1 or a table + lookup on every free. Rejected: more memory and a lookup. +3. **A declared borrowing field, proven at compile time.** No run-time cost, but the compiler must + prove the target outlives the holder, which fails as soon as a child is taken out of its parent. + Rejected by the user in favour of 1; it may come later as an opt-in for shapes that can be proven. + +For `deref()`, a run-time pin count (`deref()` raises it, the borrow's end lowers it, a free while +pinned waits) was considered and rejected by the user in favour of a compile-time borrow. + +## The design + +### Surface + +- `WeakRef` is a type built into the compiler, as `Shared` is (`!ts.weak_ref`), so it works + with `--no-default-lib` and in every model that has it. `new WeakRef(x)` and `w.deref()` are + recognised as `Shared`'s members are. +- `T` must be a class type, and the argument a class object (not `null`, not a union). Anything else + is an error: "WeakRef needs a class object". +- Under rc: "WeakRef is not supported under -mm=rc; use -mm=gc, -mm=own or -mm=none". +- The default library's `class WeakRef` is removed. `WeakMap`/`WeakSet` stay gc only: their entries + keep the disappearing link through a private library class (today's `WeakRef` body under another + name), so under the other models they fail exactly as they do now, on the `GC_*` call. The built-in + type under gc lowers to the same link. The library and the compiler are released together (the + library's own build cannot compile a `WeakRef` otherwise). *(Builder-check call.)* + +### At run time, by model + +| | `new WeakRef(x)` | `w.deref()` | a `WeakRef` released | `x` freed | +| --- | --- | --- | --- | --- | +| own | one pointer to `x`; `x`'s header count + 1 unless immortal | `x`, or `undefined` if `x`'s block is a tombstone | count - 1 unless immortal; if it reaches 0 on a tombstone, free the block | if the count is non-zero: release the fields, mark the tombstone, keep the block; else as today | +| gc | a block holding the hidden pointer, registered as a disappearing link (today's class, inlined) | `undefined` once the collector cleared the link | nothing | the collector clears the link | +| none | one pointer to `x` | always `x` (none never frees) | nothing | never | + +Under own the header word of a mortal class object becomes: + +``` + bit 63 (top) unused for class objects (the shared bit of Shared blocks) + bit 62 dead: the object was freed and the block is a tombstone + bits 0..61 the number of WeakRefs to it + all bits set immortal, as today (tested first) +``` + +The word size is 4 bytes on wasm32 and 32-bit targets; the dead bit is then bit 30. The count cannot +reach the dead bit in any program that fits in memory, since every `WeakRef` is at least a pointer. + +`deref()` on a tombstone returns `undefined`. Reading any field of a tombstone is impossible, because +`deref()` is the only way to it. + +### Ownership of the `WeakRef` value itself (own) + +A `WeakRef` is a handle in own's sense, like `Shared`: own steps aside, rc's retains and releases +of it stay, and lowering turns them into the target's count + 1 and - 1 (`isHandle` covers +`weak_ref`). Copying a `WeakRef` (`b.parent = a.parent`) is one more weak reference. So a `WeakRef` +field, element or local behaves exactly as a `Shared` handle does, with the target's header as the +count and no block of its own. + +One difference from `Shared`: releasing a `WeakRef` never frees anything but a tombstone, which nothing +can reach. So it is no drop: it does not take `__own_no_drops` from a function (`dropsInBody` counts a +handle release as one today), and it ends no borrow. *(Builder-check call.)* + +### What `deref()` returns (own) + +The result is a **borrow with an unknown place** (TSLANG 84's `anyPlace` chain), not tied to the +`WeakRef` it came from: + +- it may be used — fields read, methods called, passed to calls that do not keep it — until anything + that could free the object: a release of a value whose type may hold `T` (`typeMayHold`), a call that + may drop (`__own_no_drops` absent, or `__own_drop_fields` of a field whose type may hold `T`); +- it may not be stored, returned, captured or given to a call that keeps it: "'p' borrows the object a + WeakRef refers to and cannot be stored, returned or captured"; +- a use after something that could free it: "'p' borrows the object a WeakRef refers to but is used + here after it may be freed". + +So `const p = this.parent?.deref(); if (p) return p.v + p.depth();` compiles when `depth` frees +nothing (a method of a class nobody else extends, or `@final`), and the error explains when it does +not. `this.parent?.deref()?.v` is the same rule with one use. + +Under gc and none the result is an ordinary value; the borrow rule is own's alone. + +### Where an object may live + +An object of a class that is the `T` of any `WeakRef` in the module (or a subclass of one) is never +stack-promoted, under every model (under none a promoted target would be read through the `WeakRef` +after its frame ended): a new refusal reason, `"weak-target"`, beside `"shared-handle"`. +*(Builder-check call: every model, not own only.)* An object a +`WeakRef` refers to that is immortal (a global's, kept for good) is valid forever and needs no count. + +### Threads (own) + +Weak counts and tombstones are not atomic. If an object reached another thread and was freed there, a +`deref()` on the first thread could race with the free. So under own an object of a weak-target class +(or anything that can hold one, by `typeMayHold`) may not cross threads: it may not be captured by a +`Thread.spawn` callback, returned from one, or be (or be held by) the payload of a `Shared` that is +shared with `Shared.share`. Each is a compile error naming the class. *(Builder-check call: the goals +rule out atomics in every free and the anti-goals rule out leaving it open, so refusing is the one +consistent choice.)* + +### Across modules + +The free path changes for every own class object, and routines are generated per module. A module +compiled by an older compiler frees a block without looking at the count, which would free a block +`WeakRef`s still point at. The default library is rebuilt with the compiler, as for every release; +an object file from an older own compiler linked into a program that uses `WeakRef` is unsupported, +and nothing detects it. + +## Tests to write + +- `00weakref` (own, gc, none; and `-noopt` under own): a tree with parent links built, walked up from + a leaf, a child moved to another parent, a subtree dropped while a `WeakRef` into it remains and + `deref()` is `undefined` afterwards; churn between steps so that a freed block read by mistake shows. +- Leak and double-free checks: an ahead-of-time churn loop with flat memory; the own tests on Linux + under `MALLOC_PERTURB_` with tcache off; a tombstone freed when its last `WeakRef` goes (count the + frees, or a churn that would grow if it were not). +- Errors: a weak-target object captured by `Thread.spawn`, returned from it, or in a shared `Shared`; + `deref()` result stored, returned, captured; used after a call that may drop and after a + release of a holder of `T`; `WeakRef` of a string, array, `@value`, `Shared`; `WeakRef` under rc. +- Stack promotion: an object of a weak-target class made locally and weakly referenced through a call + is not promoted (the test reads it after the frame through the `WeakRef` and gets `undefined` or the + live object, never garbage). +- gc: the existing `WeakRef`, `WeakMap` and `WeakSet` tests of the default library still pass. + +## Left to the builder + +- The op set (`ts.WeakRefNew`, `ts.WeakRefDeref`, or members on an existing op family), the lowering + of the count, the tombstone and the gc link, and where `isHandle` is extended. +- How the release routine of a class tests the count (one load and branch before the free). +- The exact text of the messages, in the style of the existing own messages. +- Whether the borrow of a `deref()` result reuses `anyPlace` chains as they are or needs a new chain + kind; the rule above is what it must enforce. +- Spec section 84 in `tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md`, the user + guide (`docs/own-memory-model.md`), `docs/memory-models.md` and the readiness page. diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index bf1b494fd..304b5bc93 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -582,6 +582,14 @@ // HEAP_BLOCK_IMMORTAL has the bit set too, so an immortal test always comes first. It is the // word's top bit, so the value depends on the word size: 1 << (bits - 1). +// Under -mm=own a class object's header word holds no count of its own (one owner frees it), so it +// holds the number of WeakRefs to the object instead (own spec 84): the low bits up to the dead +// bit, 1 << (bits - 2). An object freed while WeakRefs remain releases its fields and keeps its block +// as a tombstone, its word the count with the dead bit set; WeakRef.deref() gives undefined for it, +// and the last WeakRef to go frees the block (the word is then the dead bit alone). Immortal still +// means every bit set, tested first. OwnershipRoutineLogic::emitWeakIncRef, emitWeakDecRef, +// emitFreeOrBury. + // Runtime type descriptor. // // The ANY_TYPE slot of an "any" box, and the UNION_TAG_INDEX slot of a tagged union, both diff --git a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h index 14dfde483..7b4c393ea 100644 --- a/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h +++ b/tslang/include/TypeScript/LowerToLLVM/OwnershipRoutineLogic.h @@ -835,6 +835,182 @@ class OwnershipRoutineLogic // // Only ever reached from inside emitIfLastReference, so the block is known to be mortal // and to have just lost its last reference. + // === WeakRef under own (spec 84; Defines.h, beside HEAP_BLOCK_IMMORTAL) === + + mlir::Value emitConstantIndex(mlir::Location loc, mlir::Type llvmIndexType, const llvm::APInt &value) + { + return rewriter.create(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, value)); + } + + mlir::Value emitConstantIndex(mlir::Location loc, mlir::Type llvmIndexType, int64_t value) + { + return rewriter.create(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, value)); + } + + llvm::APInt deadBit() + { + auto bits = compileOptions.sizeBits(); + return llvm::APInt::getOneBitSet(bits, bits - 2); + } + + llvm::APInt weakCountMask() + { + auto bits = compileOptions.sizeBits(); + return llvm::APInt::getLowBitsSet(bits, bits - 2); + } + + // `then` with the target's header word and its address, when the target is mortal + void emitIfMortalWord(mlir::Value target, llvm::function_ref then) + { + TypeConverterHelper tch(typeConverter); + TypeHelper th(rewriter); + LLVMCodeHelperBase ch(op, rewriter, typeConverter, compileOptions); + auto loc = op->getLoc(); + + auto llvmIndexType = tch.convertType(th.getIndexType()); + auto blockPtr = ch.getBlockPtrFromPayloadPtr(loc, target, llvmIndexType); + mlir::Value word = rewriter.create(loc, llvmIndexType, blockPtr); + auto mortal = rewriter.create(loc, LLVM::ICmpPredicate::ne, word, + emitConstantIndex(loc, llvmIndexType, HEAP_BLOCK_IMMORTAL)); + + auto *currentBlock = rewriter.getInsertionBlock(); + auto *continuationBlock = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint()); + auto *thenBlock = rewriter.createBlock(continuationBlock); + rewriter.setInsertionPointToEnd(thenBlock); + then(word, blockPtr); + rewriter.create(loc, ValueRange{}, continuationBlock); + + rewriter.setInsertionPointToEnd(currentBlock); + rewriter.create(loc, mortal, thenBlock, continuationBlock); + rewriter.setInsertionPointToStart(continuationBlock); + } + + // a WeakRef made or copied: one more weak count, unless the target is immortal + void emitWeakIncRef(mlir::Value target) + { + TypeConverterHelper tch(typeConverter); + TypeHelper th(rewriter); + auto loc = op->getLoc(); + auto llvmIndexType = tch.convertType(th.getIndexType()); + emitIfMortalWord(target, [&](mlir::Value word, mlir::Value blockPtr) { + auto newWord = rewriter.create(loc, llvmIndexType, word, emitConstantIndex(loc, llvmIndexType, 1)); + rewriter.create(loc, newWord, blockPtr); + }); + } + + // a WeakRef gone: one weak count less; the last one to a tombstone frees its block + void emitWeakDecRef(mlir::Value target) + { + TypeConverterHelper tch(typeConverter); + TypeHelper th(rewriter); + auto loc = op->getLoc(); + auto llvmIndexType = tch.convertType(th.getIndexType()); + emitIfMortalWord(target, [&](mlir::Value word, mlir::Value blockPtr) { + auto newWord = rewriter.create(loc, llvmIndexType, word, emitConstantIndex(loc, llvmIndexType, 1)); + rewriter.create(loc, newWord, blockPtr); + auto lastToTombstone = rewriter.create(loc, LLVM::ICmpPredicate::eq, newWord, + emitConstantIndex(loc, llvmIndexType, deadBit())); + emitIfTrueBlock(lastToTombstone, [&]() { emitFreeBlock(target); }); + }); + } + + // `then` when `condition` holds + void emitIfTrueBlock(mlir::Value condition, llvm::function_ref then) + { + auto loc = op->getLoc(); + auto *currentBlock = rewriter.getInsertionBlock(); + auto *continuationBlock = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint()); + auto *thenBlock = rewriter.createBlock(continuationBlock); + rewriter.setInsertionPointToEnd(thenBlock); + then(); + rewriter.create(loc, ValueRange{}, continuationBlock); + + rewriter.setInsertionPointToEnd(currentBlock); + rewriter.create(loc, condition, thenBlock, continuationBlock); + rewriter.setInsertionPointToStart(continuationBlock); + } + + // the end of a class object's release under own: free its block, or keep it as a tombstone while + // WeakRefs refer to it (its fields are released already) + void emitFreeOrBury(mlir::Value instance) + { + TypeConverterHelper tch(typeConverter); + TypeHelper th(rewriter); + LLVMCodeHelperBase ch(op, rewriter, typeConverter, compileOptions); + auto loc = op->getLoc(); + + auto llvmIndexType = tch.convertType(th.getIndexType()); + auto blockPtr = ch.getBlockPtrFromPayloadPtr(loc, instance, llvmIndexType); + mlir::Value word = rewriter.create(loc, llvmIndexType, blockPtr); + auto count = rewriter.create(loc, llvmIndexType, word, emitConstantIndex(loc, llvmIndexType, weakCountMask())); + auto weaklyReferred = rewriter.create(loc, LLVM::ICmpPredicate::ne, count, emitConstantIndex(loc, llvmIndexType, 0)); + + auto *currentBlock = rewriter.getInsertionBlock(); + auto *continuationBlock = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint()); + auto *buryBlock = rewriter.createBlock(continuationBlock); + auto *freeBlock = rewriter.createBlock(continuationBlock); + + rewriter.setInsertionPointToEnd(buryBlock); + auto dead = rewriter.create(loc, llvmIndexType, word, emitConstantIndex(loc, llvmIndexType, deadBit())); + rewriter.create(loc, dead, blockPtr); + rewriter.create(loc, ValueRange{}, continuationBlock); + + rewriter.setInsertionPointToEnd(freeBlock); + emitFreeBlock(instance); + rewriter.create(loc, ValueRange{}, continuationBlock); + + rewriter.setInsertionPointToEnd(currentBlock); + rewriter.create(loc, weaklyReferred, buryBlock, freeBlock); + rewriter.setInsertionPointToStart(continuationBlock); + } + + public: + // is the target of a WeakRef there and alive: not null, and immortal, or mortal and not a tombstone + mlir::Value emitWeakTargetAliveOrNull(mlir::Value target) + { + TypeHelper th(rewriter); + auto loc = op->getLoc(); + auto boolType = th.getLLVMBoolType(); + + auto *currentBlock = rewriter.getInsertionBlock(); + auto *continuationBlock = rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint()); + auto alive = continuationBlock->addArgument(boolType, loc); + auto *checkBlock = rewriter.createBlock(continuationBlock); + + rewriter.setInsertionPointToEnd(checkBlock); + auto aliveHere = emitWeakTargetAlive(target); + rewriter.create(loc, ValueRange{aliveHere}, continuationBlock); + + rewriter.setInsertionPointToEnd(currentBlock); + auto null = rewriter.create(loc, th.getPtrType()); + auto notNull = rewriter.create(loc, LLVM::ICmpPredicate::ne, target, null); + auto no = rewriter.create(loc, boolType, rewriter.getIntegerAttr(boolType, 0)); + rewriter.create(loc, notNull, checkBlock, ValueRange{}, continuationBlock, ValueRange{no}); + + rewriter.setInsertionPointToStart(continuationBlock); + return alive; + } + + // is the target of a WeakRef alive: immortal, or mortal and not a tombstone + mlir::Value emitWeakTargetAlive(mlir::Value target) + { + TypeConverterHelper tch(typeConverter); + TypeHelper th(rewriter); + LLVMCodeHelperBase ch(op, rewriter, typeConverter, compileOptions); + auto loc = op->getLoc(); + + auto llvmIndexType = tch.convertType(th.getIndexType()); + auto blockPtr = ch.getBlockPtrFromPayloadPtr(loc, target, llvmIndexType); + mlir::Value word = rewriter.create(loc, llvmIndexType, blockPtr); + auto isImmortal = rewriter.create(loc, LLVM::ICmpPredicate::eq, word, + emitConstantIndex(loc, llvmIndexType, HEAP_BLOCK_IMMORTAL)); + auto deadBits = rewriter.create(loc, llvmIndexType, word, emitConstantIndex(loc, llvmIndexType, deadBit())); + auto notDead = rewriter.create(loc, LLVM::ICmpPredicate::eq, deadBits, emitConstantIndex(loc, llvmIndexType, 0)); + return rewriter.create(loc, isImmortal, notDead); + } + + private: + void emitFreeBlock(mlir::Value payloadPtr) { TypeHelper th(rewriter); @@ -1712,11 +1888,28 @@ class OwnershipRoutineLogic auto instanceValue = rewriter.create(loc, ptrTy, slotPtr); emitIfLastReference(instanceValue, [&]() { releaseFields(storageType, instanceValue); - emitFreeBlock(instanceValue); + // under own, an object WeakRefs still refer to stays as a tombstone (spec 84) + if (compileOptions.memoryModel == MemoryModelOwn && isa(type)) + { + emitFreeOrBury(instanceValue); + } + else + { + emitFreeBlock(instanceValue); + } }); return; } + // a WeakRef (own spec 84) is its target's pointer; it owns one weak count of the target, and + // the last one to a tombstone frees the block + if (isa(type)) + { + auto target = rewriter.create(loc, ptrTy, slotPtr); + emitIfNonNull(target, [&]() { emitWeakDecRef(target); }); + return; + } + // a handle (own spec 23.2) is counted in every model that tracks ownership: the last one // releases the payload and frees the block if (auto sharedType = dyn_cast(type)) @@ -1899,6 +2092,14 @@ class OwnershipRoutineLogic return; } + // a WeakRef copied (own spec 84): one more weak count of its target + if (isa(type)) + { + auto target = rewriter.create(loc, ptrTy, slotPtr); + emitIfNonNull(target, [&]() { emitWeakIncRef(target); }); + return; + } + // an array value is a pointer to its header { data, length, capacity }, and the header // is the counted block: the copy shares it, and it already holds whatever the elements // own. A null header (an empty array, R1) is skipped by emitIncRef diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRPrinter.h b/tslang/include/TypeScript/MLIRLogic/MLIRPrinter.h index ebbf0047e..aa056cf6c 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRPrinter.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRPrinter.h @@ -464,6 +464,11 @@ class MLIRPrinter printType(out, t.getElementType()); out << ">"; }) + .template Case([&](auto t) { + out << "WeakRef<"; + printType(out, t.getElementType()); + out << ">"; + }) .template Case([&](auto t) { out << "Shared<"; printType(out, t.getElementType()); diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRTypeCore.h b/tslang/include/TypeScript/MLIRLogic/MLIRTypeCore.h index ab84ce9f4..8d6edfaaa 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRTypeCore.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRTypeCore.h @@ -39,6 +39,7 @@ class MLIRTypeCore || isa(typeIn) || isa(typeIn) || isa(typeIn) + || isa(typeIn) || isa(typeIn) || isa(typeIn) || isa(typeIn) @@ -74,6 +75,7 @@ class MLIRTypeCore || isa(typeIn) || isa(typeIn) || isa(typeIn) + || isa(typeIn) || isa(typeIn) || isa(typeIn) || isa(typeIn) diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h b/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h index 7eebd2d28..11a9b4a9e 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h @@ -2438,14 +2438,54 @@ class MLIRTypeHelper return ownsHeapMemory(location, type, visiting); } + // A WeakRef (own spec 84), an optional of one, or a union of one with members that own no + // heap memory (`WeakRef | null`). Under own it is counted on its target's header. + static bool isWeakRefType(mlir::Type type) + { + if (isa(type)) + { + return true; + } + + if (auto optionalType = dyn_cast(type)) + { + return isWeakRefType(optionalType.getElementType()); + } + + if (auto unionType = dyn_cast(type)) + { + auto weak = false; + for (auto member : unionType.getTypes()) + { + if (isa(member)) + { + weak = true; + continue; + } + + if (ownsHeapMemoryOfUnionMember(member)) + { + return false; + } + } + + return weak; + } + + return false; + } + // A handle (own spec 23.2): a value counted in every model that tracks ownership. That is a // Shared, an optional of a handle, or a union of at least one Shared whose other members // own no heap memory: null, undefined, a number, a boolean, a literal, another Shared. // `Shared | null` is one pointer; `Shared | number` is tagged, and its routines dispatch // on the tag through descriptors, each Shared member's counting and the others' doing nothing. + // + // A WeakRef (own spec 84) is a handle too: own leaves its retains and releases, and they + // count on its target's header (isWeakRefType). static bool isSharedHandleType(mlir::Type type) { - if (isa(type)) + if (isa(type) || isWeakRefType(type)) { return true; } @@ -4364,6 +4404,12 @@ class MLIRTypeHelper return true; } + // A WeakRef (own spec 84) owns one weak count of its target, which its release gives back + if (isa(type)) + { + return true; + } + // An interface is a reference to whatever it was made from, and holds it alive the // same way a class reference does. The concrete layout is not recoverable from the // type - which is why this used to answer no - but it does not have to be: the value's diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRTypeIterator.h b/tslang/include/TypeScript/MLIRLogic/MLIRTypeIterator.h index 01d0b39a1..08f7ddfb5 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRTypeIterator.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRTypeIterator.h @@ -246,6 +246,11 @@ class MLIRTypeIterator return false; return true; }) + .Case([&](auto t) { + if (!iterate(t.getElementType())) + return false; + return true; + }) .Case([&](auto t) { for (auto subType : t.getFields()) { diff --git a/tslang/include/TypeScript/MLIRLogic/TypeOfOpHelper.h b/tslang/include/TypeScript/MLIRLogic/TypeOfOpHelper.h index 47264dbd2..bfca032bd 100644 --- a/tslang/include/TypeScript/MLIRLogic/TypeOfOpHelper.h +++ b/tslang/include/TypeScript/MLIRLogic/TypeOfOpHelper.h @@ -269,7 +269,7 @@ class TypeOfOpHelper // A Shared handle (own spec 23.2) is what JavaScript's `typeof` calls a wrapper object. // Its descriptor is keyed by the type, so Shared and Shared still get distinct ones. - if (isa(type)) + if (isa(type)) { return "object"; } diff --git a/tslang/include/TypeScript/TypeScriptOps.td b/tslang/include/TypeScript/TypeScriptOps.td index 088d0bc60..cdeeff50c 100644 --- a/tslang/include/TypeScript/TypeScriptOps.td +++ b/tslang/include/TypeScript/TypeScriptOps.td @@ -2524,6 +2524,18 @@ def TypeScript_SharedValueRefOp : TypeScript_Op<"SharedValueRef"> { let results = (outs TypeScript_Ref:$result); } +// `new WeakRef(x)` (own spec 84): a reference to x that does not own it. +def TypeScript_WeakRefNewOp : TypeScript_Op<"WeakRefNew"> { + let arguments = (ins AnyType:$target); + let results = (outs TypeScript_WeakRef:$result); +} + +// `w.deref()`: the target while it lives (an optional holding it), else an empty optional. +def TypeScript_WeakRefDerefOp : TypeScript_Op<"WeakRefDeref"> { + let arguments = (ins TypeScript_WeakRef:$weakRef); + let results = (outs AnyType:$result); +} + // `Shared.count(s)`: the number of handles under rc and own, -1 under gc and none. def TypeScript_SharedCountOp : TypeScript_Op<"SharedCount"> { let arguments = (ins TypeScript_Shared:$shared); diff --git a/tslang/include/TypeScript/TypeScriptTypes.td b/tslang/include/TypeScript/TypeScriptTypes.td index 54c8dfef6..24f79572d 100644 --- a/tslang/include/TypeScript/TypeScriptTypes.td +++ b/tslang/include/TypeScript/TypeScriptTypes.td @@ -104,6 +104,22 @@ def TypeScript_Ref : TypeScript_Type<"Ref"> { ]; } +// WeakRef: a reference that does not own what it refers to (own spec 84). Under own and none it +// is the target's own pointer, under gc a collector-cleared link block. +def TypeScript_WeakRef : TypeScript_Type<"WeakRef"> { + let mnemonic = "weak_ref"; + + let parameters = (ins "Type":$elementType); + + let assemblyFormat = "`<` params `>`"; + + let builders = [ + TypeBuilderWithInferredContext<(ins "Type":$elementType), [{ + return Base::get(elementType.getContext(), elementType); + }]> + ]; +} + // Shared (own spec 23): a counted handle to a block whose payload is one T. def TypeScript_Shared : TypeScript_Type<"Shared"> { let mnemonic = "shared"; diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index 365831a7f..c59e1caf7 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -3205,7 +3205,7 @@ void AddTsAffineLegalOps(ConversionTarget &target) mlir_ts::ElementRefOp, mlir_ts::PointerOffsetRefOp, mlir_ts::FuncOp, mlir_ts::GlobalOp, mlir_ts::GlobalResultOp, mlir_ts::DefaultOp, mlir_ts::HasValueOp, mlir_ts::ValueOp, mlir_ts::ValueOrDefaultOp, mlir_ts::NullOp, mlir_ts::ParseFloatOp, mlir_ts::ParseIntOp, mlir_ts::IsNaNOp, mlir_ts::PrintOp, mlir_ts::ConvertFOp, mlir_ts::SizeOfOp, mlir_ts::TypeDescriptorOp, mlir_ts::RetainOp, mlir_ts::ReleaseOp, mlir_ts::MarkSharedOp, mlir_ts::RetainSlotOp, mlir_ts::ReleaseSlotOp, mlir_ts::RetainCellOp, mlir_ts::ReleaseCellOp, mlir_ts::StoreOp, mlir_ts::SwapOp, mlir_ts::SymbolRefOp, mlir_ts::LengthOfOp, mlir_ts::SetLengthOfOp, - mlir_ts::StringLengthOp, mlir_ts::StringResizeOp, mlir_ts::StringConcatOp, mlir_ts::StringCopyOp, mlir_ts::SharedNewOp, mlir_ts::SharedValueRefOp, mlir_ts::SharedCountOp, mlir_ts::StringCompareOp, mlir_ts::AnyCompareOp, + mlir_ts::StringLengthOp, mlir_ts::StringResizeOp, mlir_ts::StringConcatOp, mlir_ts::StringCopyOp, mlir_ts::SharedNewOp, mlir_ts::SharedValueRefOp, mlir_ts::SharedCountOp, mlir_ts::WeakRefNewOp, mlir_ts::WeakRefDerefOp, mlir_ts::StringCompareOp, mlir_ts::AnyCompareOp, mlir_ts::LoadOp, mlir_ts::LoadSaveOp, mlir_ts::NewOp, mlir_ts::CreateTupleOp, mlir_ts::DeconstructTupleOp, mlir_ts::CreateArrayOp, mlir_ts::NewEmptyArrayOp, mlir_ts::NewArrayOp, mlir_ts::DeleteOp, mlir_ts::PropertyRefOp, mlir_ts::InsertPropertyOp, mlir_ts::ExtractPropertyOp, mlir_ts::LogicalBinaryOp, mlir_ts::UndefOp, mlir_ts::VariableOp, mlir_ts::DebugVariableOp, mlir_ts::AllocaOp, diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 11580603a..347407dd5 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -1449,6 +1449,96 @@ class SharedCountOpLowering : public TsLlvmPattern } }; +// `new WeakRef(x)` (own spec 84). none: x's own pointer, never freed. gc: a block holding x's +// address complemented, which the collector does not take for a pointer to x, registered as a +// disappearing link so that the collector zeroes it when x goes (gc.h's GC_HIDE_POINTER). +class WeakRefNewOpLowering : public TsLlvmPattern +{ + public: + using TsLlvmPattern::TsLlvmPattern; + + LogicalResult matchAndRewrite(mlir_ts::WeakRefNewOp op, Adaptor transformed, + ConversionPatternRewriter &rewriter) const final + { + auto loc = op->getLoc(); + auto &compileOptions = tsLlvmContext->compileOptions; + // own: the target's pointer, counted by the retains own leaves for it (MLIRGen's first); + // none: the target's pointer, never freed + if (!compileOptions.needsGCRuntime()) + { + rewriter.replaceOp(op, ValueRange{transformed.getTarget()}); + return success(); + } + + TypeHelper th(rewriter); + TypeConverterHelper tch(getTypeConverter()); + LLVMCodeHelper ch(op, rewriter, getTypeConverter(), compileOptions); + + auto llvmIndexType = tch.convertType(th.getIndexType()); + auto link = ch.MemoryAlloc(th.getIndexType(), MemoryAllocSet::Zero); + auto address = rewriter.create(loc, llvmIndexType, transformed.getTarget()); + auto allBits = rewriter.create(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, -1)); + auto hidden = rewriter.create(loc, llvmIndexType, address, allBits); + rewriter.create(loc, hidden, link); + + auto registerLink = ch.getOrInsertFunction( + "GC_general_register_disappearing_link", + th.getFunctionType(rewriter.getI32Type(), {th.getPtrType(), th.getPtrType()})); + rewriter.create(loc, registerLink, ValueRange{link, transformed.getTarget()}); + rewriter.replaceOp(op, ValueRange{link}); + return success(); + } +}; + +// `w.deref()`: an optional holding the target, empty once it is gone. none: always the target. +// gc: the link's word, which the collector zeroed if the target went. +class WeakRefDerefOpLowering : public TsLlvmPattern +{ + public: + using TsLlvmPattern::TsLlvmPattern; + + LogicalResult matchAndRewrite(mlir_ts::WeakRefDerefOp op, Adaptor transformed, + ConversionPatternRewriter &rewriter) const final + { + auto loc = op->getLoc(); + auto &compileOptions = tsLlvmContext->compileOptions; + TypeHelper th(rewriter); + TypeConverterHelper tch(getTypeConverter()); + + mlir::Value target = transformed.getWeakRef(); + // own: the target, unless its block is a tombstone (Defines.h, beside HEAP_BLOCK_IMMORTAL) + if (compileOptions.memoryModel == MemoryModelOwn) + { + OwnershipRoutineLogic orl(op, rewriter, getTypeConverter(), compileOptions); + auto alive = orl.emitWeakTargetAliveOrNull(target); + auto null = rewriter.create(loc, th.getPtrType()); + target = rewriter.create(loc, alive, target, null); + } + if (compileOptions.needsGCRuntime()) + { + auto llvmIndexType = tch.convertType(th.getIndexType()); + auto hidden = rewriter.create(loc, llvmIndexType, transformed.getWeakRef()); + auto zero = rewriter.create(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, 0)); + auto allBits = rewriter.create(loc, llvmIndexType, rewriter.getIntegerAttr(llvmIndexType, -1)); + auto cleared = rewriter.create(loc, LLVM::ICmpPredicate::eq, hidden, zero); + auto address = rewriter.create(loc, llvmIndexType, hidden, allBits); + auto revealed = rewriter.create(loc, th.getPtrType(), address.getResult()); + auto null = rewriter.create(loc, th.getPtrType()); + target = rewriter.create(loc, cleared, null, revealed); + } + + auto null = rewriter.create(loc, th.getPtrType()); + auto alive = rewriter.create(loc, LLVM::ICmpPredicate::ne, target, null); + auto llvmOptType = tch.convertType(op.getType()); + mlir::Value optional = rewriter.create(loc, llvmOptType); + optional = rewriter.create(loc, llvmOptType, optional, target, + MLIRHelper::getStructIndex(rewriter, OPTIONAL_VALUE_INDEX)); + rewriter.replaceOpWithNewOp(op, llvmOptType, optional, alive, + MLIRHelper::getStructIndex(rewriter, OPTIONAL_HASVALUE_INDEX)); + return success(); + } +}; + class StringCompareOpLowering : public TsLlvmPattern { public: @@ -3879,6 +3969,7 @@ struct VariableOpLowering : public TsLlvmPattern auto tsStorageType = referenceType.getElementType(); if (isa(tsStorageType) || isa(tsStorageType) || + isa(tsStorageType) || isa(tsStorageType) || isa(tsStorageType) || isa(tsStorageType) || @@ -9000,6 +9091,11 @@ static void populateTypeScriptConversionPatterns(LLVMTypeConverter &converter, m return LLVM::LLVMPointerType::get(m.getContext()); }); + // WeakRef (own spec 84): the target's pointer under own and none, a link block under gc + converter.addConversion([&](mlir_ts::WeakRefType type) { + return LLVM::LLVMPointerType::get(m.getContext()); + }); + converter.addConversion([&](mlir_ts::ValueRefType type) { return LLVM::LLVMPointerType::get(m.getContext()); }); @@ -9988,6 +10084,7 @@ void TypeScriptToLLVMLoweringPass::runOnOperation() ParseFloatOpLowering, ParseIntOpLowering, IsNaNOpLowering, PrintOpLowering, ConvertFOpLowering, StoreOpLowering, SwapOpLowering, SizeOfOpLowering, TypeDescriptorOpLowering, RetainOpLowering, ReleaseOpLowering, RetainSlotOpLowering, ReleaseSlotOpLowering, RetainCellOpLowering, ReleaseCellOpLowering, ReleaseContentsOpLowering, MakeImmortalOpLowering, MarkImmortalCellOpLowering, TakeOutOfFieldOpLowering, InsertPropertyOpLowering, LengthOfOpLowering, SetLengthOfOpLowering, StringLengthOpLowering, StringResizeOpLowering, StringConcatOpLowering, StringCopyOpLowering, SharedNewOpLowering, SharedValueRefOpLowering, SharedCountOpLowering, MarkSharedOpLowering, + WeakRefNewOpLowering, WeakRefDerefOpLowering, StringCompareOpLowering, AnyCompareOpLowering, CharToStringOpLowering, UndefOpLowering, CopyStructOpLowering, MemoryCopyOpLowering, MemoryMoveOpLowering, LoadSaveValueLowering, ThrowUnwindOpLowering, ThrowCallOpLowering, VariableOpLowering, DebugVariableOpLowering, AllocaOpLowering, InvokeOpLowering, AccessorUnwindEdgeOpLowering, InvokeHybridOpLowering, VirtualSymbolRefOpLowering, ThisVirtualSymbolRefOpLowering, ThisSymbolRefOpLowering, InterfaceSymbolRefOpLowering, diff --git a/tslang/lib/TypeScript/MLIRGenCast.cpp b/tslang/lib/TypeScript/MLIRGenCast.cpp index 0e2278443..6875c9b95 100644 --- a/tslang/lib/TypeScript/MLIRGenCast.cpp +++ b/tslang/lib/TypeScript/MLIRGenCast.cpp @@ -2310,7 +2310,8 @@ namespace mlirgen || isa(type) || isa(type) // a Shared handle (own spec 23.1) is checked by its `typeof` name, "object" - || isa(type)) + || isa(type) + || isa(type)) { return castPrimitiveTypeFromAny(location, type, value, genContext); } @@ -3008,6 +3009,7 @@ namespace mlirgen // handle, so the name is all that is checked: an `any` holding a Shared passes for a // Shared, as would any other value tagged "object" (own spec 23.7). .Case([&](auto _) { typeOfs["object"] = true; }) + .Case([&](auto _) { typeOfs["object"] = true; }) // TODO: we can't use null type here and undefined otherwise code will be cycling // due to issue with TypeOf == 'null' as it should denounce UnionType into Single Type // review code to use null in "TypeGuard" diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index aedb76911..c2068ab6e 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -1232,6 +1232,16 @@ namespace mlirgen return mlirGenSharedCount(location, callExpression, genContext); } + // `w.deref()`, `w?.deref()` of a WeakRef (own spec 84) + if (MLIRHelper::getName(access->name) == "deref" && callExpression->arguments.size() == 0) + { + auto objectType = evaluate(access->expression.as(), genContext); + if (objectType && mth.isWeakRefType(objectType)) + { + return mlirGenWeakRefDeref(location, access, genContext); + } + } + // `Shared.share(v)`: v, and every block it reaches, may be used by other threads if (access->expression == SyntaxKind::Identifier && MLIRHelper::getName(access->expression.as()) == "Shared" @@ -1661,6 +1671,12 @@ namespace mlirgen // Without type arguments, the type may come from the constructor's arguments or the receiver // (below): the generic is specialized by its defaults only when neither gives one. Done // first, `new Set([1, 2])` made all of Set beside the Set it is. + // `new WeakRef(x)` is the compiler's own, whatever a library declares (own spec 84) + if (typeExpression == SyntaxKind::Identifier && MLIRHelper::getName(typeExpression.as()) == "WeakRef") + { + return mlirGenNewWeakRef(location, newExpression, genContext); + } + auto deferDefaults = newExpression->typeArguments.size() == 0 && (newExpression->arguments.size() > 0 || genContext.receiverType); auto result = deferDefaults ? mlirGen(typeExpression, genContext) @@ -1899,6 +1915,108 @@ namespace mlirgen return V(handle); } + // `new WeakRef(x)` (own spec 84): T is the type argument, else the receiver's, else x's. gc + // takes any object; own and none a class object, whose header holds the weak count under own. + ValueOrLogicalResult MLIRGenImpl::mlirGenNewWeakRef(mlir::Location location, NewExpression newExpression, + const GenContext &genContext) + { + if (compileOptions.isRefCounted()) + { + emitError(location, "WeakRef is not supported under -mm=rc; use -mm=gc, -mm=own or -mm=none"); + return mlir::failure(); + } + + if (newExpression->arguments.size() != 1 || newExpression->typeArguments.size() > 1) + { + emitError(location, "new WeakRef(target) takes one argument and one type argument"); + return mlir::failure(); + } + + mlir::Type elementType; + if (newExpression->typeArguments.size() == 1) + { + elementType = getType(newExpression->typeArguments.front(), genContext); + if (!elementType) + { + return mlir::failure(); + } + } + else if (auto receiver = genContext.receiverType + ? mlir::dyn_cast(mth.stripOptionalType(NonNullableTypes(genContext.receiverType))) + : mlir_ts::WeakRefType()) + { + elementType = receiver.getElementType(); + } + + GenContext valueGenContext(genContext); + valueGenContext.clearReceiverTypes(); + valueGenContext.receiverType = elementType; + + auto result = mlirGen(newExpression->arguments.front(), valueGenContext); + EXIT_IF_FAILED_OR_NO_VALUE(result) + auto value = V(result); + + if (!elementType) + { + elementType = mth.wideStorageType(value.getType()); + } + + // a class object, or one a test narrowed out of `C | null` (`if (m) new WeakRef(m)`) + auto isClass = mlir::isa(elementType) && + mlir::isa(mth.stripOptionalType(NonNullableTypes(value.getType()))); + if (!compileOptions.needsGCRuntime() && !isClass) + { + emitError(location, "WeakRef needs a class object"); + return mlir::failure(); + } + + CAST_A(target, location, elementType, value, genContext); + auto weakRef = builder.create(location, mlir_ts::WeakRefType::get(elementType), target); + // born with no weak count, as a Shared block with no count: the retain this adds is the first + markFreshBlockOwned(location, weakRef); + return V(weakRef); + } + + // `w.deref()`: an optional of the target; `w?.deref()` on a WeakRef that may be null or + // undefined is the empty optional then. + ValueOrLogicalResult MLIRGenImpl::mlirGenWeakRefDeref(mlir::Location location, PropertyAccessExpression access, + const GenContext &genContext) + { + auto result = mlirGen(access->expression.as(), genContext); + EXIT_IF_FAILED_OR_NO_VALUE(result) + auto value = V(result); + + auto weakRefType = mlir::dyn_cast(value.getType()); + auto deref = [&](mlir::Value weakRef) -> ValueOrLogicalResult { + auto type = mlir::cast(weakRef.getType()); + return V(builder.create(location, mlir_ts::OptionalType::get(type.getElementType()), weakRef)); + }; + + if (weakRefType) + { + return deref(value); + } + + // a WeakRef that may be null or undefined + auto nonNullType = mth.stripOptionalType(NonNullableTypes(value.getType())); + weakRefType = mlir::dyn_cast(nonNullType); + if (!weakRefType) + { + emitError(location, "deref() of a value that is no WeakRef"); + return mlir::failure(); + } + + CAST_A(condValue, location, getBooleanType(), value, genContext); + return conditionalValue(location, condValue, + [&]() -> ValueOrLogicalResult { + CAST_A(weakRef, location, weakRefType, value, genContext); + return deref(weakRef); + }, + [&](mlir::Type trueValueType) { + return ValueOrLogicalResult(builder.create(location, trueValueType)); + }); + } + mlir::LogicalResult MLIRGenImpl::mlirGen(DeleteExpression deleteExpression, const GenContext &genContext) { diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 7f39f1631..b1f33a814 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -1368,7 +1368,7 @@ class MLIRGenImpl // balanced, where a mark on its own would hand a receiver a reference nobody took. void markFreshBlockOwned(mlir::Location location, mlir::Value value) { - if (!value || !isa(value.getType())) + if (!value || !isa(value.getType())) { return; } @@ -11472,6 +11472,10 @@ class MLIRGenImpl ValueOrLogicalResult mlirGenNewShared(mlir::Location location, NewExpression newExpression, const GenContext &genContext); + ValueOrLogicalResult mlirGenNewWeakRef(mlir::Location location, NewExpression newExpression, const GenContext &genContext); + + ValueOrLogicalResult mlirGenWeakRefDeref(mlir::Location location, PropertyAccessExpression access, const GenContext &genContext); + ValueOrLogicalResult mlirGenSharedCount(mlir::Location location, CallExpression callExpression, const GenContext &genContext); ValueOrLogicalResult mlirGenSharedShare(mlir::Location location, CallExpression callExpression, const GenContext &genContext); void markAtomicInstanceShared(mlir::Location location, ClassInfo::TypePtr classInfo, mlir::Value instance); diff --git a/tslang/lib/TypeScript/MLIRGenTypes.cpp b/tslang/lib/TypeScript/MLIRGenTypes.cpp index aa63fb1be..9a565d41d 100644 --- a/tslang/lib/TypeScript/MLIRGenTypes.cpp +++ b/tslang/lib/TypeScript/MLIRGenTypes.cpp @@ -801,6 +801,16 @@ namespace mlirgen if (typeReferenceAST->typeArguments.size()) { + // WeakRef is the compiler's own whatever a library declares (own spec 84): the + // default library's gc-only class of that name is replaced by it + if (name == "WeakRef") + { + if (auto embedType = findEmbeddedType(location, name, typeReferenceAST->typeArguments, genContext)) + { + return embedType; + } + } + if (auto type = resolveGenericTypeInNamespace(location, name, typeReferenceAST, genContext)) { return type; @@ -947,6 +957,7 @@ namespace mlirgen {"Ref", true }, // alias of Reference {"BoxedObject", true }, // an object held by reference, see MLIRPrinter::printBoxedObjectTypes {"Shared", true }, // a counted handle, own spec 23 + {"WeakRef", true }, // a reference that does not own, own spec 84 {"CVarArgs", true }, // a C variadic function type, see getCVarArgsType {"Readonly", true }, {"Partial", true }, @@ -1041,6 +1052,7 @@ namespace mlirgen {"Ref", true }, // alias of Reference {"BoxedObject", true }, // an object held by reference, see MLIRPrinter::printBoxedObjectTypes {"Shared", true }, // a counted handle, own spec 23 + {"WeakRef", true }, // a reference that does not own, own spec 84 {"CVarArgs", true }, // a C variadic function type, see getCVarArgsType {"ThisType", true }, {"Promise", true }, // an async function returns its value, see getEmbeddedTypeWithParam @@ -1196,7 +1208,7 @@ namespace mlirgen { enum class EmbeddedType { - None, TypeOf, Reference, BoxedObject, Shared, CVarArgs, FirstTypeArgument, NonNullable, Array, ReadonlyArray, ReturnType, + None, TypeOf, Reference, BoxedObject, Shared, WeakRef, CVarArgs, FirstTypeArgument, NonNullable, Array, ReadonlyArray, ReturnType, Parameters, ThisParameterType, OmitThisParameter, Uppercase, Lowercase, Capitalize, Uncapitalize }; @@ -1205,6 +1217,7 @@ namespace mlirgen .Cases("Reference", "Ref", EmbeddedType::Reference) .Case("BoxedObject", EmbeddedType::BoxedObject) .Case("Shared", EmbeddedType::Shared) + .Case("WeakRef", EmbeddedType::WeakRef) .Case("CVarArgs", EmbeddedType::CVarArgs) .Cases("Readonly", "Partial", "Required", "ThisType", EmbeddedType::FirstTypeArgument) // an async function runs to completion and returns its value (an `await` waits for @@ -1246,6 +1259,8 @@ namespace mlirgen return getObjectType(type); case EmbeddedType::Shared: return mlir_ts::SharedType::get(type); + case EmbeddedType::WeakRef: + return mlir_ts::WeakRefType::get(type); case EmbeddedType::CVarArgs: return getCVarArgsType(type); case EmbeddedType::FirstTypeArgument: @@ -1304,7 +1319,7 @@ namespace mlirgen { enum class EmbeddedType { - None, TypeOf, Reference, BoxedObject, Shared, CVarArgs, ThisType, Array + None, TypeOf, Reference, BoxedObject, Shared, WeakRef, CVarArgs, ThisType, Array }; auto kind = llvm::StringSwitch(name) @@ -1312,6 +1327,7 @@ namespace mlirgen .Cases("Reference", "Ref", EmbeddedType::Reference) .Case("BoxedObject", EmbeddedType::BoxedObject) .Case("Shared", EmbeddedType::Shared) + .Case("WeakRef", EmbeddedType::WeakRef) .Case("CVarArgs", EmbeddedType::CVarArgs) .Case("ThisType", EmbeddedType::ThisType) // a promise of T is T, see getEmbeddedTypeWithParamBuiltins @@ -1338,6 +1354,8 @@ namespace mlirgen case EmbeddedType::Shared: // the type argument is not checked above in this variant return type ? mlir_ts::SharedType::get(type) : mlir::Type(); + case EmbeddedType::WeakRef: + return type ? mlir_ts::WeakRefType::get(type) : mlir::Type(); case EmbeddedType::CVarArgs: return type ? getCVarArgsType(type) : mlir::Type(); case EmbeddedType::ThisType: diff --git a/tslang/lib/TypeScript/OwnershipFacts.h b/tslang/lib/TypeScript/OwnershipFacts.h index be02b592f..fdd61eea6 100644 --- a/tslang/lib/TypeScript/OwnershipFacts.h +++ b/tslang/lib/TypeScript/OwnershipFacts.h @@ -1245,10 +1245,13 @@ inline bool ownsHeap(mlir::Value value) // The release of a slot whose type owns no block - a captured number assigned (`t += v.n` in a // callback) - destroys nothing (spec 52) +// A WeakRef's release (own spec 84) frees at most a tombstone, which nothing can reach: it ends no +// borrow and makes no function one that drops. inline bool releasesNothing(mlir_ts::ReleaseSlotOp releaseSlotOp) { auto refType = mlir::dyn_cast(releaseSlotOp.getSlot().getType()); - return refType && !ownsHeap(releaseSlotOp.getLoc(), refType.getElementType()); + return refType && (!ownsHeap(releaseSlotOp.getLoc(), refType.getElementType()) || + MLIRTypeHelper::isWeakRefType(refType.getElementType())); } // ---- Fills (TSLANG 47) ---- diff --git a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp index d43182e87..930eee63e 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceBorrowers.cpp @@ -801,6 +801,14 @@ namespace own_inference continue; } + // A WeakRef made of the borrow, or the object a borrowed WeakRef refers to, is no part + // of it: the WeakRef finds the object gone, and the object is checked as its own + // borrow (spec 84) + if (mlir::isa(user)) + { + continue; + } + // A result that borrows what it was given borrows this too, and is walked as it. for (auto result : user->getResults()) { diff --git a/tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp b/tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp index 02ecf8f50..449322e83 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceDiagnostics.cpp @@ -13,6 +13,11 @@ namespace own_inference std::string OwnershipInferencePass::describeCall(mlir::Operation *call) { + if (mlir::isa(call)) + { + return "the object a WeakRef refers to"; + } + auto callee = calleeName(call); return callee.empty() ? "a call's result" : "the result of '" + callee + "'"; } diff --git a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp index 9e490262a..f03183b9e 100644 --- a/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp +++ b/tslang/lib/TypeScript/OwnershipInferenceMoves.cpp @@ -440,6 +440,13 @@ namespace own_inference return true; } + // `new WeakRef(x)` counts x's header and keeps nothing of it; `w.deref()` reads the WeakRef + // (spec 84) + if (mlir::isa(user)) + { + return true; + } + // `Shared.share(v)` sets bits in the headers of what `v` reaches, and keeps nothing if (mlir::isa(user)) { diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.cpp b/tslang/lib/TypeScript/OwnershipInferencePass.cpp index b6952a808..3c9e55a38 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePass.cpp @@ -377,10 +377,35 @@ namespace own_inference // yielded field's old value: the yield gave the state's count to the caller, who owns the // record `next` returns and releases it; nor the retain for a box that borrows what it holds: // that box gives nothing back + // ...nor the release of a fresh handle stored into a record literal that moves (recordRetainOf: + // `const o = { w: new WeakRef(n) }`, `return { s: new Shared(k) }`): rc's retain of the record + // is erased as the move, so the handle's count moved in with it, and the record's release + // gives it back; the temporary's release would give it back twice + llvm::SmallPtrSet movedHandleReleases; + getFunction().walk([&](mlir_ts::ReleaseOp releaseOp) { + auto value = releaseOp.getReference(); + if (!isHandle(value)) + { + return; + } + + for (auto *user : value.getUsers()) + { + auto *retain = recordRetainOfStore(user); + if (retain && toErase.contains(retain)) + { + movedHandleReleases.insert(releaseOp); + return; + } + } + }); + toErase.remove_if([&](mlir::Operation *op) { auto releaseSlotOp = mlir::dyn_cast(op); - return touchesHandle(op) && !(releaseSlotOp && isYieldedField(releaseSlotOp.getSlot())) && !captureRetains.contains(op); + return touchesHandle(op) && !(releaseSlotOp && isYieldedField(releaseSlotOp.getSlot())) && !captureRetains.contains(op) && + !movedHandleReleases.contains(op); }); + toErase.insert(movedHandleReleases.begin(), movedHandleReleases.end()); placeEdgeReleases(); // what owns nothing gives nothing back, wherever the moves put its releases (spec 36) dropUnboxedRecordCounts(toErase); @@ -577,6 +602,12 @@ namespace own_inference // an exchange of two places, which moves their values (spec 42) drops.push_back(op); } + else if (mlir::isa(op)) + { + // the object a WeakRef refers to: a borrow of a place nobody here knows, which anything + // that may free an object of its type ends (spec 84) + ops.placeReads.push_back(op); + } else if (mlir::isa(op) || isCall(op)) diff --git a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp index ea1e500f9..eca1ce2d4 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp @@ -288,6 +288,12 @@ namespace own_inference { refs.push_back(loadOp.getReference()); } + else if (mlir::isa(read)) + { + // the object a WeakRef refers to (spec 84): no root here owns it, and wherever it is + // held, a release of a holder of its type or a call that may drop ends it + chain.anyPlace = true; + } else { // a call's result that borrows its arguments - a plain borrow, or a borrowing record's box @@ -721,6 +727,22 @@ namespace own_inference return false; } + // the object a WeakRef refers to (spec 84): owned anywhere, so freed by any call that may + // drop, unless all it overwrites are fields that cannot hold it + if (read && mlir::isa(read)) + { + auto targetType = mlir::cast(read->getOperand(0).getType()).getElementType(); + if (auto places = callDropFields(drop)) + { + return llvm::any_of(*places, [&](auto &place) { + auto refType = mlir::dyn_cast(place.second); + return !refType || typeMayHold(module, refType.getElementType(), targetType); + }); + } + + return true; + } + // a private field nothing writes after construction, read straight off a parameter or `this` // (TSLANG 73): no call overwrites it, and it is released only with its object, which the // caller holds while this function runs @@ -1302,6 +1324,43 @@ namespace own_inference } } + // The object a WeakRef refers to may be any owner's, a local's too: a release of a local or + // of a temporary that may hold its type ends the borrow as well (spec 84). Not a WeakRef's. + if (mlir::isa(read)) + { + auto targetType = mlir::cast(read->getOperand(0).getType()).getElementType(); + auto module = read->getParentOfType(); + auto ended = false; + getFunction()->walk([&](mlir::Operation *op) { + if (ended) + { + return; + } + + mlir::Type held; + if (auto releaseSlotOp = mlir::dyn_cast(op)) + { + held = mlir::cast(releaseSlotOp.getSlot().getType()).getElementType(); + } + else if (auto releaseOp = mlir::dyn_cast(op)) + { + held = releaseOp.getReference().getType(); + } + + if (!held || MLIRTypeHelper::isWeakRefType(held) || !typeMayHold(module, held, targetType)) + { + return; + } + + ended = outlives(op); + }); + + if (ended) + { + return false; + } + } + // the taker has the value now: the field is left empty, so the local's release frees nothing // of it if (take.taker && !dryRun) diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index ceba99796..526b55ad3 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -257,6 +257,7 @@ namespace own_signature computeNoThrow(); pinAssignedGlobals(); rejectGlobalArgumentsDropped(); + rejectWeakTargetsShared(module); findBoundedForwarders(); findYieldedFields(module); findImportedStates(module); @@ -1710,6 +1711,46 @@ namespace own_signature computeDropsRound(true); } + void OwnershipSignaturePass::rejectWeakTargetsShared(mlir::ModuleOp module) + { + llvm::SmallVector weakRefs; + llvm::SmallVector shares; + module.walk([&](mlir::Operation *op) { + if (auto weakRefNewOp = mlir::dyn_cast(op)) + { + weakRefs.push_back(weakRefNewOp); + } + else if (auto markSharedOp = mlir::dyn_cast(op)) + { + shares.push_back(markSharedOp); + } + }); + + for (auto weakRefNewOp : weakRefs) + { + auto targetType = mlir::cast(weakRefNewOp.getType()).getElementType(); + for (auto markSharedOp : shares) + { + if (!typeMayHold(module, markSharedOp->getOperand(0).getType(), targetType)) + { + continue; + } + + llvm::SmallString<64> name; + llvm::raw_svector_ostream nameStream(name); + MLIRPrinter printer{}; + printer.printType(nameStream, targetType); + + auto diag = weakRefNewOp->emitError("-mm=own cannot make a WeakRef to a '") + << name + << "' in a program that shares what may hold one with another thread: a weak count is not atomic"; + diag.attachNote(markSharedOp->getLoc()) << "shared with another thread here"; + signalPassFailure(); + break; + } + } + } + void OwnershipSignaturePass::computeNoThrow() { llvm::DenseMap> callsIn; @@ -2410,7 +2451,8 @@ namespace own_signature // a drop. if (mlir::isa(op) && touchesHandle(op)) { - drops = true; + // a WeakRef's frees at most a tombstone (spec 84) + drops = !MLIRTypeHelper::isWeakRefType(op->getOperand(0).getType()); } else if (auto releaseSlotOp = mlir::dyn_cast(op)) { diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.h b/tslang/lib/TypeScript/OwnershipSignaturePass.h index e78df9506..3f82f52fa 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.h +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.h @@ -1181,6 +1181,11 @@ class OwnershipSignaturePass : public mlir::PassWrapper([&](auto) { return "returned"; }) + // `new WeakRef(x)` (own spec 84): the WeakRef may outlive the frame and read x's header there + .Case([&](auto) { return "weak-target"; }) // -mm=own's keepers: kept for good, it outlives every frame .Case([&](auto) { return "kept for good"; }) .Case([&](auto) { return "thrown"; }) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index f3504ea1e..1c9ac4712 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -5087,6 +5087,24 @@ foreach(shared_mm ${shared_models}) tslang_add_test(NAME test-compile-${shared_mm}-${shared_test} COMMAND test-runner -mm=${shared_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/${shared_test}.ts") endforeach() endforeach() +# WeakRef (own spec 84): built into the compiler under gc, none and own; rc has no weak count +foreach(weakref_mm gc none own) + foreach(weakref_test 00weakref_basic 00weakref 00weakref_frame 00weakref_literal) + tslang_add_test(NAME test-jit-${weakref_test}-${weakref_mm} COMMAND test-runner -jit -mm=${weakref_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${weakref_test}.ts") + tslang_add_test(NAME test-compile-${weakref_test}-${weakref_mm} COMMAND test-runner -mm=${weakref_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/${weakref_test}.ts") + endforeach() + tslang_add_test(NAME test-jit-own_weakref_share_unrelated-${weakref_mm} COMMAND test-runner -jit -mm=${weakref_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/own_weakref_share_unrelated.ts") + tslang_add_test(NAME test-compile-own_weakref_share_unrelated-${weakref_mm} COMMAND test-runner -mm=${weakref_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/own_weakref_share_unrelated.ts") + tslang_add_test(NAME test-jit-own_weakref_borrow-${weakref_mm} COMMAND test-runner -jit -mm=${weakref_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/own_weakref_borrow.ts") + tslang_add_test(NAME test-compile-own_weakref_borrow-${weakref_mm} COMMAND test-runner -mm=${weakref_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/own_weakref_borrow.ts") +endforeach() +add_test(NAME test-rc-err-own_err_weakref_rc + COMMAND $ --emit=obj --no-default-lib -mm=rc + "${PROJECT_SOURCE_DIR}/test/tester/own/own_err_weakref_rc.ts" + -o "${CMAKE_CURRENT_BINARY_DIR}/rc-err-own_err_weakref_rc.obj") +set_tests_properties(test-rc-err-own_err_weakref_rc + PROPERTIES PASS_REGULAR_EXPRESSION ":4:48: error: WeakRef is not supported under -mm=rc; use -mm=gc, -mm=own or -mm=none" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") # Shared.count is a count only under rc and own. foreach(shared_count_mm rc own) tslang_add_test(NAME test-jit-${shared_count_mm}-own_shared_count COMMAND test-runner -jit -mm=${shared_count_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/own_shared_count.ts") @@ -5530,6 +5548,14 @@ set(own_error_cases "own_err_list_walk_up|:21:24: error: '[^']*' borrows a field but is used here after it may be released or overwritten" "own_err_list_pop_used_after|:14:16: error: '[^']*' borrows a field but is used here after it may be released or overwritten" "own_err_list_next_kept_twice|:14:9: error: '[^']*' borrows a field and cannot be stored, returned or captured" + "own_err_weakref_not_class|:2:47: error: WeakRef needs a class object" + "own_err_weakref_stored|:4:126: error: '[^']*' borrows the object a WeakRef refers to and cannot be stored, returned or captured" + "own_err_weakref_returned|:4:55: error: '[^']*' borrows the object a WeakRef refers to and cannot be stored, returned or captured" + "own_err_weakref_captured|:5:92: error: '[^']*' is used here after its value was moved" + "own_err_weakref_after_call|:5:137: error: '[^']*' borrows the object a WeakRef refers to but is used here after it may be released or overwritten" + "own_err_weakref_after_release|:4:137: error: '[^']*' borrows the object a WeakRef refers to but is used here after it may be released or overwritten" + "own_err_weakref_thread|:14:15: error: -mm=own cannot make a WeakRef to a 'Node' in a program that shares what may hold one with another thread" + "own_err_weakref_local_reassigned|:5:108: error: '[^']*' borrows the object a WeakRef refers to but is used here after it may be released or overwritten" "own_err_list_refill_from_param|:13:9: error: '[^']*' borrows a field and cannot be stored, returned or captured" "own_err_list_unlink_walk|:16:9: error: '[^']*' borrows a field but is used here after it may be released or overwritten" "own_err_list_cut_through_alias|:13:15: error: '[^']*' borrows a field and cannot be stored, returned or captured" @@ -6691,7 +6717,8 @@ endforeach() tslang_add_test(NAME test-compile-debug-shared-export-import-class-implements-interface-abstract COMMAND test-runner -shared -noopt "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_implements_interface_abstract.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_implements_interface_abstract.ts") tslang_add_test(NAME test-jit-debug-shared-export-import-class-implements-interface-abstract COMMAND test-runner -jit -shared -noopt "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_implements_interface_abstract.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_implements_interface_abstract.ts") foreach(debug_own_test own/own_interface_getter_read own/own_immortal_keepers tests/00nullable_interface_to_interface - stack-promotion/to_any tests/00linked_list tests/00linked_list_kept tests/00linked_list_build) + stack-promotion/to_any tests/00linked_list tests/00linked_list_kept tests/00linked_list_build + tests/00weakref_basic tests/00weakref tests/00weakref_frame tests/00weakref_literal) get_filename_component(debug_own_name ${debug_own_test} NAME) tslang_add_test(NAME test-jit-debug-own-${debug_own_name} COMMAND test-runner -jit -mm=own -noopt "${PROJECT_SOURCE_DIR}/test/tester/${debug_own_test}.ts") tslang_add_test(NAME test-compile-debug-own-${debug_own_name} COMMAND test-runner -mm=own -noopt "${PROJECT_SOURCE_DIR}/test/tester/${debug_own_test}.ts") diff --git a/tslang/test/tester/own/own_err_weakref_after_call.ts b/tslang/test/tester/own/own_err_weakref_after_call.ts new file mode 100644 index 000000000..a13464cd7 --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_after_call.ts @@ -0,0 +1,5 @@ +// A call that may free a Node ends a borrow of the object a WeakRef refers to. +class Node { v = 1; } +class Holder { w: WeakRef | null = null; n: Node | null = null; kept: Node | null = null; } +function dropAll(h: Holder) { h.n = null; } +function main() { const h = new Holder(); const n = new Node(); h.w = new WeakRef(n); h.n = n; const p = h.w.deref(); dropAll(h); print(p ? p.v : 0); } diff --git a/tslang/test/tester/own/own_err_weakref_after_release.ts b/tslang/test/tester/own/own_err_weakref_after_release.ts new file mode 100644 index 000000000..92c6bb1a9 --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_after_release.ts @@ -0,0 +1,4 @@ +// The owner's release of a Node ends a borrow of the object a WeakRef refers to. +class Node { v = 1; } +class Holder { w: WeakRef | null = null; n: Node | null = null; kept: Node | null = null; } +function main() { const h = new Holder(); const n = new Node(); h.w = new WeakRef(n); h.n = n; const p = h.w.deref(); h.n = null; print(p ? p.v : 0); } diff --git a/tslang/test/tester/own/own_err_weakref_captured.ts b/tslang/test/tester/own/own_err_weakref_captured.ts new file mode 100644 index 000000000..b7abc3969 --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_captured.ts @@ -0,0 +1,5 @@ +// The object a WeakRef refers to is its owner's: a closure that escapes cannot capture a borrow of it. +class Node { v: number = 1; } +class Holder { w: WeakRef | null = null; n: Node | null = null; kept: Node | null = null; } +let FNS: (() => number)[] = []; +function main() { const n = new Node(); const w = new WeakRef(n); const p = w.deref(); if (p) FNS.push(() => p.v); } diff --git a/tslang/test/tester/own/own_err_weakref_local_reassigned.ts b/tslang/test/tester/own/own_err_weakref_local_reassigned.ts new file mode 100644 index 000000000..639f2c123 --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_local_reassigned.ts @@ -0,0 +1,5 @@ +// The object a WeakRef refers to may be a local's: the local given another value frees it, and ends +// a borrow of it. +class Node { v = 1; } +class Holder { w: WeakRef | null = null; n: Node | null = null; kept: Node | null = null; } +function main() { let n = new Node(); const w = new WeakRef(n); const p = w.deref(); n = new Node(); print(p ? p.v : 0); } diff --git a/tslang/test/tester/own/own_err_weakref_not_class.ts b/tslang/test/tester/own/own_err_weakref_not_class.ts new file mode 100644 index 000000000..844115302 --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_not_class.ts @@ -0,0 +1,2 @@ +// Under own a WeakRef refers to a class object: its header word holds the weak count. +function main() { const a = [1, 2]; const w = new WeakRef(a); print(a.length); } diff --git a/tslang/test/tester/own/own_err_weakref_rc.ts b/tslang/test/tester/own/own_err_weakref_rc.ts new file mode 100644 index 000000000..8ba87369e --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_rc.ts @@ -0,0 +1,4 @@ +// rc's header word is the strong count: a weak count needs a second word, which rc does not have yet. +class C { v = 1; } + +function main() { const c = new C(); const w = new WeakRef(c); print(c.v); } diff --git a/tslang/test/tester/own/own_err_weakref_returned.ts b/tslang/test/tester/own/own_err_weakref_returned.ts new file mode 100644 index 000000000..e150d97c4 --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_returned.ts @@ -0,0 +1,5 @@ +// The object a WeakRef refers to is its owner's: a borrow of it cannot be returned. +class Node { v = 1; } +class Holder { w: WeakRef | null = null; n: Node | null = null; kept: Node | null = null; } +function get(w: WeakRef) { const p = w.deref(); return p; } +function main() { const n = new Node(); const w = new WeakRef(n); print(get(w) ? 1 : 0); } diff --git a/tslang/test/tester/own/own_err_weakref_stored.ts b/tslang/test/tester/own/own_err_weakref_stored.ts new file mode 100644 index 000000000..25a810e70 --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_stored.ts @@ -0,0 +1,4 @@ +// The object a WeakRef refers to is its owner's: a borrow of it cannot be stored. +class Node { v = 1; } +class Holder { w: WeakRef | null = null; n: Node | null = null; kept: Node | null = null; } +function main() { const h = new Holder(); const n = new Node(); h.w = new WeakRef(n); h.n = n; const p = h.w.deref(); if (p) h.kept = p; } diff --git a/tslang/test/tester/own/own_err_weakref_thread.ts b/tslang/test/tester/own/own_err_weakref_thread.ts new file mode 100644 index 000000000..a26c51f0a --- /dev/null +++ b/tslang/test/tester/own/own_err_weakref_thread.ts @@ -0,0 +1,18 @@ +// A weak count is not atomic: under -mm=own a program that makes WeakRefs to a class may not share +// what could hold one with another thread (Shared.share, as Thread.spawn and a worker do), where it +// could be freed while this thread reads the count. +class Node { + v: number = 1; +} + +class Box { + n: Node | null = null; +} + +function main() { + const n = new Node(); + const w = new WeakRef(n); + const b = new Box(); + Shared.share(b); + print(w.deref() ? 1 : 0, n.v); +} diff --git a/tslang/test/tester/own/own_weakref_borrow.ts b/tslang/test/tester/own/own_weakref_borrow.ts new file mode 100644 index 000000000..5d8dc1676 --- /dev/null +++ b/tslang/test/tester/own/own_weakref_borrow.ts @@ -0,0 +1,23 @@ +// A borrow of the object a WeakRef refers to, used while nothing can free it: a call into a @final +// class that frees nothing keeps it. +@final +class Node { + parent: WeakRef | null = null; + constructor(public v: number) {} + + depth(): number { + const p = this.parent ? this.parent.deref() : undefined; + if (p) return p.v + p.depth(); + return 0; + } +} + +function main() { + const a = new Node(1); + const b = new Node(2); + const c = new Node(3); + b.parent = new WeakRef(a); + c.parent = new WeakRef(b); + assert(c.depth() == 3, "depth"); + print("done."); +} diff --git a/tslang/test/tester/own/own_weakref_share_unrelated.ts b/tslang/test/tester/own/own_weakref_share_unrelated.ts new file mode 100644 index 000000000..ed1a5c0c2 --- /dev/null +++ b/tslang/test/tester/own/own_weakref_share_unrelated.ts @@ -0,0 +1,22 @@ +// A program making WeakRefs to a class may still share with other threads what cannot hold one: a +// @final class of numbers. The WeakRef's class is @final too: else a subclass of it could be held in a +// field of any class type. +@final +class Node { + v: number = 1; +} + +@final +class Point { + x: number = 2; +} + +function main() { + const n = new Node(); + const w = new WeakRef(n); + const p = new Point(); + Shared.share(p); + assert(w.deref() ? true : false, "alive"); + assert(p.x == 2, "point"); + print("done."); +} diff --git a/tslang/test/tester/tests/00weakref.ts b/tslang/test/tester/tests/00weakref.ts new file mode 100644 index 000000000..0e3e49f91 --- /dev/null +++ b/tslang/test/tester/tests/00weakref.ts @@ -0,0 +1,115 @@ +// WeakRef in the shapes that matter: a tree with parent links, a subtree dropped while a WeakRef +// into it remains, copies of one WeakRef, a WeakRef given another target after its own died, an object +// that refers to itself, WeakRefs held by an array, and a WeakRef to an object that never dies. Under +// -mm=own the count is kept in the target's header word and a freed target stays a tombstone until +// the last WeakRef to it goes; gc frees when its collector runs and none never, so there a WeakRef may +// still find what it refers to. New objects of the same size are made between the steps: a freed +// block read by mistake would show. +class Node { + parent: WeakRef | null = null; + children: Node[] = []; + me: WeakRef | null = null; + constructor(public v: number) {} + + add(c: Node) { + c.parent = new WeakRef(this); + this.children.push(c); + } + + depth(): number { + const p = this.parent ? this.parent.deref() : undefined; + return p ? 1 + p.depth() : 0; + } +} + +class Holder { + w: WeakRef | null = null; + other: WeakRef | null = null; +} + +const G = new Node(7); + +// -mm=own frees an object when its owner gives it up (`root.children = []` here): only there is a +// dropped target certainly gone +function freesAtOnce() { + return Shared.count(new Shared(0)) >= 0; +} + +function alive(w: WeakRef | null) { + if (!w) return -1; + const n = w.deref(); + return n ? n.v : -1; +} + +function churn() { + let s = 0.0; + for (let i = 0; i < 4; i++) { + const n = new Node(1000); + s += n.v; + } + + return s; +} + +function main() { + const FREES_AT_ONCE = freesAtOnce(); + let checks = 0.0; + for (let i = 0; i < 2000; i++) { + // tree + const root = new Node(1); + const a = new Node(2); + const b = new Node(3); + const leaf = new Node(4); + b.add(leaf); + a.add(b); + root.add(a); + assert(root.children[0].children[0].children[0].depth() == 3, "tree"); + + // dropped: the subtree goes (`a`, `b` and `leaf`), the WeakRef into it stays + const h = new Holder(); + h.w = new WeakRef(root.children[0].children[0]); + assert(alive(h.w) == 3, "before drop"); + root.children = []; + checks += churn(); + assert(!FREES_AT_ONCE || alive(h.w) == -1, "after drop"); + + // copies: two WeakRefs to one target, the target goes, both see it gone + const t = new Node(5); + h.w = new WeakRef(t); + h.other = h.w; + assert(alive(h.w) == 5 && alive(h.other) == 5, "copies alive"); + root.children.push(t); + root.children = []; + checks += churn(); + assert(!FREES_AT_ONCE || (alive(h.w) == -1 && alive(h.other) == -1), "copies dead"); + + // retarget: a dead WeakRef's field given another target + h.w = new WeakRef(root); + assert(alive(h.w) == 1, "retarget"); + h.other = null; + + // self: an object that refers to itself, freed + const s = new Node(6); + s.me = new WeakRef(s); + root.children.push(s); + root.children = []; + checks += churn(); + + // elements: WeakRefs held by an array, popped, spliced, dropped + const refs: WeakRef[] = []; + for (let j = 0; j < 4; j++) refs.push(new WeakRef(root)); + refs.pop(); + refs.splice(0, 1); + assert(refs.length == 2 && alive(refs[0]) == 1, "elements"); + + // immortal: a global's object + h.other = new WeakRef(G); + checks += churn(); + assert(alive(h.other) == 7, "immortal"); + + checks += root.v; + } + + assert(checks == 2000 * (4 * 4000.0 + 1), "checks"); + print("done."); +} diff --git a/tslang/test/tester/tests/00weakref_basic.ts b/tslang/test/tester/tests/00weakref_basic.ts new file mode 100644 index 000000000..496e62d1f --- /dev/null +++ b/tslang/test/tester/tests/00weakref_basic.ts @@ -0,0 +1,28 @@ +// WeakRef: a child refers to its parent without owning it. deref() gives the parent while it lives. +class Node { + parent: WeakRef | null = null; + children: Node[] = []; + constructor(public v: number) {} + + add(c: Node) { + c.parent = new WeakRef(this); + this.children.push(c); + } + + depth(): number { + const p = this.parent ? this.parent.deref() : undefined; + return p ? 1 + p.depth() : 0; + } +} + +function main() { + const root = new Node(1); + const mid = new Node(2); + const leaf = new Node(3); + mid.add(leaf); + root.add(mid); + assert(root.children[0].children[0].depth() == 2, "depth"); + const w = new WeakRef(root); + assert(w.deref() === root, "deref"); + print("done."); +} diff --git a/tslang/test/tester/tests/00weakref_frame.ts b/tslang/test/tester/tests/00weakref_frame.ts new file mode 100644 index 000000000..b69fefdcf --- /dev/null +++ b/tslang/test/tester/tests/00weakref_frame.ts @@ -0,0 +1,52 @@ +// An object made in a function and referred to by a WeakRef that outlives the call stays on the heap: +// on the stack its frame would be gone while the WeakRef still reads it. Under -mm=own the object is +// freed when the function ends and deref() gives undefined; under gc and none it may still be found, +// and then it is the object as it was. +class Node { + constructor(public v: number) {} +} + +class Holder { + w: WeakRef | null = null; +} + +function link(h: Holder, n: Node) { + h.w = new WeakRef(n); +} + +// the WeakRef is made by a function the object is only lent to +function make(h: Holder, v: number) { + const n = new Node(v); + link(h, n); + return n.v; +} + +function scribble(v: number) { + // a frame of the same size over the old one + const a = new Node(v); + const b = new Node(v); + return a.v + b.v; +} + +function freesAtOnce() { + return Shared.count(new Shared(0)) >= 0; +} + +function main() { + const own = freesAtOnce(); + const h = new Holder(); + let s = 0.0; + for (let i = 0; i < 1000; i++) { + s += make(h, 5); + s += scribble(77); + const n = h.w ? h.w.deref() : undefined; + if (own) { + assert(!n, "freed"); + } else if (n) { + assert(n.v == 5, "intact"); + } + } + + assert(s == 1000 * (5 + 154.0), "sum"); + print("done."); +} diff --git a/tslang/test/tester/tests/00weakref_literal.ts b/tslang/test/tester/tests/00weakref_literal.ts new file mode 100644 index 000000000..d38c56c8a --- /dev/null +++ b/tslang/test/tester/tests/00weakref_literal.ts @@ -0,0 +1,71 @@ +// A WeakRef (or a Shared) made in an object literal or a tuple is counted once: the record holds it, +// and its release gives the count back once. Counted twice down, the target was freed while a WeakRef +// still referred to it, and deref() gave a freed block. +class Node { + pad1 = 1.0; + pad2 = 2.0; + constructor(public v: number) {} +} + +class Holder { + n: Node | null = null; +} + +function freesAtOnce() { + return Shared.count(new Shared(0)) >= 0; +} + +function inLiteral(n: Node): number { + const o = { w: new WeakRef(n), x: 1 }; + return o.x; +} + +function inTuple(n: Node): number { + const t: [WeakRef, number] = [new WeakRef(n), 2]; + return t[1]; +} + +function returned(n: Node) { + return { w: new WeakRef(n), x: 3 }; +} + +function check(kind: number) { + const h = new Holder(); + const n = new Node(42); + const w0 = new WeakRef(n); + h.n = n; + assert((kind == 0 ? inLiteral(n) : kind == 1 ? inTuple(n) : returned(n).x) > 0, "made"); + assert(w0.deref()?.v == 42, "alive while held"); + h.n = null; + const n2 = new Node(77); + const n3 = new Node(88); + if (freesAtOnce()) { + assert(w0.deref() === undefined, "gone once its owner freed it"); + } + + return n2.v + n3.v; +} + +function main() { + let total: number = 0; + for (let i = 0; i < 1000; i++) { + total += check(0); + total += check(1); + total += check(2); + + const n = new Node(1); + const o = { w: new WeakRef(n), x: 1 }; + const t: [WeakRef, number] = [new WeakRef(n), 2]; + total += o.x + t[1] + (o.w.deref()?.v ?? 0) + (t[0].deref()?.v ?? 0); + + const s = { s: new Shared(i), x: 1 }; + if (freesAtOnce()) { + assert(Shared.count(s.s) == 1, "one count in a literal"); + } + + total += s.x; + } + + assert(total == 1000 * (3 * 165 + 6), "total"); + print("done."); +} diff --git a/tslang/test/tester/verify-ownership.cmake b/tslang/test/tester/verify-ownership.cmake index 3ab40979b..18bdf5ec1 100644 --- a/tslang/test/tester/verify-ownership.cmake +++ b/tslang/test/tester/verify-ownership.cmake @@ -35,6 +35,11 @@ set(not_compilable_alone import_vars.ts # written for `-nostrictnull` raytrace-0.ts + # WeakRef is an error under rc (own spec 84): rc has no weak count + 00weakref.ts + 00weakref_basic.ts + 00weakref_frame.ts + 00weakref_literal.ts # a @cpp class is an error under rc and own (cppvtable/memory_model.ts) 00cpp_slot.ts 00cpp_overloads.ts