Observations from a simple JMH benchmark

Vladimir Ivanov vladimir.x.ivanov at oracle.com
Fri Feb 16 12:38:26 UTC 2018


> 
> Better! still some artifacts:
> 
...
> 
> I think you can drop the first index check for case 2. IIUC you are assuming constraints vlen > 0 and length >= 0, so for:
> 
>    Objects.checkIndex(ix, length - (vlen - 1));
> 
> the check will fail if length - (vlen - 1) < 0.

 From correctness perspective, yes. But I was mostly concerned about C2 
and having 2 checks which mimicing ordinary range checks looked like a 
safer bet. Preconditions.checkIndex() is intrinsified as CmpI(length,0) 
+ CmpU(index,length) and strength-reduction to single CmpU works only if 
(length >= 0) which is the case for array length. So, the difference is 
CmpU + CmpU vs CmpI + CmpU.

I hoped that C2 could statically prove that (length - (vlen - 1)) >= 0 
after the first check and optimize it accordingly. But something goes wrong.

Briefly looking into generated IR, it seems compiler does prove (length 
- (vlen - 1)) >= 0, but (length - constant) shape confuses some other 
transformation, so hoisting isn't performed.

>   
> However, curiously, the single check makes no difference, the same code is generated.

Best regards,
Vladimir Ivanov

>> On Feb 15, 2018, at 10:44 AM, Vladimir Ivanov <vladimir.x.ivanov at oracle.com> wrote:
>>
>> Here's a quick prototype of enhanced array OOB checks:
>>
>>   http://cr.openjdk.java.net/~vlivanov/panama/vector.oob/webrev.00
>>
>> -Djdk.incubator.vector.VECTOR_ACCESS_OOB_CHECK:
>>
>> =0: no checks
>>
>> =1: use Objects.checkFromIndexSize (default)
>>
>> =2: enhanced check
>> +                // vlen > 0, length >=0
>> +                //
>> +                // 0 <= i, (i + vlen) <= length
>> +                //   =>
>> +                // 0 <= vlen <= length, 0 <= i < (length - vlen);
>> +                Objects.checkIndex(vlen - 1, length);
>> +                Objects.checkIndex(ix, length - (vlen - 1));
>>
>> Best regards,
>> Vladimir Ivanov
>>
>> On 2/15/18 3:58 PM, Vladimir Ivanov wrote:
>>>> I have been playing around with a simple benchmark and JMH (separately i can get asm hotspots working on the mac now via dtrace!)
>>> ...
>>>> Vladimir, i guess this is the kind of thing you were mentioning with regards to bounds checks?
>>> Yes.
>>>> Perhaps there are general optimization possibilities for such bounds checks. Only Preconditions.checkIndex is currently an intrinsic.
>>> I proposed some tweaks earlier [1], but haven't done any experiments yet:
>>>    0 <= i
>>>    (i + vlen) <= length
>>> ==>
>>>    (a) 0 <= i <= (length - vlen)
>>>    (b) (vlen <= length)
>>> a - upper bound is loop invariant
>>> b - loop invariant
>>> Best regards,
>>> Vladimir Ivanov
>>> [1] http://mail.openjdk.java.net/pipermail/panama-dev/2017-December/000889.html
> 


More information about the panama-dev mailing list