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
34 changes: 31 additions & 3 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2177,9 +2177,37 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT,
whose next() takes no value is a Generator whose next takes one, and the other way round. The library's
`Generator` declares `next: (value?: TNext)` once a compiler with this is released (a released one without it
can't give a plain generator as one).
- return(value) and throw(e) of a generator typed `Generator<T, ...>`, as JavaScript's: paused at a yield, it
resumes there finishing (`return`, so its finally blocks run) or throwing e (its own try/catch may catch it, and
it goes on to its next yield); one not started yet, or finished, does not run - return(value) gives `{ value,
done: true }`, throw(e) throws e. The generator object holds a mode its yields check where they resume
(addGeneratorResumeMethods, mlirGenGeneratorResumeChecks). Under -mm=own return() only for a T that owns
nothing (a number, a boolean): its result would hold the value the caller lends. The library's `Generator`
declares them optional (`return?`, `throw?`: a generator declared otherwise has neither) once a compiler with
them is released. Under -mm=own a call through an optional one is read as moving the generator, so there a
program declaring them required calls them.
- Found on the way, generators and `try` (all memory models):
- A throw or a call after a resumed yield inside a `try` went past it - its catch did not catch, its finally did
not run (`try { yield 1; throw "x"; } catch (e) {...}`: "uncaught exception"). A yield splits the body at its
label, and only the body's first block got unwind edges (TryOpLowering: `continue` in a `do ... while
(false)`).
- A throw after a yield in a loop in a `try` did not compile ("operand #0 does not dominate this use" at the
`try`): the catch-type constants the landing pad reads were made at the start of the body, which a resumed
turn does not pass - in a generator they go to the function's entry.
- A `let` in a `try` read after a yield did not compile, the same error: its storage was put in front of the
try, not of the generator's switch (GenContext::generatorSwitchOperation).
- A catch variable of a generator was a field of the generator object, and its use crashed: the catch handler
copies the exception into a slot of the frame. It is a local of the frame now, living in one turn.
- A yield in a catch block is an error at the yield: resuming there is resuming inside the exception handler
(it crashed in LLVM's instruction selection; before the fixes above the catch did not catch).

Still open, looked at:
- return(v) / throw(e) of a generator.
- -mm=own: an object literal's field of `<any>undefined` or `<any>null` (`{ t: <any>undefined, n: 1 }`) is
released twice: OwnershipInferencePass does not count the store of its box into the object as a move (a box of
`<any>5` it does), so the release at the end of the function stays and the object's release frees the box again.
wasm's heap caught it ("free of ...: not a block the heap gave out"), and Linux under -mm=own; Windows did not.
The generator object's `.thrown` holds `<any>0` for that reason (its first version was `<any>undefined`).
- A yield in a finally block is skipped (`try { yield 1; } finally { yield 2; }` gives 1 and not 2), as before.
- -mm=own: next(value) of a generator sending it an object (`Generator<T, void, Box>`) is refused ("'this value'
is a parameter, kept here, but -mm=own cannot make the callers of this function expression give it up"): next()
keeps what it is given, and a method used other than by a call can't make its callers give it up. A number or a
Expand All @@ -2194,5 +2222,5 @@ Still open, looked at:
`[Symbol.asyncIterator]` of an async generator.

Tests: `00iterables.ts`, `00generator_typed_yield.ts` (compile, JIT, rc),
diagnostic-location `generator_return_of_other_type.ts`; `00generator_next_value.ts`, `00object_method_arity.ts`
(compile, JIT, rc, own).
diagnostic-location `generator_return_of_other_type.ts`; `00generator_next_value.ts`, `00object_method_arity.ts`,
`00generator_return_throw.ts` (compile, JIT, rc, own), diagnostic-location `generator_yield_in_catch.ts`.
4 changes: 4 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@
// next(value) of a generator typed Generator<T, TReturn, TNext>: the value, the parameter of its next(), is what
// the `yield` it resumes evaluates to
#define GENERATOR_SENT_NAME ".sent"
// return(v) / throw(e) of a generator typed Generator<...>: how the paused generator resumes - 0 as next() asks,
// 1 throwing GENERATOR_THROWN_NAME at its yield, 2 returning there (its finally blocks run)
#define GENERATOR_MODE_NAME ".mode"
#define GENERATOR_THROWN_NAME ".thrown"
#define OBJECT_DICTIONARY_CLASS_NAME "ObjectDictionary"
// the default library's function a spread of a dictionary into a new one calls
#define OBJECT_DICTIONARY_ASSIGN_NAME "objectDictionaryAssign"
Expand Down
1 change: 1 addition & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ class MLIRCustomMethods
// __switchstate call in the caller's scope - see GenContext
const_cast<GenContext &>(genContext).allocateVarsOutsideOfOperation = true;
const_cast<GenContext &>(genContext).currentOperation = switchStateOp;
const_cast<GenContext &>(genContext).generatorSwitchOperation = switchStateOp;

return mlir::success();
}
Expand Down
5 changes: 5 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct GenContext
unwindCleanupOp = nullptr;

currentOperation = nullptr;
generatorSwitchOperation = nullptr;
allocateVarsOutsideOfOperation = false;
allocateScopeOwnedVarsOutsideOfOperation = false;
}
Expand Down Expand Up @@ -200,6 +201,10 @@ struct GenContext
bool discoverParamsOnly = false;
bool insertIntoParentScope = false;
mlir::Operation *currentOperation = nullptr;
// a generator's `__switchstate` (armed with currentOperation): a local of its body is a field of the
// generator object, and its storage goes in front of the switch, which every resume point follows - not in
// front of a `try` it is declared in (currentOperation there), which the resume points inside it don't
mlir::Operation *generatorSwitchOperation = nullptr;
mlir_ts::FuncOp funcOp;
// funcOp's `return` statements evaluate their value and drop it (an arrow given to a function
// type that returns nothing, see FunctionPrototypeDOM::discardsReturnValue)
Expand Down
42 changes: 34 additions & 8 deletions tslang/lib/TypeScript/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,21 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
auto module = tryOp->getParentOfType<mlir::ModuleOp>();
auto parentTryOp = tsContext->parentTryOp.lookup(tryOp.getOperation());

// a generator's next(): its switch resumes turns in the middle of the body (see "Body:catch vars")
mlir::Block *generatorEntryBlock = nullptr;
if (auto funcOp = tryOp->getParentOfType<mlir_ts::FuncOp>())
{
auto resumes = funcOp->walk([](mlir::Operation *op) {
return isa<mlir_ts::SwitchStateOp, mlir_ts::SwitchStateInternalOp, mlir_ts::StateLabelOp>(op)
? WalkResult::interrupt()
: WalkResult::advance();
});
if (resumes.wasInterrupted())
{
generatorEntryBlock = &funcOp.getBody().front();
}
}

// Where an exception this try does not catch has to go next within the function: the
// parent's landing pad for a try in the parent's body, the parent's finally for one in
// the parent's catch clause (whose exceptions the parent does not catch, but whose
Expand Down Expand Up @@ -2190,20 +2205,31 @@ struct TryOpLowering : public TsPattern<mlir_ts::TryOp>
tsFuncContext->accessorUnwinds.push_back({op, landingBlock});
}
};
auto it = bodyBlock;
do
// every block of the body: a generator's yield splits it at its label, and a throw or a call after a
// resumed yield went past the try (only the first block was walked - `continue` in a `do ... while
// (false)` ends it): its finally did not run, its catch did not catch
for (auto it = bodyBlock;; it = it->getNextNode())
{
it->walk(visitorCallOpContinue);
if (it != bodyBlockLast)
if (it == bodyBlockLast)
{
it = it->getNextNode();
continue;
break;
}
} while (false);
}
}

