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
6 changes: 5 additions & 1 deletion src/passes/Heap2Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,13 +929,17 @@ struct Struct2Local : PostWalker<Struct2Local> {
}

auto type = allocation->desc->type;
Expression* value = builder.makeLocalGet(localIndexes[fields.size()], type);
if (type != curr->type) {
// We know exactly the allocation that flows into this expression, so we
// know the exact type of the descriptor. This type may be more precise
// than the static type of this expression.
refinalize = true;
if (type.isNull()) {
// This traps.
value = builder.makeUnreachable();
}
}
auto* value = builder.makeLocalGet(localIndexes[fields.size()], type);
replaceCurrent(builder.blockify(builder.makeDrop(curr->ref), value));
}

Expand Down
44 changes: 43 additions & 1 deletion test/lit/passes/heap2local-desc.wast
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.

;; (remove-unused-names allows the pass to see that blocks flow values)
;; RUN: wasm-opt %s -all --remove-unused-names --heap2local -S -o - | filecheck %s
;; RUN: foreach %s %t wasm-opt -all --remove-unused-names --heap2local -S -o - | filecheck %s

(module
(rec
Expand Down Expand Up @@ -856,3 +856,45 @@
(local.get $desc)
)
)

(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $struct (sub (descriptor $desc (struct))))
(type $struct (sub (descriptor $desc (struct))))
;; CHECK: (type $desc (describes $struct (struct)))
(type $desc (describes $struct (struct)))
)

;; CHECK: (type $2 (func (result (ref (exact $desc)))))

;; CHECK: (func $test (type $2) (result (ref (exact $desc)))
;; CHECK-NEXT: (local $0 nullref)
;; CHECK-NEXT: (local $1 nullref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (ref.null none)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This traps, as you say. But the ref.null below is what determines the type of the entire expression, and it is wrong without the unreachable this PR appends.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, thanks. So another solution would be to add a ref.as_non_null, but adding unreachable is no more complex and gives better results.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could ref.as_non_null work? It just affects nullability, but the problem is we can't return an exact descriptor since we don't have one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But none <: (exact $desc), so the only problem here is the nullability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, I forgot exact types do have one subtype...

;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $test (result (ref (exact $desc)))
;; Read a null descriptor from a struct.new we can convert to locals. We do
;; not end up with a (ref (exact $desc)) here, since this will trap, so we
;; emit an unreachable.
(ref.get_desc $struct
(struct.new_default $struct
(ref.null none)
)
)
)
)

Loading