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

Optimizing a series of divisions (#74020) #74314

Closed
wants to merge 8 commits into from

Conversation

SkiFoD
Copy link
Contributor

@SkiFoD SkiFoD commented Aug 21, 2022

Fixes #74020

There was a cascade of DIVs/UDIVs generated for code snippets like this.
I tried to handle the overflow scenario like this
Now it folds into one DIV and the result of multiplication of parent and child values, if it is possible.

The same happens with MUL (example), but I decided to not touch it right now, because there is an optimization that changes MUL to Shifts. It complicates everything a little bit.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Aug 21, 2022
@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 Aug 21, 2022
@ghost
Copy link

ghost commented Aug 21, 2022

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

Issue Details

A draft to figure out if I'm on a right way.

Author: SkiFoD
Assignees: -
Labels:

area-CodeGen-coreclr, community-contribution

Milestone: -

@SkiFoD
Copy link
Contributor Author

SkiFoD commented Aug 23, 2022

/azp run runtime

@azure-pipelines
Copy link

Commenter does not have sufficient privileges for PR 74314 in repo dotnet/runtime

@SkiFoD SkiFoD changed the title Draft of optimizing a series of divisions (#74020) Optimizing a series of divisions (#74020) Aug 24, 2022
@SkiFoD SkiFoD marked this pull request as ready for review August 24, 2022 07:16
@SkiFoD
Copy link
Contributor Author

SkiFoD commented Aug 24, 2022

@jakobbotsch Hey, could you please review this PR?

src/coreclr/jit/morph.cpp Outdated Show resolved Hide resolved
src/coreclr/jit/morph.cpp Outdated Show resolved Hide resolved
@JulieLeeMSFT JulieLeeMSFT added this to the 8.0.0 milestone Aug 24, 2022
@SkiFoD SkiFoD requested a review from EgorBo August 30, 2022 06:11
@SkiFoD
Copy link
Contributor Author

SkiFoD commented Sep 3, 2022

@EgorBo Hey, could you please check the PR?

@danmoseley
Copy link
Member

The same happens with MUL

@SkiFoD Do you want to open an issue for that too?

@SkiFoD
Copy link
Contributor Author

SkiFoD commented Sep 11, 2022

The same happens with MUL

@SkiFoD Do you want to open an issue for that too?

@danmoseley Hey. Created an issue #75413 for the bug.

@JulieLeeMSFT
Copy link
Member

@EgorBo, this community PR is ready for another review.

@EgorBo Hey, could you please check the PR?

@SkiFoD
Copy link
Contributor Author

SkiFoD commented Oct 12, 2022

@EgorBo I updated the PR.

@BruceForstall
Copy link
Member

@EgorBo PTAL

@EgorBo
Copy link
Member

EgorBo commented Oct 24, 2022

Oops, sorry for the delay, will review tomorrow

@BruceForstall
Copy link
Member

@EgorBo PTAL

@BruceForstall
Copy link
Member

@EgorBo ping

@@ -10313,6 +10313,21 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA

#endif // defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64)

#if defined(TARGET_XARCH)
Copy link
Member

Choose a reason for hiding this comment

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

Why it's limited to xarch only?

if (optimizedTree != nullptr)
{
tree = optimizedTree;
if (tree->IsIntegralConst())
Copy link
Member

Choose a reason for hiding this comment

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

it doesn't look like this condition can possibly be true?

// Make sure it is not folded into overflow
// If it is going to overflow, then result of such a division
// is 0
if ((upperBound / child_val) < root_val)
Copy link
Member

Choose a reason for hiding this comment

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

potential division by zero in jit code if child_val is 0

GenTree* chOp2 = op1->gtGetOp2();

IntegralRange boundRange = IntegralRange::ForNode(op2, this);
int64_t upperBound = IntegralRange::SymbolicToRealValue(boundRange.GetUpperBound());
Copy link
Member

Choose a reason for hiding this comment

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

it's not clear to me why IntegralRange is used here - you already have child_val and root_val, don't you?

@EgorBo
Copy link
Member

EgorBo commented Jan 4, 2023

From what I see locally this PR only hits a single diff across all collections so it needs unit tests at least, e.g. the division by zero case. e.g. this code:

uint foo() => 0;

uint ff(uint u2) => u2 / foo() / 4;

crashes jit in this PR

@JulieLeeMSFT
Copy link
Member

CC @TIHan will look into this.

Copy link
Contributor

@TIHan TIHan left a comment

Choose a reason for hiding this comment

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

Overall, this looks fine. There is an outstanding @EgorBo comment regarding the IntegralRange and why we need it.

I would suggest that we add a few disasm check tests, see here for details on how to do this.

@ghost ghost added the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 14, 2023
@ghost ghost added the no-recent-activity label Mar 1, 2023
@ghost
Copy link

ghost commented Mar 1, 2023

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@SkiFoD
Copy link
Contributor Author

SkiFoD commented Mar 6, 2023

Overall, this looks fine. There is an outstanding @EgorBo comment regarding the IntegralRange and why we need it.

I would suggest that we add a few disasm check tests, see here for details on how to do this.

The code changes produce too small diffs, so I'm not sure if it is worth it to keep the code or it is just better to reject the PR.

@ghost ghost removed needs-author-action An issue or pull request that requires more info or actions from the author. no-recent-activity labels Mar 6, 2023
@EgorBo
Copy link
Member

EgorBo commented Mar 27, 2023

Let's focus on a more impactful optimization (in DM)

@EgorBo EgorBo closed this Mar 27, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 26, 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 community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary shr emitted for repeated multiplication
6 participants