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

Improve IsKnownConstant #84002

Closed
wants to merge 5 commits into from
Closed

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Mar 28, 2023

If we fail to detect a constant in Morph - delegate it to late phases like VN/ConstantProp.
It allows to use IsKnownConstant for e.g. spans

@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 Mar 28, 2023
@ghost ghost assigned EgorBo Mar 28, 2023
@ghost
Copy link

ghost commented Mar 28, 2023

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

Issue Details

If we fail to detect a constant in Morph - delegate it to late phases like VN/ConstantProp.
It allows to use IsKnownConstant for e.g. spans

Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

IDisposable

This comment was marked as outdated.

src/coreclr/jit/lower.cpp Outdated Show resolved Hide resolved
Comment on lines 11052 to 11054
bool isKnownConstant = arg0VNP.BothEqual() && vnStore->IsVNConstant(arg0VNP.GetLiberal());
ValueNum isCnsVN = vnStore->VNForIntCon(isKnownConstant ? 1 : 0);
intrinsic->gtVNPair = vnStore->VNPWithExc(ValueNumPair(isCnsVN, isCnsVN), arg0VNPx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the implementation with both liberal and conservative VNs was lost along the way...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, fixed

Comment on lines +7797 to +7798
// IsKnownConstant is expected to be folded in Importer/Morph/VN+ConstantProp
// This path is just in case if VN+ConstantProp didn't fold them for some reason (or e.g. they were disabled)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is "not enough"...

Consider a case where VN assigns this 1 (i. e. the operand is constant). Then CSE CSEs this as a def with some other unrelated uses of the value 1. Then lowering comes along, and we get 0 instead of 1.

Essentially, each subsequent phase can only further "refine" the value of this intrinsic. The tricky part here is of course that by the time we get to lowering, VNs are no longer valid. So this needs a separate "lower IsKnownConstant" phase, that would use the still-valid VNs. Bit yucky.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but the current code is correct, isn't it? it justs conservatively assumes that input is not a constant so we end up in the non-constant path.

I agree that it'd be nice to have a separate phase for such transforms somewhere after assert prop with still valid VNs. Maybe I'll add one eventuallly (and move unrollings there e.g.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the current code is correct, isn't it?

No - consider the CSE example. We cannot have this assigned 1 in VN and then lower it to zero.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the current code is correct, isn't it?

No - consider the CSE example. We cannot have this assigned 1 in VN and then lower it to zero.

Ah, I see what you mean. I'll implement it in a separate phase if we find a real use case for it

@EgorBo EgorBo closed this Mar 28, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 28, 2023
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