-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8366815: C2: Delay Mod/Div by constant transformation #27886
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
base: master
Are you sure you want to change the base?
Conversation
👋 Welcome back hgreule! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
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.
Thank you for working on this, @SirYwell. This seems like a tricky problem. To be honest, the fix seems a bit hacky. Have you explored any alternatives to this method of delaying the optimizations?
I kicked off some testing in the meantime that I will report back upon completion.
// Keep this node as-is for now; we want Value() and | ||
// other optimizations checking for this node type to work |
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.
// Keep this node as-is for now; we want Value() and | |
// other optimizations checking for this node type to work | |
// Keep this node as-is initially; we want Value() and | |
// other optimizations checking for this node type to work. |
This is a small nit: I find it confusing to talk about "for now" in a method that is called at almost every stage of the compilation. Perhaps "initially" conveys the intention of first letting other optimizations do their magic first a bit better.
Thanks for running tests. I tried delaying until post loop opts, but that prevents some vectorization and isn't really less hacky I guess. I didn't find any other good existing approach. Calculating Value before Ideal would work, but I assume that it is rarely useful, with Div/Mod being an exception. I a dream world, I guess we would have e-graphs or something similar, which would allow calculating a more precise type from different alternatives. If you can think of a better approach, please let me know. |
Thinking about it a bit more, I think your fix is too superficial. If the discovery of the constant is slightly delayed, nothing is folded again. Consider the followig program for an example: class Test {
static boolean test(int x, boolean flag) {
Integer a;
if (flag) {
a = 171384;
} else {
a = 2902;
}
return x % a >= a;
}
public static void main(String[] args) {
for (int i = 0; i < 20000; i++) {
if (test(i, false)) {
throw new RuntimeException("wrong result");
}
}
}
} In my opinion, the benefits do not outweigh the drawbacks for this PR. A better solution would probably be to delay the expansion of the Mod and Div nodes to post-loop optimizations and extend Superword to expand Div/Mod nodes to shifts. However, this is quite a bit of complexity, which raises if this complexity is worth it (@eme64 probably has opinions and/or guidance on this). |
I'm not sure about the drawbacks here, but I think optimizing this on the superword level doesn't make things less complicated. If cases where we end up idealizing before calling Value are a more general problem, I'd say it's worth to also address it on exactly that level: make sure that Value is called before Ideal. I'm just hesitant because I'm not aware of any other situations where this matters. One middle ground here would be some kind of |
The test cases show examples of code where
Value()
previously wasn't run because idealization took place before, resulting in less precise type analysis.Please let me know what you think.
Progress
Issue
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27886/head:pull/27886
$ git checkout pull/27886
Update a local copy of the PR:
$ git checkout pull/27886
$ git pull https://git.openjdk.org/jdk.git pull/27886/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 27886
View PR using the GUI difftool:
$ git pr show -t 27886
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27886.diff
Using Webrev
Link to Webrev Comment