Skip to content

crc: avoid x18 in crc32_iscsi_x6 and enable it on macOS - #478

Open
cielavenir wants to merge 3 commits into
intel:masterfrom
cielavenir:crc32-iscsi-x6-apple
Open

cielavenir wants to merge 3 commits into
intel:masterfrom
cielavenir:crc32-iscsi-x6-apple

Conversation

@cielavenir

@cielavenir cielavenir commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

crc32_iscsi_x6 (added in b60f302) uses x18 as a scratch register (crc0_data8) without saving it. x18 is the AAPCS64 platform register: it is reserved on Apple and Windows, and holds the shadow call stack pointer under -fsanitize=shadow-call-stack. That is also why the file is wrapped in #ifndef __APPLE__.

  • crc: avoid x18 platform register in crc32_iscsi_x6 — use x27 instead. push_stack/pop_stack already save/restore x27, and it was otherwise unused, so there is no extra cost.
  • crc: enable crc32_iscsi_x6 on macOS — drop the __APPLE__ guard and follow the existing aarch64 CRC conventions (cdecl(), @PAGE/@PAGEOFF, .type/.size only on ELF). The Apple dispatcher selects it when both CRC32 and PMULL are present, matching the Linux order. The SVE2 fusion path stays Linux-only (no SVE2 on Apple cores; it also uses x18/x29).

Testing

  • crc32_iscsi_x6.S assembles with clang 19 for arm64-apple-macos13 and aarch64-linux-gnu; objdump shows no x18/w18 use.
  • Builds and passes crc32_iscsi tests on the macos-15 (arm64) GitHub runner.

Performance on Apple M1

crc32_iscsi_perf (8 KB, warm cache) on the macos-15 GitHub runner (Apple M1 (Virtual), 3 vCPUs, CRC32 + PMULL, no SVE). The same job builds master (af6a939, which dispatches to crc32_iscsi_3crc_fold on Apple) in a git worktree and this branch (crc32_iscsi_x6), then runs them alternately for 5 rounds (run).

Round master (3crc_fold) this PR (x6) Speedup
1 21.97 GB/s 23.52 GB/s 1.07x
2 21.81 GB/s 23.09 GB/s 1.06x
3 20.49 GB/s 20.90 GB/s 1.02x
4 21.13 GB/s 22.65 GB/s 1.07x
5 21.78 GB/s 24.05 GB/s 1.10x
Mean 21.43 GB/s 22.84 GB/s 1.07x

Both produce the same CRC. The runner is a shared VM, so treat the numbers as indicative.

🤖 Generated with Claude Code

cielavenir and others added 2 commits November 19, 2026 12:41
x18 is the AAPCS64 platform register. It is reserved on Apple and
Windows, and holds the shadow call stack pointer when built with
-fsanitize=shadow-call-stack. crc32_iscsi_x6 used it as a scratch data
register without saving it.

Use x27 instead, which push_stack/pop_stack already save and restore
but which was otherwise unused, so there is no extra cost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Taiju Yamada <tyamada@bi.a.u-tokyo.ac.jp>
Now that crc32_iscsi_x6 no longer clobbers x18, make it build on Apple
by following the same conventions as the other aarch64 CRC files:
cdecl() for the symbol name, @PAGE/@PAGEOFF relocations, and .type/.size
only on ELF.

Select it on Apple when both CRC32 and PMULL are available, matching the
Linux dispatch order. The SVE2 fusion path stays Linux-only since Apple
cores lack SVE2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Taiju Yamada <tyamada@bi.a.u-tokyo.ac.jp>
@cielavenir

Copy link
Copy Markdown
Contributor Author

@cielavenir
cielavenir marked this pull request as ready for review November 19, 2026 04:18
@liuqinfei

Copy link
Copy Markdown

The x18 -> x27 substitution and Mach-O adaptations look correct. I tested head a010b2c38d83 locally and found two performance points worth addressing:

  1. 1024-byte regression: in crc32_iscsi_x6.S:162-163, cmp LEN, 1024; bls 1f sends exactly 1024 bytes to the serial CRC path, while the previous Apple implementation (3crc_fold) starts its parallel path at 1024. In this benchmark, this reduces throughput from 34.53 to 13.46 GB/s (~61%). Changing only bls to blo restores 33.41 GB/s. Please include exactly 1024 bytes in the fast path when enabling this dispatch change.

  2. Compare against refl_pmull too: the 8 KiB improvement over 3crc_fold reproduces here, but crc32_iscsi_refl_pmull is faster than x6 at several tested sizes, including after the threshold fix. Please include it in the Apple benchmarks before choosing the dispatch priority. These results support further comparison across Apple CPUs before changing the dispatch priority.

Local median throughput, decimal GB/s:

Bytes x6 (PR) x6 (bls -> blo, local variant) 3crc_fold refl_pmull
128 25.17 25.21 26.17 36.89
512 15.46 15.40 15.41 45.20
1024 13.46 33.41 34.53 45.64
1025 32.48 32.81 33.75 45.14
2048 33.49 33.80 33.12 41.53
8192 34.03 33.92 31.38 36.99
1048576 33.32 33.05 29.57 34.20

Method: an Apple Silicon system, compiled with -O2; direct calls to the four kernels in one executable; one thread, warm cache, the same 64-byte-aligned buffer; two independent runs with 15 randomized rounds each (~50 ms per sample), reporting the median of all 30 samples per cell. Timing surrounds the entire batch, not each call. All samples were retained; median thread CPU time / wall time was 99.99%.

