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
Merged
+378
−203
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -778,6 +778,17 @@ struct IRBuilder::ChildPopper | |
| ConstraintCollector{builder, children}.visitStackSwitch(curr, ct); | ||
| return popConstrainedChildren(children); | ||
| } | ||
|
|
||
| Result<std::vector<Expression*>> popInputs(Type inputType) { | ||
| std::vector<Expression*> inputs(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]}}); | ||
| } | ||
| CHECK_ERR(popConstrainedChildren(children)); | ||
| return inputs; | ||
| } | ||
| }; | ||
|
|
||
| Result<> IRBuilder::visit(Expression* curr) { | ||
|
|
@@ -877,6 +888,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()) { | ||
| auto inputs = ChildPopper{*this}.popInputs(scope.inputType); | ||
| CHECK_ERR(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 +1259,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 +1269,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 +1426,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 +1447,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 +1460,22 @@ 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); | ||
| ASSERT_OK(tupleScratch); | ||
| 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; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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<>.