Skip to content
Open
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
31 changes: 25 additions & 6 deletions src/ir/linear-execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ struct LinearExecutionWalker : public PostWalker<SubType, VisitorType> {
static void scan(SubType* self, Expression** currp) {
Expression* curr = *currp;

auto handleCall = [&](bool isReturn) {
auto handleCall = [&](bool mayThrow, bool isReturn) {
if (!self->connectAdjacentBlocks) {
// Control is nonlinear if we return, or if EH is enabled or may be.
if (isReturn || !self->getModule() ||
self->getModule()->features.hasExceptionHandling()) {
// Control is nonlinear if we return or throw.
if (isReturn || !self->getModule() || mayThrow) {
self->pushTask(SubType::doNoteNonLinear, currp);
}
}
Expand Down Expand Up @@ -153,11 +152,31 @@ struct LinearExecutionWalker : public PostWalker<SubType, VisitorType> {
break;
}
case Expression::Id::CallId: {
handleCall(curr->cast<Call>()->isReturn);
auto* call = curr->cast<Call>();

bool mayThrow = !self->getModule() ||
self->getModule()->features.hasExceptionHandling();
if (mayThrow && self->getModule()) {
auto* effects =
self->getModule()->getFunction(call->target)->effects.get();

if (effects && !effects->throws_) {
mayThrow = false;
}
}

handleCall(mayThrow, call->isReturn);
return;
}
case Expression::Id::CallRefId: {
handleCall(curr->cast<CallRef>()->isReturn);
auto* callRef = curr->cast<CallRef>();

// TODO: Effect analysis for indirect calls isn't implemented yet.
// Assume any indirect call my throw for now.
bool mayThrow = !self->getModule() ||
self->getModule()->features.hasExceptionHandling();

handleCall(mayThrow, callRef->isReturn);
return;
}
case Expression::Id::TryId: {
Expand Down
57 changes: 57 additions & 0 deletions test/lit/passes/simplify-locals-global-effects-eh.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: foreach %s %t wasm-opt --enable-exception-handling --generate-global-effects --simplify-locals -S -o - | filecheck %s

(module
;; CHECK: (global $g (mut i32) (i32.const 0))

;; CHECK: (tag $t (type $0))
(tag $t)

(global $g (mut i32) (i32.const 0))

;; CHECK: (func $nop
;; CHECK-NEXT: )
(func $nop
)

;; CHECK: (func $throws
;; CHECK-NEXT: (throw $t)
;; CHECK-NEXT: )
(func $throws
(throw $t)
)

;; CHECK: (func $read-g (result i32)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (call $nop)
;; CHECK-NEXT: (global.get $g)
;; CHECK-NEXT: )
(func $read-g (result i32)
(local $x i32)
(local.set $x (global.get $g))

;; With --global-effects, we can tell that this doesn't throw, so it
;; doesn't act as a barrier to optimize. The local is optimized away.
(call $nop)
(local.get $x)
)

;; CHECK: (func $read-g-with-throw-in-between (result i32)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (global.get $g)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $throws)
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
(func $read-g-with-throw-in-between (result i32)
(local $x i32)
(local.set $x (global.get $g))

;; A potential throw halts our optimizations.
(call $throws)

(local.get $x)
)
)
Loading