Skip to content

TypeGeneralizing: Optimize ref.cast when trapsNeverHappen - #9403

Open
kripken wants to merge 8 commits into
WebAssembly:mainfrom
kripken:typegen.casts.todo
Open

kripken wants to merge 8 commits into
WebAssembly:mainfrom
kripken:typegen.casts.todo

Conversation

@kripken

@kripken kripken commented Nov 18, 2026

Copy link
Copy Markdown
Member

This form of cast is very easy to generalize: it can become as general as
where its output goes to.

@kripken
kripken requested a review from tlively November 18, 2026 19:50

@tlively tlively left a comment

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.

Gemini noticed that this exposes a bug with call.without.effects:

(module
  (type $sig (func))
  (import "binaryen-intrinsics" "call.without.effects" (func $cwe (param funcref)))
  (func $test (param $f funcref)
    (call $cwe (ref.cast (ref $sig) (local.get $f)))
  )
)

return;
}
auto& glb = exprGLBs[curr];
auto newType = glb.noted() ? glb.getGLB() : getTop(curr->type);

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.

I see that we have this pattern above where we update local types as well, but it would be nice to clean this up. I believe we could do that by using exprGLBs.find(curr) instead of exprGLBs[curr] just above this to avoid inserting new default-initialized GLBs. All the GLBs already in the map should already have been noted.

(And if we wanted to, we could clean this pattern up for the localGLBs by initializing them with their top types.)

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 shorter code than using find, though? Another benefit of the current code is that it looks the same in the other case too, even though one is a vector and one is a map - makes the overall pass easier to read I think?

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.

Here's another alternative. It just seems a shame to have a nice utility like GLBFinder and then not take advantage of its simple interface just because we didn't initialize it.

Suggested change
auto newType = glb.noted() ? glb.getGLB() : getTop(curr->type);
glb.note(getTop(curr->type);
auto newType = glb.noted();

I don't feel too strongly about this, though. I agree short and consistent code is a good goal.

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 reads oddly to me (note, then check if noted). How about glb.getNotedGLBOr(otherwise) which returns the GLB if noted, otherwise a given value?

Comment thread src/passes/TypeGeneralizing.cpp Resolved

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.

We could add a test with nested exact casts, where generalizability propagates from outer to inner casts. Something like this:

(func (result anyref)
  (local $x (exact $foo))
  (ref.cast (ref (exact $foo))
    (ref.cast (ref (exact $foo))
      (local.get $x)
    )
  )
)

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.

Does func $chained in test/lit/passes/type-generalizing-tnh.wast not do this?

 ;; TNH:      (func $chained (type $6) (param $any anyref) (result (ref $Mid))
 ;; TNH-NEXT:  (ref.cast (ref $Mid)
 ;; TNH-NEXT:   (ref.cast anyref
 ;; TNH-NEXT:    (local.get $any)
 ;; TNH-NEXT:   )
 ;; TNH-NEXT:  )
 ;; TNH-NEXT: )
 ;; NO_TNH:      (func $chained (type $7) (param $any anyref) (result (ref $Mid))
 ;; NO_TNH-NEXT:  (ref.cast (ref $Bot)
 ;; NO_TNH-NEXT:   (ref.cast (ref $Mid)
 ;; NO_TNH-NEXT:    (local.get $any)
 ;; NO_TNH-NEXT:   )
 ;; NO_TNH-NEXT:  )
 ;; NO_TNH-NEXT: )
 (func $chained (param $any anyref) (result (ref $Mid))
  ;; Inner cast weakens to anyref; outer cast weakens to (ref $Mid).
  (ref.cast (ref $Bot)
   (ref.cast (ref $Mid)
    (local.get $any)
   )
  )
 )

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.

Almost, but that doesn't test the special treatment of exact casts over several steps.

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.

Ok, added a test with a chain of exact casts.

@kripken

kripken commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

call.without.effects fixed.

This branch has not been deployed

No deployments
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