Repository navigation
Fix JIT symbol lookup on macOS - #104946
Conversation
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
|
Workflow [PR], commit [e9ef8ab] Summary: ❌
AI ReviewSummaryThis PR fixes Final Verdict
|
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.
|
Bugfix validation expectedly fails - it uses linux to check |
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
left a comment
There was a problem hiding this comment.
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
|
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 Don't worry I got it. I'll test it on Darwin and merge, don't worry |
Algunenano
left a comment
There was a problem hiding this comment.
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.
LLVM Coverage Report
Changed lines: 90.91% (10/11) · Uncovered code |
b5024af
|
Thanks for taking care of it |
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.
…gling-macos Fix JIT symbol lookup on macOS
Cherry pick #104946 to 26.5: Fix JIT symbol lookup on macOS
Backport #104946 to 26.5: Fix JIT symbol lookup on macOS
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]>
On macOS,
llvm::Mangler::getNameWithPrefixprepends_to C symbol names, butCHJITregisters external symbols unmangled (memset,memcpy,memcmpSmallCharsAllowOverflow15, etc.).JITSymbolResolver::findSymbolthengets handed the mangled name and the lookup misses, producing:
This fires for any query that JIT-compiles a comparator over a
Stringcolumn,e.g. an
ORDER BYwithcompile_sort_description = 1. It's one of the tworoot causes called out in the
Fast test (arm_darwin)umbrella issue, and theone I can reproduce locally on Apple Silicon.
Linux is unaffected because LLVM's mangler doesn't add the
_prefix on ELFtargets, 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 exactpath that used to fail (sort description JIT on a
Stringcolumn).Verification
Built
Build (arm_darwin)locally and verified:Compile sort description (type: String, ...)trace emitted, noCould not find symbol).04230_jit_sort_description_string_symbolpasses viapython3 -m ci.praktika run "Fast test (arm_darwin)".description, expression, aggregate JIT paths all exercised).
Fixes part of #103235.
Changelog category (leave one):
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
26.5.3.35