Skip to content

Improve performance of the {Norm}Distance and dotProduct functions - #106007

Merged
alexey-milovidov merged 3 commits into
masterfrom
vec-Q2-fp-contract
May 30, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
vec-Q2-fp-contract

Conversation

@nickitat

@nickitat nickitat commented May 27, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Improved performance of the {Norm}Distance and dotProduct functions.


Screenshot 2026-05-28 at 15 00 04 Screenshot 2026-05-28 at 14 59 43

Version info

  • Merged into: 26.6.1.260

The project sets `-ffp-contract=off` globally in the top-level
`CMakeLists.txt` for numerical determinism across CPUs, which blocks
the compiler from fusing `a*b + c` into `vfmadd*` in every TU.

The dot-product and distance kernels in `arrayDotProduct.cpp` /
`arrayDistance.cpp` have hot inner loops that depend on FMA generation
to saturate the FP pipeline. A comment in `arrayDotProduct.cpp` even
claims "compiler auto-vectorizes with FMA", which is currently false
because of the global flag.

Re-enable FMA fusion only for these two TUs via per-file
`COMPILE_FLAGS "-ffp-contract=fast"`. Per-source `COMPILE_FLAGS` are
appended after `CMAKE_CXX_FLAGS`, so this override wins over the
global setting without touching any other TU.

Picked Option B (per-source `set_source_files_properties` in
`src/Functions/array/CMakeLists.txt`) over Option A (in-source
`#pragma clang fp contract(fast)`) because (a) the convention is
already used in this same file for `array.cpp`, (b) each file has
multiple FMA-eligible scopes (the leaf `accumulate` functions plus
the templated dispatch loops that inline them), and a file-scope
override cannot miss any of them, and (c) this leaves the existing
source comments untouched and makes them accurate.

The override is a no-op for distance kernels that don't have
`a*b + c` patterns (`L1` uses `fabs`, `Linf` uses `fmax`, `Lp` uses
`pow`).
@clickhouse-gh

clickhouse-gh Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [03ec248]

Summary: ✅


AI Review

Summary

This PR enables -ffp-contract=fast for arrayDotProduct.cpp and arrayDistance.cpp so the compiler may fuse floating-point multiply-adds in the affected array kernels. The change is narrowly scoped, but I cannot approve it yet because the PR is categorized as Performance Improvement and the current review still has no completed before/after performance evidence for the claimed gain.

PR Metadata
  • Changelog category as Performance Improvement matches the intent.

  • The Changelog entry is required and present, but it should be more user-facing, backtick SQL function names, and disclose the expected ULP-level floating-point result change from enabling FMA fusion.

  • Suggested replacement:

    Improves performance of arrayDotProduct, arrayL2Distance, arrayL2SquaredDistance, and arrayCosineDistance by enabling FMA fusion; floating-point results may change at the ULP level.

Missing context / blind spots
  • ⚠️ The current CI report still shows all Performance Comparison shards as PENDING; completed amd/arm performance results or a focused benchmark would close the review gap.
  • ⚠️ I could not run .claude/tools/fetch_perf_report.py locally because this checkout has no clickhouse binary in PATH.
Findings

⚠️ Majors

  • [src/Functions/array/CMakeLists.txt:33] [dismissed by author -- https://github.com/Improve performance of the {Norm}Distance and dotProduct functions #106007#discussion_r3313441436] The PR promises a Performance Improvement, but the PR body has no before/after measurement and the CI Performance Comparison shards are still PENDING. Because this change deliberately opts arrayDotProduct.cpp and arrayDistance.cpp out of the global -ffp-contract=off determinism policy, the performance benefit and any regression profile need concrete evidence before merge. Suggested fix: wait for the amd/arm Performance Comparison reports or attach a focused benchmark covering the affected arrayDotProduct and distance kernels.
Tests
  • ⚠️ Missing performance evidence for the affected kernels. The smallest useful proof is either completed Performance Comparison output for the relevant queries on amd and arm, or a focused before/after benchmark that exercises arrayDotProduct and the FMA-eligible distance kernels.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions: provide completed performance evidence for the claimed speedup and update the changelog entry to be user-facing and explicit about the floating-point result change.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label May 27, 2026
@nickitat nickitat added the ci-performance performance only label May 27, 2026
Comment thread src/Functions/array/CMakeLists.txt Resolved
@nickitat nickitat removed the ci-performance performance only label May 27, 2026
@nickitat
nickitat marked this pull request as ready for review May 28, 2026 13:06
@nickitat nickitat changed the title Enable FMA fusion for arrayDotProduct and arrayDistance kernels Improve performance of the {Norm}Distance and dotProduct functions May 28, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

But I'd risk and try to enable fp-contract fast everywhere.

@alexey-milovidov alexey-milovidov self-assigned this May 30, 2026
@alexey-milovidov
alexey-milovidov merged commit c0c4b80 into master May 30, 2026
326 of 328 checks passed
@alexey-milovidov
alexey-milovidov deleted the vec-Q2-fp-contract branch May 30, 2026 07:52
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 30, 2026
@nickitat

Copy link
Copy Markdown
Member Author

Good.

But I'd risk and try to enable fp-contract fast everywhere.

ok, will do

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

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants