RFR: 8273454: C2: Transform (-a)*(-b) into a*b [v4]

Tobias Hartmann thartmann at openjdk.java.net
Thu Sep 16 07:26:51 UTC 2021


On Wed, 15 Sep 2021 15:51:55 GMT, Zhengyu Gu <zgu at openjdk.org> wrote:

>> test/hotspot/jtreg/compiler/integerArithmetic/TestNegMultiply.java line 45:
>> 
>>> 43:     private static void testInt(int a, int b) {
>>> 44:         int expected = (-a) * (-b);
>>> 45:         for (int i = 0; i < 20_000; i++) {
>> 
>> Why do you need a second loop in here? It's sufficient to set `TEST_COUNT` high enough to trigger compilation. I would suggest something like this:
>> 
>> 
>> private static int testInt(int a, int b) {
>>     return (-a) * (-b);
>> }
>> 
>> private static void runIntTests() {
>>     for (int i = 0; i < TEST_COUNT; i++) {
>>         int a = random.nextInt();
>>         int b = random.nextInt();
>>         int res = testInt(a, b);
>>         Asserts.assertEQ(a * b, res);
>>     }
>> }
>> 
>> 
>> And then run with `-XX:CompileCommand=dontinline,TestNegMultiply::test*`. No need to disable OnStackReplacement.
>
> The inner loop ensures that all tests hit JIT-ed version. If the transformation is broken, I would prefer the test fails for the very first iteration, instead of somewhere in the middle.
> 
> I refactored the code to remove inner loop.
> 
> Also, fixed command option.

You can't control the iteration in which the test would fail if there's a bug in C2 (it could only fail for **some** random values). Therefore, you could as well use random values for the warmup and simply increase `TEST_COUNT` to ensure that C2 compilation is triggered and we run a reasonable amount of iterations with C2 compiled code. 

Your newest version of the test now has the problem that OSR compilation might C2 compile the computation of the expected value and then you are comparing the output of a C2 compiled method to a C2 compiled method instead of the interpreter. You have the following options:
- Compute the expected value as `a * b`. In that case it's fine if the computation is C2 compiled as well.
- Prevent compilation of the `run*` methods (either by disabling OSR compilation or by completely disabling compilation of these methods)

-------------

PR: https://git.openjdk.java.net/jdk/pull/5403


More information about the hotspot-compiler-dev mailing list