Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package-lock.json
node_modules
gc-*
libatomic_ops-*
*.log

# the JIT cache (tslang --emit=jit)
__jit/
29 changes: 29 additions & 0 deletions tslang/lib/TypeScript/OwnershipFacts.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/STLFunctionalExtras.h"

#include <optional>

// 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.
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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<llvm::SmallVector<std::pair<int64_t, mlir::Type>>> callDropFields(mlir::Operation *op)
{
auto attr = op->getAttrOfType<mlir::ArrayAttr>(OWN_DROP_FIELDS_ATTR_NAME);
if (!attr)
{
return std::nullopt;
}

llvm::SmallVector<std::pair<int64_t, mlir::Type>> places;
for (auto entry : attr.getAsRange<mlir::ArrayAttr>())
{
places.push_back({mlir::cast<mlir::IntegerAttr>(entry[0]).getInt(), mlir::cast<mlir::TypeAttr>(entry[1]).getValue()});
}

return places;
}

// The argument indices a call's callee keeps.
inline llvm::ArrayRef<int32_t> ownedParams(mlir::Operation *op)
{
Expand Down
3 changes: 2 additions & 1 deletion tslang/lib/TypeScript/OwnershipInferencePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
4 changes: 3 additions & 1 deletion tslang/lib/TypeScript/OwnershipInferencePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ class OwnershipInferencePass
// position in the object is not known here (spec 28)
llvm::SmallVector<std::pair<int64_t, mlir::Type>> places;
static constexpr int64_t SharedValue = -2;
static constexpr int64_t InterfaceField = -3;
static constexpr int64_t InterfaceField = OWN_INTERFACE_FIELD_POSITION;
llvm::SmallVector<std::pair<mlir::Value, Kind>> 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.
Expand Down Expand Up @@ -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:
Expand Down
65 changes: 42 additions & 23 deletions tslang/lib/TypeScript/OwnershipInferencePlaceReads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<mlir_ts::PropertyRefOp>())
{
Expand All @@ -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))
Expand All @@ -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
Expand Down
150 changes: 150 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignaturePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<mlir::Attribute> 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) &&
Expand Down Expand Up @@ -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::FieldDrops> 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<mlir_ts::ReleaseOp>(op) && touchesHandle(op))
{
drops.any = true;
}
else if (auto releaseSlotOp = mlir::dyn_cast<mlir_ts::ReleaseSlotOp>(op))
{
auto slot = releaseSlotOp.getSlot();
if (slot.getDefiningOp<mlir_ts::AddressOfOp>() || 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<mlir_ts::SharedValueRefOp>())
{
drops.any = true;
return;
}

int64_t position = -1;
if (auto propertyRefOp = slot.getDefiningOp<mlir_ts::PropertyRefOp>())
{
position = propertyRefOp.getPosition();
}
else if (isInterfaceField(slot))
{
position = OWN_INTERFACE_FIELD_POSITION;
}

drops.add({position, slot.getType()});
}
}
else if (mlir::isa<mlir_ts::ArrayPopOp, mlir_ts::ArrayShiftOp, mlir_ts::ArraySpliceOp, mlir_ts::SetLengthOfOp>(op))
{
drops.any |= reachesOutside(op->getOperand(0), funcOp);
}
else if (mlir::isa<mlir_ts::ArrayPushOp, mlir_ts::ArrayUnshiftOp>(op))
{
drops.any |= reachesSharedValue(op->getOperand(0));
}
else if (mlir::isa<mlir_ts::DeleteOp>(op))
{
drops.any = true;
}
});

return drops;
}

bool OwnershipSignaturePass::callMayDrop(const Callees &callees)
Expand Down
30 changes: 30 additions & 0 deletions tslang/lib/TypeScript/OwnershipSignaturePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,36 @@ class OwnershipSignaturePass : public mlir::PassWrapper<OwnershipSignaturePass,
// Functions that destroy nothing their caller can reach.
llvm::DenseSet<mlir::Operation *> 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<std::pair<int64_t, mlir::Type>, 4> places;

bool add(std::pair<int64_t, mlir::Type> place)
{
if (llvm::is_contained(places, place))
{
return false;
}

places.push_back(place);
return true;
}
};

llvm::DenseMap<mlir::Operation *, FieldDrops> 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<FieldDrops> calleesFieldDrops(const Callees &callees);

// A class's vtable is a global named `<class>..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.
Expand Down
Loading
Loading