summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp33
-rw-r--r--test/lit/passes/optimize-instructions-gc-tnh.wast84
2 files changed, 116 insertions, 1 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index c2142fe6c..916991ecb 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -1606,7 +1606,38 @@ struct OptimizeInstructions
// Either way we trap here, but refining the type may have benefits later.
if (ref->type.isNullable()) {
if (auto* cast = ref->dynCast<RefCast>()) {
- cast->type = Type(cast->type.getHeapType(), NonNullable);
+ // Note that we must be the last child of the parent, otherwise effects
+ // in the middle may need to remain:
+ //
+ // (struct.set
+ // (ref.cast null
+ // (call ..
+ //
+ // The call here must execute before the trap in the struct.set. To
+ // avoid that problem, inspect all children after us. If there are no
+ // such children, then there is no problem; if there are, see below.
+ auto canOptimize = true;
+ auto seenRef = false;
+ for (auto* child : ChildIterator(curr)) {
+ if (child == ref) {
+ seenRef = true;
+ } else if (seenRef) {
+ // This is a child after the reference. Check it for effects. For
+ // simplicity, focus on the case of traps-never-happens: if we can
+ // assume no trap occurs in the parent, then there must not be a
+ // trap in the child either, unless control flow transfers and we
+ // might not reach the parent.
+ // TODO: handle more cases.
+ if (!getPassOptions().trapsNeverHappen ||
+ effects(child).transfersControlFlow()) {
+ canOptimize = false;
+ break;
+ }
+ }
+ }
+ if (canOptimize) {
+ cast->type = Type(cast->type.getHeapType(), NonNullable);
+ }
}
}
diff --git a/test/lit/passes/optimize-instructions-gc-tnh.wast b/test/lit/passes/optimize-instructions-gc-tnh.wast
index d9520b18c..fec4c17f0 100644
--- a/test/lit/passes/optimize-instructions-gc-tnh.wast
+++ b/test/lit/passes/optimize-instructions-gc-tnh.wast
@@ -446,6 +446,90 @@
)
)
+ ;; TNH: (func $set-get-cast (type $dataref_=>_none) (param $ref dataref)
+ ;; TNH-NEXT: (drop
+ ;; TNH-NEXT: (struct.get $struct 0
+ ;; TNH-NEXT: (ref.cast $struct
+ ;; TNH-NEXT: (local.get $ref)
+ ;; TNH-NEXT: )
+ ;; TNH-NEXT: )
+ ;; TNH-NEXT: )
+ ;; TNH-NEXT: (struct.set $struct 0
+ ;; TNH-NEXT: (ref.cast $struct
+ ;; TNH-NEXT: (local.get $ref)
+ ;; TNH-NEXT: )
+ ;; TNH-NEXT: (i32.const 1)
+ ;; TNH-NEXT: )
+ ;; TNH-NEXT: (struct.set $struct 0
+ ;; TNH-NEXT: (ref.cast null $struct
+ ;; TNH-NEXT: (local.get $ref)
+ ;; TNH-NEXT: )
+ ;; TNH-NEXT: (block (result i32)
+ ;; TNH-NEXT: (return)
+ ;; TNH-NEXT: (i32.const 1)
+ ;; TNH-NEXT: )
+ ;; TNH-NEXT: )
+ ;; TNH-NEXT: )
+ ;; NO_TNH: (func $set-get-cast (type $dataref_=>_none) (param $ref dataref)
+ ;; NO_TNH-NEXT: (drop
+ ;; NO_TNH-NEXT: (struct.get $struct 0
+ ;; NO_TNH-NEXT: (ref.cast $struct
+ ;; NO_TNH-NEXT: (local.get $ref)
+ ;; NO_TNH-NEXT: )
+ ;; NO_TNH-NEXT: )
+ ;; NO_TNH-NEXT: )
+ ;; NO_TNH-NEXT: (struct.set $struct 0
+ ;; NO_TNH-NEXT: (ref.cast null $struct
+ ;; NO_TNH-NEXT: (local.get $ref)
+ ;; NO_TNH-NEXT: )
+ ;; NO_TNH-NEXT: (i32.const 1)
+ ;; NO_TNH-NEXT: )
+ ;; NO_TNH-NEXT: (struct.set $struct 0
+ ;; NO_TNH-NEXT: (ref.cast null $struct
+ ;; NO_TNH-NEXT: (local.get $ref)
+ ;; NO_TNH-NEXT: )
+ ;; NO_TNH-NEXT: (block (result i32)
+ ;; NO_TNH-NEXT: (return)
+ ;; NO_TNH-NEXT: (i32.const 1)
+ ;; NO_TNH-NEXT: )
+ ;; NO_TNH-NEXT: )
+ ;; NO_TNH-NEXT: )
+ (func $set-get-cast (param $ref (ref null data))
+ ;; A nullable cast flowing into a place that traps on null can become a
+ ;; non-nullable cast.
+ (drop
+ (struct.get $struct 0
+ (ref.cast null $struct
+ (local.get $ref)
+ )
+ )
+ )
+ ;; Ditto for a set, at least in traps-happen mode.
+ ;; TODO handle non-TNH as well, but we need to be careful of effects in
+ ;; other children.
+ (struct.set $struct 0
+ (ref.cast null $struct
+ (local.get $ref)
+ )
+ (i32.const 1)
+ )
+ ;; Even in TNH mode, a child with an effect of control flow transfer
+ ;; prevents us from optimizing - if the parent is not necessarily reached,
+ ;; we cannot infer the child won't trap.
+ (struct.set $struct 0
+ (ref.cast null $struct
+ (local.get $ref)
+ )
+ (block (result i32)
+ ;; This block has type i32, to check that we don't just look for
+ ;; unreachable. We must scan for any transfer of control flow in the
+ ;; child of the struct.set.
+ (return)
+ (i32.const 1)
+ )
+ )
+ )
+
;; Helper functions.
;; TNH: (func $get-i32 (type $none_=>_i32) (result i32)