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
48 changes: 40 additions & 8 deletions src/ir/linear-execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ 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. Traps don't need to be
// taken into account since they don't break control flow in a way
// that's observable.
if (mayThrow || isReturn) {
self->pushTask(SubType::doNoteNonLinear, currp);
}
}
Expand Down Expand Up @@ -153,12 +154,43 @@ struct LinearExecutionWalker : public PostWalker<SubType, VisitorType> {
break;
}
case Expression::Id::CallId: {
handleCall(curr->cast<Call>()->isReturn);
return;
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);
break;
}
case Expression::Id::CallRefId: {
handleCall(curr->cast<CallRef>()->isReturn);
return;
auto* callRef = curr->cast<CallRef>();

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

handleCall(mayThrow, callRef->isReturn);
Comment thread
stevenfontanella marked this conversation as resolved.
break;
}
case Expression::Id::CallIndirectId: {
auto* callIndirect = curr->cast<CallIndirect>();

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

handleCall(mayThrow, callIndirect->isReturn);
break;
}
case Expression::Id::TryId: {
self->pushTask(SubType::doVisitTry, currp);
Expand Down
163 changes: 163 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,163 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: foreach %s %t wasm-opt --closed-world --enable-exception-handling --enable-gc --enable-reference-types --generate-global-effects --simplify-locals -S -o - | filecheck %s

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

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

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

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

;; CHECK: (func $read-g (type $1) (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 (type $1) (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)
)
)
Comment thread
stevenfontanella marked this conversation as resolved.

(module
;; CHECK: (type $const-type (func (result f32)))
(type $const-type (func (result f32)))

;; CHECK: (type $throw-type (func (result f64)))
(type $throw-type (func (result f64)))

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

;; CHECK: (table $t 2 2 funcref)
(table $t 2 2 funcref)

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

;; CHECK: (func $const (type $const-type) (result f32)
;; CHECK-NEXT: (f32.const 1)
;; CHECK-NEXT: )
(func $const (type $const-type)
(f32.const 1)
)
(elem declare $const)


;; CHECK: (func $throws (type $throw-type) (result f64)
;; CHECK-NEXT: (throw $t)
;; CHECK-NEXT: (f64.const 1)
;; CHECK-NEXT: )
(func $throws (type $throw-type)
(throw $t)
(f64.const 1)
)
(elem declare $throws)

;; CHECK: (func $read-g (type $3) (param $ref (ref null $const-type)) (result i32)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (global.get $g)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call_ref $const-type
;; CHECK-NEXT: (local.get $ref)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
(func $read-g (param $ref (ref null $const-type)) (result i32)
(local $x i32)
(local.set $x (global.get $g))

;; With more precise effect analysis for indirect calls, we can determine
;; that the only possible target for this ref is $const in a closed world,
;; which wouldn't block our optimizations.
;; TODO: Add effects analysis for indirect calls.
(drop (call_ref $const-type (local.get $ref)))

(local.get $x)
)

;; CHECK: (func $read-g-with-throw-in-between (type $4) (param $ref (ref $throw-type)) (result i32)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (global.get $g)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call_ref $throw-type
;; CHECK-NEXT: (local.get $ref)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
(func $read-g-with-throw-in-between (param $ref (ref $throw-type)) (result i32)
(local $x i32)
(local.set $x (global.get $g))

;; Similar to above, except here we can tell that the indirect call may
;; throw so optimization is halted.
(drop (call_ref $throw-type (local.get $ref)))

(local.get $x)
)

;; CHECK: (func $read-g-with-call-indirect-in-between (type $5) (result i32)
;; CHECK-NEXT: (local $x i32)
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (global.get $g)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call_indirect $t (type $const-type)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
(func $read-g-with-call-indirect-in-between (result i32)
(local $x i32)
(local.set $x (global.get $g))

;; Similar to above with call_indirect instead of call_ref.
;; TODO: Add effects analysis for indirect calls.
(drop (call_indirect (type $const-type) (i32.const 0)))

(local.get $x)
)
)
Loading