Measurement limitation: these results are not CPU-pinned; core placement and frequency were not fixed.

crc32_iscsi_x6 entered its 6-lane loop only for LEN > 1024, but one
iteration consumes exactly 1024 bytes and the loop itself continues
while LEN >= 1024. A 1024-byte buffer therefore fell back to the serial
crc32cx loop, at roughly 40% of the parallel throughput.

Use blo instead of bls so LEN == 1024 takes the parallel path. This
affects Linux as well, where x6 is already dispatched.

Reported-by: liuqinfei
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Taiju Yamada <tyamada@bi.a.u-tokyo.ac.jp>
@cielavenir

Copy link
Copy Markdown
Contributor Author

Thanks for testing this so carefully!

1. 1024-byte threshold: fixed in 9af3609 (bls → blo). One iteration of the 6-lane loop consumes exactly 1024 bytes and the loop already continues while LEN >= 1024, so this only makes entry consistent with it. Note this also affects Linux, where x6 is already dispatched.

2. refl_pmull comparison: I benchmarked all kernels with direct calls from one executable on the GitHub macos-15 runner (Apple M1 (Virtual)), using a method similar to yours: warm cache, one thread, the same 64-byte-aligned buffer, 15 rounds in randomized kernel order, ~30 ms per sample, median. Before timing, every kernel is checked against crc32_iscsi_base for lengths 0..4200 at 5 offsets (all pass). Benchmark source, run.

Median throughput, decimal GB/s (x6 includes the blo fix):

Bytes crc_ext 3crc_fold refl_pmull x6
64 7.38 7.22 2.76 7.25
128 7.45 7.46 5.27 7.37
256 7.20 7.72 8.73 7.65
512 7.73 7.66 12.68 7.63
1000 7.72 7.50 5.75 7.67
1023 7.47 7.39 4.25 7.42
1024 7.69 19.09 16.53 21.93
1025 7.52 18.68 16.26 21.45
2048 7.39 19.61 20.23 21.74
4096 7.52 19.55 22.14 21.08
8192 7.45 19.21 21.45 21.73
65536 7.37 19.19 21.83 21.73
1048576 7.39 19.45 23.73 21.64

On M1, x6 is equal to or faster than 3crc_fold (the current Apple default) at every size, so this PR's dispatch change does not regress anything there. refl_pmull is mixed: it wins at 256–512 B and at 1 MiB, but drops to 3–6 GB/s at 64–128 B and at lengths just below 1 KiB (1000, 1023), where x6 stays at ~7.5 GB/s. Your results show refl_pmull winning more broadly on your chip, so the best choice seems CPU-dependent.

I'd suggest keeping x6 as the Apple default in this PR, since it is a strict improvement over 3crc_fold on M1, and revisiting x6 vs refl_pmull (perhaps a size-based split) in a follow-up once there is data from more Apple CPUs. Results from your system at non-power-of-two sizes like 1000/1023 would be very helpful there.

Limitation: the runner is a shared VM (3 vCPUs, no core pinning), so absolute numbers are lower than bare metal and should be taken as indicative.

Side note, unrelated to this PR: the same benchmark on the ubuntu-26.04-arm runner (Neoverse N2, 128-bit SVE2) shows that the SVE2 path falls back to x6 there, and x6 is ~8% slower than crc_ext and ~5% slower than 3crc_fold at ≥1 KiB (e.g. 8 KiB: crc_ext 27.07, 3crc_fold 26.19, x6 24.89 GB/s). It may be worth a separate look.

Neoverse N2 results (ubuntu-26.04-arm runner, same benchmark and run)

Median throughput, decimal GB/s. dispatched is crc32_iscsi() itself; on this CPU it ends up in x6 via the SVE2 vector-length fallback.

Bytes crc_ext 3crc_fold refl_pmull x6 dispatched
64 18.10 16.70 3.84 14.85 13.60
128 23.17 23.26 6.66 21.64 19.74
256 24.98 25.19 10.96 25.46 24.93
512 26.04 26.26 16.13 26.23 25.98
1000 26.30 26.60 7.43 26.72 26.66
1023 26.28 26.20 5.61 26.29 26.26
1024 26.63 26.01 18.19 23.54 23.34
1025 26.42 25.82 17.87 23.55 23.28
2048 26.92 26.16 18.96 24.22 24.14
4096 27.01 26.14 19.35 24.70 24.55
8192 27.07 26.19 19.54 24.89 24.88
65536 27.06 26.14 19.71 25.03 25.00
1048576 26.84 25.92 19.50 24.60 24.61

@liuqinfei

Copy link
Copy Markdown

LGTM for the current scope. The bls -> blo fix in 9af3609 addresses the original 1024-byte issue.

In my local comparisons using both fixed independent seeds and chained CRC seeds, x6 appeared to have an advantage over 3crc_fold for larger buffers. The results at smaller sizes were more mixed, so the benefit seems to depend on the buffer size.

One detail that may be worth double-checking is whether the benchmark uses a fixed seed on each call or feeds the previous CRC result into the next call. ISA-L's existing crc32_iscsi_perf.c and crc_funcs_perf.c use a fixed TEST_SEED. My local comparison suggests that the seed dependency pattern can affect the relative performance of x6 and refl_pmull, so confirming this detail may help interpret the differences alongside CPU variation.

I agree that selecting refl_pmull can be left to a separate tuning effort, covering independent-block CRC calculations and chained CRC updates across buffer sizes and Apple CPU variants. It should not block this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants