-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[comments only] matching.ml: explain the compilation strategy for switches on constructors #10512
[comments only] matching.ml: explain the compilation strategy for switches on constructors #10512
Conversation
P.S.: One reason for sending this PR is that having a documentation of the current code would help our implementation (with @nchataing) of matching on unboxed constructors. Having would have helped us in the past, but now we can update the documentation as we change things, it serves as a useful sanity check, and it will also help reviewers of our work. |
dd72c8f
to
93b78ad
Compare
After more tinkering, I believe that the idea of preferring switch in bytecode is not accurate: in very-sparse cases where most of the cases on one of the two domains are missing, switch generates a large bytecode instruction that lists many exit labels, when a test followed by a better compilation runs slightly slower but can reduce bytecode size significantly. An edge case of interest is when This sounds like something to discuss with @maranget. |
93b78ad
to
d6a0f14
Compare
I tweaked the comments to explain why we probably want the test-generating approach when all non-constant actions are shared. I discussed the matter with @maranget yesterday, and would be interested in a review :-) |
d6a0f14
to
affe759
Compare
(I had a look, but am not knowledgeable enough to approve or reject) |
One small comment: despite the message |
@lthls can you explain why this optimization would be bad for the native backend? This is news to me. |
First, with the native backend But the real issue is that it introduces non-boolean arguments to |
Two remarks:
We could introduce an |
I'll see if I can make a PR out of our relevant patches. But I think that your original comment that backend-specific optimisations should be done in the relevant backend is correct, and that moving some of the code in
For the native backend at least, keeping the original |
affe759
to
454593a
Compare
I rebased the PR on top of the current trunk. @lthls, would you mind having another look? My understanding is that @lthls objections to the documentation were mostly fixed by his own follow-up PR #10681. In that PR we optimized native code generation slightly more, and we simplified the assumptions between bytecomp/matching and the backend. One place remains in matching.ml where the interests of bytecode and native compilations diverge, but this place is clearly marked by a Clflags.native_code conditional with explanatory comments. All this to say: to me, the rebased PR is in a good state and the proposed documentation improves the compiler codebase. |
454593a
to
5697dfd
Compare
5697dfd
to
e870586
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still annoyed to have a comment saying that backend-specific optimisations should stay in their respective backends immediately followed by two bytecode-specific optimisations, but I can live with that. I may propose a PR moving bytecode-specific optimisations to the bytecode backend, where they belong, later.
(* This case is very frequent, it corresponds to | ||
options and lists. *) | ||
(* Keeping the Pisint test would make the bytecode | ||
slightly worse, but it lets the native compilre generate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slightly worse, but it lets the native compilre generate | |
slightly worse, but it lets the native compiler generate |
Thanks! I like how productive you are when my comments annoy you. I should write more. (I'll merge now and fix the typo later, because.) |
I'm working on constructor unboxing with @nchataing, and this involves looking at the lambda/matching.ml machinery. There are some decisions / design choices around the compilation of switches that are not obvious -- I understood them after trying some changes and realizing they were not ideal. This comments-only PR tries to document and explain the current state of this code.
cc @maranget @xavierleroy
In one case there is an optimization that conflicts with the general explanation I propose -- that we want to avoid generating several bytecode instructions. I believe that this optimization could and probably should be removed, but the details are a bit subtle and I left this idea for a future PR.