Skip to content

Conversation

@kripken
Copy link
Member

@kripken kripken commented Oct 24, 2025

Defined functions remain exact, but imported ones are inexact.

This is a step along the recent Custom Descriptors spec changes.

  • New RefFunc::finalize and Literal::makeFunc variants get the module, and look up
    the type there.
  • New Builder::makeRefFunc variant gets a Type and applies it. The HeapType
    variant does a lookup on the module (so the Type one is more efficient/applicable
    if the IR is not fully built yet).
  • ReFinalize now updates RefFunc types (following the pattern of a few other places).
  • C and JS APIs now assume RefFuncs are created after imported functions (so we can
    look up the type of the import; see changelog, this seems the least-annoying way to
    update here, avoiding new APIs, and less breakage for users - hopefully none, all our
    tests here pass as is).
  • wasm-split adds a cast when a function becomes an inexact import.
  • Fix GUFA to handle inexact function literals.
  • Update types in passes and fuzzer as needed.

tlively and others added 30 commits October 8, 2025 13:44
Update the Literal constructors for funcrefs to take Type instead of
HeapType to allow them to be given inexact function references types
when the referenced function is an import. Use the new capability to
give references to imported functions inexact types in GUFA. Add a test
where this change fixes a misoptimization as well as tests where this
change simply changes the nature of the misoptimization. Future PRs will
fix these tests.
@kripken
Copy link
Member Author

kripken commented Nov 4, 2025

Fuzzer noticed that we didn't type imported ref.funcs correctly. Last two (non-merge) commits fix and test that.

Comment on lines +994 to +997
if (wasm.features.hasCustomDescriptors()) {
// Add a cast, as the parent may depend on the exactness to validate.
replaceCurrent(Builder(wasm).makeRefCast(curr, curr->type));
}
Copy link
Member

Choose a reason for hiding this comment

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

We don't have issues about needing this cast even when custom descriptors are disabled?

Copy link
Member Author

Choose a reason for hiding this comment

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

None that our tests or the fuzzer can find. What concerns you, the exactness? I think you made the binary writer strip that out as needed for all instructions?

Copy link
Member

Choose a reason for hiding this comment

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

For example in the test I commented on here: #7993 (comment). Shouldn't it be an IR validation error that we have an inexact reference being stored in an exact local? The binary writer would strip exactness and make the output valid, but I would have expected our validator to complain about the IR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't follow you. First, the linked comment was resolved. Second, how is an inexact reference stored in an exact local? The cast is ensuring things don't change (keeping the exact type from before, which was presumed valid).

Comment on lines +651 to +652
// This is imported, so it might be anything of the proper type.
addRoot(curr);
Copy link
Member

Choose a reason for hiding this comment

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

Don't we still want to track that this is a reference to the imported function? It would just have an inexact type.

Copy link
Member Author

Choose a reason for hiding this comment

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

But without knowing the identity, I think we can misoptimize? Imagine we have equality for a second, then ref.func a == ref.func a is definitely 1, but ref.func a == ref.func b is not necessarily 0 (can have duplicate imports).

Copy link
Member

Choose a reason for hiding this comment

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

But functions references cannot be compared for equality, so there is nothing to misoptimize, unless I'm missing something.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did say "imagine" 😆

But, while we don't have ref.eq on functions in wasm userspace, we do have optimizations that compare functions in other ways. E.g. folding an if with ref.eq arms, or GUFA inferences. I admit I don't see an actual bug atm in our optimizer, but a future one is conceivable.

@@ -0,0 +1,36 @@
;; Import a function of type $C as type $A, cast to $A, $B, $C. All those casts
;; should succeed. Exact casts, however TODO
Copy link
Member

Choose a reason for hiding this comment

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

Is this still TODO?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, just some old text. These casts become exact. Cleaned up now.

Copy link
Member

Choose a reason for hiding this comment

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

Let's add some exact casts to the test to show that they work as expected. I don't think the test currently demonstrates that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, added.

Comment on lines +1232 to +1234
;; CHECK-NEXT: (ref.test (ref (exact $func))
;; CHECK-NEXT: (ref.func $f)
;; CHECK-NEXT: )
Copy link
Member

Choose a reason for hiding this comment

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

The comment suggests we should have been able to optimize this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I think we need to look at finality in GUFA somehow. I added a TODO.

Copy link
Member

Choose a reason for hiding this comment

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

I suspect this will be fixed by using a non-exact Literal for references to function imports.

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