Skip to content

Fix JIT symbol lookup on macOS - #104946

Merged
Algunenano merged 7 commits into
ClickHouse:masterfrom
leshikus:fix-jit-symbol-mangling-macos
May 20, 2026
Merged

Algunenano merged 7 commits into
ClickHouse:masterfrom
leshikus:fix-jit-symbol-mangling-macos

Conversation

@leshikus

@leshikus leshikus commented May 14, 2026 •

Copy link
Copy Markdown
Collaborator

On macOS, llvm::Mangler::getNameWithPrefix prepends _ to C symbol names, but
CHJIT registers external symbols unmangled (memset, memcpy,
memcmpSmallCharsAllowOverflow15, etc.). JITSymbolResolver::findSymbol then
gets handed the mangled name and the lookup misses, producing:

Code: 263. DB::Exception: Could not find symbol _memcmpSmallCharsAllowOverflow15.
(CANNOT_COMPILE_CODE)

This fires for any query that JIT-compiles a comparator over a String column,
e.g. an ORDER BY with compile_sort_description = 1. It's one of the two
root causes called out in the Fast test (arm_darwin) umbrella issue, and the
one I can reproduce locally on Apple Silicon.

Linux is unaffected because LLVM's mangler doesn't add the _ prefix on ELF
targets, so the lookup succeeds on the first try.

Approach

In JITSymbolResolver::findSymbol, on a miss for a name that begins with _,
retry the lookup with the leading underscore stripped. No platform #ifdef —
the fallback is unreachable on Linux because the first lookup already succeeds.
Six inserted lines, no behavior change for any name that's already in the map.

A small regression test under tests/queries/0_stateless/ exercises the exact
path that used to fail (sort description JIT on a String column).

Verification

Built Build (arm_darwin) locally and verified:

  • The previously failing query now succeeds (correct result, Compile sort description (type: String, ...) trace emitted, no Could not find symbol).
  • New test 04230_jit_sort_description_string_symbol passes via
    python3 -m ci.praktika run "Fast test (arm_darwin)".
  • Existing JIT-tagged fast tests pass with no regressions (10/10 — sort
    description, expression, aggregate JIT paths all exercised).

Fixes part of #103235.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix JIT symbol resolution on macOS so that sort, expression, and aggregate JIT compilation no longer fail with CANNOT_COMPILE_CODE: Could not find symbol _<name>.

Version info

  • Backported to: 26.5.3.35

leshikus added 2 commits May 13, 2026 12:45
llvm::Mangler::getNameWithPrefix prepends '_' to C symbol names on macOS,
but registerSymbol() in CHJIT stores them unmangled. The lookup in
JITSymbolResolver::findSymbol misses, surfacing as:

  Could not find symbol _memcmpSmallCharsAllowOverflow15
  (CANNOT_COMPILE_CODE)

Any query that JIT-compiles a comparator over a String column hits this
in the macOS arm_darwin Fast test job — for example, a sort description
that includes a String key with compile_sort_description=1.

Retry the lookup without the leading underscore as a fallback. Linux is
unaffected because LLVM doesn't add the '_' prefix there. macOS now finds
the symbol on the second try and JIT compilation succeeds.

Issue: ClickHouse#103235
Exercises the sort-description JIT path on a String column with
`compile_sort_description=1` and `min_count_to_compile_sort_description=0`.
Before the parent commit on macOS this query failed with
`CANNOT_COMPILE_CODE: Could not find symbol _memcmpSmallCharsAllowOverflow15`.

Issue: ClickHouse#103235
@clickhouse-gh

clickhouse-gh Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e9ef8ab]

Summary: ❌

job_name test_name status info comment
Upgrade check (amd_release) FAIL
Check failed FAIL cidb IGNORED
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL IGNORED

AI Review

Summary

This PR fixes CHJIT external symbol resolution on macOS by canonicalizing registered symbol names with llvm::Mangler::getNameWithPrefix against the active DataLayout, so findSymbol lookup keys match LLVM-provided mangled names directly. I reviewed the current diff, surrounding CHJIT code, and prior PR discussion threads; I did not find unresolved correctness, safety, or performance issues in the final patch.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 14, 2026
@Algunenano Algunenano self-assigned this May 14, 2026
@leshikus
leshikus requested a review from tavplubix May 14, 2026 14:15
Comment thread src/Interpreters/JIT/CHJIT.cpp Resolved
@leshikus
leshikus removed the request for review from tavplubix May 14, 2026 15:48
leshikus added 2 commits May 14, 2026 19:42
Per review on ClickHouse#104946 (review),
replace the macOS-specific fallback in JITSymbolResolver::findSymbol with
target-mangling at registration time. registerSymbol now runs the symbol
name through llvm::Mangler::getNameWithPrefix using the JIT's DataLayout,
so the map keys match exactly the names LLVM produces inside the generated
object code. findSymbol becomes a single map lookup with no platform-specific
fallback path.

