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
13 changes: 13 additions & 0 deletions src/passes/ConstantFieldPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ struct FunctionOptimizer : public WalkerPass<PostWalker<FunctionOptimizer>> {

void visitRefGetDesc(RefGetDesc* curr) {
optimizeRead(curr, curr->ref, StructUtils::DescriptorIndex);

// RefGetDesc has the interesting property that we can write a value into
// the field that cannot be read from it: it is valid to write a null, but
// a null can never be read (it would have trapped on the write). Fix that
// up as needed to not break validation.
if (!Type::isSubType(getCurrent()->type, curr->type)) {
Builder builder(*getModule());
replaceCurrent(builder.makeRefAs(RefAsNonNull, getCurrent()));
}
}

void optimizeRead(Expression* curr,
Expand Down Expand Up @@ -485,6 +494,10 @@ struct PCVScanner
Index index,
PossibleConstantValues& info) {
info.note(expr, *getModule());
// TODO: For descriptors we can ignore nullable values that are written, as
// they trap. That is, if one place writes a null and another writes a
// global, only the global is readable, and we can optimize there -
// the null is not a second value.
}

void noteDefault(Type fieldType,
Expand Down
35 changes: 35 additions & 0 deletions test/lit/passes/cfp-desc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,38 @@
)
)

(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $A (descriptor $B (struct)))
(type $A (descriptor $B (struct)))
;; CHECK: (type $B (describes $A (struct)))
(type $B (describes $A (struct)))
)
;; CHECK: (type $2 (func (result (ref (exact $B)))))

;; CHECK: (func $test (type $2) (result (ref (exact $B)))
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (struct.new_default $A
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $test (result (ref (exact $B)))
;; We need to add a ref.as_non_null on the descriptor that is read, as the
;; function result is non-nullable.
(ref.get_desc $A
(struct.new_default $A
(ref.null none)
Copy link
Member

Choose a reason for hiding this comment

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

We could do even better in this case by ignoring null writes to the descriptor in the first place. Then if there is another location that writes a real value, we can still optimize all the reads to return the real value.

However, we'll still need the current code to fix up the case where the single written value is a nullable global, but the output of the ref.get_cast is required to be non-null.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, I added a TODO

)
)
)
)

Loading