Skip to content
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

[NFC] Finalize blocks with explicit breakability in IRBuilder #7085

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions src/passes/Outlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ struct Outlining : public Pass {
// Position the outlined functions first in the functions vector to make
// the outlining lit tests far more readable.
moveOutlinedFunctions(module, substrings.size());

// Because we visit control flow in an odd order, IRBuilder is not able to
// properly track branches, so it may not have finalized blocks with the
// correct types. ReFinalize now to fix any issues.
PassRunner runner(getPassRunner());
runner.add(std::make_unique<ReFinalize>());
runner.run();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment sort of suggests to me that Outlining should not be using IRBuilder...? What does "an odd order" mean?

lgtm otherwise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outlining visits control flow expressions in a breadth-first order rather than doing a normal postorder traversal. So it visits every top-level expression in a function body, ends the function, then visits the contents of every block it saw while traversing those top-level expressions, etc. Another way to look at it is that Outlining uses IRBuilder to build one control flow structure at a time rather than a whole function at at a time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This "odd" traversal should be well documented in passes/stringify-walker.h.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sounds good. Maybe add a link to there?

}

Name addOutlinedFunction(Module* module,
Expand Down
12 changes: 8 additions & 4 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,12 @@ Result<> IRBuilder::visitEnd() {
block->type = blockType;
return block;
}
return builder.makeBlock(label, {curr}, blockType);
auto* block = builder.makeBlock();
block->name = label;
block->list.push_back(curr);
block->finalize(blockType,
scope.labelUsed ? Block::HasBreak : Block::NoBreak);
return block;
};

if (auto* func = scope.getFunction()) {
Expand All @@ -985,9 +990,8 @@ Result<> IRBuilder::visitEnd() {
} else if (auto* block = scope.getBlock()) {
assert(*expr == block);
block->name = scope.label;
// TODO: Track branches so we can know whether this block is a target and
// finalize more efficiently.
block->finalize(block->type);
block->finalize(block->type,
scope.labelUsed ? Block::HasBreak : Block::NoBreak);
push(block);
} else if (auto* loop = scope.getLoop()) {
loop->body = *expr;
Expand Down
Loading