This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new 747e87e8 perf(compute): add AVX2 take-index compaction (#1348)
747e87e8 is described below
commit 747e87e83c44579837d2cd6f9143bd97b51591e0
Author: Minh Vu <[email protected]>
AuthorDate: Mon Oct 19 18:18:43 2026 +0200
perf(compute): add AVX2 take-index compaction (#1348)
### What
- Add AVX2 compaction for fragmented uint32 take indices on amd64.
### Why
- Fragmented filters make scalar set-bit run expansion expensive.
### Implementation
- Reuse the uint32 shuffle tables. Use AVX2 for non-null, byte-aligned
filters that pass the 256-byte fragmentation sample, and keep the scalar
and NEON fallbacks.
- Cover tails, offsets, nullable filters, and fallback cases. Benchmark
all/none, run, alternating, short-run, and random 25/50/75% masks. The
1K case is a fallback control because it uses uint16 indices.
- Local compaction-only medians on AMD EPYC 9V74 (64K / 1M): alternating
3.89x / 4.01x; short runs 1.64x / 1.64x; random 25% 2.28x / 1.47x, 50%
2.65x / 4.50x, and 75% 2.67x / 4.00x; long runs 0.99x / 1.00x. These are
kernel-only results, not full `BenchmarkGetTakeIndices` results.
---------
Co-authored-by: Matt Topol <[email protected]>
---
arrow/compute/internal/kernels/Makefile | 17 +-
.../kernels/_lib/get_take_indices_avx2_amd64.cc | 91 ++++++++++
.../kernels/_lib/get_take_indices_avx2_amd64.s | 121 +++++++++++++
.../kernels/get_take_indices_avx2_amd64.go | 103 +++++++++++
.../internal/kernels/get_take_indices_avx2_amd64.s | 118 ++++++++++++
.../kernels/get_take_indices_avx2_amd64_test.go | 201 +++++++++++++++++++++
.../kernels/get_take_indices_avx2_noasm.go | 29 +++
arrow/compute/internal/kernels/vector_selection.go | 3 +
.../kernels/vector_selection_bench_test.go | 21 ++-
9 files changed, 699 insertions(+), 5 deletions(-)
diff --git a/arrow/compute/internal/kernels/Makefile
b/arrow/compute/internal/kernels/Makefile
index d33a8a9a..5024d503 100644
--- a/arrow/compute/internal/kernels/Makefile
+++ b/arrow/compute/internal/kernels/Makefile
@@ -42,7 +42,7 @@ INTEL_SOURCES := \
cast_numeric_avx2_amd64.s cast_numeric_sse4_amd64.s
constant_factor_avx2_amd64.s \
constant_factor_sse4_amd64.s base_arithmetic_avx2_amd64.s
base_arithmetic_sse4_amd64.s \
scalar_comparison_avx2_amd64.s scalar_comparison_sse4_amd64.s \
- filter_uint32_avx2_amd64.s filter_uint64_avx2_amd64.s
filter_uint16_avx2_amd64.s
+ filter_uint32_avx2_amd64.s filter_uint64_avx2_amd64.s
filter_uint16_avx2_amd64.s get_take_indices_avx2_amd64.s
#
# ARROW-15336: DO NOT add the assembly target for Arm64 (ARM_SOURCES) until
c2goasm added the Arm64 support.
@@ -76,6 +76,12 @@ _lib/scalar_comparison_sse4_amd64.s:
_lib/scalar_comparison.cc
_lib/filter_uint32_avx2_amd64.s: _lib/filter_uint32.cc
$(CXX) -std=c++17 -S $(C_FLAGS) $(ASM_FLAGS_AVX2) $^ -o $@ ;
$(PERL_FIXUP_ROTATE) $@
+_lib/get_take_indices_avx2_amd64.s: _lib/get_take_indices_avx2_amd64.cc
+ # Keep saves/restores in one prologue/epilogue for c2goasm to remove.
+ $(CXX) -std=c++17 -S $(C_FLAGS) $(ASM_FLAGS_AVX2) -mno-stackrealign
-fomit-frame-pointer \
+ -mllvm -enable-shrink-wrap=false -mllvm
-tail-dup-placement=false $^ -o $@
+ $(PERL_FIXUP_ROTATE) $@
+
_lib/filter_uint64_avx2_amd64.s: _lib/filter_uint64.cc
$(CXX) -std=c++17 -S $(C_FLAGS) $(ASM_FLAGS_AVX2) -mno-stackrealign
-fomit-frame-pointer $^ -o $@ ; $(PERL_FIXUP_ROTATE) $@
@@ -121,6 +127,15 @@ scalar_comparison_sse4_amd64.s:
_lib/scalar_comparison_sse4_amd64.s
filter_uint32_avx2_amd64.s: _lib/filter_uint32_avx2_amd64.s
$(C2GOASM) -a -f $^ $@
+get_take_indices_avx2_amd64.s: _lib/get_take_indices_avx2_amd64.s
+ # c2goasm drops callee-saved pushes, so BP must not be used even as a
scratch register.
+ perl -ne 'die "c2goasm cannot preserve BP: $$_" if
/\b(?:rbp|ebp|bp|bpl)\b/' $<
+ # Multiple returns can leave native assembly directives in the Go
output.
+ perl -ne '$$returns += /^\s*ret\s*$$/; END { die "c2goasm requires a
single return\n" unless $$returns == 1 }' $<
+ $(C2GOASM) -a -f $^ $@
+ perl -i -pe 's{^//\s*\+build\b.*}{//go:build go1.18 && amd64 && !noasm
&& !appengine}' $@
+ perl -ne 'die "unexpected frame or stack use in take-index assembly:
$$_" if /\b(?:push|pop|[re]?[bs]p|[bs]pl)\b/i' $@ || { rm -f $@; exit 1; }
+
filter_uint64_avx2_amd64.s: _lib/filter_uint64_avx2_amd64.s
$(C2GOASM) -a -f $^ $@
perl -i -pe 's{^//\s*\+build\b.*}{//go:build go1.18 && amd64 && !noasm
&& !appengine}' $@
diff --git a/arrow/compute/internal/kernels/_lib/get_take_indices_avx2_amd64.cc
b/arrow/compute/internal/kernels/_lib/get_take_indices_avx2_amd64.cc
new file mode 100644
index 00000000..10666512
--- /dev/null
+++ b/arrow/compute/internal/kernels/_lib/get_take_indices_avx2_amd64.cc
@@ -0,0 +1,91 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <arch.h>
+#include <immintrin.h>
+#include <stdint.h>
+
+extern "C" void FULL_NAME(get_take_indices_uint32)(const uint8_t* filter,
+ uint32_t* output,
+ const uint8_t* tables,
+ const int64_t nbytes,
+ const int64_t tail_mask) {
+ if (nbytes <= 0) {
+ return;
+ }
+
+ const uint8_t* shuffle_masks = tables;
+ const int32_t* store_masks = reinterpret_cast<const int32_t*>(tables +
256);
+ const uint8_t* popcount = tables + 336;
+ const __m128i offsets = _mm_loadu_si128(reinterpret_cast<const
__m128i*>(tables + 352));
+ const __m128i high_offsets = _mm_loadu_si128(reinterpret_cast<const
__m128i*>(tables + 368));
+ const __m128i increment = _mm_loadu_si128(reinterpret_cast<const
__m128i*>(tables + 384));
+ __m128i base = _mm_setzero_si128();
+
+ const auto compact = [&](uint8_t mask) {
+ if (mask == 0) {
+ base = _mm_add_epi32(base, increment);
+ return;
+ }
+
+ if (mask == 0xff) {
+ const __m128i low = _mm_or_si128(base, offsets);
+ const __m128i high = _mm_or_si128(base, high_offsets);
+ _mm_storeu_si128(reinterpret_cast<__m128i*>(output), low);
+ _mm_storeu_si128(reinterpret_cast<__m128i*>(output + 4), high);
+ output += 8;
+ base = _mm_add_epi32(base, increment);
+ return;
+ }
+
+ // Keep the nibble index wide and defer the high-half lookup. This
+ // lowers register pressure so clang does not use BP as scratch state.
+ const int low_mask = mask & 0x0f;
+ const int low_count = popcount[low_mask];
+
+ if (low_count != 0) {
+ const __m128i shuffle = _mm_loadu_si128(
+ reinterpret_cast<const __m128i*>(shuffle_masks + low_mask *
16));
+ const __m128i values = _mm_or_si128(base, offsets);
+ const __m128i compacted = _mm_shuffle_epi8(values, shuffle);
+ const __m128i store_mask = _mm_loadu_si128(
+ reinterpret_cast<const __m128i*>(store_masks + low_count * 4));
+ _mm_maskstore_epi32(reinterpret_cast<int*>(output), store_mask,
compacted);
+ output += low_count;
+ }
+
+ const int high_mask = mask >> 4;
+ const int high_count = popcount[high_mask];
+ if (high_count != 0) {
+ const __m128i shuffle = _mm_loadu_si128(
+ reinterpret_cast<const __m128i*>(shuffle_masks + high_mask *
16));
+ const __m128i values = _mm_or_si128(base, high_offsets);
+ const __m128i compacted = _mm_shuffle_epi8(values, shuffle);
+ const __m128i store_mask = _mm_loadu_si128(
+ reinterpret_cast<const __m128i*>(store_masks + high_count *
4));
+ _mm_maskstore_epi32(reinterpret_cast<int*>(output), store_mask,
compacted);
+ output += high_count;
+ }
+
+ base = _mm_add_epi32(base, increment);
+ };
+
+ // Only the last byte contains padding bits. Keep its mask out of the loop.
+ for (int64_t i = 0; i < nbytes - 1; ++i) {
+ compact(filter[i]);
+ }
+ compact(filter[nbytes - 1] & static_cast<uint8_t>(tail_mask));
+}
diff --git a/arrow/compute/internal/kernels/_lib/get_take_indices_avx2_amd64.s
b/arrow/compute/internal/kernels/_lib/get_take_indices_avx2_amd64.s
new file mode 100644
index 00000000..b9b59a8f
--- /dev/null
+++ b/arrow/compute/internal/kernels/_lib/get_take_indices_avx2_amd64.s
@@ -0,0 +1,121 @@
+ .text
+ .intel_syntax noprefix
+ .file "get_take_indices_avx2_amd64.cc"
+ .globl get_take_indices_uint32_avx2 # -- Begin function
get_take_indices_uint32_avx2
+ .p2align 4, 0x90
+ .type get_take_indices_uint32_avx2,@function
+get_take_indices_uint32_avx2: # @get_take_indices_uint32_avx2
+# %bb.0:
+ test rcx, rcx
+ jle .LBB0_19
+# %bb.1:
+ vmovdqu xmm2, xmmword ptr [rdx + 352]
+ vmovdqu xmm0, xmmword ptr [rdx + 368]
+ lea rax, [rcx - 1]
+ vpxor xmm1, xmm1, xmm1
+ cmp rcx, 1
+ jne .LBB0_5
+.LBB0_2:
+ movzx eax, byte ptr [rdi + rax]
+ and r8d, eax
+ je .LBB0_19
+# %bb.3:
+ cmp r8d, 255
+ jne .LBB0_15
+# %bb.4:
+ vpor xmm2, xmm1, xmm2
+ vpor xmm0, xmm1, xmm0
+ vmovdqu xmmword ptr [rsi], xmm2
+ vmovdqu xmmword ptr [rsi + 16], xmm0
+ jmp .LBB0_19
+.LBB0_5:
+ vmovdqu xmm3, xmmword ptr [rdx + 384]
+ xor ecx, ecx
+ jmp .LBB0_9
+ .p2align 4, 0x90
+.LBB0_6: # in Loop: Header=BB0_9 Depth=1
+ vpor xmm4, xmm1, xmm2
+ vpor xmm5, xmm1, xmm0
+ vmovdqu xmmword ptr [rsi], xmm4
+ vmovdqu xmmword ptr [rsi + 16], xmm5
+ mov r10d, 8
+.LBB0_7: # in Loop: Header=BB0_9 Depth=1
+ lea rsi, [rsi + 4*r10]
+.LBB0_8: # in Loop: Header=BB0_9 Depth=1
+ vpaddd xmm1, xmm3, xmm1
+ inc rcx
+ cmp rax, rcx
+ je .LBB0_2
+.LBB0_9: # =>This Inner Loop Header: Depth=1
+ movzx r9d, byte ptr [rdi + rcx]
+ test r9d, r9d
+ je .LBB0_8
+# %bb.10: # in Loop: Header=BB0_9 Depth=1
+ cmp r9d, 255
+ je .LBB0_6
+# %bb.11: # in Loop: Header=BB0_9 Depth=1
+ mov r11d, r9d
+ and r11d, 15
+ movzx r10d, byte ptr [rdx + r11 + 336]
+ test r10, r10
+ je .LBB0_13
+# %bb.12: # in Loop: Header=BB0_9 Depth=1
+ shl r11d, 4
+ vpor xmm4, xmm1, xmm2
+ vpshufb xmm4, xmm4, xmmword ptr [rdx + r11]
+ mov r11d, r10d
+ shl r11d, 4
+ vmovdqu xmm5, xmmword ptr [rdx + r11 + 256]
+ vpmaskmovd xmmword ptr [rsi], xmm5, xmm4
+ lea rsi, [rsi + 4*r10]
+.LBB0_13: # in Loop: Header=BB0_9 Depth=1
+ mov r10d, r9d
+ shr r10d, 4
+ movzx r10d, byte ptr [rdx + r10 + 336]
+ test r10, r10
+ je .LBB0_8
+# %bb.14: # in Loop: Header=BB0_9 Depth=1
+ and r9d, -16
+ vpor xmm4, xmm1, xmm0
+ vpshufb xmm4, xmm4, xmmword ptr [rdx + r9]
+ mov r9d, r10d
+ shl r9d, 4
+ vmovdqu xmm5, xmmword ptr [rdx + r9 + 256]
+ vpmaskmovd xmmword ptr [rsi], xmm5, xmm4
+ jmp .LBB0_7
+.LBB0_15:
+ mov ecx, r8d
+ and ecx, 15
+ movzx eax, byte ptr [rdx + rcx + 336]
+ test rax, rax
+ je .LBB0_17
+# %bb.16:
+ shl ecx, 4
+ vpor xmm2, xmm1, xmm2
+ vpshufb xmm2, xmm2, xmmword ptr [rdx + rcx]
+ mov ecx, eax
+ shl ecx, 4
+ vmovdqu xmm3, xmmword ptr [rdx + rcx + 256]
+ vpmaskmovd xmmword ptr [rsi], xmm3, xmm2
+ lea rsi, [rsi + 4*rax]
+.LBB0_17:
+ mov eax, r8d
+ shr eax, 4
+ movzx eax, byte ptr [rdx + rax + 336]
+ test rax, rax
+ je .LBB0_19
+# %bb.18:
+ and r8d, -16
+ vpor xmm0, xmm1, xmm0
+ vpshufb xmm0, xmm0, xmmword ptr [rdx + r8]
+ shl eax, 4
+ vmovdqu xmm1, xmmword ptr [rdx + rax + 256]
+ vpmaskmovd xmmword ptr [rsi], xmm1, xmm0
+.LBB0_19:
+ ret
+.Lfunc_end0:
+ .size get_take_indices_uint32_avx2,
.Lfunc_end0-get_take_indices_uint32_avx2
+ # -- End function
+ .ident "Ubuntu clang version 18.1.3 (1)"
+ .section ".note.GNU-stack","",@progbits
+ .addrsig
diff --git a/arrow/compute/internal/kernels/get_take_indices_avx2_amd64.go
b/arrow/compute/internal/kernels/get_take_indices_avx2_amd64.go
new file mode 100644
index 00000000..03b9f288
--- /dev/null
+++ b/arrow/compute/internal/kernels/get_take_indices_avx2_amd64.go
@@ -0,0 +1,103 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//go:build go1.18 && amd64 && !noasm && !appengine
+
+package kernels
+
+import (
+ "encoding/binary"
+ "unsafe"
+
+ "github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/bitutil"
+ "github.com/apache/arrow-go/v18/arrow/compute/exec"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+ "golang.org/x/sys/cpu"
+)
+
+//go:noescape
+func _get_take_indices_uint32_avx2(filter, output, tables unsafe.Pointer,
nbytes, tailMask int64)
+
+var getTakeIndicesUint32AVX2Tables = makeGetTakeIndicesUint32AVX2Tables()
+
+func makeGetTakeIndicesUint32AVX2Tables() (tables [400]byte) {
+ copy(tables[:], filterUint32Tables[:])
+ for lane := 0; lane < 8; lane++ {
+ binary.LittleEndian.PutUint32(tables[352+lane*4:], uint32(lane))
+ }
+ for lane := 0; lane < 4; lane++ {
+ binary.LittleEndian.PutUint32(tables[384+lane*4:], 8)
+ }
+ return tables
+}
+
+func getTakeIndicesUint32AVX2(mem memory.Allocator, filter *exec.ArraySpan)
(arrow.ArrayData, bool) {
+ if !cpu.X86.HasAVX2 || filter.MayHaveNulls() || filter.Offset%8 != 0 ||
filter.Len < 64 {
+ return nil, false
+ }
+
+ filterData := filter.Buffers[1].Buf
+ filterByteOffset := filter.Offset / 8
+ nbytes := (filter.Len + 7) / 8
+ if filterByteOffset < 0 || filterByteOffset+nbytes >
int64(len(filterData)) {
+ return nil, false
+ }
+ filterBytes := filterData[filterByteOffset : filterByteOffset+nbytes]
+
+ // VisitSetBitRuns is especially effective for long runs, so only use
the
+ // compactor when a short sample shows enough fragmented bytes to
amortize
+ // its setup cost.
+ const (
+ sampleBytes = 256
+ minMixed = 4
+ )
+ mixed := 0
+ for i := int64(0); i < nbytes && i < sampleBytes; i++ {
+ mask := filterBytes[i]
+ if mask != 0 && mask != 0xff {
+ mixed++
+ if mixed == minMixed {
+ break
+ }
+ }
+ }
+ if mixed < minMixed {
+ return nil, false
+ }
+
+ length := int64(bitutil.CountSetBits(filterBytes, 0, int(filter.Len)))
+ if length == 0 {
+ return array.NewData(arrow.PrimitiveTypes.Uint32, 0,
[]*memory.Buffer{nil, memory.NewBufferBytes(nil)}, nil, 0, 0), true
+ }
+
+ outputBuf := memory.NewBufferWithAllocator(mem.Allocate(int(length)*4),
mem)
+ defer outputBuf.Release()
+ output := arrow.GetData[uint32](outputBuf.Bytes())
+ tailMask := int64(0xff)
+ if tailBits := filter.Len & 7; tailBits != 0 {
+ tailMask = int64((uint64(1) << uint(tailBits)) - 1)
+ }
+ _get_take_indices_uint32_avx2(
+ unsafe.Pointer(unsafe.SliceData(filterBytes)),
+ unsafe.Pointer(unsafe.SliceData(output)),
+
unsafe.Pointer(unsafe.SliceData(getTakeIndicesUint32AVX2Tables[:])),
+ nbytes,
+ tailMask,
+ )
+ return array.NewData(arrow.PrimitiveTypes.Uint32, int(length),
[]*memory.Buffer{nil, outputBuf}, nil, 0, 0), true
+}
diff --git a/arrow/compute/internal/kernels/get_take_indices_avx2_amd64.s
b/arrow/compute/internal/kernels/get_take_indices_avx2_amd64.s
new file mode 100644
index 00000000..c0e1e8e9
--- /dev/null
+++ b/arrow/compute/internal/kernels/get_take_indices_avx2_amd64.s
@@ -0,0 +1,118 @@
+//go:build go1.18 && amd64 && !noasm && !appengine
+// AUTO-GENERATED BY C2GOASM -- DO NOT EDIT
+
+TEXT ยท_get_take_indices_uint32_avx2(SB), $0-40
+
+ MOVQ filter+0(FP), DI
+ MOVQ output+8(FP), SI
+ MOVQ tables+16(FP), DX
+ MOVQ nbytes+24(FP), CX
+ MOVQ tailMask+32(FP), R8
+
+ WORD $0x8548; BYTE $0xc9 // test rcx, rcx
+ JLE LBB0_19
+ QUAD $0x00000160926ffac5 // vmovdqu xmm2, oword [rdx + 352]
+ QUAD $0x00000170826ffac5 // vmovdqu xmm0, oword [rdx + 368]
+ LONG $0xff418d48 // lea rax, [rcx - 1]
+ LONG $0xc9eff1c5 // vpxor xmm1, xmm1, xmm1
+ LONG $0x01f98348 // cmp rcx, 1
+ JNE LBB0_5
+
+LBB0_2:
+ LONG $0x0704b60f // movzx eax, byte [rdi +
rax]
+ WORD $0x2141; BYTE $0xc0 // and r8d, eax
+ JE LBB0_19
+ LONG $0xfff88141; WORD $0x0000; BYTE $0x00 // cmp r8d, 255
+ JNE LBB0_15
+ LONG $0xd2ebf1c5 // vpor xmm2, xmm1, xmm2
+ LONG $0xc0ebf1c5 // vpor xmm0, xmm1, xmm0
+ LONG $0x167ffac5 // vmovdqu oword [rsi],
xmm2
+ LONG $0x467ffac5; BYTE $0x10 // vmovdqu oword [rsi +
16], xmm0
+ JMP LBB0_19
+
+LBB0_5:
+ QUAD $0x000001809a6ffac5 // vmovdqu xmm3, oword [rdx + 384]
+ WORD $0xc931 // xor ecx, ecx
+ JMP LBB0_9
+
+LBB0_6:
+ LONG $0xe2ebf1c5 // vpor xmm4, xmm1, xmm2
+ LONG $0xe8ebf1c5 // vpor xmm5, xmm1, xmm0
+ LONG $0x267ffac5 // vmovdqu oword [rsi], xmm4
+ LONG $0x6e7ffac5; BYTE $0x10 // vmovdqu oword [rsi + 16], xmm5
+ LONG $0x0008ba41; WORD $0x0000 // mov r10d, 8
+
+LBB0_7:
+ LONG $0x96348d4a // lea rsi, [rsi + 4*r10]
+
+LBB0_8:
+ LONG $0xc9fee1c5 // vpaddd xmm1, xmm3, xmm1
+ WORD $0xff48; BYTE $0xc1 // inc rcx
+ WORD $0x3948; BYTE $0xc8 // cmp rax, rcx
+ JE LBB0_2
+
+LBB0_9:
+ LONG $0x0cb60f44; BYTE $0x0f // movzx r9d, byte [rdi +
rcx]
+ WORD $0x8545; BYTE $0xc9 // test r9d, r9d
+ JE LBB0_8
+ LONG $0xfff98141; WORD $0x0000; BYTE $0x00 // cmp r9d, 255
+ JE LBB0_6
+ WORD $0x8945; BYTE $0xcb // mov r11d, r9d
+ LONG $0x0fe38341 // and r11d, 15
+ QUAD $0x0001501a94b60f46; BYTE $0x00 // movzx r10d, byte [rdx
+ r11 + 336]
+ WORD $0x854d; BYTE $0xd2 // test r10, r10
+ JE LBB0_13
+ LONG $0x04e3c141 // shl r11d, 4
+ LONG $0xe2ebf1c5 // vpor xmm4, xmm1, xmm2
+ LONG $0x0059a2c4; WORD $0x1a24 // vpshufb xmm4, xmm4,
oword [rdx + r11]
+ WORD $0x8945; BYTE $0xd3 // mov r11d, r10d
+ LONG $0x04e3c141 // shl r11d, 4
+ QUAD $0x01001aac6f7aa1c4; WORD $0x0000 // vmovdqu xmm5, oword
[rdx + r11 + 256]
+ LONG $0x8e51e2c4; BYTE $0x26 // vpmaskmovd oword
[rsi], xmm5, xmm4
+ LONG $0x96348d4a // lea rsi, [rsi + 4*r10]
+
+LBB0_13:
+ WORD $0x8945; BYTE $0xca // mov r10d, r9d
+ LONG $0x04eac141 // shr r10d, 4
+ QUAD $0x0001501294b60f46; BYTE $0x00 // movzx r10d, byte [rdx +
r10 + 336]
+ WORD $0x854d; BYTE $0xd2 // test r10, r10
+ JE LBB0_8
+ LONG $0xf0e18341 // and r9d, -16
+ LONG $0xe0ebf1c5 // vpor xmm4, xmm1, xmm0
+ LONG $0x0059a2c4; WORD $0x0a24 // vpshufb xmm4, xmm4, oword
[rdx + r9]
+ WORD $0x8945; BYTE $0xd1 // mov r9d, r10d
+ LONG $0x04e1c141 // shl r9d, 4
+ QUAD $0x01000aac6f7aa1c4; WORD $0x0000 // vmovdqu xmm5, oword [rdx +
r9 + 256]
+ LONG $0x8e51e2c4; BYTE $0x26 // vpmaskmovd oword [rsi],
xmm5, xmm4
+ JMP LBB0_7
+
+LBB0_15:
+ WORD $0x8944; BYTE $0xc1 // mov ecx, r8d
+ WORD $0xe183; BYTE $0x0f // and ecx, 15
+ QUAD $0x000001500a84b60f // movzx eax, byte [rdx + rcx +
336]
+ WORD $0x8548; BYTE $0xc0 // test rax, rax
+ JE LBB0_17
+ WORD $0xe1c1; BYTE $0x04 // shl ecx, 4
+ LONG $0xd2ebf1c5 // vpor xmm2, xmm1, xmm2
+ LONG $0x0069e2c4; WORD $0x0a14 // vpshufb xmm2, xmm2, oword
[rdx + rcx]
+ WORD $0xc189 // mov ecx, eax
+ WORD $0xe1c1; BYTE $0x04 // shl ecx, 4
+ QUAD $0x0001000a9c6ffac5; BYTE $0x00 // vmovdqu xmm3, oword [rdx +
rcx + 256]
+ LONG $0x8e61e2c4; BYTE $0x16 // vpmaskmovd oword [rsi],
xmm3, xmm2
+ LONG $0x86348d48 // lea rsi, [rsi + 4*rax]
+
+LBB0_17:
+ WORD $0x8944; BYTE $0xc0 // mov eax, r8d
+ WORD $0xe8c1; BYTE $0x04 // shr eax, 4
+ QUAD $0x000001500284b60f // movzx eax, byte [rdx + rax +
336]
+ WORD $0x8548; BYTE $0xc0 // test rax, rax
+ JE LBB0_19
+ LONG $0xf0e08341 // and r8d, -16
+ LONG $0xc0ebf1c5 // vpor xmm0, xmm1, xmm0
+ LONG $0x0079a2c4; WORD $0x0204 // vpshufb xmm0, xmm0, oword
[rdx + r8]
+ WORD $0xe0c1; BYTE $0x04 // shl eax, 4
+ QUAD $0x000100028c6ffac5; BYTE $0x00 // vmovdqu xmm1, oword [rdx +
rax + 256]
+ LONG $0x8e71e2c4; BYTE $0x06 // vpmaskmovd oword [rsi],
xmm1, xmm0
+
+LBB0_19:
+ RET
diff --git a/arrow/compute/internal/kernels/get_take_indices_avx2_amd64_test.go
b/arrow/compute/internal/kernels/get_take_indices_avx2_amd64_test.go
new file mode 100644
index 00000000..37c6efc3
--- /dev/null
+++ b/arrow/compute/internal/kernels/get_take_indices_avx2_amd64_test.go
@@ -0,0 +1,201 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//go:build go1.18 && amd64 && !noasm && !appengine
+
+package kernels
+
+import (
+ "io"
+ "runtime/trace"
+ "testing"
+ "unsafe"
+
+ "github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/array"
+ "github.com/apache/arrow-go/v18/arrow/compute/exec"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+ "github.com/stretchr/testify/require"
+ "golang.org/x/sys/cpu"
+)
+
+func TestGetTakeIndicesUint32AVX2(t *testing.T) {
+ if !cpu.X86.HasAVX2 {
+ t.Skip("AVX2 is not available")
+ }
+
+ mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+ defer mem.AssertSize(t, 0)
+
+ const length = 256*8 + 3
+ values := make([]bool, length)
+ want := make([]uint32, 0, length/2)
+ for i := range values {
+ mask := byte((i/8 + 1) % 256)
+ values[i] = mask&(1<<uint(i%8)) != 0
+ if values[i] {
+ want = append(want, uint32(i))
+ }
+ }
+
+ t.Run("fragmented_aligned_tail", func(t *testing.T) {
+ filter := makeSlicedBooleanFilter(t, values, nil, 8, mem)
+ defer filter.Release()
+ var span exec.ArraySpan
+ span.SetMembers(filter.Data())
+
+ result, ok := getTakeIndicesUint32AVX2(mem, &span)
+ require.True(t, ok)
+ defer result.Release()
+ assertTakeIndices[uint32](t, result, want, nil)
+ })
+
+ t.Run("runtime_trace", func(t *testing.T) {
+ filter := makeSlicedBooleanFilter(t, values, nil, 8, mem)
+ defer filter.Release()
+ var span exec.ArraySpan
+ span.SetMembers(filter.Data())
+
+ startedTrace := !trace.IsEnabled()
+ if startedTrace {
+ require.NoError(t, trace.Start(io.Discard))
+ defer trace.Stop()
+ }
+
+ for i := 0; i < 1024; i++ {
+ result, ok := getTakeIndicesUint32AVX2(mem, &span)
+ require.True(t, ok)
+ result.Release()
+ }
+ })
+
+ t.Run("dirty_tail_padding", func(t *testing.T) {
+ const tailLength = 67
+ filterBytes := []byte{0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0xff}
+ want := make([]uint32, 0, tailLength/2)
+ for i := 0; i < tailLength; i++ {
+ if filterBytes[i/8]&(1<<uint(i%8)) != 0 {
+ want = append(want, uint32(i))
+ }
+ }
+
+ valuesBuffer := memory.NewBufferBytes(filterBytes)
+ defer valuesBuffer.Release()
+ data := array.NewData(arrow.FixedWidthTypes.Boolean,
tailLength, []*memory.Buffer{nil, valuesBuffer}, nil, 0, 0)
+ defer data.Release()
+ var span exec.ArraySpan
+ span.SetMembers(data)
+
+ result, ok := getTakeIndicesUint32AVX2(mem, &span)
+ require.True(t, ok)
+ defer result.Release()
+ assertTakeIndices[uint32](t, result, want, nil)
+ })
+
+ t.Run("unaligned_offset_falls_back", func(t *testing.T) {
+ filter := makeSlicedBooleanFilter(t, values, nil, 1, mem)
+ defer filter.Release()
+ var span exec.ArraySpan
+ span.SetMembers(filter.Data())
+
+ result, ok := getTakeIndicesUint32AVX2(mem, &span)
+ require.False(t, ok)
+ require.Nil(t, result)
+ })
+
+ t.Run("long_run_falls_back", func(t *testing.T) {
+ longRuns := make([]bool, length)
+ for i := range longRuns {
+ longRuns[i] = i%1024 < 900
+ }
+ filter := makeBooleanFilter(t, longRuns, nil, mem)
+ defer filter.Release()
+ var span exec.ArraySpan
+ span.SetMembers(filter.Data())
+
+ result, ok := getTakeIndicesUint32AVX2(mem, &span)
+ require.False(t, ok)
+ require.Nil(t, result)
+ })
+
+ t.Run("nullable_filter_falls_back", func(t *testing.T) {
+ valid := make([]bool, length)
+ for i := range valid {
+ valid[i] = true
+ }
+ valid[64] = false
+ filter := makeBooleanFilter(t, values, valid, mem)
+ defer filter.Release()
+ var span exec.ArraySpan
+ span.SetMembers(filter.Data())
+
+ result, ok := getTakeIndicesUint32AVX2(mem, &span)
+ require.False(t, ok)
+ require.Nil(t, result)
+ })
+
+ t.Run("short_filter_falls_back", func(t *testing.T) {
+ filter := makeBooleanFilter(t, values[:63], nil, mem)
+ defer filter.Release()
+ var span exec.ArraySpan
+ span.SetMembers(filter.Data())
+
+ result, ok := getTakeIndicesUint32AVX2(mem, &span)
+ require.False(t, ok)
+ require.Nil(t, result)
+ })
+}
+
+func TestGetTakeIndicesUint32AVX2MaskedStores(t *testing.T) {
+ if !cpu.X86.HasAVX2 {
+ t.Skip("AVX2 is not available")
+ }
+
+ const sentinel = ^uint32(0)
+ for _, nbytes := range []int{1, 9} {
+ filter := make([]byte, nbytes)
+ for i := range filter {
+ filter[i] = 0x55
+ }
+ // Guard both ends of the output and every lane after the
selected values.
+ got := make([]uint32, nbytes*8+2)
+ want := make([]uint32, len(got))
+ for tailBits := 1; tailBits <= 8; tailBits++ {
+ for mask := 0; mask < 256; mask++ {
+ filter[nbytes-1] = byte(mask)
+ for i := range got {
+ got[i], want[i] = sentinel, sentinel
+ }
+ pos := 1
+ for i := 0; i < (nbytes-1)*8+tailBits; i++ {
+ if filter[i/8]&(1<<uint(i%8)) != 0 {
+ want[pos] = uint32(i)
+ pos++
+ }
+ }
+
+ _get_take_indices_uint32_avx2(
+ unsafe.Pointer(&filter[0]),
+ unsafe.Pointer(&got[1]),
+
unsafe.Pointer(&getTakeIndicesUint32AVX2Tables[0]),
+ int64(nbytes),
+ int64(1<<uint(tailBits))-1,
+ )
+ require.Equal(t, want, got, "nbytes=%d
tailBits=%d mask=%#02x", nbytes, tailBits, mask)
+ }
+ }
+ }
+}
diff --git a/arrow/compute/internal/kernels/get_take_indices_avx2_noasm.go
b/arrow/compute/internal/kernels/get_take_indices_avx2_noasm.go
new file mode 100644
index 00000000..47574c6d
--- /dev/null
+++ b/arrow/compute/internal/kernels/get_take_indices_avx2_noasm.go
@@ -0,0 +1,29 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//go:build go1.18 && (noasm || !amd64 || appengine)
+
+package kernels
+
+import (
+ "github.com/apache/arrow-go/v18/arrow"
+ "github.com/apache/arrow-go/v18/arrow/compute/exec"
+ "github.com/apache/arrow-go/v18/arrow/memory"
+)
+
+func getTakeIndicesUint32AVX2(memory.Allocator, *exec.ArraySpan)
(arrow.ArrayData, bool) {
+ return nil, false
+}
diff --git a/arrow/compute/internal/kernels/vector_selection.go
b/arrow/compute/internal/kernels/vector_selection.go
index 20a8379a..b3d880d6 100644
--- a/arrow/compute/internal/kernels/vector_selection.go
+++ b/arrow/compute/internal/kernels/vector_selection.go
@@ -239,6 +239,9 @@ func GetTakeIndices(mem memory.Allocator, filter
*exec.ArraySpan, nullSelect Nul
return getTakeIndices[uint16](mem, filter, nullSelect), nil
} else if filter.Len < math.MaxUint32 {
if nullSelect == DropNulls {
+ if result, ok := getTakeIndicesUint32AVX2(mem, filter);
ok {
+ return result, nil
+ }
if result, ok := getTakeIndicesUint32NEON(mem, filter);
ok {
return result, nil
}
diff --git a/arrow/compute/internal/kernels/vector_selection_bench_test.go
b/arrow/compute/internal/kernels/vector_selection_bench_test.go
index e9000ff5..f9b6e030 100644
--- a/arrow/compute/internal/kernels/vector_selection_bench_test.go
+++ b/arrow/compute/internal/kernels/vector_selection_bench_test.go
@@ -34,20 +34,32 @@ type filterBenchmarkPattern struct {
}
var filterBenchmarkPatterns = []filterBenchmarkPattern{
+ {name: "mixed-prefix-long-runs", selected: func(i int) bool {
+ if i < 32 {
+ return i%2 == 0
+ }
+ return (i/4096)%2 == 0
+ }},
{name: "all-selected", selected: func(int) bool { return true }},
{name: "all-clear", selected: func(int) bool { return false }},
{name: "long-runs", selected: func(i int) bool { return i%1024 < 900 }},
{name: "alternating", selected: func(i int) bool { return i%2 == 0 }},
{name: "short-runs", selected: func(i int) bool { return i%100 < 10 }},
- {name: "random", selected: func(i int) bool { return
getTakeIndicesBenchmarkRandom(i) }},
+ {name: "random-25", selected: func(i int) bool { return
getTakeIndicesBenchmarkSelected(i, 25) }},
+ {name: "random-50", selected: func(i int) bool { return
getTakeIndicesBenchmarkSelected(i, 50) }},
+ {name: "random-75", selected: func(i int) bool { return
getTakeIndicesBenchmarkSelected(i, 75) }},
{name: "nullable-long-runs", selected: func(i int) bool { return i%1024
< 900 }, nullable: true},
}
-func getTakeIndicesBenchmarkRandom(i int) bool {
+func getTakeIndicesBenchmarkRandom(i int) uint32 {
x := uint32(i)*747796405 + 2891336453
x = ((x >> ((x >> 28) + 4)) ^ x) * 277803737
x = (x >> 22) ^ x
- return x&1 == 0
+ return x
+}
+
+func getTakeIndicesBenchmarkSelected(i, percent int) bool {
+ return uint64(getTakeIndicesBenchmarkRandom(i))*100>>32 <
uint64(percent)
}
func makeFilterBenchmarkSpan(tb testing.TB, n int, pattern
filterBenchmarkPattern) *exec.ArraySpan {
@@ -70,11 +82,12 @@ func makeFilterBenchmarkSpan(tb testing.TB, n int, pattern
filterBenchmarkPatter
}
func BenchmarkGetTakeIndices(b *testing.B) {
- for _, n := range []int{64 * 1024, 1024 * 1024} {
+ for _, n := range []int{1024, 64 * 1024, 1024 * 1024} {
for _, pattern := range filterBenchmarkPatterns {
b.Run(fmt.Sprintf("%s/%d", pattern.name, n), func(b
*testing.B) {
filter := makeFilterBenchmarkSpan(b, n, pattern)
b.ReportAllocs()
+ b.SetBytes(int64((n + 7) / 8))
b.ResetTimer()
for i := 0; i < b.N; i++ {
result, err :=
GetTakeIndices(memory.DefaultAllocator, filter, DropNulls)