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
10 changes: 10 additions & 0 deletions src/passes/HeapStoreOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ struct HeapStoreOptimization
}
}

// Similarly, we must move the set's value past the allocation's descriptor,
// if it exists.
if (new_->desc) {
auto descEffects = effects(new_->desc);
if (descEffects.invalidates(setValueEffects)) {
// TODO: we could use locals to reorder everything
return false;
}
}

// We also cannot reorder if the struct.new itself has effects (which it can
// in the case of a descriptor) that interact with X' (from the comment
// above), as e.g. a struct.new trap happens before effects in X', but after
Expand Down
61 changes: 52 additions & 9 deletions test/lit/passes/heap-store-optimization-desc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $struct (descriptor $desc (struct (field (mut i32)))))
(type $struct (sub final (descriptor $desc (struct (field (mut i32))))))
;; CHECK: (type $desc (sub (describes $struct (struct))))
(type $desc (sub (describes $struct (struct))))
(type $struct (descriptor $desc (struct (field (mut i32)))))
;; CHECK: (type $desc (describes $struct (descriptor $meta (struct))))
(type $desc (describes $struct (descriptor $meta (struct))))
;; CHECK: (type $meta (describes $desc (struct)))
(type $meta (describes $desc (struct)))
)

;; CHECK: (import "" "" (func $effect (type $2)))
;; CHECK: (import "" "" (func $effect (type $3)))
(import "" "" (func $effect))

;; CHECK: (func $no-reorder (type $2)
;; CHECK: (func $no-reorder (type $3)
;; CHECK-NEXT: (local $struct (ref $struct))
;; CHECK-NEXT: (local.set $struct
;; CHECK-NEXT: (struct.new_default $struct
Expand Down Expand Up @@ -47,25 +49,66 @@
)
)

;; CHECK: (func $yes-reorder (type $2)
;; CHECK: (func $no-reorder-nested (type $3)
;; CHECK-NEXT: (local $struct (ref $struct))
;; CHECK-NEXT: (local.set $struct
;; CHECK-NEXT: (struct.new_default $struct
;; CHECK-NEXT: (struct.new_default $desc
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.set $struct 0
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: (block $block (result i32)
;; CHECK-NEXT: (call $effect)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $no-reorder-nested
(local $struct (ref $struct))
;; As above, but now it is not the top-level allocation that traps, but
;; rather its descriptor operand. We still cannot optimize.
(local.set $struct
(struct.new_default $struct
(struct.new $desc
(ref.null none)
)
)
)
(struct.set $struct 0
(local.get $struct)
(block $block (result i32)
(call $effect)
(i32.const 0)
)
)
)

;; CHECK: (func $yes-reorder (type $3)
;; CHECK-NEXT: (local $struct (ref $struct))
;; CHECK-NEXT: (local.set $struct
;; CHECK-NEXT: (struct.new $struct
;; CHECK-NEXT: (block $block (result i32)
;; CHECK-NEXT: (call $effect)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.new_default $desc)
;; CHECK-NEXT: (struct.new_default $desc
;; CHECK-NEXT: (struct.new_default $meta)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $yes-reorder
(local $struct (ref $struct))
;; As above, but now the descriptor does not trap, so we optimize.
;; Nothing traps, so we can reorder.
(local.set $struct
(struct.new_default $struct
(struct.new $desc)
(struct.new $desc
(struct.new $meta)
)
)
)
(struct.set $struct 0
Expand Down
Loading