diff --git a/.gitignore b/.gitignore index b1394d0c0..d5116e526 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ package-lock.json node_modules gc-* libatomic_ops-* +*.log # the JIT cache (tslang --emit=jit) __jit/ diff --git a/tslang/lib/TypeScript/OwnershipFacts.h b/tslang/lib/TypeScript/OwnershipFacts.h index dcf2b89af..b92804106 100644 --- a/tslang/lib/TypeScript/OwnershipFacts.h +++ b/tslang/lib/TypeScript/OwnershipFacts.h @@ -13,6 +13,8 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/STLFunctionalExtras.h" +#include + // What -mm=own's two passes both need to read the ownership ops: which op is a view of the block // its operand holds, where a value was made, which ops are calls and places. See // docs/superpowers/specs/2026-09-24-own-memory-model-design.md, sections 3.2, 3.3 and 14. @@ -57,6 +59,14 @@ namespace mlir_ts = mlir::typescript; // The callee destroys nothing its caller can reach: no overwrite of a place it did not make, no // assignment of a global, no call that may. #define OWN_NO_DROPS_ATTR_NAME "__own_no_drops" +// The callee destroys nothing its caller can reach but the old values of the places it overwrites: +// [position, slot type] pairs - a field's position (OWN_INTERFACE_FIELD_POSITION for a field reached +// through an interface), -1 for an element - judged as those stores would be if they were the +// caller's own (OwnershipInferencePass::overwritesChain). +#define OWN_DROP_FIELDS_ATTR_NAME "__own_drop_fields" +// The position a place reached through an interface is given: its position in the object is not +// known (spec 28) +constexpr int64_t OWN_INTERFACE_FIELD_POSITION = -3; // On a function whose body relies on a fact its callers cannot all know (a parameter it keeps, a // result that borrows): why, for the error the body gets instead. #define OWN_FACTS_LOST_ATTR_NAME "__own_facts_lost" @@ -156,6 +166,25 @@ inline bool callNoDrops(mlir::Operation *op) return op->hasAttr(OWN_NO_DROPS_ATTR_NAME); } +// The places a call's callees may overwrite (OWN_DROP_FIELDS_ATTR_NAME), when that is all they +// destroy; none when they may destroy anything. +inline std::optional>> callDropFields(mlir::Operation *op) +{ + auto attr = op->getAttrOfType(OWN_DROP_FIELDS_ATTR_NAME); + if (!attr) + { + return std::nullopt; + } + + llvm::SmallVector> places; + for (auto entry : attr.getAsRange()) + { + places.push_back({mlir::cast(entry[0]).getInt(), mlir::cast(entry[1]).getValue()}); + } + + return places; +} + // The argument indices a call's callee keeps. inline llvm::ArrayRef ownedParams(mlir::Operation *op) { diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.cpp b/tslang/lib/TypeScript/OwnershipInferencePass.cpp index 2ae1d0824..9b7bc8f7f 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePass.cpp @@ -483,7 +483,8 @@ namespace own_inference for (auto *name : {OWN_PARAMS_ATTR_NAME, OWN_PARAMS_RELEASED_ATTR_NAME, OWN_RESULT_BORROWS_ATTR_NAME, OWN_RESULT_RECORD_ATTR_NAME, OWN_RESULT_RECORD_UNBOXED_ATTR_NAME, OWN_RECORD_UNBOXED_FIELDS_ATTR_NAME, OWN_RECORD_TEMPORARY_ATTR_NAME, - OWN_NO_DROPS_ATTR_NAME, OWN_FACTS_LOST_ATTR_NAME, OWN_FRESH_RESULT_ATTR_NAME, + OWN_NO_DROPS_ATTR_NAME, OWN_DROP_FIELDS_ATTR_NAME, OWN_FACTS_LOST_ATTR_NAME, + OWN_FRESH_RESULT_ATTR_NAME, OWN_ASSIGNS_CAPTURES_ATTR_NAME, OWN_RESULT_BOUNDED_ATTR_NAME, OWN_CAPTURE_BORROW_ATTR_NAME}) diff --git a/tslang/lib/TypeScript/OwnershipInferencePass.h b/tslang/lib/TypeScript/OwnershipInferencePass.h index 9f8724d50..33b855db5 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePass.h +++ b/tslang/lib/TypeScript/OwnershipInferencePass.h @@ -735,7 +735,7 @@ class OwnershipInferencePass // position in the object is not known here (spec 28) llvm::SmallVector> places; static constexpr int64_t SharedValue = -2; - static constexpr int64_t InterfaceField = -3; + static constexpr int64_t InterfaceField = OWN_INTERFACE_FIELD_POSITION; llvm::SmallVector> roots; // The roots that are handles - what a `s.value` was read through - each also NotOwned // among `roots` (spec 23.3). Known from the op, not the root's type: a view may hide it. @@ -776,6 +776,8 @@ class OwnershipInferencePass // from the root that the read did not produce. // `borrowed`, when given, is the value the borrow holds in place of the read's result (a value // moved into the place, spec 29), and `read` may then be null. + // Does an overwrite of a place at this position, with this slot type, drop the chain? + bool overwritesChain(int64_t position, mlir::Type slotType, const Chain &chain); bool dropsChain(mlir::Operation *drop, const Chain &chain, mlir::Operation *read, mlir::Value borrowed = {}); // A read out of a container is a borrow of it (spec 2.3), and so is a `let` declared from one: diff --git a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp index 6bf732325..458a67b52 100644 --- a/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp +++ b/tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp @@ -573,6 +573,25 @@ namespace own_inference return derived; } + bool OwnershipInferencePass::overwritesChain(int64_t position, mlir::Type slotType, const Chain &chain) + { + auto isField = [](int64_t at) { return at >= 0 || at == Chain::InterfaceField; }; + + // A field reached through an interface may be any field of the object under it, of any + // type (a class may declare the field narrower than the interface): an overwrite through + // an interface drops every field on the chain, and any field's overwrite drops a field + // read through an interface (spec 28). + return llvm::any_of(chain.places, [&](auto &place) { + if (isField(position) && isField(place.first) && + (position == Chain::InterfaceField || place.first == Chain::InterfaceField)) + { + return true; + } + + return place.first == position && place.second == slotType; + }); + } + bool OwnershipInferencePass::dropsChain(mlir::Operation *drop, const Chain &chain, mlir::Operation *read, mlir::Value borrowed) { if (read && drop == read) @@ -611,7 +630,6 @@ namespace own_inference return llvm::any_of(chain.places, [&](auto &place) { return place.first == Chain::SharedValue; }); } - auto isField = [](int64_t position) { return position >= 0 || position == Chain::InterfaceField; }; int64_t position = -1; if (auto propertyRefOp = slot.getDefiningOp()) { @@ -622,19 +640,7 @@ namespace own_inference position = Chain::InterfaceField; } - // A field reached through an interface may be any field of the object under it, of any - // type (a class may declare the field narrower than the interface): an overwrite through - // an interface drops every field on the chain, and any field's overwrite drops a field - // read through an interface (spec 28). - return llvm::any_of(chain.places, [&](auto &place) { - if (isField(position) && isField(place.first) && - (position == Chain::InterfaceField || place.first == Chain::InterfaceField)) - { - return true; - } - - return place.first == position && place.second == slot.getType(); - }); + return overwritesChain(position, slot.getType(), chain); } if (isCall(drop)) @@ -645,18 +651,31 @@ namespace own_inference } // a call given a closure may run it, and its box may hold anything - if (!chain.owned() || llvm::any_of(drop->getOperands(), holdsCaptures)) + auto givenClosure = llvm::any_of(drop->getOperands(), holdsCaptures); + auto mayDrop = true; + if (chain.owned() && !givenClosure) + { + auto &fromRead = derivedFrom(borrowed ? borrowed : read->getResult(0)); + auto sources = rootSources(chain); + mayDrop = llvm::any_of(drop->getOperands(), [&](mlir::Value operand) { + return !fromRead.contains(operand) && llvm::any_of(sources, [&](mlir::Value source) { + return derivedFrom(source).contains(operand); + }); + }); + } + + // Callees that destroy only the old values of places they overwrite drop the chain as + // those stores would here. Not a chain whose places are not all known (a borrowing + // call's result is somewhere under its arguments). + if (mayDrop && !givenClosure && !chain.anyPlace) { - return true; + if (auto places = callDropFields(drop)) + { + mayDrop = llvm::any_of(*places, [&](auto &place) { return overwritesChain(place.first, place.second, chain); }); + } } - auto &fromRead = derivedFrom(borrowed ? borrowed : read->getResult(0)); - auto sources = rootSources(chain); - return llvm::any_of(drop->getOperands(), [&](mlir::Value operand) { - return !fromRead.contains(operand) && llvm::any_of(sources, [&](mlir::Value source) { - return derivedFrom(source).contains(operand); - }); - }); + return mayDrop; } // pop, shift, splice, `length =`; push and unshift on an array reached through a handle diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp index 63427ffc8..9a6c27b43 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.cpp +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.cpp @@ -83,6 +83,7 @@ namespace own_signature call.op->setAttr(OWN_FRESH_RESULT_ATTR_NAME, mlir::UnitAttr::get(&getContext())); } + call.op->removeAttr(OWN_DROP_FIELDS_ATTR_NAME); if (!call.callees.known) { continue; @@ -93,6 +94,18 @@ namespace own_signature { call.op->setAttr(OWN_NO_DROPS_ATTR_NAME, mlir::UnitAttr::get(&getContext())); } + else if (auto drops = calleesFieldDrops(call.callees)) + { + llvm::SmallVector places; + for (auto [position, type] : drops->places) + { + places.push_back(mlir::ArrayAttr::get( + &getContext(), {mlir::IntegerAttr::get(mlir::IntegerType::get(&getContext(), 64), position), + mlir::TypeAttr::get(type)})); + } + + call.op->setAttr(OWN_DROP_FIELDS_ATTR_NAME, mlir::ArrayAttr::get(&getContext(), places)); + } // `new C()` of a class from a library built under own: see OWN_FRESH_RESULT_ATTR_NAME if (call.callees.imported && call.callees.importedName.ends_with("." NEW_METHOD_NAME) && @@ -1126,6 +1139,143 @@ namespace own_signature } } } + + // What the others destroy, when it is only places they overwrite: their bodies' stores and + // their callees' together, to a fixed point (a recursive call adds nothing new) + for (auto &entry : functions) + { + auto funcOp = entry.second; + if (!funcOp.isDeclaration() && !noDrops.contains(funcOp)) + { + fieldDrops[funcOp] = fieldDropsInBody(funcOp); + } + } + + for (auto changed = true; changed;) + { + changed = false; + for (auto &entry : functions) + { + auto funcOp = entry.second; + auto found = fieldDrops.find(funcOp); + if (found == fieldDrops.end() || found->second.any) + { + continue; + } + + for (auto *call : callsIn[funcOp]) + { + if (!callMayDrop(call->callees)) + { + continue; + } + + auto callee = calleesFieldDrops(call->callees); + auto &mine = fieldDrops[funcOp]; + if (!callee || callee->any) + { + mine.any = true; + changed = true; + break; + } + + for (auto place : callee->places) + { + changed |= mine.add(place); + } + } + } + } + } + + std::optional OwnershipSignaturePass::calleesFieldDrops(const Callees &callees) + { + if (!callees.known || callees.instanceOf || callees.imported) + { + return std::nullopt; + } + + FieldDrops all; + for (auto callee : callees.funcs) + { + if (noDrops.contains(callee)) + { + continue; + } + + auto found = fieldDrops.find(callee); + if (found == fieldDrops.end() || found->second.any) + { + return std::nullopt; + } + + for (auto place : found->second.places) + { + all.add(place); + } + } + + return all; + } + + OwnershipSignaturePass::FieldDrops OwnershipSignaturePass::fieldDropsInBody(mlir_ts::FuncOp funcOp) + { + // what dropsInBody counts, with the overwrites of places it can name kept apart + FieldDrops drops; + funcOp.walk([&](mlir::Operation *op) { + if (drops.any) + { + return; + } + + if (mlir::isa(op) && touchesHandle(op)) + { + drops.any = true; + } + else if (auto releaseSlotOp = mlir::dyn_cast(op)) + { + auto slot = releaseSlotOp.getSlot(); + if (slot.getDefiningOp() || isLoadedCell(slot) || (!isPlace(slot) && touchesHandle(op))) + { + drops.any = true; + } + else if (isPlace(slot) && reachesOutside(slot, funcOp)) + { + // `x.value = ...` of a handle may replace the payload any other handle reads + if (slot.getDefiningOp()) + { + drops.any = true; + return; + } + + int64_t position = -1; + if (auto propertyRefOp = slot.getDefiningOp()) + { + position = propertyRefOp.getPosition(); + } + else if (isInterfaceField(slot)) + { + position = OWN_INTERFACE_FIELD_POSITION; + } + + drops.add({position, slot.getType()}); + } + } + else if (mlir::isa(op)) + { + drops.any |= reachesOutside(op->getOperand(0), funcOp); + } + else if (mlir::isa(op)) + { + drops.any |= reachesSharedValue(op->getOperand(0)); + } + else if (mlir::isa(op)) + { + drops.any = true; + } + }); + + return drops; } bool OwnershipSignaturePass::callMayDrop(const Callees &callees) diff --git a/tslang/lib/TypeScript/OwnershipSignaturePass.h b/tslang/lib/TypeScript/OwnershipSignaturePass.h index d24a5b2c4..99ad175ca 100644 --- a/tslang/lib/TypeScript/OwnershipSignaturePass.h +++ b/tslang/lib/TypeScript/OwnershipSignaturePass.h @@ -516,6 +516,36 @@ class OwnershipSignaturePass : public mlir::PassWrapper noDrops; + // What else a function destroys that its caller can reach, when it is only the old values of + // places it overwrites: each place's position and slot type (OWN_DROP_FIELDS_ATTR_NAME). A + // caller judges a call of it as it would those stores, made in its own body. `any` when it may + // destroy something else (a global, a handle's payload, a captured variable, an element popped, + // a call that may). + struct FieldDrops + { + bool any = false; + llvm::SmallVector, 4> places; + + bool add(std::pair place) + { + if (llvm::is_contained(places, place)) + { + return false; + } + + places.push_back(place); + return true; + } + }; + + llvm::DenseMap fieldDrops; + + // The body's own share of a function's FieldDrops: its stores, not its calls. + FieldDrops fieldDropsInBody(mlir_ts::FuncOp funcOp); + + // The FieldDrops of a call's callees together, when every one has them. + std::optional calleesFieldDrops(const Callees &callees); + // A class's vtable is a global named `..vtbl` that holds its `..instanceOf`. An // interface's vtable for a class (`B.I..vtbl`) holds only the methods and is not one: a call // through an interface does not name what it reaches. diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 9d2cc70b1..f98f18a49 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -3849,7 +3849,8 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o own_interface_facts own_declared_scalar_no_drops own_borrowing_record own_borrowing_record_arg own_reduce own_fresh_call_result own_ternary_outer_loop own_destructure_elements - own_tuple_record_values own_value_record own_value_loop own_value_temporaries) + own_tuple_record_values own_value_record own_value_loop own_value_temporaries + own_call_field_drops) tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") tslang_add_test(NAME test-compile-own-${own_test} COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts") # The promise: no counting at all in what own emits. @@ -4320,6 +4321,8 @@ set(own_error_cases "own_err_call_given_borrow|:21:5: error: '[^']*' borrows 'g[.]c' but is used here after it may be released or overwritten" "own_err_call_this_borrow|:19:5: error: '[^']*' borrows 'g[.]c' but is used here after it may be released or overwritten" "own_err_declared_drops|:15:16: error: '[^']*' borrows a field but is used here after it may be released or overwritten" + "own_err_call_field_drops_owner|:30:11: error: '[^']*' borrows a field but is used here after it may be released or overwritten" + "own_err_call_field_drops_under|:30:11: error: '[^']*' borrows a field but is used here after it may be released or overwritten" "own_err_exported_virtual_call|:11:16: error: '[^']*' borrows a field but is used here after it may be released or overwritten" "own_err_borrowed_result_stored|borrows the result of 'firstOf' and cannot be stored, returned or captured" "own_err_borrowed_result_outlives|:19:11: error: '[^']*' borrows the result of 'firstOf' but is used here after it may be released or overwritten" diff --git a/tslang/test/tester/own/own_call_field_drops.ts b/tslang/test/tester/own/own_call_field_drops.ts new file mode 100644 index 000000000..1efbbd61d --- /dev/null +++ b/tslang/test/tester/own/own_call_field_drops.ts @@ -0,0 +1,71 @@ +// -mm=own: a callee that destroys only the old values of the places it overwrites drops a borrow +// only as those stores would in the caller (OWN_DROP_FIELDS_ATTR_NAME). A method that replaces an +// array field of its own object, called on an object held in a field of `this` - a camera kept by +// a render pass - leaves the borrow of that object alone, directly, through a method that calls it, +// and through an interface. The same overwrite of a place on the chain still drops it +// (own_err_call_field_drops_owner, own_err_call_field_drops_under). +class Cam { + position: number[] = [0.0, 0.0]; + private initial: number[] = [0.0, 0.0]; + + init(x: number, y: number): void { + this.initial = [x, y]; + this.reset(); + } + + reset(): void { + this.position = [this.initial[0], this.initial[1]]; + } +} + +interface Mover { + move(c: Cam): void; +} + +class Step implements Mover { + move(c: Cam): void { + c.position = [c.position[0] + 1.0, c.position[1]]; + } +} + +function churn() { + const keep: Cam[] = []; + for (let i = 0; i < 2000; i++) { + const c = new Cam(); + c.position = [77777.0, 77777.0]; + keep.push(c); + } + + return keep.length; +} + +class Pass { + private camera: Cam = new Cam(); + + direct(): number { + this.camera.reset(); + churn(); + return this.camera.position[0]; + } + + nested(): number { + this.camera.init(3.0, 4.0); + churn(); + return this.camera.position[0] + this.camera.position[1]; + } + + viaInterface(m: Mover): number { + m.move(this.camera); + churn(); + return this.camera.position[0]; + } +} + +function main() { + const p = new Pass(); + assert(p.direct() == 0, "a method that replaces its own field"); + assert(p.nested() == 7, "through a method that calls it"); + assert(p.viaInterface(new Step()) == 4, "through an interface"); + print(p.direct(), p.nested(), p.viaInterface(new Step())); // 3 7 4 (init kept 3, 4) + print("done."); +} diff --git a/tslang/test/tester/own/own_err_call_field_drops_owner.ts b/tslang/test/tester/own/own_err_call_field_drops_owner.ts new file mode 100644 index 000000000..735368535 --- /dev/null +++ b/tslang/test/tester/own/own_err_call_field_drops_owner.ts @@ -0,0 +1,31 @@ +// -mm=own: a method that overwrites the field a borrow was read from drops the borrow, through +// the method's summary of the places it overwrites (OWN_DROP_FIELDS_ATTR_NAME) as through a store +// in the caller: the old object is freed, and the borrow would read it. +class C { + constructor(public x: number) {} +} + +class H { + c: C = new C(0); + + replace(): void { + this.c = new C(9); + } +} + +function churn() { + const keep: C[] = []; + for (let i = 0; i < 2000; i++) { + keep.push(new C(77777)); + } + + return keep.length; +} + +function main() { + const h = new H(); + const c = h.c; + h.replace(); + churn(); + print(c.x); +} diff --git a/tslang/test/tester/own/own_err_call_field_drops_under.ts b/tslang/test/tester/own/own_err_call_field_drops_under.ts new file mode 100644 index 000000000..74f82859c --- /dev/null +++ b/tslang/test/tester/own/own_err_call_field_drops_under.ts @@ -0,0 +1,31 @@ +// -mm=own: a method that overwrites a field under the borrowed value - the array a borrow reads, +// replaced by a method of the object holding it - drops the borrow (OWN_DROP_FIELDS_ATTR_NAME), +// though the method is called on that object and not on the borrow's root. +class Cam { + position: number[] = [0.0, 0.0]; + + reset(): void { + this.position = [1.0, 1.0]; + } +} + +class Pass { + camera: Cam = new Cam(); +} + +function churn() { + const keep: number[][] = []; + for (let i = 0; i < 2000; i++) { + keep.push([77777.0, 77777.0]); + } + + return keep.length; +} + +function main() { + const p = new Pass(); + const position = p.camera.position; + p.camera.reset(); + churn(); + print(position[0]); +}