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
20 changes: 19 additions & 1 deletion tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,25 @@ class MLIRGenImpl
{
return isOwnedLocalSlot(reference) || isCapturedVariableCell(reference) ||
isCapturedCellSlot(reference) || isOwnedGlobalSlot(location, reference) ||
isOwnedFieldSlot(location, reference) || isOwnedElementSlot(location, reference);
isOwnedFieldSlot(location, reference) || isOwnedElementSlot(location, reference) ||
isOwnedInterfaceFieldSlot(location, reference);
}

// A field reached through an interface: it belongs to the object behind the interface, which releases
// it - a class instance through its release routine, an object literal through the block it was boxed
// into, which retained its fields (castTupleToInterface). So `i.o = x` hands the count over as `obj.o = x`
// does. It did not: the old value was never released, and the new one kept no reference of its own, so
// its block was freed at the end of the producer's block while the field still pointed at it.
bool isOwnedInterfaceFieldSlot(mlir::Location location, mlir::Value reference)
{
auto interfaceSymbolRefOp = reference.getDefiningOp<mlir_ts::InterfaceSymbolRefOp>();
if (!interfaceSymbolRefOp)
{
return false;
}

auto refType = dyn_cast<mlir_ts::RefType>(reference.getType());
return refType && mth.ownsHeapMemory(location, refType.getElementType());
}

// One `using` declaration's `[Symbol.dispose]()`, at one scope exit.
Expand Down
51 changes: 51 additions & 0 deletions tslang/test/tester/tests/00owned_interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,55 @@ function interfacesInALoop() {
return total + last.v.x;
}

// A store through an interface hands the count over as a store into the field itself does: the
// object behind the interface owns the field. It did not - the replaced value was never released,
// and the stored one kept no reference of its own, so it was freed at the end of the storing
// function while the field still pointed at it.
class Box {
v: Vec;

constructor(v: Vec) {
this.v = v;
}
}

// churn() frees each filler at once, so it can keep reusing one block; keeping them makes the
// allocations spread over the blocks just freed, which is what finds this one
let fillers: Vec[] = [];

function churnKeeping() {
for (let i = 0; i < 256; i++) {
fillers.push(new Vec(999));
}
}

function storeThroughInterface(): Box {
let box = new Box(new Vec(1));
let h: Holder = box;
h.v = new Vec(31);
return box;
}

function storedThroughInterfaceSurvives() {
let box = storeThroughInterface();
churnKeeping();

return box.v.x;
}

function storeIntoBoxedLiteral(): Holder {
let h: Holder = { v: new Vec(1) };
h.v = new Vec(37);
return h;
}

function storedIntoBoxedLiteralSurvives() {
let h = storeIntoBoxedLiteral();
churnKeeping();

return h.v.x;
}

function main() {
assert(returnedThroughInterface() == 7, "a literal boxed as an interface survives its maker");
assert(argumentNeverBound() == 11, "an interface argument nothing bound survives the call");
Expand All @@ -165,6 +214,8 @@ function main() {
assert(classThroughInterface() == 46, "a class behind an interface keeps its own owner");
assert(nullInterfaceIsInert() == 29, "a null interface releases nothing");
assert(interfacesInALoop() == 44, "a loop's interface temporaries do not disturb what it carries out");
assert(storedThroughInterfaceSurvives() == 31, "a value stored through an interface is owned by the object");
assert(storedIntoBoxedLiteralSurvives() == 37, "a value stored through an interface into a boxed literal is owned by it");

print("done.");
}
Loading