Skip to content

Audit uses of bind together with int_array_ref and other peephole-sensitive functions #3718

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

Open
Gbury opened this issue Mar 20, 2025 · 2 comments
Assignees

Comments

@Gbury
Copy link
Contributor

Gbury commented Mar 20, 2025

Following some recent investigations on code generation around unboxed products array by @mshinwell , I found out that at least some functions in cmm_helpers.ml use the bind function a bit too eagerly, which can degrade the generated code when other functions are also used (e.g. int_array_ref and other array indexing functions).

For more explanations, the bind function is used to introduce let-bindings in cmm expressions, and is meant to be used to avoid duplicating a cmm expression, as well as ensure that the expression is actually evaluated, and that its evaluation is done in a coherent order (though that last point is still a bit unclear). So in practice these should only be used if the bound value (or the variable created for the binding) is used at least twice, or has effects/co-effects. When that is not the case, and the bound expression is only used once (and is pure, which happens reasonably frequently for expressions for e.g. array indexes), that prevents some (most ?) of the peephole optimizations, particularly around addressing in the case of array loads (but there may be other impacts).

Therefore, uses of int_array_ref and other related functions (e.g. array_indexing) should be reviewed to see whether some binds can be removed.

@Gbury Gbury self-assigned this Mar 20, 2025
@mshinwell
Copy link
Collaborator

We need to be super careful not to risk introducing accidental duplication of effectful code, though.

@lpw25
Copy link
Collaborator

lpw25 commented Mar 21, 2025

You also need to be careful because currently removing a use of bind can cause instruction selection to produce worse output.

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

No branches or pull requests

3 participants