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
8 changes: 7 additions & 1 deletion src/passes/ConstraintAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "cfg/cfg-traversal.h"
#include "ir/constraint.h"
#include "ir/drop.h"
#include "ir/eh-utils.h"
#include "ir/literal-utils.h"
#include "ir/local-graph.h"
#include "ir/properties.h"
Expand Down Expand Up @@ -192,14 +193,19 @@ struct ConstraintAnalysis
optimizeExpression(currp, constraints);
} else {
// This is unreachable code: just mark it so.
*currp = Builder(*getModule()).makeUnreachable();
*currp = getDroppedChildrenAndAppend(
*currp,
*getModule(),
getPassOptions(),
Builder(*getModule()).makeUnreachable());
refinalize = true;
}
}
}

if (refinalize) {
ReFinalize().walkFunctionInModule(getFunction(), getModule());
EHUtils::handleBlockNestedPops(getFunction(), *getModule());
}
}

Expand Down
78 changes: 78 additions & 0 deletions test/lit/passes/constraint-analysis-eh.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.

;; RUN: wasm-opt %s --constraint-analysis -all -S -o - | filecheck %s

(module
;; CHECK: (tag $tag (type $0) (param i32))
(tag $tag (param i32))

;; CHECK: (func $pop-unreachable (type $1)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (try
;; CHECK-NEXT: (do
;; CHECK-NEXT: )
;; CHECK-NEXT: (catch $tag
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (pop i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $pop-unreachable
(local $x i32)
(try
(do
)
(catch $tag
;; This code is unreachable: no throw exists that can reach it. However, it
;; is invalid to remove a pop, as that would not validate, so we keep it. We
;; also keep the set, as it may be needed for non-nullable local validation.
(local.set $x
(pop i32)
)
)
)
)

;; CHECK: (func $pop-unreachable-result (type $2) (result i32)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (try (result i32)
;; CHECK-NEXT: (do
;; CHECK-NEXT: (i32.const 10)
;; CHECK-NEXT: )
;; CHECK-NEXT: (catch $tag
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (pop i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 20)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $pop-unreachable-result (result i32)
(local $x i32)
;; As above, but now with a result. This adds a block we need to fix up EH
;; for.
(try (result i32)
(do
(i32.const 10)
)
(catch $tag
(local.set $x
(pop i32)
)
(i32.const 20)
)
)
)
)

21 changes: 18 additions & 3 deletions test/lit/passes/constraint-analysis.wast
Original file line number Diff line number Diff line change
Expand Up @@ -3476,17 +3476,32 @@
;; CHECK: (func $unreachable-loop (type $1)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (loop $loop (result i32)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; OPTIN: (func $unreachable-loop (type $1)
;; OPTIN-NEXT: (local $x i32)
;; OPTIN-NEXT: (unreachable)
;; OPTIN-NEXT: (unreachable)
;; OPTIN-NEXT: (block
;; OPTIN-NEXT: (local.set $x
;; OPTIN-NEXT: (loop $loop (result i32)
;; OPTIN-NEXT: (i32.const 0)
;; OPTIN-NEXT: )
;; OPTIN-NEXT: )
;; OPTIN-NEXT: (unreachable)
;; OPTIN-NEXT: )
;; OPTIN-NEXT: )
(func $unreachable-loop
(local $x i32)
;; The entire loop is unreachable. The control flow here should not cause any
;; internal errors, and we can just optimize this to unreachable.
;; internal errors, and we can just optimize this to unreachable (though we
;; keep the local.set, because of non-nullable local validation).
Comment on lines +3503 to +3504

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see the unreachable that was appended, but where are the dropped children? And what's the connection to non-nullable local validation here? The i32 local is not non-nullable.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

getDroppedChildren handles effects in pops and local.set etc., by keeping them around if necessary, with their children.

Good point that it might not need to do so for locals that are not non-nullable. But it isn't important to optimize that here, I think - other passes will.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(getDroppedChildren has a flag, the last parameter, that controls whether to ignore parent effects or not. Here we use the default, which does not ignore them)

(unreachable)
(local.set $x
(loop $loop (result i32)
Expand Down
Loading