The behavior on Linux is unchanged because the mangler does not add a prefix
on ELF targets - the mangled form is identical to the input. On macOS the
mangler adds the leading underscore that the previous code stripped on a miss.
The previous version of the test used `count()` over an inner ORDER BY,
which the optimizer drops because count is order-independent. The sort
step was eliminated before any JIT decision was made, so
`compileSortDescriptionIfNeeded` never ran and the test passed whether
or not the JIT symbol-mangling fix was present. Confirmed empirically
on `Fast test (arm_darwin)`: the test passed against the unfixed
master binary and the server log contained no `Compile sort description`
or `Could not find symbol` entries.

The new query uses a top-level ORDER BY with `FORMAT Null` so the
optimizer cannot drop the sort. A follow-up SELECT against
`system.query_log` asserts that `ProfileEvents['CompileFunction']`
is non-zero, so a future regression that bypasses the JIT path (e.g.
optimizer drops the sort again, or a setting default changes) fails
loudly instead of silently passing.
Address review feedback on the regression test:
- `SYSTEM FLUSH LOGS` without an argument fails the repository checks
  and flushes more than the test needs; narrow it to `query_log`.
- Tag the JIT query with a unique `log_comment` and filter the
  assertion `SELECT` by that comment, so concurrent or pre-existing
  entries in the shared `system.query_log` cannot influence the result.
@leshikus

Copy link
Copy Markdown
Collaborator Author

Bugfix validation expectedly fails - it uses linux to check

@leshikus
leshikus marked this pull request as ready for review May 14, 2026 22:44
CI on https://github.com/ClickHouse/ClickHouse/actions/runs/25888833167/job/76087238926?pr=104946
flagged two issues against the rewritten test:

1. Style check rejects queries against `system.query_log` that lack a
   `current_database = currentDatabase()` clause. Add it - it composes
   with the `log_comment` filter and tightens isolation further.

2. Linux Fast test failed `expected 1, got 0`. The compiled-expression
   cache is server-wide, so an earlier test with the same sort-description
   hash had already populated it. Our SELECT hit the cache,
   `compileSortDescription` did not run, and `ProfileEvents['CompileFunction']`
   stayed at zero. Run `SYSTEM DROP COMPILED EXPRESSION CACHE` before the
   JIT query to force a fresh compilation.

@Algunenano Algunenano 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.

We can just drop the test. If it works and solves Darwin I trust you. We have many JIT tests that will pass in that platform now

@leshikus

leshikus commented May 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

Raul, I just need a bit more time to complete this, after I resolve first priority issues. I want reliable reproducer before committing this.

@leshikus
leshikus marked this pull request as draft May 20, 2026 11:36
@Algunenano

Copy link
Copy Markdown
Member

@leshikus Don't worry I got it. I'll test it on Darwin and merge, don't worry

@Algunenano
Algunenano marked this pull request as ready for review May 20, 2026 11:48

@Algunenano Algunenano 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.

I can't reproduce the issue locally without this patch, because I'm using LLVM 22
to build and apparently it does not have this problem. But using the CI build from master it does reproduce, and this PR fixes it.

@clickhouse-gh

clickhouse-gh Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 90.91% (10/11) · Uncovered code

Full report · Diff report

@Algunenano
Algunenano added this pull request to the merge queue May 20, 2026
Merged via the queue into ClickHouse:master with commit b5024af May 20, 2026
163 of 166 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 20, 2026
@leshikus

Copy link
Copy Markdown
Collaborator Author

Thanks for taking care of it

DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
Per review on ClickHouse#104946 (review),
replace the macOS-specific fallback in JITSymbolResolver::findSymbol with
target-mangling at registration time. registerSymbol now runs the symbol
name through llvm::Mangler::getNameWithPrefix using the JIT's DataLayout,
so the map keys match exactly the names LLVM produces inside the generated
object code. findSymbol becomes a single map lookup with no platform-specific
fallback path.

The behavior on Linux is unchanged because the mangler does not add a prefix
on ELF targets - the mangled form is identical to the input. On macOS the
mangler adds the leading underscore that the previous code stripped on a miss.
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…gling-macos

Fix JIT symbol lookup on macOS
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 16, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Jun 16, 2026
Cherry pick #104946 to 26.5: Fix JIT symbol lookup on macOS
Algunenano added a commit that referenced this pull request Jun 16, 2026
Backport #104946 to 26.5: Fix JIT symbol lookup on macOS
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 16, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 3, 2026
Pick up merged fix ClickHouse#104946 (JIT symbol lookup on macOS) and current master
so CI re-runs the Fast test (arm_darwin) job on a healthy runner. The prior
run's arm_darwin red was a stale 2026-04-29 setup abort during the ~3-day
macOS runner outage (root cause tracked in ClickHouse#103235, LLVM 21 on Apple Silicon).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.5-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants