Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
119bd34
-mm=rc: an argument read out of a place a closure may overwrite is he…
ASDAlexander77 Nov 24, 2026
3fc0e02
-mm=own: an array literal of no values given straight to a call is gi…
ASDAlexander77 Nov 24, 2026
6aeec02
rc/own: conditionals and pattern defaults are settled in a module wit…
ASDAlexander77 Nov 24, 2026
2495d6f
`x == null` of a value that may be undefined does not narrow it to null
ASDAlexander77 Nov 24, 2026
9dddc3d
-mm=own: a `let` a pattern binds with a default can be given another …
ASDAlexander77 Nov 24, 2026
c42b795
test: pin that --own-fallback=count reads OwnershipModuleFactsPass's …
ASDAlexander77 Nov 24, 2026
ede5658
--own-fallback=count: a function that fails reports no count beside i…
ASDAlexander77 Nov 24, 2026
4c91b51
test: --own-fallback=count against the default library
ASDAlexander77 Nov 24, 2026
c7861e9
-mm=own: a kept parameter is released where an exception leaves throu…
ASDAlexander77 Nov 24, 2026
09c3aff
Deterministic IR: landing pad ids per function, stable fields sorted,…
ASDAlexander77 Nov 24, 2026
330ed79
rc/own: a concise arrow gives back what its destructured parameter ow…
ASDAlexander77 Nov 24, 2026
6d6297a
-mm=own: a borrowed `N | null | undefined` narrowed by a loose `!= nu…
ASDAlexander77 Nov 24, 2026
587b2ab
-mm=own: only a pattern default's merge is a borrow of its own; other…
ASDAlexander77 Nov 24, 2026
a4ab109
OwnedReturnConsumptionPass: say that writesPlace is deliberately coarse
ASDAlexander77 Nov 24, 2026
551a11e
-mm=own: a @value record narrowed by a loose != null is also narrowed…
ASDAlexander77 Nov 24, 2026
a2c6ac0
-mm=rc: hold an argument whose place another name may reach and the c…
ASDAlexander77 Nov 24, 2026
4fb0e60
-mm=rc: `a.length = n` and `delete` count as writes for the argument …
ASDAlexander77 Nov 24, 2026
8956cc9
-mm=own: an empty literal given to a keeping callee is moved, not als…
ASDAlexander77 Nov 24, 2026
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
7 changes: 6 additions & 1 deletion tslang/include/TypeScript/TypeScriptPassContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ struct TSContext
catchPadIdOf.beginRun();
finallyPadIdOf.beginRun();
inParentTryFinallyPad.beginRun();
// numbered from 1 in each function: the pass runs over functions on several threads, each copy with a
// context of its own, so a count carried over from the functions a thread lowered before numbered a
// function's pads differently from one compile to the next (nondeterministic IR); only a function's own pads
// name each other (Win32ExceptionPass)
nextCatchPadId = 0;
}

// options
Expand Down Expand Up @@ -165,7 +170,7 @@ struct TSContext
OpSideTable<int> catchesLeft{"catchesLeft"};
// Windows: the id a try's catch landing pad goes by, so that the pads of a try inside that
// catch clause can name it as the funclet they are nested in - see tagPad in TryOpLowering
// and ts.internal.eh_pad in Win32ExceptionPass. Ids are unique within the module.
// and ts.internal.eh_pad in Win32ExceptionPass. Ids are unique within the function (beginRun).
OpSideTable<int> catchPadIdOf{"catchPadIdOf"};
// Windows: the same for the pad a try's finally is entered through on the exceptional path,
// and whether a try sits in that copy of its parentTryOp's finally - the one copy that runs
Expand Down
41 changes: 23 additions & 18 deletions tslang/lib/TypeScript/MLIRGenClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2911,7 +2911,8 @@ namespace mlirgen
}

auto viewOp = builder.create<mlir_ts::SafeCastOp>(location, declaredType, castValue, loaded);
viewOp->setAttr(WEAK_FIELD_VIEW_ATTR_NAME, builder.getUnitAttr());
// (naming the field: a reread or a write of the view takes the name from here, weakFieldNameOf)
viewOp->setAttr(WEAK_FIELD_VIEW_ATTR_NAME, builder.getStringAttr(fieldName));
return viewOp;
}

Expand Down Expand Up @@ -2949,21 +2950,24 @@ namespace mlirgen
return V(result);
}

// The field a @weak field's view reads (weakFieldRead names it on the view): a reread or a write of the view is
// of the same field. It was taken from the checked read found by walking the first operand of what made the
// view's value, which did not always reach it - the field's name was then lost from one compile to the next
// (nondeterministic IR).
static mlir::StringAttr weakFieldNameOf(mlir::Operation *viewOp)
{
return viewOp->getAttrOfType<mlir::StringAttr>(WEAK_FIELD_VIEW_ATTR_NAME);
}

mlir::Value MLIRGenImpl::weakFieldReread(mlir::Location location, mlir_ts::SafeCastOp viewOp, mlir::Value loaded,
const GenContext &genContext)
{
auto weakRefType = mlir::cast<mlir_ts::WeakRefType>(loaded.getType());
auto derefOp = builder.create<mlir_ts::WeakRefDerefOp>(location, weakRefType.getElementType(), loaded);
derefOp.setChecked(true);
auto *below = viewOp->getOperand(0).getDefiningOp();
while (below && !mlir::isa<mlir_ts::WeakRefDerefOp>(below))
if (auto name = weakFieldNameOf(viewOp))
{
below = below->getNumOperands() > 0 ? below->getOperand(0).getDefiningOp() : nullptr;
}

if (below)
{
derefOp.setWeakFieldAttr(mlir::cast<mlir_ts::WeakRefDerefOp>(below).getWeakFieldAttr());
derefOp.setWeakFieldAttr(name);
}

auto castValue = weakFieldValue(location, derefOp.getResult(), viewOp.getType(), genContext);
Expand All @@ -2973,7 +2977,14 @@ namespace mlirgen
}

auto rereadOp = builder.create<mlir_ts::SafeCastOp>(location, viewOp.getType(), castValue, loaded);
rereadOp->setAttr(WEAK_FIELD_VIEW_ATTR_NAME, builder.getUnitAttr());
if (auto name = weakFieldNameOf(viewOp))
{
rereadOp->setAttr(WEAK_FIELD_VIEW_ATTR_NAME, name);
}
else
{
rereadOp->setAttr(WEAK_FIELD_VIEW_ATTR_NAME, builder.getUnitAttr());
}
return rereadOp;
}

Expand Down Expand Up @@ -3134,16 +3145,10 @@ namespace mlirgen
return mlir::Value();
}

auto derefOp = viewOp->getOperand(0).getDefiningOp();
while (derefOp && !mlir::isa<mlir_ts::WeakRefDerefOp>(derefOp))
{
derefOp = derefOp->getNumOperands() > 0 ? derefOp->getOperand(0).getDefiningOp() : nullptr;
}

auto weakRef = builder.create<mlir_ts::WeakRefNewOp>(location, weakRefType, target, mlir::StringAttr());
if (derefOp)
if (auto name = weakFieldNameOf(viewOp))
{
weakRef.setWeakFieldAttr(mlir::cast<mlir_ts::WeakRefDerefOp>(derefOp).getWeakFieldAttr());
weakRef.setWeakFieldAttr(name);
}

// born with no weak count: the retain the store adds is the first (as `new WeakRef(x)`)
Expand Down
15 changes: 13 additions & 2 deletions tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2705,6 +2705,15 @@ namespace mlirgen

GenContext bindingGenContext(genContext);
bindingGenContext.bindingParamPatterns = true;
// -mm=rc: a name a destructured parameter binds owns what it reads, as a local does: it borrowed the
// argument's element or field, which the callee may give a new value (`([o]: N[]) => { arr[0] =
// mk(i); o.x }`, `arr` the array given) - the store freed the block `o` read. Listed with the
// parameters' storage when the prologue is done (mlirGenFunctionBody), given back at exit.
SmallVector<mlir::Value> bindingOwned;
if (compileOptions.memoryModel == MemoryModelRC && !bindingGenContext.ownedVars)
{
bindingGenContext.ownedVars = &bindingOwned;
}
if (bindingPattern == SyntaxKind::ArrayBindingPattern)
{
auto arrayBindingPattern = bindingPattern.as<ArrayBindingPattern>();
Expand Down Expand Up @@ -2981,9 +2990,11 @@ namespace mlirgen
paramCells->push_back(prologueOp.getResult(0));
}
// ...and the slot owning a parameter's default value (processOptionalParam), or, under own, a default in a
// destructured parameter (ownPatternDefaultSlot), given back at exit
// destructured parameter (ownPatternDefaultSlot), or, under rc, a name a destructured parameter binds
// (mlirGenFunctionParamsBindings), given back at exit
else if (isa<mlir_ts::VariableOp>(prologueOp) &&
(prologueOp.hasAttr(DEFAULT_VALUE_SLOT_ATTR_NAME) || prologueOp.hasAttr(PATTERN_DEFAULT_SLOT_ATTR_NAME)))
(prologueOp.hasAttr(DEFAULT_VALUE_SLOT_ATTR_NAME) || prologueOp.hasAttr(PATTERN_DEFAULT_SLOT_ATTR_NAME) ||
(compileOptions.memoryModel == MemoryModelRC && prologueOp.hasAttr(OWNED_LOCAL_ATTR_NAME))))
{
paramCells->push_back(prologueOp.getResult(0));
}
Expand Down
119 changes: 119 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6307,6 +6307,109 @@ class MLIRGenImpl
return mlir::failure();
}

