Reboot TypeGeneralizing to use SubtypingDiscoverer #9263
Why "non-flow" here? The ref does flow into the throw and out of a later catch.
Again, why non-flow? Previously the only use of noteNonFlowSubtype was for ref.eq. If all these new uses of noteNonFlowSubtype are correct, then I suspect there are many more places that should be using it.
If visitation order matters, this should come after visiting the fields. (And even if visitation order doesn't matter now, it would probably be better to be consistent about it.)
;; incomparable with the i32 field of $inner-sub; we must still stop at $outer-sub.
Also the naming could be clearer here; "outer" and "inner" have opposing meanings when applied to the struct layout and IR layout.
Is this change related?
It's probably worth a TODO about additional things that are not handled optimally by SubtypeDiscoverer because it assumes the result types cannot be generalized: specifically ref.as_non_null, br_on_null, publish, br_if, and maybe a couple others.
Some initial comments before I get into the meat of the new implementation.
Test coverage doesn't look great, especially for the new changes in type-generalizing.
Metric Covered / Total Details
------------ ---------------------- ----------------------------------
Lines 243 / 326 (74.5%) Executable lines in diff
Regions 195 / 232 (84.1%) Sub-line / block AST regions in diff
Branches 131 / 170 (77.1%) True/False branch outcomes in diff
MC/DC 6 / 21 (28.6%) Condition independence pairs in diff
Functions 28 / 103 (27.2%) Modified functions called >= 1x
BrOn and RefCast are also missing descriptor handling.
ChildTyper would probably be a better utility to use here because it does not assume the parent expression's type cannot be generalized.
(In fact, ChildTyper is a better fit for all current users of SubtypingDiscoverer except for Unsubtyping, which is where SubtypingDiscoverer originally came from.)
Actually, ChildTyper is not enough because it does not know about the required upper bounds on the parent expression's type, which means it cannot reflect those bounds in the constraints it reports for child types. We would need to build a full principal types utility (built on top of ChildTyper) to let us propagate type contraints from parents to children. I'll consider that as a follow-on project rather than holding up this PR.
Yes, but it doesn't flow out of this expression. Reading the docs on top, I guess this can be ambiguous. I can reword that?
Yes. The fuzzer uses SubtypingDiscoverer internally (to find mutations, of the proper types). So improvements here lead to fuzzer improvements.
I think this is correct (for the reasons I mentioned above) but yes, there might be more places it makes sense to use. I didn't try to find them all in this PR - would make more sense in a place where we can cover them more easily with tests, though I don't know that we have a better pass, actually...
I can remove the ones not directly needed for tests in this PR if you prefer, though e.g. the String ones seem obvious?
(Aside from them, the ThrowRef one can also be removed from this PR, it looks like.)
The order doesn't matter, but I agree it is nice to follow. Fixed.
BrOn and RefCast are also missing descriptor handling.
How about leaving those for later + tests? This PR is mainly focused on the reboot, but not adding new functionality.
Ok, renamed the vars, and completely rewrote the comment.
Added a TODO
Commit pushed with comprehensive tests for new functionality in this PR.
The one thing I did not do here is verify they are new and not previously covered (as I didn't write the old ones originally, so I'd need to do a lot of work to match things up). But I had Gemini check that. In the worst case, if it got it wrong, we might be adding some small amount of overlap.
Thanks, that sounds like the right trade off to me.
No need. It's probably worth going through later and using noteNonFlowSubtype in most other places.
It would be good to say what these represent. I assume the Index is a local.
Can we just have note return a bool saying whether anything changed?
Gemini suggests I leave this comment:
LGTM! Thanks for adding the comprehensive test coverage and addressing the earlier feedback.
As a minor follow-up (not blocking this PR), we can update the doc comment at the top of `src/ir/subtype-exprs.h` to mention that `noteNonFlowSubtype` is also used for instructions that consume references without propagating them (such as `array.len`, string operations, etc.).
But I have more comments!
The one thing I did not do here is verify they are new and not previously covered (as I didn't write the old ones originally, so I'd need to do a lot of work to match things up). But I had Gemini check that. In the worst case, if it got it wrong, we might be adding some small amount of overlap.
I asked Gemini to use a coverage build to find the minimal set of tests. These can apparently be deleted without regressing coverage:
1. $struct-get-non-null-field (struct.get subtyping constraints were already fully exercised by module 0).
2. :$br_if (br_if flow constraints were already covered by baseline control-flow tests).
3. $return (return constraints were already covered by existing return tests).
4. $any-convert-extern-shared-nullable
5. $any-convert-extern-shared-non-nullable
6. $extern-convert-any-shared-nullable
7. $extern-convert-any-shared-non-nullable (The shared variants of any.convert_extern and extern.convert_any execute identical visitor and propagation paths as the unshared variants already tested in baseline).
8. $call-ref-non-null-result (generalizeCallRefTarget and non-null result propagation were already fully covered by module 0).
9. $throw (visitThrow was already covered by existing tag/exception tests).
Ok, just the very first item of that AI-generated list of tests is wrong, because I added it after we had a crash in the fuzzer that did not happen on the test suite. I went back in the history to verify that, and yes, that new testcase crashes while the old tests all pass.
Perhaps, but it would break the current symmetry between LUBFinder::note and GLBFinder::note - ?
comment added.
Ok, maybe the AI was lying to me or maybe line coverage wouldn't regress, but it's still useful as a regression test. That's fine. If any of those listed tests aren't useful for some other reason not captured by line coverage, though, it seems fine to delete them.
Well LUBFinder::note would probably benefit from the same change :) But that's fine, we can leave that out of this PR.
Approved these changes.
tlively
kripken
Now that we have a "late opts" stage, we can do things like generalize
types. This initial PR rewrites the pass to use the SubtypingDiscoverer
logic that was added since, and which makes the pass a lot
simpler (about half the length).
More work is needed to get this fuzzed etc., but just this rewrite
already removes some TODOs and asserts that are no longer relevant.
The pass can now run on a large Kotlin testcase without erroring, and
it shrinks the size there by 0.25% (not much yet, but after adding cast
generalization, perhaps more..?)
All old tests pass like before. (One new test is added, to handle a
crash that happened during this porting process.)
This is basically NFC, except that removing the old asserts mean
that more things can run.