mbutrovich opened a new issue, #27973:
URL: https://github.com/apache/datafusion/issues/27973

   ## Is your feature request related to a problem or challenge?
   
   With `-C target-cpu=x86-64-v3`, the loop that hashes primitive arrays 
without nulls 
([`hash_prim_fresh_dense`](https://github.com/apache/datafusion/blob/b9756bd146c4e1c498b72d8c22ab576cfbec2a57/datafusion/common/src/hash_utils.rs#L332-L346))
 costs as much as hashing the rows one at a time. With the default x86-64 
target it is 1.5x faster than the per-row loop.
   
   These are the baseline columns (`main` plus the benchmark cases from #27559) 
of two `with_hashes` runs on the benchmark bot on a Xeon Platinum 8581C. `no 
nulls` takes the dense loop. `nulls` has 3% random nulls and takes the per-row 
`valid_indices()` loop.
   
   | case | default 
([run](https://github.com/apache/datafusion/pull/27559#issuecomment-7045011123))
 | `x86-64-v3` 
([run](https://github.com/apache/datafusion/pull/27559#issuecomment-7045017844))
 |
   |---|---:|---:|
   | `int32: single, no nulls` | 4.2 us | 6.3 us |
   | `int32: single, nulls` | 6.2 us | 6.2 us |
   | `int64: single, no nulls` | 5.0 us | 5.5 us |
   | `int64: single, nulls` | 6.2 us | 6.1 us |
   
   The two columns come from separate runs, so compare rows within a column. 
With `x86-64-v3`, the dense loop gains nothing over the per-row loop for 
`int32` and 1.11x for `int64`, against 1.48x and 1.24x on the default target.
   
   #27559 uses this loop for 64-row chunks with at least 48 valid rows. With 
`x86-64-v3`, that makes 15% to 25% random nulls 8% to 16% slower than the 
per-row loop 
([discussion](https://github.com/apache/datafusion/pull/27559#discussion_r4727426189)).
 A faster loop would turn those cases into gains.
   
   ## Describe the solution you'd like
   
   The generated code suggests where to look. In 
`hash_array_primitive::<Date32Type, foldhash::fast::FixedState>` on b9756bd14 
(bench profile without LTO, `--target x86_64-apple-darwin`), neither target 
vectorizes the multiply. LLVM vectorizes the load, the xor with the seed, and 
the store, and moves every value between vector and general-purpose registers 
around a scalar 64x64-to-128-bit multiply. With `x86-64-v3` the moves out of 
the vector registers use `vextracti128` and `vpextrq`.
   
   <details><summary>Four rows of the 16-row loop on each target</summary>
   
   Default x86-64:
   
   ```asm
        movdqu xmm2, xmmword ptr [r12 + r13]
        movdqa xmm3, xmm2
        punpckhdq xmm3, xmm1
        punpckldq xmm2, xmm1
        pxor xmm2, xmm0
        pxor xmm3, xmm0
        pshufd xmm4, xmm3, 238
        movq r9, xmm4
        movq r10, xmm3
        pshufd xmm3, xmm2, 238
        movq rax, xmm2
        mul rsi
        mov rdi, rax
        mov r8, rdx
        movq rax, xmm3
        mul rsi
        mov r11, rdx
        mov r14, rax
        mov rax, r10
        mul rsi
        mov r10, rdx
        mov r15, rax
        mov rax, r9
        mul rsi
        xor rdx, rax
        xor r10, r15
        xor r11, r14
        xor r8, rdi
        movq xmm2, r8
        movq xmm3, r11
        punpcklqdq xmm2, xmm3
        movq xmm3, r10
        movq xmm4, rdx
        punpcklqdq xmm3, xmm4
        movdqu xmmword ptr [rbx + 2*r13 + 16], xmm3
        movdqu xmmword ptr [rbx + 2*r13], xmm2
   ```
   
   `x86-64-v3`:
   
   ```asm
        vpmovzxdq ymm1, xmmword ptr [r9 + r15]
        vpxor ymm1, ymm0, ymm1
        vextracti128 xmm2, ymm1, 1
        vpextrq rax, xmm2, 1
        vmovq rdi, xmm2
        vmovq r8, xmm1
        vpextrq rdx, xmm1, 1
        mulx r13, rdx, r12
        xor r13, rdx
        mov rdx, r8
        mulx r8, rdx, r12
        xor r8, rdx
        mov rdx, rax
        mulx r14, rax, r12
        xor r14, rax
        mov rdx, rdi
        mulx rdx, rax, r12
        xor rdx, rax
        vmovq xmm1, r13
        vmovq xmm2, r8
        vpunpcklqdq xmm1, xmm2, xmm1
        vmovq xmm2, r14
        vmovq xmm3, rdx
        vpunpcklqdq xmm2, xmm3, xmm2
        vmovdqu xmmword ptr [rbx + 2*r15 + 16], xmm2
        vmovdqu xmmword ptr [rbx + 2*r15], xmm1
   ```
   
   </details>
   
   The `x86-64-v3` loop runs fewer instructions (26 against 36 per 4 rows) but 
is slower. Contention for the shuffle port would explain that, but I haven't 
measured it. A loop that keeps the values in general-purpose registers, for 
example one that LLVM doesn't vectorize, would avoid the moves on both targets. 
It needs a `with_hashes` run on the bot with the default target and with 
`x86-64-v3`, and on Neoverse-V2 to check that aarch64 doesn't lose.
   
   ## Describe alternatives you've considered
   
   None.
   
   ## Additional context
   
   The listings come from a build without LTO, while the benchmark bot builds 
with thin LTO. They show the shape of the loop but not necessarily the exact 
code the bot ran.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to