Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): Improve constant pattern matching #2173

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

spotandjake
Copy link
Member

@spotandjake spotandjake commented Oct 9, 2024

This improves the code generation for our pattern matching engine, the goal is to make it so binaryen can build br_tables in more cases.

This changes how we generate code for our wasm output generation so in certain cases when we are matching on a static constant such as a short type, or constructor as long as your data is continous i.e 'a', 'b', 'c' and we are not outputting a default (not grain's wildcard default but a default in the constant path of our decision tree) then we will use a jump table, in cases where the data is not continous and you have a default we perform the untagging first and do direct i32 equality checks which reduces code size a little and allows binaryen to optimize into a jump table in more cases). This lowers codegen our output size by a couple bytes in a few cases but more importantly on large constant matches it allows us todo a direct jump instead of a bunch of equality checks and it allows us to save on memory operations by getting the values of int32, int64 the uint equivlants and adt variants before our comparisions.

We could furthur improve codegen in a few ways, we could handle the default case this would require changing up compile_switch to accept a Default(anf_expression) instead of just Partial or Total which would allow us to optimize more cases into jump tables, additionally we could handle non continous datasets (i.e ones with gaps), by changing how we generate our jump_table slightly (I did not have a good heurtistic for this though and could not find good data on how many dead branches make sense vs not, as we just point the dead_branches in the gaps to the default branch).

closes: #1185

@spotandjake spotandjake marked this pull request as ready for review October 12, 2024 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize pattern matching on constants
1 participant