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

JIT: Stop creating "location" commas #83814

Merged
merged 7 commits into from
Mar 25, 2023

Conversation

jakobbotsch
Copy link
Member

Avoid creating commas that are on the LHS of an assignment or below an ADDR node.

These primarily were created by morph which has an IND(COMMA(x, y)) -> COMMA(x, IND(y)) transformation that did not pay attention to whether it was on the left of an assignment.
The IR shape is pretty odd; the RHS of these commas are not actually evaluated in the traditional sense, but happen as part of the parent ASG, so the semantics of the construct ends up being confusing.
Additionally it complicates #83590.

I initially tried removing the IND(COMMA(x, y)) -> COMMA(x, IND(y)) transformation entirely but this has a lot of diffs -- probably a bit of work to teach downstream phases about these new commas would be required. I gave up after doing that in a few separate places.

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

ghost commented Mar 23, 2023

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

Issue Details

Avoid creating commas that are on the LHS of an assignment or below an ADDR node.

These primarily were created by morph which has an IND(COMMA(x, y)) -> COMMA(x, IND(y)) transformation that did not pay attention to whether it was on the left of an assignment.
The IR shape is pretty odd; the RHS of these commas are not actually evaluated in the traditional sense, but happen as part of the parent ASG, so the semantics of the construct ends up being confusing.
Additionally it complicates #83590.

I initially tried removing the IND(COMMA(x, y)) -> COMMA(x, IND(y)) transformation entirely but this has a lot of diffs -- probably a bit of work to teach downstream phases about these new commas would be required. I gave up after doing that in a few separate places.

Author: jakobbotsch
Assignees: jakobbotsch
Labels:

area-CodeGen-coreclr

Milestone: -

Comment on lines 10051 to 10067
if ((op1 != nullptr) && fgIsCommaThrow(op1, true))
bool mightBeLocation = (tree->OperIsIndir() || tree->OperIsLocal()) && ((tree->gtFlags & GTF_DONT_CSE) != 0);
if (!mightBeLocation)
{
GenTree* propagatedThrow = fgPropagateCommaThrow(tree, op1->AsOp(), GTF_EMPTY);
if (propagatedThrow != nullptr)
if ((op1 != nullptr) && fgIsCommaThrow(op1, true))
{
return propagatedThrow;
GenTree* propagatedThrow = fgPropagateCommaThrow(tree, op1->AsOp(), GTF_EMPTY);
if (propagatedThrow != nullptr)
{
return propagatedThrow;
}
}
}

if ((op2 != nullptr) && fgIsCommaThrow(op2, true))
{
GenTree* propagatedThrow = fgPropagateCommaThrow(tree, op2->AsOp(), op1->gtFlags & GTF_ALL_EFFECT);
if (propagatedThrow != nullptr)
if ((op2 != nullptr) && fgIsCommaThrow(op2, true))
{
return propagatedThrow;
GenTree* propagatedThrow = fgPropagateCommaThrow(tree, op2->AsOp(), op1->gtFlags & GTF_ALL_EFFECT);
if (propagatedThrow != nullptr)
{
return propagatedThrow;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This transform was creating illegal IR like the following:

fgMorphTree BB01, STMT00017 (after)
               [000098] -ACXG+-----                           ASG       long  
               [000134] --CXG+-N---                         ├──▌  COMMA     long  
               [000137] --CXG+-----                           ├──▌  CALL help void   CORINFO_HELP_OVERFLOW
               [000136] -----+----- arg0 in rcx                 └──▌  CNS_INT   int    0
               [000142] -----+-----                           └──▌  CNS_INT   long   0
               [000095] -----+-----                         └──▌  CNS_INT   long   0xFFFFFFFF

@jakobbotsch jakobbotsch changed the title JIT: Disallow "location" commas JIT: Stop creating "location" commas Mar 23, 2023
@jakobbotsch
Copy link
Member Author

/azp run runtime-coreclr jitstress, runtime-coreclr libraries-jitstress

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@jakobbotsch
Copy link
Member Author

/azp run runtime-coreclr jitstress, runtime-coreclr libraries-jitstress

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@jakobbotsch jakobbotsch marked this pull request as ready for review March 23, 2023 23:12
@jakobbotsch
Copy link
Member Author

This passed jitstress with a failure that looked like #82690.

cc @dotnet/jit-contrib PTAL @AndyAyersMS @SingleAccretion

Use of GTF_DONT_CSE to detect locations is not that elegant but it is on par with other places in morph...

@jakobbotsch
Copy link
Member Author

Diffs seem to be due to new/missed CSE opportunities

The TP regressions are the same as in #83590 (comment). Apparently removing the call to MorphBlock from PrepareDst is enough to cause MSVC to change its inlining decisions for fgMorphCopyBlock and fgMorphInitBlock.

Copy link
Member

@AndyAyersMS AndyAyersMS left a comment

Choose a reason for hiding this comment

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

These can still be created explicitly?

Wondering at what point we can transition from "avoid creating more of these" to "avoid having these all together" ?

@jakobbotsch
Copy link
Member Author

These can still be created explicitly?

Wondering at what point we can transition from "avoid creating more of these" to "avoid having these all together" ?

There are no known cases where we create these anymore, and we have asserts in block morphing (and gtSplitTree) that the case doesn't happen anymore. So effectively after this PR it is an "illegal" IR shape and we can/do rely on it to not be created.

Copy link
Contributor

@SingleAccretion SingleAccretion left a comment

Choose a reason for hiding this comment

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

There is also some code which can be deleted/simplified:

// We have to handle the case where the LHS is a comma. In that case, we don't evaluate the comma,
// and we're really just interested in the effective value.
lhs = lhs->gtEffectiveVal();

GenTree* lhs = tree->AsOp()->gtOp1->gtEffectiveVal(/*commaOnly*/ true);

Nice to see the IR getting better :).

@@ -11504,6 +11502,13 @@ GenTree* Compiler::fgPropagateCommaThrow(GenTree* parent, GenTreeOp* commaThrow,
assert(fgGlobalMorph);
assert(fgIsCommaThrow(commaThrow));

bool mightBeLocation = (parent->OperIsLocal() || parent->OperIsIndir()) && ((parent->gtFlags & GTF_DONT_CSE) != 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

parent->OperIsLocal looks a bit unnecessary as LCL_VAR/LCL_FLD LHSs cannot have children, and STORE_LCL_VAR/FLD (when those start appearing here) won't need to be restricted like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed that check.

Comment on lines 1598 to 1599
// Data is a location.
tree->Data()->gtFlags |= GTF_DONT_CSE;
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're relying on this NO_CSE, seems prudent to set it in import as well.

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 decided to leave this here only, I think we should either set it on all locations in the importer or none of them, and today we don't set it on any of them. As you probably know we rely on this flag for the similar purpose in fgMorphLocalVar as well.

I guess today only the IND form of locations is allowed for STORE_DYN_BLK, but you could imagine if we allowed locations in general then something like STORE_DYN_BLK(LCL_VAR int, LCL_VAR int, 4) would have also been incorrectly handled without this.

@jakobbotsch
Copy link
Member Author

Failures are known according to build analysis.

@jakobbotsch jakobbotsch merged commit d795694 into dotnet:main Mar 25, 2023
@jakobbotsch jakobbotsch deleted the disallow-location-commas branch March 25, 2023 11:06
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