// `val == null` / `val == undefined` (loose) of a value whose type may be undefined (an optional, a union with
// undefined): the equal side may be either, and is left as it is; the other side is the type without null and
// undefined. Fails for anything else (the strict forms, a type that cannot be undefined), which the null and
// undefined cases narrow as before.
mlir::LogicalResult checkSafeCastLooseNullish(Expression val, Expression nullishVal, bool inverse, ElseSafeCase *elseSafeCase,
const GenContext &genContext)
{
auto expr = stripParentheses(nullishVal);
auto isNullish = expr == SyntaxKind::NullKeyword ||
(expr == SyntaxKind::Identifier && expr.as<ts::Identifier>()->escapedText == S(UNDEFINED_NAME));
if (!isNullish)
{
return mlir::failure();
}

auto type = evaluate(val, genContext);
auto mayBeUndefined = [](mlir::Type type) {
if (isa_and_nonnull<mlir_ts::OptionalType>(type))
{
return true;
}

auto unionType = dyn_cast_or_null<mlir_ts::UnionType>(type);
return unionType && llvm::any_of(unionType.getTypes(), [](mlir::Type member) {
return isa<mlir_ts::UndefinedType, mlir_ts::OptionalType>(member);
});
};
if (!mayBeUndefined(type))
{
return mlir::failure();
}

// not undefined: an optional's value, read as `c !== undefined` reads it (a `ts.Value`, which -mm=own sees as a
// view of a borrow; a cast to the type without null too was taken for a store of it, and own refused a
// borrowed `N | null | undefined` narrowed so). A null left in it reads as null, which is all `c.x` needs.
auto present = type;
if (auto optionalType = dyn_cast<mlir_ts::OptionalType>(present))
{
present = optionalType.getElementType();
}
else if (auto unionType = dyn_cast<mlir_ts::UnionType>(present))
{
SmallVector<mlir::Type> members;
for (auto member : unionType.getTypes())
{
if (auto optionalMember = dyn_cast<mlir_ts::OptionalType>(member))
{
member = optionalMember.getElementType();
}

if (!isa<mlir_ts::NullType, mlir_ts::UndefinedType>(member))
{
members.push_back(member);
}
}

if (members.empty())
{
return mlir::failure();
}

present = members.size() == 1 ? members.front() : getUnionType(members);
}

// `!=`: present where true - and, for a @value record whose null is a niche of its own (`Hit | null |
// undefined`), not null either, narrowed again from the value just read as `c !== null` narrows a union: its
// fields are read so (own refused a borrow of `this` stored in a `Hit` read as `Hit | null`). A class's null
// stays in it, its reads are the same either way, and a second narrowing of a field read (`h.v`) was taken
// for a store of the borrow
if (inverse)
{
if (mlir::failed(addSafeCastStatement(val, present, false, nullptr, genContext)))
{
return mlir::failure();
}

if (auto unionType = dyn_cast<mlir_ts::UnionType>(present);
unionType && llvm::any_of(unionType.getTypes(), [](mlir::Type member) { return isa<mlir_ts::NullType>(member); }) &&
llvm::any_of(unionType.getTypes(), [](mlir::Type member) { return isa<mlir_ts::ValueType>(member); }))
{
SmallVector<mlir::Type> members;
llvm::copy_if(unionType.getTypes(), std::back_inserter(members),
[](mlir::Type member) { return !isa<mlir_ts::NullType>(member); });
if (!members.empty())
{
return addSafeCastStatement(val, members.size() == 1 ? members.front() : getUnionType(members), false,
nullptr, genContext);
}
}

return mlir::success();
}

// `==`: present where false
if (elseSafeCase)
{
elseSafeCase->expr = stripParenthesesAndUntangleEquals(val);
elseSafeCase->safeType = present;
}

return mlir::success();
}