// Body:catch vars
rewriter.setInsertionPointToStart(bodyBlock);
// Body:catch vars - constants the pads below read. In a generator's next() they go to the function's entry:
// a turn resuming inside the body (its switch jumps to the yield's label) does not pass the body's start,
// and the pad a throw there unwinds to read a constant that did not dominate it ("operand #0 does not
// dominate this use" at the `try`)
if (generatorEntryBlock)
{
rewriter.setInsertionPointToStart(generatorEntryBlock);
}
else
{
rewriter.setInsertionPointToStart(bodyBlock);
}
auto catch1 = rttih.hasType()
? (mlir::Value)rttih.typeInfoPtrValue(loc)
: /*catch all*/ (mlir::Value)rewriter.create<mlir_ts::NullOp>(loc, mth.getNullType());
Expand Down
15 changes: 15 additions & 0 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,14 @@ namespace mlirgen
return mlir::failure();
}

if (!!(yieldExpressionAST->internalFlags & InternalFlags::YieldInCatch))
{
emitError(location, "a yield in a catch block of a generator is not supported: the generator can't resume "
"inside the block's exception handler; keep what the catch needs in a variable and "
"yield after the try statement");
return mlir::failure();
}

if (yieldExpressionAST->asteriskToken)
{
// it gives back nothing yet: it can only stand as a statement of its own
Expand Down Expand Up @@ -513,6 +521,13 @@ namespace mlirgen
label << GENERATOR_STATELABELPREFIX << state;
builder.create<mlir_ts::StateLabelOp>(location, label.str());

// resumed by return() or throw() of a generator typed Generator<...>: it finishes or throws here
if (!!(yieldExpressionAST->internalFlags & InternalFlags::GeneratorResumable)
&& evaluateProperty(nf.createToken(SyntaxKind::ThisKeyword), GENERATOR_MODE_NAME, genContext))
{
EXIT_IF_FAILED(mlirGenGeneratorResumeChecks(location, genContext));
}

// The value passed to next(value) - next()'s parameter, which a generator typed Generator<T, TReturn,
// TNext> has - or undefined. It is read after the label: the turn that resumes here is the one that uses it.
if (auto sent = resolveIdentifier(location, GENERATOR_SENT_NAME, genContext))
Expand Down
Loading
Loading