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

More general VN const casting #94359

Closed
wants to merge 1 commit into from

Conversation

BruceForstall
Copy link
Member

When creating a VN cast, skip the cast if the target type is any GC type (not just TYP_BYREF) if the source operand is any TYP_I_IMPL constant (not just handle constant). We can lose track of whether a value is a handle due to shared const CSE.

When creating a VN cast, skip the cast if the target type is any GC
type (not just TYP_BYREF) if the source operand is any TYP_I_IMPL constant
(not just handle constant). We can lose track of whether a value
is a handle due to shared const CSE.
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 3, 2023
@ghost ghost assigned BruceForstall Nov 3, 2023
@ghost
Copy link

ghost commented Nov 3, 2023

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

When creating a VN cast, skip the cast if the target type is any GC type (not just TYP_BYREF) if the source operand is any TYP_I_IMPL constant (not just handle constant). We can lose track of whether a value is a handle due to shared const CSE.

Author: BruceForstall
Assignees: BruceForstall
Labels:

area-CodeGen-coreclr

Milestone: -

@jakobbotsch
Copy link
Member

I think we should disallow these kinds of mismatches. The implicit TYP_BYREF <-> TYP_I_IMPL conversions in JIT IR presumably only exist as an artifact of implicit TYP_I_IMPL -> TYP_BYREF conversions in IL. I don't think we should allow this to be a thing for TYP_REF <-> TYP_I_IMPL in JIT IR.

@BruceForstall
Copy link
Member Author

The implicit TYP_BYREF <-> TYP_I_IMPL conversions in JIT IR presumably only exist as an artifact of implicit TYP_I_IMPL -> TYP_BYREF conversions in IL

Not in the case I'm looking at. If we have a constant JIT-time handle (pointer to a frozen segment string object, maybe?) then we treat that as a constant with a "handle" attribute in VN. We can take this constant and constant propagate it to TYP_REF uses. We can also lose track of it being a handle due to "shared const CSE", where we pick a base constant X and then construct X + C as the actual constant, which has no place to hang the "handle" flags (on the ADD).

Maybe we shouldn't create these type mismatches during constant prop, etc.

This fix is actually a smaller, more targeted fix, than the other change I had in draft form #94330. This one seems slightly better -- it's avoiding creating a VNF_Cast between TYP_I_IMPL and TYP_REF, which the VN cast code (deeper down) doesn't like. Seems like a reasonable extension of the BYREF exception that already exists.

If we wanted to actually outlaw these kind of type mismatches, we'd have to do a bunch of work in assertion prop and CSE to avoid creating them (and also add some kind of type checking to ensure they don't get created).

@jakobbotsch
Copy link
Member

Not in the case I'm looking at. If we have a constant JIT-time handle (pointer to a frozen segment string object, maybe?) then we treat that as a constant with a "handle" attribute in VN. We can take this constant and constant propagate it to TYP_REF uses. We can also lose track of it being a handle due to "shared const CSE", where we pick a base constant X and then construct X + C as the actual constant, which has no place to hang the "handle" flags (on the ADD).

The VN code here is allowing implicit TYP_I_IMPL -> TYP_BYREF. That's more reasonable to me since we allow implicit TYP_I_IMPL <-> TYP_BYREF conversion in JIT IR. I do not think we allow the same for TYP_I_IMPL <-> TYP_REF.

If we wanted to actually outlaw these kind of type mismatches, we'd have to do a bunch of work in assertion prop and CSE to avoid creating them (and also add some kind of type checking to ensure they don't get created).

Yes, I think we should outlaw this, and treat places that create them as creating illegal IR... having these kinds of implicit conversions make things really confusing when reasoning about the IR. I very much doubt that all places in the JIT are correctly handling TYP_REF and TYP_I_IMPL as if they are implicitly convertible to each other, so trying to make this one place in VN handle it does not seem right.

It sounds like we have a few bugs around this, which is not surprising given that the frozen object constants are very new. For one, I think VN should not be giving two differently typed trees the same VN, since they are by definition not equal if they have different types (maybe except the TYP_I_IMPL <-> TYP_BYREF case, due to allowed implicit conversion in JIT IR).

@SingleAccretion
Copy link
Contributor

It sounds to me like not typing handles properly is causing some (new, with the frozen objects) issues; it should be simple to fix by passing the right type when creating the handle VN instead of always using I_IMPL.

The assertion prop issue could be solvable in a similar manner I think. We should create TYP_REF trees in gtNewIconHandle for frozen object handles.

@ghost ghost closed this Dec 4, 2023
@ghost
Copy link

ghost commented Dec 4, 2023

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 4, 2024
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants