-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Conversation
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsA draft to figure out if I'm on a right way.
|
931f18c
to
90a110a
Compare
/azp run runtime |
Commenter does not have sufficient privileges for PR 74314 in repo dotnet/runtime |
@jakobbotsch Hey, could you please review this PR? |
@EgorBo Hey, could you please check the PR? |
@SkiFoD Do you want to open an issue for that too? |
@danmoseley Hey. Created an issue #75413 for the bug. |
cc2f94f
to
03f5404
Compare
@EgorBo I updated the PR. |
@EgorBo PTAL |
Oops, sorry for the delay, will review tomorrow |
@EgorBo PTAL |
@EgorBo ping |
src/coreclr/jit/morph.cpp
Outdated
@@ -10313,6 +10313,21 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA | |||
|
|||
#endif // defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64) | |||
|
|||
#if defined(TARGET_XARCH) |
There was a problem hiding this comment.
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?
src/coreclr/jit/morph.cpp
Outdated
if (optimizedTree != nullptr) | ||
{ | ||
tree = optimizedTree; | ||
if (tree->IsIntegralConst()) |
There was a problem hiding this comment.
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?
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) |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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?
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 |
75b6855
to
13a7e54
Compare
CC @TIHan will look into this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pull request has been automatically marked |
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. |
Let's focus on a more impactful optimization (in DM) |
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.