Skip to content

[NFC] Allow SubTypes::iterSubTypes to stop early#8573

Merged
kripken merged 6 commits into
WebAssembly:mainfrom
kripken:subtypes.failfast
Apr 6, 2026
Merged

[NFC] Allow SubTypes::iterSubTypes to stop early#8573
kripken merged 6 commits into
WebAssembly:mainfrom
kripken:subtypes.failfast

Conversation

@kripken

@kripken kripken commented Apr 3, 2026

Copy link
Copy Markdown
Member

In ConstantFieldPropagation this is important: we scan subtypes
to check if they all have 2 possible values we can ref.test between.
In the common case there are many values and we can stop early.

This makes the pass 4.2x faster on a large Dart testcase, and
-O3 overall 3.5% faster.

Fun fact, the test that is added to here was slightly wrong before:
lacking a rec group, types B and C were actually identical, so we
didn't fully test what we thought we did.

@kripken kripken requested a review from a team as a code owner April 3, 2026 23:52
@kripken kripken requested review from stevenfontanella and removed request for a team April 3, 2026 23:52
Comment thread src/passes/ConstantFieldPropagation.cpp Outdated
@@ -332,27 +332,23 @@ struct FunctionOptimizer : public WalkerPass<PostWalker<FunctionOptimizer>> {
// as having failed.
auto fail = false;

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.

nit: Maybe it would be useful for iterSubTypes to return a bool indicating whether it returned early or not? Seems like we could replace this variable with that return value if we had it.

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.

Good idea, thanks. Done.

@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.

LGTM as a minimal fix, but I think it would be even nicer to replace iterSubtypes with a proper iterator so the users can be in full control. Maybe Gemini could crank that out. There is a similar iterator over a tree in Unsubtyping.cpp that could be mostly copied. We could also experiment with using C++20 coroutines to simplify the code for such an iterator, but perhaps that would best be done separately.

@kripken

kripken commented Apr 6, 2026

Copy link
Copy Markdown
Member Author

@tlively Interesting, yeah, maybe an iterator would be even better. I'll land this as a perf win, and look into pure refactorings later (i.e. this is a minimal change to get the speedup, and later changes will be larger but not alter speed).

@kripken kripken merged commit a3ac1d9 into WebAssembly:main Apr 6, 2026
16 checks passed
@kripken kripken deleted the subtypes.failfast branch April 6, 2026 19:52
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.

3 participants