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

wazevo: initial work on constant folding #1851

Closed
wants to merge 23 commits into from
Closed

wazevo: initial work on constant folding #1851

wants to merge 23 commits into from

Conversation

evacchi
Copy link
Contributor

@evacchi evacchi commented Nov 28, 2023

#1496

This implements an initial, simple constant folding pass for add/sub/mul for i32/i64/f32/f64.
It also implements a new case for passNopInstElimination where one of the args is const 0.

  • is it ok to mutate instructions in-place that way?
  • do we need to add checks for integer underflow/overflow? the behavior right now is essentially whatever Go does (i.e. let integers wrap, let floats go to ±inf)
  • todo: add a few more test cases

Signed-off-by: Edoardo Vacchi <[email protected]>
Signed-off-by: Edoardo Vacchi <[email protected]>
Signed-off-by: Edoardo Vacchi <[email protected]>
Signed-off-by: Edoardo Vacchi <[email protected]>
@evacchi evacchi requested a review from mathetake November 29, 2023 22:00
Copy link
Member

@mathetake mathetake left a comment

Choose a reason for hiding this comment

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

I think this really needs fuzzing yeah

Comment on lines 377 to 379
// The fixed point is reached through a simple iteration over the list of instructions.
// Note: Instead of just an unbounded loop with a flag, we may also add an upper bound to the number of iterations.
isFixedPoint := false
Copy link
Member

Choose a reason for hiding this comment

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

yeah I think I would suggest to see how much overhead costs will be added like by compiling zig stdlib binray and let's have a reasonable O(1) upper bound

Copy link
Contributor Author

@evacchi evacchi Nov 30, 2023

Choose a reason for hiding this comment

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

even without an upper bound the pass seems to account for less than 1% CPU time against the Wasm library.

Screenshot 2023-11-30 at 18 27 49

in terms of hard numbers, on my machine it compiles the wasm binary in about 7.02 seconds with the extra pass and 6.94-6.99 without

To be fair, that might not be surprising: e.g., if the wasm binary went through wasm-opt it might be already minimized, and if it weren't, operations between constants may not naturally occur in a Wasm binary very often (after all, it's already a compilation target)

Copy link
Contributor Author

@evacchi evacchi Dec 1, 2023

Choose a reason for hiding this comment

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

...in fact, I am not even sure how often this pass will be useful, I don't know how often we'll find cases in the wild. Anyway it's a useful playground for me...

Besides, other, related techniques such as constant tracking and constant propagation might still be beneficial, and the framework should be similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured out what (silly) mistake I was making with my experiment with algebraic simplification (just add X, const1; add Y, const2 => add X, (const1+const2)). This, together with const-fold adds a more significant overhead (~7.4); but that should also mean that it does make some difference. On a first quick look, the first largest functions in the zig stdlib were not significantly smaller.

constFold, no simplification

1809672
1358096
906528

constfold + simplification

1809668
1358092
906524

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

@mathetake mathetake Dec 11, 2023

Choose a reason for hiding this comment

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

The diff in the size(?) seems negligible to me even with simplification…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah agreed (yeah that's size in bytes)

Signed-off-by: Edoardo Vacchi <[email protected]>
Signed-off-by: Edoardo Vacchi <[email protected]>
Signed-off-by: Edoardo Vacchi <[email protected]>
Signed-off-by: Edoardo Vacchi <[email protected]>
Signed-off-by: Edoardo Vacchi <[email protected]>
@evacchi
Copy link
Contributor Author

evacchi commented Dec 15, 2023

I pushed some new simple substitutions extending nop elimination. Algebraic simplification need more research to be effective. Now rebased and running some fuzzing locally.

Signed-off-by: Edoardo Vacchi <[email protected]>
@evacchi
Copy link
Contributor Author

evacchi commented Dec 18, 2023

closing for now as no significant benefit so far

@evacchi evacchi closed this Dec 18, 2023
@mathetake mathetake deleted the opt-const-fold branch December 22, 2023 20:14
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