Skip to content

Avoid invalid exact casts in TypeRefining#7529

Merged
tlively merged 17 commits intomainfrom
gufa-type-refining-exact
Apr 21, 2025
Merged

Avoid invalid exact casts in TypeRefining#7529
tlively merged 17 commits intomainfrom
gufa-type-refining-exact

Conversation

@tlively
Copy link
Copy Markdown
Member

@tlively tlively commented Apr 18, 2025

TypeRefining (both the normal and GUFA variants) is able to infer that a
field can be more precise than is immediately apparent from the type of
an expression set to that field. In these cases, the pass inserts a cast
to recover the more precise type information. When custom descriptors
are not enabled, casts to exact types invalid, so avoid introducing new
exactness that might need such invalid casts in that case.

tlively added 14 commits April 17, 2025 17:20
We previously allowed trivial exact casts (i.e. those where the input
and target types are the same) to validate even without custom
descriptors enabled to ensure that finalization always produces valid
casts. But validation can also produce casts where the cast target is a
non-nullable exact reference and the input type is a nullable exact
reference to the same heap type. Update validation to allow these casts.

This is safe because erasing the exactness of the input and cast types
does not change the results of these casts. They succeed iff the input
is a non-null value.
Do not merge types that differ in exactness.
RemoveUnusedBrs can introduce new casts when optimizing out br_on_cast
and br_on_cast_fail instructions. This happens when the pass finds a
more precise type for the fallthrough value reaching the casting branch,
allowing it to improve the cast target type. When the branch is
subsequently optimized out, this precise type information is recovered
with an inserted cast.

Update the pass to avoid creating invalid casts to exact types when
custom descriptors is not enabled.
These features were previously missed when reading target features
sections.
There are several places in the code where we need to make a type
inexact only if custom descriptors is disabled. Add a helper to slightly
simplify those places.
TypeRefining (both the normal and GUFA variants) is able to infer that a
field can be more precise than is immediately apparent from the type of
an expression set to that field. In these cases, the pass inserts a cast
to recover the more precise type information. When custom descriptors
are not enabled, casts to exact types invalid, so avoid introducing new
exactness that might need such invalid casts in that case.
@tlively tlively requested a review from kripken April 18, 2025 18:35
;; NO_CD-NEXT: )
(func $struct.new (param $inexact (ref null $foo)) (param $exact (ref (exact $foo))) (result anyref)
(struct.new $bar
(try (result (ref null $foo))
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.

Why do we need the try etc?

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.

This is the pattern used in both the explanatory comments and the tests for non-GUFA type refining where casts might be inserted. I don't know if there are any simpler patterns that would lead non-GUFA type refining to insert casts.

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.

Oh, non-GUFA... yeah, it's harder to get things to happen there. Following the existing pattern sgtm.

Base automatically changed from get-maybe-without-exact to main April 21, 2025 20:18
@tlively tlively merged commit eb00324 into main Apr 21, 2025
27 of 28 checks passed
@tlively tlively deleted the gufa-type-refining-exact branch April 21, 2025 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants