Repository navigation
[IRBuilder] Use multiple scratch locals for inputs #9336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -778,6 +778,16 @@ struct IRBuilder::ChildPopper | |
| ConstraintCollector{builder, children}.visitStackSwitch(curr, ct); | ||
| return popConstrainedChildren(children); | ||
| } | ||
|
|
||
| Result<> popInputs(Type inputType, std::vector<Expression*>& inputs) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to mutate the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good idea. The current interface is consistent with |
||
| inputs.resize(inputType.size()); | ||
| std::vector<Child> children; | ||
| children.reserve(inputType.size()); | ||
| for (size_t i = 0; i < inputType.size(); ++i) { | ||
| children.push_back({&inputs[i], {inputType[i]}}); | ||
| } | ||
| return popConstrainedChildren(children); | ||
| } | ||
| }; | ||
|
|
||
| Result<> IRBuilder::visit(Expression* curr) { | ||
|
|
@@ -877,6 +887,42 @@ IRBuilder::visitTryTableStart(TryTable* trytable, Name label, Type inputType) { | |
| return pushScope(ScopeCtx::makeTryTable(trytable, label, inputType)); | ||
| } | ||
|
|
||
| Result<> IRBuilder::pushScope(ScopeCtx&& scope) { | ||
| if (auto label = scope.getOriginalLabel()) { | ||
| // Assign a fresh label to the scope, if necessary. | ||
| if (!scope.label) { | ||
| scope.label = makeFresh(label); | ||
| } | ||
| // Record the original label to handle references to it correctly. | ||
| labelDepths[label].push_back(scopeStack.size() + 1); | ||
| } | ||
| if (binaryPos) { | ||
| scope.startPos = lastBinaryPos; | ||
| lastBinaryPos = *binaryPos; | ||
| } | ||
| bool hasInput = scope.inputType != Type::none; | ||
| if (hasInput && !scope.isDelimiter()) { | ||
| std::vector<Expression*> inputs; | ||
| CHECK_ERR(ChildPopper{*this}.popInputs(scope.inputType, inputs)); | ||
| scope.inputLocals.reserve(inputs.size()); | ||
| for (size_t i = 0; i < inputs.size(); ++i) { | ||
| auto scratch = addScratchLocal(scope.inputType[i]); | ||
| CHECK_ERR(scratch); | ||
| scope.inputLocals.push_back(*scratch); | ||
| pushSynthetic(builder.makeLocalSet(*scratch, inputs[i])); | ||
| } | ||
| } | ||
| scopeStack.emplace_back(std::move(scope)); | ||
| if (hasInput) { | ||
| auto& newScope = getScope(); | ||
| for (size_t i = 0; i < newScope.inputLocals.size(); ++i) { | ||
| pushSynthetic( | ||
| builder.makeLocalGet(newScope.inputLocals[i], newScope.inputType[i])); | ||
| } | ||
| } | ||
| return Ok{}; | ||
| } | ||
|
|
||
| Result<Expression*> IRBuilder::finishScope(Block* block) { | ||
| #if IR_BUILDER_DEBUG | ||
| if (auto* loc = std::get_if<Function::DebugLocation>(&debugLoc)) { | ||
|
|
@@ -1212,8 +1258,8 @@ Result<> IRBuilder::visitEnd() { | |
| if (scope.inputType != Type::none && scope.labelUsed) { | ||
| // Branches to this loop carry values, but Binaryen IR does not support | ||
| // that. Fix this by trampolining the branches through new code that sets | ||
| // the branch value to the appropriate scratch local. | ||
| fixLoopWithInput(loop, scope.inputType, scope.inputLocal); | ||
| // the branch value to the appropriate scratch locals. | ||
| fixLoopWithInput(loop, scope.inputType, scope.inputLocals); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| loop->finalize(loop->type); | ||
| push(loop); | ||
|
|
@@ -1222,8 +1268,19 @@ Result<> IRBuilder::visitEnd() { | |
| if (scope.inputType != Type::none) { | ||
| // Normally an if without an else must have type none, but if there is an | ||
| // input parameter, the empty else arm must propagate its value. | ||
| // Synthesize an else arm that loads the value from the scratch local. | ||
| iff->ifFalse = builder.makeLocalGet(scope.inputLocal, scope.inputType); | ||
| // Synthesize an else arm that loads the value from the scratch locals. | ||
| if (scope.inputType.isSingle()) { | ||
| iff->ifFalse = | ||
| builder.makeLocalGet(scope.inputLocals[0], scope.inputType); | ||
| } else { | ||
| std::vector<Expression*> gets; | ||
| gets.reserve(scope.inputLocals.size()); | ||
| for (size_t i = 0; i < scope.inputLocals.size(); ++i) { | ||
| gets.push_back( | ||
| builder.makeLocalGet(scope.inputLocals[i], scope.inputType[i])); | ||
| } | ||
| iff->ifFalse = builder.makeTupleMake(gets); | ||
| } | ||
| } else { | ||
| iff->ifFalse = nullptr; | ||
| } | ||
|
|
@@ -1368,17 +1425,18 @@ IRBuilder::fixExtraOutput(ScopeCtx& scope, Name label, Expression* curr) { | |
| return curr; | ||
| } | ||
|
|
||
| // Branches to this loop need to be trampolined through code that sets the value | ||
| // carried by the branch to the appropriate scratch local before branching to | ||
| // the loop. Transform this: | ||
| // Branches to this loop need to be trampolined through code that sets the | ||
| // values carried by the branch to the appropriate scratch locals before | ||
| // branching to the loop. Transform this: | ||
| // | ||
| // (loop $l (param t1) (result t2) ...) | ||
| // | ||
| // to this: | ||
| // to this (or, when t1 is a tuple, using a tuple scratch local to extract each | ||
| // element into its corresponding input scratch local): | ||
| // | ||
| // (loop $l0 (result t2) | ||
| // (block $l1 (result t2) | ||
| // (local.set $scratch ;; set the branch values to the scratch local | ||
| // (local.set $scratch ;; set the branch value to the scratch local | ||
| // (block $l (result t1) | ||
| // (br $l1 ;; exit the loop with the fallthrough value, if any. | ||
| // ... ;; contains branches to $l | ||
|
|
@@ -1388,7 +1446,9 @@ IRBuilder::fixExtraOutput(ScopeCtx& scope, Name label, Expression* curr) { | |
| // (br $l0) ;; continue the loop | ||
| // ) | ||
| // ) | ||
| void IRBuilder::fixLoopWithInput(Loop* loop, Type inputType, Index scratch) { | ||
| void IRBuilder::fixLoopWithInput(Loop* loop, | ||
| Type inputType, | ||
| const std::vector<Index>& scratch) { | ||
| auto l = loop->name; | ||
| auto l0 = makeFresh(l, 0); | ||
| auto l1 = makeFresh(l, 1); | ||
|
|
@@ -1399,10 +1459,21 @@ void IRBuilder::fixLoopWithInput(Loop* loop, Type inputType, Index scratch) { | |
| loop->body, l, builder.makeBreak(l1), inputType) | ||
| : builder.makeBlock(l, {builder.makeBreak(l1, loop->body)}, inputType); | ||
|
|
||
| Block* outer = builder.makeBlock( | ||
| l1, | ||
| {builder.makeLocalSet(scratch, inner), builder.makeBreak(l0)}, | ||
| loop->type); | ||
| std::vector<Expression*> list; | ||
| if (inputType.isSingle()) { | ||
| list.push_back(builder.makeLocalSet(scratch[0], inner)); | ||
| } else { | ||
| auto tupleScratch = *addScratchLocal(inputType); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As Gemini mentioned we should check the error here
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add an assertion. It's impossible for |
||
| list.push_back(builder.makeLocalSet(tupleScratch, inner)); | ||
| for (Index i = 0; i < scratch.size(); ++i) { | ||
| list.push_back(builder.makeLocalSet( | ||
| scratch[i], | ||
| builder.makeTupleExtract(builder.makeLocalGet(tupleScratch, inputType), | ||
| i))); | ||
| } | ||
| } | ||
| list.push_back(builder.makeBreak(l0)); | ||
| Block* outer = builder.makeBlock(l1, list, loop->type); | ||
|
|
||
| loop->body = outer; | ||
| loop->name = l0; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
fixLoopWithInputnow allocates a new scratch local usingaddScratchLocal(which returns aResult<Index>), it can fail. To handle this failure robustly and avoid unchecked dereferencing of theResult, change the return type offixLoopWithInputfromvoidtoResult<>.