On Thu, 12 Nov 2026 07:25:42 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 
> Interim AI Policy](https://openjdk.org/legal/ai).

Overall, looks good.

I don't see any test cases introduced. Does it make sense to validate new shape 
with targeted IR tests?

Minor questions/suggestions follow.

src/hotspot/cpu/x86/c2_MacroAssembler_x86.cpp line 6237:

> 6235: //      CTZ = POPC((x - 1) & ~x)
> 6236: // Ref: "Hacker's Delight" by Henry Warren
> 6237: void C2_MacroAssembler::vector_count_trailing_zeros_evex_popc(BasicType 
> bt, XMMRegister dst, XMMRegister src,

Any particular reason to introduce 
`C2_MacroAssembler::vector_count_trailing_zeros_evex_popc()`? If it is not 
intended to be shared, why not simply putting it inside 
`vcount_trailing_zeros_popcnt_evex`?

src/hotspot/cpu/x86/c2_MacroAssembler_x86.cpp line 6247:

> 6245:   vpadd(bt, xtmp, xtmp, src, vec_enc);
> 6246:   // xtmp = xtmp & ~src
> 6247:   vpternlogd(xtmp, 0x40, xtmp, src, vec_enc);

Any particular reason to use `vpternlogd` here? `vpandn` does the job and is 
slightly more compact, isn't it?

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

> 2880: 
> 2881: static inline bool is_vector_ctz_popcnt_evex(BasicType bt, int 
> vlen_bytes) {
> 2882:   if (!(bt == T_BYTE || bt == T_SHORT || bt == T_INT || bt == T_LONG)) {

Nit: I suggest to revert it. IMO it becomes easier to follow the logic. 

  if (bt == T_BYTE || bt == T_SHORT || bt == T_INT || bt == T_LONG) {
    return is_vector_popcount_predicate(bt) &&
           (VM_Version::supports_avx512vl() || vlen_bytes == 64);
  }
  return false;
``` 

Also, predicate can be simplified by passing `n->in(1)` into the helper:

static inline bool is_vector_ctz_popcnt_evex(Node* n) {
  assert(n->bottom_type()->isa_vect(), "not a vector");
  BasicType bt = Matcher::vector_element_basic_type(n);
  int vlen_bytes = Matcher::vector_length_in_bytes(n);
  ...
}
...
predicate(is_vector_ctz_popcnt_evex(n->in(1)));

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

PR Review: https://git.openjdk.org/jdk/pull/33863#pullrequestreview-5986081998
PR Review Comment: https://git.openjdk.org/jdk/pull/33863#discussion_r4637319655
PR Review Comment: https://git.openjdk.org/jdk/pull/33863#discussion_r4637355949
PR Review Comment: https://git.openjdk.org/jdk/pull/33863#discussion_r4637312250

Reply via email to