On Fri, 20 Nov 2026 07:25:50 GMT, Jatin Bhateja <[email protected]> wrote:

>> Hi All,
>> 
>> On x86, C2 currently implements vector count trailing zeros using following 
>> formula :-
>> 
>> `        ctz(x) = width - clz((x - 1) & ~x)
>> `
>> On targets supporting AVX-512 BITALG or AVX512_VPOPCNTDQ features , we can 
>> use following shorter identity:-
>> 
>> `       ctz(x) = popcount((x - 1) & ~x)
>> `
>> This will enable `vpopcntb/w` for byte/short lanes and `vpopcntd/q` for 
>> int/long lanes. Existing lowering should remain as the fallback when these 
>> CPU features are unavailable.
>> 
>> Following are the performance number of benchmark included with the patch.
>> 
>> 
>> System: AMD EPYC 9755 128-Core Processor (Turin) Fixed frequency 2.5Ghz
>> 
>> Baseline:-
>> ----------
>> Benchmark                                           Mode  Cnt     Score   
>> Error   Units
>> VectorCountTrailingZerosBenchmark.byteThroughput   thrpt    2   569.742      
>>     ops/us
>> VectorCountTrailingZerosBenchmark.intThroughput    thrpt    2  1378.783      
>>     ops/us
>> VectorCountTrailingZerosBenchmark.longThroughput   thrpt    2  1154.720      
>>     ops/us
>> VectorCountTrailingZerosBenchmark.shortThroughput  thrpt    2   717.462      
>>     ops/us
>> VectorCountTrailingZerosBenchmark.byteLatency       avgt    2     7.848      
>>      ns/op
>> VectorCountTrailingZerosBenchmark.intLatency        avgt    2     3.328      
>>      ns/op
>> VectorCountTrailingZerosBenchmark.longLatency       avgt    2     3.284      
>>      ns/op
>> VectorCountTrailingZerosBenchmark.shortLatency      avgt    2     5.017      
>>      ns/op
>> 
>> Withopt:-
>> ----------
>> Benchmark                                           Mode  Cnt     Score   
>> Error   Units
>> VectorCountTrailingZerosBenchmark.byteThroughput   thrpt    2  1174.785      
>>     ops/us
>> VectorCountTrailingZerosBenchmark.intThroughput    thrpt    2  1600.252      
>>     ops/us
>> VectorCountTrailingZerosBenchmark.longThroughput   thrpt    2  1396.755      
>>     ops/us
>> VectorCountTrailingZerosBenchmark.shortThroughput  thrpt    2  1193.041      
>>     ops/us
>> VectorCountTrailingZerosBenchmark.byteLatency       avgt    2     2.352      
>>      ns/op
>> VectorCountTrailingZerosBenchmark.intLatency        avgt    2     2.344      
>>      ns/op
>> VectorCountTrailingZerosBenchmark.longLatency       avgt    2     2.345      
>>      ns/op
>> VectorCountTrailingZerosBenchmark.shortLatency      avgt    2     2.338      
>>      ns/op
>> 
>> 
>> Kindly review and share your feedback.
>> 
>> Best Regards,
>> Jatin
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK I...
>
> Jatin Bhateja has updated the pull request with a new target base due to a 
> merge or a rebase. The incremental webrev excludes the unrelated changes 
> brought in by the merge/rebase. The pull request contains four additional 
> commits since the last revision:
> 
>  - Merge branch 'master' of http://github.com/openjdk/jdk into JDK-8393511
>  - Review comments resolution
>  - Adding comments
>  - 8393511: Optimize x86 vector count trailing zeros using popcount 
> instructions

src/hotspot/cpu/x86/x86.ad line 2880:

> 2878:   BasicType bt = Matcher::vector_element_basic_type(n);
> 2879:   int vlen_bytes = (int)Matcher::vector_length_in_bytes(n);
> 2880:   if (bt == T_BYTE || bt == T_SHORT || bt == T_INT || bt == T_LONG) {

It looks to me that this if check can be removed; is_vector_popcount_predicate 
covers this.

src/hotspot/cpu/x86/x86.ad line 23513:

> 23511:   match(Set dst (CountTrailingZerosV src));
> 23512:   effect(TEMP xtmp);
> 23513:   ins_cost(100);

The ins_cost can be removed.

src/hotspot/cpu/x86/x86.ad line 23520:

> 23518:     // ctz(x) = popcnt((x - 1) & ~x). See "Hacker's Delight" by Henry 
> Warren.
> 23519:     // Targets with AVX512_VPOPCNTDQ or AVX512_BITALG have a direct 
> vpopcnt.
> 23520:     __ vpxor($xtmp$$XMMRegister, $xtmp$$XMMRegister, 
> $xtmp$$XMMRegister, vlen_enc);

Now that we use vpandn,  do we still need the vpxor to clear xtmp?

src/hotspot/cpu/x86/x86.ad line 23522:

> 23520:     __ vpxor($xtmp$$XMMRegister, $xtmp$$XMMRegister, 
> $xtmp$$XMMRegister, vlen_enc);
> 23521:     // xtmp = -1
> 23522:     __ vpternlogd($xtmp$$XMMRegister, 0xff, $xtmp$$XMMRegister, 
> $xtmp$$XMMRegister, vlen_enc);

Could use __ vallones($xtmp$$XMMRegister, vlen_enc) here.

test/hotspot/jtreg/compiler/vectorapi/TestVectorCountTrailingZeros.java line 78:

> 76:             srcB[i] = 0;
> 77:             if (1 < BSP.length()) {
> 78:                 srcB[i + 1] = (byte) (1 << (i % 7));

We should use i % 8 here, likewise i % 16, etc in loops below.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/33863#discussion_r4759648656
PR Review Comment: https://git.openjdk.org/jdk/pull/33863#discussion_r4759796256
PR Review Comment: https://git.openjdk.org/jdk/pull/33863#discussion_r4759903886
PR Review Comment: https://git.openjdk.org/jdk/pull/33863#discussion_r4762763740
PR Review Comment: https://git.openjdk.org/jdk/pull/33863#discussion_r4762853494

Reply via email to