-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8349139: C2: Div looses dependency on condition that guarantees divisor not zero in counted loop #23617
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
Conversation
|
👋 Welcome back roland! A progress list of the required criteria for merging this PR into |
|
@rwestrel This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 1 new commit pushed to the
Please see this link for an up-to-date comparison between the source branch of this pull request and the ➡️ To integrate this PR with the above commit message to the |
|
@rwestrel this pull request can not be integrated into git checkout JDK-8349139
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push |
|
My understanding here is that when the backedge is removed, the loop head (which is a merge) is idealized out, and as a result, the I think this may be true for other kind of merge points as well. For example, if you perform conditional elimination (the patch you have worked at) on this code: Then we can be very clever and see that |
For any I thought about adding the
Are you talking about https://bugs.openjdk.org/browse/JDK-8275202 ? |
I think it is probably because we are more aggressive with the type of loop phis. Conceptually, a |
In general a Counted loops are a bit different: the loop's Trouble occurs when the loop is cloned by pre/main/post. Then the AFAICT, this issue really only exists for counted loop |
|
Hmmm, may be you are right. I think adding a comment at For this bug, I think a more general fix is to try to compare the type of the |
I don't think so. Consider this test case: It reproduces this issue and is actually a better test case because it doesn't even need All we know about the loop (one copy for each one). The 2 The type of the input to the main loop |
eme64
left a comment
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.
Looks reasonable. I have a few questions / suggestions.
I'm running some testing, please ping me again in a day or two ;)
| do { | ||
| i1 <<= i3; | ||
| } while (++i2 < 68); | ||
| for (i23 = 68; i23 > 2; otherPhi=i23-1, i23--) { |
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 is essencially the test from #3190, we only changed the 3 to a 2. This indicates that we need to probably slightly generalize the test. Can we maybe randomize the constant, just to get a little better coverage?
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.
Because the other test became ineffective, and it would be a shame if the same happened to this one ;)
src/hotspot/share/opto/loopnode.cpp
Outdated
| // skip over the cast added by PhaseIdealLoop::cast_incr_before_loop() when pre/post/main loops are created because | ||
| // it can get in the way of type propagation |
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.
I think it would be nice if you said more about how it can get in the way of type propagation. Why would we sometimes have uncast on and sometimes off? You may even have a quick comment about it at the use-site.
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.
I improved the comment in the new commit.
| initialize_assertion_predicates_for_post_loop(main_head, post_head, first_node_index_in_cloned_loop_body); | ||
| } | ||
| // CastII for the new post loop: | ||
| cast_incr_before_loop(zer_opaq->in(1), zer_taken, post_head); |
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.
I see it is added for the main and post loop. Why not for the pre loop?
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.
The type of the trip phi of the loop before pre/main/post is computed from the type of the init value (input 1 of the trip phi) and the limit input to the exit condition. The way the trip phi type is computed, it can't be narrower than the init value type. If the loop body has a Div node and its control dependency is cleared because the divisor is known not zero from the type of the trip phi, then the divisor computed from the init value is also not zero and if the backedge of the loop is removed, the Div node can float.
i.e.
for (int i = start; i < stop; i += stride) {
v = x / i;
}
if i not zero, it's because its type is computed from start and start is not zero so, if, for some reason, the loop runs for a single iteration:
v = x/start;
can float as high as the control of start.
Same reasoning applies to the pre loop because the trip phi of that loop "includes" the whole type of the init value. But that doesn't work for the main (or post loop) because its trip phi gets its type from the loop before transformation and it captures the type of the init value for what is now the pre loop which is not the same as the type of the init value for the main (or post loop).
In the example above, let's say start has type [min, -1] , stop is 0 and stride is 1. i's type is [min, -1]. That doesn't include 0 so the Div is free to float. Now once pre/main/post loops are created, the type of the trip phi for the main loop is [min, -1]. Now let's say the main loop looses its back edge. The Div in the main loop can float and say the value for i out of the pre loop is 0. Now the floating Div from the main loop can trigger a fault.
|
Ah, I only just now read the comments from @merykitty and you. Oops. Hmm. Yes it seems that the
Are you saying this is another test? I'd really be happy if we had more tests for this case, because the current version seems fragile, since it is an almost perfect copy of a previous test that became ineffective this makes me even more nervous 😅 |
There's no About the |
|
@merykitty see above my late reply to your comments if you missed it. |
|
@rwestrel I have thought about this issue for a while and come to the conclusion that we do depend on a loop phi being a pinned node when doing optimizations (e.g |
I added that test case. |
|
@eme64 the PR is ready for reviews in case you have time. |
eme64
left a comment
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.
I think this looks reasonable.
test/hotspot/jtreg/compiler/controldependency/TestDivDependentOnMainLoopGuard.java
In this test you could also randomize the 2 I commented on. Because in #3190 you had the same test but with a constant 3. A public static final int with a random value in a reasonable range could do the trick.
| * @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileOnly=TestDivDependentOnMainLoopGuard::* | ||
| * -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:StressSeed=139899009 TestDivDependentOnMainLoopGuard | ||
| * @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileOnly=TestDivDependentOnMainLoopGuard::* | ||
| * -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM TestDivDependentOnMainLoopGuard |
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.
Would it make sense to have a run without some of the flags here?
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.
Done.
|
Just submitted some more testing :) |
Updated the test. Does it look like what you had in mind? |
Thanks. Any update on test results? @eme64 |
eme64
left a comment
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.
Looks good, except for the whitespace issue :)
| * @run main/othervm -Xcomp -XX:-TieredCompilation -XX:CompileOnly=TestDivDependentOnMainLoopGuard::* | ||
| * -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM TestDivDependentOnMainLoopGuard | ||
| * @run main/othervm -Xcomp -XX:CompileOnly=TestDivDependentOnMainLoopGuard::* TestDivDependentOnMainLoopGuard | ||
| * |
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.
| * |
You got some whitespace issues here, we could just remove the line :)
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.
Thanks. I missed that. Fixed now.
|
@merykitty @eme64 thanks for the reviews. |
eme64
left a comment
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.
Looks good, thanks for the work @rwestrel 😊
chhagedorn
left a comment
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.
Looks good to me, too.
Nit: You should probably update the title: "divisor not null" -> "divisor not zero".
src/hotspot/share/opto/loopnode.cpp
Outdated
| // skip over the cast added by PhaseIdealLoop::cast_incr_before_loop() when pre/post/main loops are created because | ||
| // it can get in the way of type propagation. For instance, the index tested by an assert predicate, if the cast is | ||
| // not skipped over, could be (1): | ||
| // (AddI (CastII (AddI pre_loop_iv -2) int) 1) | ||
| // while without the cast, it is (2): | ||
| // (AddI (AddI pre_loop_iv -2) 1) | ||
| // which is be transformed to (3): | ||
| // (AddI pre_loop_iv -1) | ||
| // The compiler may be able to constant fold the assert predicate condition for (3) but not (1) |
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.
| // skip over the cast added by PhaseIdealLoop::cast_incr_before_loop() when pre/post/main loops are created because | |
| // it can get in the way of type propagation. For instance, the index tested by an assert predicate, if the cast is | |
| // not skipped over, could be (1): | |
| // (AddI (CastII (AddI pre_loop_iv -2) int) 1) | |
| // while without the cast, it is (2): | |
| // (AddI (AddI pre_loop_iv -2) 1) | |
| // which is be transformed to (3): | |
| // (AddI pre_loop_iv -1) | |
| // The compiler may be able to constant fold the assert predicate condition for (3) but not (1) | |
| // skip over the cast added by PhaseIdealLoop::cast_incr_before_loop() when pre/post/main loops are created because | |
| // it can get in the way of type propagation. For instance, the index tested by an Assertion Predicate, if the cast is | |
| // not skipped over, could be (1): | |
| // (AddI (CastII (AddI pre_loop_iv -2) int) 1) | |
| // while without the cast, it is (2): | |
| // (AddI (AddI pre_loop_iv -2) 1) | |
| // which is be transformed to (3): | |
| // (AddI pre_loop_iv -1) | |
| // The compiler may be able to constant fold the Assertion Predicate condition for (3) but not (1) |
Thanks for reviewing this. I made the changes you suggested. |
chhagedorn
left a comment
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.
Thanks for the update!
|
@chhagedorn thanks for the review. |
|
/integrate |
The test crashes because of a division by zero. The
Divnode forthat one is initially part of a counted loop. The control input of the
node is cleared because the divisor is non zero. This is because the
divisor depends on the loop phi and the type of the loop phi is
narrowed down when the counted loop is created. pre/main/post loops
are created, unrolling happens, the main loop looses its backedge. The
Divnode can then float above the zero trip guard for the mainloop. When the zero trip guard is not taken, there's no guarantee the
divisor is non zero so the
Divnode should be pinned below it.I propose we revert the change I made with 8334724 which removed
PhaseIdealLoop::cast_incr_before_loop(). TheCastIIthat thismethod inserted was there to handle exactly this problem. It was added
initially for a similar issue but with array loads. That problem with
loads is handled some other way now and that's why I thought it was
safe to proceed with the removal.
The code in this patch is somewhat different from the one we had
before for a couple reasons:
1- assert predicate code evolved and so previous logic can't be
resurrected as it was.
2- the previous logic has a bug.
Regarding 1-: during pre/main/post loop creation, we used to add the
CastIIand then to add assertion predicates (so assertion predicatesdepended on the
CastII). Then when unrolling, when assertionpredicates are updated, we would skip over the
CastII. What Ipropose here is to add the
CastIIafter assertion predicates areadded. As a result, they don't depend on the
CastIIand there's noneed for any extra logic when unrolling happens. This, however,
doesn't work when the assertion predicates are added by RCE. In that
case, I had to add logic to skip over the
CastII(similar to whatexisted before I removed it).
Regarding 2-: previous implementation for
PhaseIdealLoop::cast_incr_before_loop()would add theCastIIatthe first loop
Phiit encounters that's a use of the loop increment:it's usually the iv but not always. I tweaked the test case to show,
this bug can actually cause a crash and changed the logic for
PhaseIdealLoop::cast_incr_before_loop()accordingly.Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/23617/head:pull/23617$ git checkout pull/23617Update a local copy of the PR:
$ git checkout pull/23617$ git pull https://git.openjdk.org/jdk.git pull/23617/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 23617View PR using the GUI difftool:
$ git pr show -t 23617Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/23617.diff
Using Webrev
Link to Webrev Comment