Skip to content

Optimizing a series of divisions (#74020) - #74314

Closed
SkiFoD wants to merge 8 commits into
dotnet:mainfrom
SkiFoD:skifod/issue-74020
Closed

SkiFoD wants to merge 8 commits into
dotnet:mainfrom
SkiFoD:skifod/issue-74020

Conversation

@SkiFoD

@SkiFoD SkiFoD commented Aug 21, 2022 •

Copy link
Copy Markdown
Contributor

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 community-contribution Indicates that the PR has been added by a community member area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI labels Aug 21, 2022
@ghost

ghost commented Aug 21, 2022

Copy link
Copy Markdown

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
SkiFoD force-pushed the skifod/issue-74020 branch from 931f18c to 90a110a Compare August 22, 2022 19:51
@SkiFoD

SkiFoD commented Aug 23, 2022

Copy link
Copy Markdown
Contributor Author

/azp run runtime

@azure-pipelines

Copy link
Copy Markdown
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

SkiFoD commented Aug 24, 2022

Copy link
Copy Markdown
Contributor Author

@jakobbotsch Hey, could you please review this PR?

Comment thread src/coreclr/jit/morph.cpp Resolved
Comment thread src/coreclr/jit/morph.cpp 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

SkiFoD commented Sep 3, 2022

Copy link
Copy Markdown
Contributor Author

@EgorBo Hey, could you please check the PR?

@danmoseley

Copy link
Copy Markdown
Contributor

The same happens with MUL

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

@SkiFoD

SkiFoD commented Sep 11, 2022

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Member

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

@EgorBo Hey, could you please check the PR?

@SkiFoD
SkiFoD force-pushed the skifod/issue-74020 branch from cc2f94f to 03f5404 Compare October 12, 2022 17:47
@SkiFoD

SkiFoD commented Oct 12, 2022

Copy link
Copy Markdown
Contributor Author

@EgorBo I updated the PR.

@BruceForstall

Copy link
Copy Markdown
Contributor

@EgorBo PTAL

@EgorBo

EgorBo commented Oct 24, 2022

Copy link
Copy Markdown
Member

Oops, sorry for the delay, will review tomorrow

@BruceForstall

Copy link
Copy Markdown
Contributor

@EgorBo PTAL

@BruceForstall

Copy link
Copy Markdown
Contributor

@EgorBo ping

Comment thread src/coreclr/jit/morph.cpp Outdated

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

#if defined(TARGET_XARCH)

Copy link
Copy Markdown
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?

Comment thread src/coreclr/jit/morph.cpp Outdated
if (optimizedTree != nullptr)
{
tree = optimizedTree;
if (tree->IsIntegralConst())

Copy link
Copy Markdown
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?

Comment thread src/coreclr/jit/morph.cpp Outdated
// 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
Copy Markdown
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

Comment thread src/coreclr/jit/morph.cpp
GenTree* chOp2 = op1->gtGetOp2();

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

Copy link
Copy Markdown
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

EgorBo commented Jan 4, 2023

Copy link
Copy Markdown
Member

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

@SkiFoD
SkiFoD force-pushed the skifod/issue-74020 branch from 75b6855 to 13a7e54 Compare January 22, 2023 18:19
@JulieLeeMSFT
JulieLeeMSFT requested a review from TIHan February 13, 2023 17:50
@JulieLeeMSFT

Copy link
Copy Markdown
Member

CC @TIHan will look into this.

@TIHan TIHan left a comment •

Copy link
Copy Markdown
Contributor

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

ghost commented Mar 1, 2023

Copy link
Copy Markdown

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

SkiFoD commented Mar 6, 2023

Copy link
Copy Markdown
Contributor Author

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

EgorBo commented Mar 27, 2023

Copy link
Copy Markdown
Member

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