mlir::LogicalResult checkSafeCastNull(Expression val, Expression nullVal, bool inverse, ElseSafeCase *elseSafeCase, const GenContext &genContext)
{
auto expr = stripParentheses(nullVal);
Expand Down Expand Up @@ -6831,6 +6934,22 @@ class MLIRGenImpl
auto left = stripParentheses(binExpr->left);
auto right = stripParentheses(binExpr->right);

// `c == null`, `c != undefined` - loose - of a value that may be undefined: equal for null and for
// undefined alike, so the equal side is not narrowed to null (a later `c === undefined` in the same
// `&&` chain was false for undefined, `c === null` true); the other side is neither
if (op == SyntaxKind::EqualsEqualsToken || op == SyntaxKind::ExclamationEqualsToken)
{
if (auto result = checkSafeCastLooseNullish(left, right, inverse, elseSafeCase, genContext); mlir::succeeded(result))
{
return result;
}

if (auto result = checkSafeCastLooseNullish(right, left, inverse, elseSafeCase, genContext); mlir::succeeded(result))
{
return result;
}
}

// TODO: refactor it
// typeof
if (mlir::failed(checkSafeCastTypeOf(left, right, inverse, elseSafeCase, genContext)))
Expand Down
8 changes: 6 additions & 2 deletions tslang/lib/TypeScript/MLIRGenStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@ namespace mlirgen
mlirGenRetainCaptured(loc(body), mlir::ValueRange{resultValue}, /*atLastUse=*/true);

// ...but a slot owning a parameter's default value (-mm=rc, processOptionalParam) is given back
// here, as a return statement's scope exit gives it back: what follows the return is not reached
// here, as a return statement's scope exit gives it back: what follows the return is not reached.
// So are a destructured parameter's default slot (-mm=own, ownPatternDefaultSlot) and the names a
// destructured parameter binds (-mm=rc, mlirGenFunctionParamsBindings): `({ a, b }) => a + b`
// never gave them back (the ownership verifier: "some path out of the function never gives back")
if (genContext.ownedVars)
{
for (auto storage : llvm::reverse(*genContext.ownedVars))
{
auto varOp = storage.getDefiningOp<mlir_ts::VariableOp>();
if (varOp && varOp->hasAttr(DEFAULT_VALUE_SLOT_ATTR_NAME))
if (varOp && (varOp->hasAttr(DEFAULT_VALUE_SLOT_ATTR_NAME) || varOp->hasAttr(PATTERN_DEFAULT_SLOT_ATTR_NAME) ||
(compileOptions.memoryModel == MemoryModelRC && varOp->hasAttr(OWNED_LOCAL_ATTR_NAME))))
{
builder.create<mlir_ts::ReleaseSlotOp>(loc(body), storage)
->setAttr(SCOPE_EXIT_RELEASE_ATTR_NAME, builder.getUnitAttr());
Expand Down
Loading
Loading