Skip to content

perf(minmax): keep the AVX2 float valid kernels out of legacy SSE (#33463) - #33502

Merged
fengttt merged 8 commits into
matrixorigin:mainfrom
fengttt:minmax-float-null
Nov 30, 2026
Merged

fengttt merged 8 commits into
matrixorigin:mainfrom
fengttt:minmax-float-null

Conversation

@fengttt

@fengttt fengttt commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #33463

What this PR does / why we need it:

Follow-up to #33464. On amd64, its SIMD float valid kernels (FloatValid, used by GetMinMaxValue on columns with NULLs) were slower than the scalar loop at high NULL density: float32 at 90% NULLs took 2.3 µs per 8192 rows against 0.49 µs scalar.

Cause. A kernel visits words with few valid rows row by row (the visit path) and masks the others with 256-bit vectors. In the float kernels the visit path ran scalar float code, which Go encodes as legacy SSE even with GOAMD64=v3. Each switch between that code and the 256-bit loops costs about 300 cycles. The evidence:

  • A strict alternation of visit and masked words, which branch prediction gets right every time, was the slowest pattern: 6.6 µs, against 0.43 µs for visit words alone and 1.0 µs for masked words alone. So the cost is the switch itself, not mispredicts.
  • The int kernels, whose visit path stays in integer registers, run the same patterns at the mean of the two paths.
  • In the disassembly, the only legacy-SSE code inside the 256-bit region was the float visit path. Any change that left a legacy-SSE instruction there kept paying the penalty, including XMM loads hidden in math.Float64bits(rows[i]) and register spills in the setup code.

Fix (AVX2 only, in mkminmax.go):

  • The float kernels now visit in integer registers. They read the values' bits through an integer view of the column and map them to integer keys that order like the floats: a negative value's other bits flip. NaN is skipped with an integer test, and the bounds are converted back after the loop.
  • The keys are set up before any vector instruction. Between the first 256-bit instruction and the vzeroupper, the kernels now contain no legacy-SSE instruction (checked with binutils objdump).
  • With the switch cost gone, float32 visits up to 12 valid rows per word (was 8); float64 stays at 16, which a sweep found best.
  • Mostly NULL columns go to the scalar loop (amd64). At high NULL density the plain scalar loop beats every kernel variant, because it runs no 256-bit code. FloatValid now counts the valid rows, stopping as soon as there are enough, and takes the scalar loop below a per-architecture threshold. On AVX2 that is 25% valid rows for float32 and 40% for float64, the measured crossovers (float64 vectors hold half as many lanes, so the scalar loop wins sooner).
  • arm64 keeps its kernels. Its generated file only gains the two thresholds, both 0 (never take the scalar loop). NEON scalar and vector code share registers with no such penalty, and this PR has no arm64 measurements. See "arm64 thresholds" below.

Benchmarks

Intel Core Ultra 9 275HX (AVX2), GOEXPERIMENT=simd GOAMD64=v3, pinned to one performance core, median of 8 interleaved runs, main → this PR.

BenchmarkSwitch (new), 128 words, each with 4 valid rows (visit) or 32 (masked), ns:

kernel visit only masked only alternate random 50/50
float32 433 → 395 1006 → 1022 6593 → 557 4133 → 611
float64 405 → 406 2002 → 2018 7120 → 1062 4627 → 1079
int32 (unchanged) 252 → 252 471 → 469 336 → 337 340 → 336

BenchmarkValid, 8192 rows with random NULLs, ns. "scalar" is the plain scalar loop:

NULLs float32 scalar float32 main → PR float64 scalar float64 main → PR
0% 3928 936 → 961 3906 1865 → 1883
10% 3948 1012 → 1040 4431 2019 → 2040
50% 2508 1010 → 1046 2838 2024 → 2046
80% 799 1634 → 829 931 2469 → 955
90% 489 2295 → 519 512 586 → 542
95% 314 404 → 334 316 402 → 338
99% 155 249 → 181 155 244 → 181

BenchmarkGetMinMaxValueNulls (8192 rows, main → PR): DOUBLE at 90% NULLs 754 → 721 ns (−4%), at 99% 409 → 370 ns (−9%), and at 0–50% +0.6% to +2.2%. BIGINT and every BenchmarkGetMinMaxValueByType case are within ±1%.

Costs: counting the valid rows adds about 20 ns per 8192 rows on float32 columns that have some NULLs but are mostly valid (+1.7% to +3.6%); float64 pays about 1%. It stops after a quarter (float32) or two fifths (float64) of the words of a dense column. Columns without NULLs never reach it. At 80–99% NULLs the PR stays 10–30 ns above the scalar loop: that's the count plus the call.

arm64 thresholds

arm64 has not been measured, so its thresholds are 0. To set them, run the crossover on an arm64 machine:

GOEXPERIMENT=simd go test -run xxx -bench 'Valid$/float' -count 5 ./pkg/common/minmax

For each type, find the NULL percentage where the scalar row starts beating default. Then set scalarBelow["NEON"] in mkminmax.go to 100 minus that percentage, run go generate ./pkg/common/minmax, and check TestSIMDKernelsRun.

Tests

  • Direct kernel test (new): TestValidKernelsDifferential calls the float valid kernels directly, past the density threshold, so they stay tested at every NULL density. TestSIMDKernelsRun checks that a mostly NULL column takes the scalar loop where the threshold is set, and the kernel where it isn't.
  • Differential tests (new): TestIntegerDifferential and TestFloatDifferential compare Integer, Float, IntegerValid and FloatValid (whichever kernels the build has) with plain loops. They cover every element type and a named type, with random lengths up to 700 rows.
    • Masks have an exact random number of valid rows per word, or whole valid and NULL runs.
    • Values come from random bits (every NaN, subnormal and sign pattern), mixed with extremes, -0, and signaling and negative NaNs.
  • Fuzz target (new): FuzzMinMax runs the same comparisons on fuzzer input. 3 minutes of fuzzing in the SIMD build (26.8M executions) found nothing.
  • Mutations: four mutations of the generated kernels each fail the new tests: no NaN test, no negative-value key flip, a wrong lane-select table, and an off-by-one tail reload in the int64 kernel.
  • TestFloatValid and TestIntegerValid gain a "sparse words" mask, about 13 valid rows per word, which lands on both sides of the visit thresholds.
  • pkg/common/minmax passes in the scalar build and in the SIMD build with -race; arm64 SIMD vets.
  • make static-check-changed reports 0 issues.

🤖 Generated with Claude Code

At 80-90% NULLs the float valid kernels were up to 4.7x slower than the
scalar loop. Their visit path, for words with few valid rows, ran scalar
float code, which Go encodes as legacy SSE even with GOAMD64=v3, between
the 256-bit loops. Each switch between the two costs about 300 cycles: a
strict alternation of visit and masked words, which predicts perfectly,
took 6.6 us per 8192 rows against 0.4 and 1.0 us for either path alone.
int kernels, whose visit path stays in integer registers, do not pay it.

On AVX2 the float kernels now visit in integer registers: they read the
values' bits, map them to integer keys that order like the floats (a
negative value's other bits flip), skip NaN with an integer test, and
convert the bounds back after the loop. The keys are set up before any
vector code, so no legacy SSE runs while the 256-bit state is live.
float32 visits up to 12 valid rows a word, float64 up to 16. arm64 is
unchanged.

BenchmarkSwitch measures the paths alone and in alternation, and the
valid tests gain a mask near the visit thresholds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fengttt
fengttt requested a review from XuPeng-SH as a code owner November 30, 2026 07:52
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

TestIntegerDifferential and TestFloatDifferential compare Integer, Float,
IntegerValid and FloatValid, whichever kernels the build has, with plain
loops for every element type and a named one: random lengths up to 700,
masks with an exact random count of valid rows per word or whole runs,
and values from random bits (every NaN, subnormal and sign pattern) mixed
with extremes, -0, signaling and negative NaN. FuzzMinMax runs the same
comparisons on fuzzer input.

Each of four kernel mutations fails them: no NaN skip, no negative-key
flip, a wrong lane-select table, and an off-by-one tail reload. 3 minutes
of fuzzing the SIMD build (26.8M runs) found nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fengttt

fengttt commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

ARM64 fuzz and benchmark results

Apple M2 Max, macOS, Go 1.27.1, GOEXPERIMENT=simd, at 32f2ecc. Benchmarks: median of 8 rounds of 100 ms, cores not pinned.

No float regression on ARM64. This PR does not change the arm64 kernels (minmax_simd_arm64.go is identical to main). NEON shows no path-switch penalty, and the float valid kernels are faster than the scalar loop up to 90% NULLs and equal to it, within noise, at 95–99%.

Fuzz

FuzzMinMax: 9 minutes, 12 workers, 76.4M executions, no failures. pkg/common/minmax also passes in the SIMD build, the SIMD build with -race, and the scalar build.

Gap in FuzzMinMax: replaying its 446 inputs (13 seeds, 433 found) never runs the NULL-free-word path of the valid kernels for 8 of the 10 types (all but int8 and uint8). An all-valid word needs 8 consecutive 0xff mask bytes. Two checks on that:

  • A temporary stronger target (not in this PR), which forces full words and columns with one outlier, ran 4 minutes / 32.9M executions with no failures. Between the two targets, replaying the inputs runs every statement of the generated NEON code.
  • 68 mutations of the NEON kernels (dropped accumulator updates, wrong lane selects or select tables, flipped visit compares, zero instead of NaN fill): the regular tests (-run '^Test') catch all 68. Replaying the FuzzMinMax inputs catches 46. That is replay rather than live fuzzing, so it may understate the target.

So the tests cover the kernels well, but the fuzz target would be stronger with a mask mode that produces full words.

BenchmarkSwitch (ns, 128 words)

kernel visit only masked only mean of the two alternate random 50/50
float32 860 1322 1091 973 898
float64 872 2504 1688 1505 1516
int32 383 934 658 632 626

Alternate and random are below the mean, so there is no switch cost.

BenchmarkValid, scalar loop → NEON kernel (ns, 8192 rows)

NULLs float32 float64
one row 6031 → 963 6.3x 6035 → 1787 3.4x
1% 9091 → 1112 8.2x 9030 → 2160 4.2x
10% 12828 → 1345 9.5x 12802 → 2516 5.1x
50% 7102 → 1322 5.4x 7049 → 2511 2.8x
80% 2882 → 1342 2.1x 2814 → 2418 1.2x
90% 1368 → 1063 1.3x 1372 → 1346 1.02x
95% 696 → 700 0.99x 693 → 699 0.99x
99% 259 → 260 1.0x 259 → 261 0.99x

At 95–99% almost every word is visited row by row, as in the scalar loop. The integer kernels are at worst equal to scalar (int64 at 80%: 1210 → 1213).

GetMinMaxValue, before #33464 → this PR (ns, 8192 rows)

The old side is this PR's tree with the pre-#33464 vector/minmax.go and vector/utils.go. Runs are interleaved, SIMD build on both sides.

NULLs FLOAT DOUBLE
none 5517 → 850 6.5x 5572 → 1660 3.4x
one row 14284 → 1245 11x 14256 → 2071 6.9x
1% 14157 → 1413 10x 14155 → 2418 5.9x
10% 13989 → 1622 8.6x 13472 → 2790 4.8x
50% 22532 → 1611 14x 22072 → 2785 7.9x
80% 7354 → 1700 4.3x 7291 → 2622 2.8x
90% 6819 → 1319 5.2x 6774 → 1677 4.0x
95% 6938 → 933 7.4x 6788 → 955 7.1x
99% 6716 → 552 12x 6716 → 553 12x

BenchmarkMinMax without NULLs, scalar → NEON (8192 rows)

int8 23x, int16 14x, int32 6.5x, int64 1.8x, uint8 23x, uint16 14x, uint32 6.5x, uint64 1.8x, float32 6.7x, float64 3.3x.

🤖 Generated with Claude Code

The integer visit path left float64 at 90% NULLs 12-15% slower than
before: nearly every word took the old visit path, which never switched
and whose float compares were predicted. But at high NULL density the
plain scalar loop, which runs no 256-bit code, beats every kernel
variant. FloatValid now counts the valid rows, stopping once there are
enough, and below a per-architecture threshold takes the scalar loop:
on AVX2 25% valid for float32 and 40% for float64, the measured
crossovers. arm64 keeps the kernels (0) until it is measured.

TestValidKernelsDifferential runs the float valid kernels directly, past
the threshold, so they stay tested at every density, and
TestSIMDKernelsRun checks the dispatch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fengttt and others added 2 commits November 30, 2026 00:45
FuzzMinMax never reached the NULL-free-word path of the valid kernels for
8 of the 10 types: an all-valid word needs 8 consecutive 0xff mask bytes.
The kind byte now also selects whether even and odd words are all valid.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Measured on an M2 Max: faster than the scalar loop up to 90% NULLs, equal
at 95% and 99%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XuPeng-SH added a commit that referenced this pull request Nov 30, 2026
## What type of PR is this?

- [x] Documentation
- [x] Test and CI

## Which issue(s) this PR fixes:

Related to #30150 for the required check on cancelled producers. The
rollout uses the control runner support from #28717 and addresses the
queue observed on #33494 and #33501.

## What this PR does / why we need it:

On #33501 the old required check waited 7m35s for a hosted runner and
ran for four seconds. The replacement workflow was unable to start
preflight during that wait. On #33494 the old required check and the
cancellation controller also queued together. [Required check from
#33501](https://github.com/matrixorigin/matrixone/actions/runs/46374859836/job/141702801415)
· [Required check from
#33494](https://github.com/matrixorigin/matrixone/actions/runs/46368023504/job/141705095759)

The repository setting for preflight, the required check and the
cancellation controller now selects the shared CS medium pool. This
update records the operational setting and how to restore hosted
execution and release a superseded run if a runner label stops serving
work.

Two simultaneous jobs using the shared label started on MTY and RUH in 7
and 11 seconds. A hosted reference submitted with them waited 61
seconds. JED and MNL2 also ran the same checkout, PR metadata read and
required-check verification. [Shared-label
comparison](https://github.com/matrixorigin/matrixone/actions/runs/46378188128)
· [JED and
MNL2](https://github.com/matrixorigin/matrixone/actions/runs/46380046197)

In a branch-isolated handoff, cancellation made the old required check
fail and the successor reached a runner 50 seconds after its workflow
was created. A second exercise held the old required check on a
nonexistent label, restored the hosted default for the successor, and
used GitHub's force-cancel operation on the obsolete probe. The 65
existing scope and cancellation tests passed locally. [Cancelled
producer](https://github.com/matrixorigin/matrixone/actions/runs/46379765379)
· [Successful
successor](https://github.com/matrixorigin/matrixone/actions/runs/46379836410)
· [Hosted
recovery](https://github.com/matrixorigin/matrixone/actions/runs/46380130392)

After the repository setting changed on November 30 at 08:20 UTC, the
production preflight picked up this main PR on RUH in 15 seconds and a
4.2-dev PR on MTY in 5 seconds. The cancellation controller for this PR
picked up on RUH in 10 seconds. [Main
preflight](https://github.com/matrixorigin/matrixone/actions/runs/46380837572/job/141720723388)
· [4.2-dev
preflight](https://github.com/matrixorigin/matrixone/actions/runs/46381492276/job/141721422578)
· [Cancellation
controller](https://github.com/matrixorigin/matrixone/actions/runs/46380847802/job/141719313212)

On main PR #33502 the deployed controller picked up on RUH in 11
seconds, waited the existing grace period, and automatically
force-cancelled the superseded run while its hosted required check was
queued. The successor preflight picked up on MTY in 5 seconds. From the
new workflow being created to preflight starting took 2m48s. This
documentation PR's required check also passed on MTY after a 6-second
queue. [Automatic
cancellation](https://github.com/matrixorigin/matrixone/actions/runs/46382877728/job/141725947578)
· [Successor
preflight](https://github.com/matrixorigin/matrixone/actions/runs/46382871951/job/141727210246)
· [Passing required
check](https://github.com/matrixorigin/matrixone/actions/runs/46380837572/job/141731647152)
@fengttt
fengttt merged commit 46ba076 into matrixorigin:main Nov 30, 2026
8 of 10 checks passed

This branch was successfully deployed

No deployments
ci — 279fa151 Deployed Nov 30, 2026 by fengttt via Matrixone Utils CI / pr-size-label #67095
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants