Skip to content

perf(backend-native-cpu): runtime FEAT_DotProd dispatch for Apple arm64 Q4_K/Q6_K kernels (#958) - #961

Merged
michalharakal merged 1 commit into
developfrom
feature/apple-dotprod-dispatch-958
Aug 11, 2026
Merged

michalharakal merged 1 commit into
developfrom
feature/apple-dotprod-dispatch-958

Conversation

@michalharakal

Copy link
Copy Markdown
Contributor

Closes #958; first slice of the iOS/Apple kernel track of #920 (prerequisite for #959, which adds the iosArm64/iosSimulatorArm64/macosArm64 targets with embedded archives).

Why: a K/N klib embeds exactly ONE static archive (the Android baseline/v8.2 two-.so runtime pick doesn't translate), and Apple A12 — iPhone XS/XR, still supported by current iOS — lacks FEAT_DotProd while A13+/M-series have it. TU-level -march=+dotprod would SIGILL supported devices; baseline-only would leave Q4_K/Q6_K (the formats real GGUFs use) scalar.

How:

  • New skainet_cpu_features.{h,c}: skainet_cpu_has_dotprod() — on Apple arm64 a cached sysctlbyname("hw.optional.arm.FEAT_DotProd") probe (key exists since iOS 15/macOS 12; absence → 0 → scalar-int arm, never crashes); elsewhere a compile-time constant. Internal header, not in the cinterop headerFilter.
  • skainet_simd.h: SKAINET_DOTPROD_DISPATCH / SKAINET_DOTPROD_TARGET (__attribute__((target("dotprod")))) defined only for Apple arm64 TUs built without __ARM_FEATURE_DOTPROD. The attribute is what gates vdotq_s32 codegen in clang, and attributed functions are never inlined into baseline callers — sdot stays out of the baseline path.
  • q4k_matmul.c / q6k_matmul.c: the guarded hot bodies extracted into _dp/_generic twins (verbatim bodies, one call per block × output row — call overhead amortized over the block's arithmetic), 3-way call site with use_dp hoisted once per matmul.
  • CMakeLists.txt: aarch64 -march block now AND NOT APPLE; skainet_cpu_features.c added; SKAINET_STATIC_ONLY option (auto-on for CMAKE_SYSTEM_NAME=iOS — no dylib story there).

Behavior notes: Linux codegen is unchanged — on dotprod TUs the call site is a direct call that inlines back under -O3. The macOS host dylib (JVM/FFM path) moves from TU-level dotprod to baseline+dispatch: runtime-equivalent on every Apple Silicon Mac, and it makes the existing macos-14 jvmTest CI lane an automatic exerciser of the dispatch fast arm. The new Q5_0/Q5_1 kernels are plain NEON (no dotprod) and need no dispatch.

Verified:

…64 q4k/q6k kernels (#958)

A Kotlin/Native klib embeds exactly one static archive, and Apple A12
(iPhone XS/XR, still iOS-supported) lacks FEAT_DotProd while A13+ and
all Apple Silicon have it — so the Android two-.so trick doesn't
translate and TU-level -march=+dotprod is not shippable to iOS.

Apple arm64 TUs now compile at the SDK-default baseline; the q4k/q6k
dotprod hot bodies are extracted into _dp (target("dotprod")-attributed)
/ _generic twins and selected once per matmul call via a cached
sysctlbyname(hw.optional.arm.FEAT_DotProd) probe (key since iOS 15 /
macOS 12; absence degrades to the scalar-int arm, never crashes). The
attribute is what gates vdotq_s32 codegen, and attributed functions are
not inlined into baseline callers, keeping sdot out of the baseline path.

Non-Apple builds keep the compile-time guard as the only mechanism: with
SKAINET_HAVE_DOTPROD the call site is a direct call that inlines back
under -O3 — verified: qemu linuxArm64Test parity green, 32 sdot
instructions in the cross archive, no _dp symbols (inlined). The macOS
FFM dylib moves from TU-level dotprod to baseline+dispatch —
runtime-equivalent on every Apple Silicon Mac, and it turns the macOS
jvmTest CI lane into an exerciser of the dispatch fast arm. iOS builds
are static-only (SKAINET_STATIC_ONLY, auto-on for CMAKE_SYSTEM_NAME=iOS).

Refs #920
@michalharakal
michalharakal merged commit a1dc6aa into develop Aug 11, 2026
14 checks passed
@michalharakal
michalharakal deleted the feature/apple-dotprod-dispatch-958 branch August 11, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apple arm64: runtime FEAT_DotProd dispatch for the Q4_K/Q6_K C kernels

2 participants