Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
flambda does not collapse pattern matching in some cases #7259
Original bug ID: 7259
In a program I've been writing, I have a few functions which return the same field of of an inline record, no matter which of the variants are chosen. For example:
type node =
Without flambda, my computer creates the following CMM code:
With flambda (-O3), my computer creates this:
Essentially, the first version sees that parent is always the first field, so it simplifies the function down to a simple pointer lookup. The second version checks the tag and loads the first field in both branches.
The issue is the same when not using inline records (other than an extra pointer load that inline records avoid), and in this case 4.02.0 seems to be the same as 4.03.0 without flambda. This issue seems to be a regression in flambda, though I don't presume to know why.
There are some cases when this doesn't happen though: if no fields that after the shortest record's end are mutable, the problem goes away. That is, leaving Branch alone and adding whatever you want to Leaf won't make this happen UNLESS one of the fields after dv are mutable.
This was tested on 64-bit Mac OS-X 10.11 and 64-bit MSVC-compiled Windows 10 installs.
Comment author: @chambart
With flambda there is an intermediate let in both branches. Those have different identifiers, hence are not detected by cmmgen as equal.
When there are no mutable field, the translation from parsedtree to lambda already does the sharing.
This simple case could be fixed by some improvement in the un-anf pass to get rid of the let. A more general fix would be to do a better equality check.