Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions src/passes/RemoveUnusedModuleElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,27 @@ struct Analyzer {

auto* new_ = curr->cast<StructNew>();

// Use the descriptor right now, normally. (We only have special
// optimization for struct.new operands, below.)
// Use the descriptor right now, normally. We only have special
// optimization for struct.new operands, below, because this is not needed
// for descriptors: descriptors always have a struct "in the middle", so
// optimizing normal struct fields is enough. That is, imagine we have a
// struct with a descriptor:
//
// (struct.new $A
// (ref.func $c)
// (struct.new $A.desc
// (ref.func $d)
// )
// )
//
// The struct has a ref.func on it, and the descriptor does as well. Say we
// never field 0 from $A, then we can avoid marking $c as reached; this is
Comment thread
kripken marked this conversation as resolved.
Outdated
// the usual struct optimization we do, below. Now, say we never read the
// descriptor, then we also never read field 0 from $A.desc, that is, the
// usual struct optimization on the descriptor class is enough for us to
// avoid marking $d as reached. Put another way, a descriptor must be a
// struct; if it could be a function, then we'd need to optimize descriptors
// as we do normal fields.
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.

Maybe lead with this? I think this clarifies things more than saying descriptors have a struct "in the middle."

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.

Sure, rewritten.

if (new_->desc) {
use(new_->desc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,73 @@
(elem $no-trap-get anyref (item (struct.new $struct (global.get $desc))))
)

(module
;; CHECK: (type $void (func))
(type $void (func))

(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $vtable (sub (descriptor $vtable.desc (struct (field (ref $void))))))
(type $vtable (sub (descriptor $vtable.desc (struct (field (ref $void))))))
;; CHECK: (type $vtable.desc (sub (describes $vtable (struct (field (ref $void))))))
(type $vtable.desc (sub (describes $vtable (struct (field (ref $void))))))
)

;; CHECK: (global $vtable (ref $vtable) (struct.new $vtable
;; CHECK-NEXT: (ref.func $a)
;; CHECK-NEXT: (struct.new $vtable.desc
;; CHECK-NEXT: (ref.func $b)
;; CHECK-NEXT: )
;; CHECK-NEXT: ))
(global $vtable (ref $vtable) (struct.new $vtable
(ref.func $a)
(struct.new $vtable.desc
(ref.func $b)
)
))

;; CHECK: (export "export" (func $export))

;; CHECK: (func $export (type $void)
;; CHECK-NEXT: (call_ref $void
;; CHECK-NEXT: (struct.get $vtable 0
;; CHECK-NEXT: (global.get $vtable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $export (export "export")
;; Read $a and call it. $b, in the descriptor, should not be callable.
(call_ref $void
(struct.get $vtable 0
(global.get $vtable)
)
)
)

;; CHECK: (func $a (type $void)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $a (type $void)
;; This is reached from above.
(drop (i32.const 42))
)

;; CHECK: (func $b (type $void)
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $b (type $void)
;; This is not reached: We never read the descriptor, so we never read field 0
;; in it, leaving this as dead (in closed world). That it itself seems to read
;; the descriptor should not confuse us.
(call_ref $void
(struct.get $vtable.desc 0
(ref.get_desc $vtable
(global.get $vtable)
)
)
)
)
)

Loading