Skip to content

Don't generate redundant unreachables in Flatten #2583

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion scripts/fuzz_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

INPUT_SIZE_LIMIT = 150 * 1024

LOG_LIMIT = 125
LOG_LIMIT = 500


# utilities
Expand Down
35 changes: 35 additions & 0 deletions src/ir/flat.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ inline bool isControlFlowStructure(Expression* curr) {
curr->is<Try>();
}

// Returns true if the given control flow structure preserves all its childrens'
// preludes within it. For example, blocks satisfy this property, because when
// the original block is in the form of
// (block
// (some expression)
// ...
// )
// and we replace (some expression) with a local.get whose prelude is (some
// expression), the final block will be in the form of
// (block
// (some expression)
// (local.get ...)
// ...
// )
// So the block's children's preludes do not escape the boundary of the block.
// 'if' does not satisfy this property, because the prelude of its condition
// ends up preceding (= escaping) the 'if'. For example, if the original 'if' is
// in the form of
// (if
// (some expression)
// ...
// )
// and (some expression) is replaced with a local.get whose prelude is (some
// expression), the final 'if' will be in the form of
// (some expression)
// (if
// (local.get ...)
// ...
// )
// So 'if''s condition's preludes escapes the boundary of 'if'. Refer to Flatten
// pass for detailed algorithms.
inline bool containsChildrensPreludes(Expression* curr) {
return curr->is<Block>() || curr->is<Loop>() || curr->is<Try>();
}

inline void verifyFlatness(Function* func) {
struct VerifyFlatness
: public PostWalker<VerifyFlatness,
Expand Down
10 changes: 7 additions & 3 deletions src/passes/Flatten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ struct Flatten

if (br->condition) {
// the value must also flow out
ourPreludes.push_back(br);
Expression* parent = getParent();
if (br->type.isConcrete()) {
ourPreludes.push_back(br);
replaceCurrent(builder.makeLocalGet(temp, type));
} else {
} else if (parent && !Flat::containsChildrensPreludes(parent)) {
ourPreludes.push_back(br);
assert(br->type == Type::unreachable);
replaceCurrent(builder.makeUnreachable());
}
Expand Down Expand Up @@ -277,7 +279,9 @@ struct Flatten
curr = getCurrent(); // we may have replaced it
// we have changed children
ReFinalizeNode().visit(curr);
if (curr->type == Type::unreachable) {
Expression* parent = getParent();
if (curr->type == Type::unreachable && !curr->is<Unreachable>() && parent &&
!Flat::containsChildrensPreludes(parent)) {
ourPreludes.push_back(curr);
replaceCurrent(builder.makeUnreachable());
} else if (curr->type.isConcrete()) {
Expand Down
1 change: 0 additions & 1 deletion test/passes/flatten.bin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
(nop)
(unreachable)
)
(unreachable)
)
(func $9 (; 9 ;) (param $0 i64) (param $1 f32) (param $2 f64) (param $3 i32) (param $4 i32) (result f64)
(local $5 i64)
Expand Down
Loading