Skip to content

Lower Julia types in any library's context, validate CPU names and options, select an ABI - #734

Merged
maleadt merged 4 commits into
mainfrom
tb/interop-libraries
Nov 26, 2026
Merged

maleadt merged 4 commits into
mainfrom
tb/interop-libraries

Conversation

@maleadt

@maleadt maleadt commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

These are the APIs that GPUCompiler needs once a top-level job moves its module to libLLVMGPU (JuliaGPU/GPUCompiler.jl#1122). Today GPUCompiler covers for them with tables and raw C API calls.

  • convert(LLVMType, T) and isboxed(T) in a context of any library. Julia's code generator only lowers types into a context of Julia's LLVM, so convert refused other contexts. It now lowers the type in a scratch context of Julia's LLVM and rebuilds it in the active context. That works because jl_type_to_llvm only produces literal types (void, integers, floating point, pointers, arrays, vectors, literal structs), on 1.10 up to master. In a context with typed pointers, types that contain pointers also need Julia's LLVM to support typed pointers (LLVM 16 or older). The rebuild costs about 2 µs. GPUCompiler can then delete its own julia_llvm_type, and Metal.jl's convert calls keep working after the move.
  • LLVM.processors(sti) and LLVM.isprocessor(sti, name), from new LLVMExtra functions around MCSubtargetInfo's processor table and isCPUStringValid. LLVM only warns about an unknown CPU and then ignores it, so callers that want to reject a bad CPU name currently keep their own table of processors (GPUCompiler has one for GCN).
  • LLVM.clopts throws an LLVMException when parsing fails. It uses LLVMParseCommandLineOptionsWithError, which LLVM 24 adds ([llvm-c] Add LLVMParseCommandLineOptionsWithError llvm/llvm-project#238461) and LLVMExtra backports to older versions. Before this, an unknown option or an invalid value was silently ignored. A failed call can still have set some options, as the docstring says. res/wrap.jl only knows LLVM 15..23, so the generated wrapper always calls LLVMExtra. Regenerating once LLVM 24 is supported adds the branch to LLVM's own function.
  • TargetMachine(t, triple; abi) sets the ABI name, like llc -target-abi. It uses LLVMCreateTargetMachineWithOptions and so needs LLVM 18; below that it throws an ArgumentError. With an empty abi the machine is created as before. GPUCompiler uses it with libLLVMGPU's AMDGPU back-end (abi="i128:64", which keeps the 8-byte i128 alignment that Julia needs before 1.12) instead of calling the options API directly.

Each commit adds its NEWS entry under 10.8. lib/ and res/ABI.md were regenerated with res/wrap.jl in the commits that change LLVMExtra.h. A release needs a new LLVMExtra_jll (and LLVMGPU_jll) built from this branch's merge commit, followed by the usual "Bump LLVMExtra, and the version to 10.8.0" commit.

Validation (pegasus, locally built LLVMExtra):

  • Pkg.test() on Julia 1.12 (10957 pass) and 1.10 (9956 pass); 1.10 with LLVM_TEST_LIBRARY set to a standalone LLVM 23 (10026 pass).
  • test/standalone/runtests.jl with standalone LLVM 16 and 23 (185 + 12 pass); docs build on 1.12.
  • LLVM_TEST_LIBRARY set to libLLVMGPU 1.4.1: the only failures (17 fail, 10 error) are tests that call the new LLVMExtra functions, which that build doesn't contain yet.
  • res/wrap.jl regenerates lib/ and res/ABI.md without changes.
  • abi="i128:64" with libLLVMGPU's AMDGPU back-end adds i128:64 to the data layout. The test suite checks the ABI name with WebAssembly's experimental-mv (LLVM 19+), and the forwarding of the other options with X86.

@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview: https://juliallvm.github.io/LLVM.jl/previews/PR734/

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.29032% with 36 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/interop/base.jl 44.61% 36 Missing ⚠️
@@            Coverage Diff             @@
##             main     #734      +/-   ##
==========================================
- Coverage   94.70%   94.40%   -0.31%     
==========================================
  Files          59       59              
  Lines        9950    10020      +70     
==========================================
+ Hits         9423     9459      +36     
- Misses        527      561      +34     
Files with missing lines Coverage Δ
src/subtarget.jl 96.22% <100.00%> (+0.57%) ⬆️
src/support.jl 100.00% <100.00%> (ø)
src/targetmachine.jl 98.92% <100.00%> (+0.17%) ⬆️
src/interop/base.jl 50.68% <44.61%> (-45.75%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

convert(LLVMType, T) refused contexts of libraries other than Julia's, as
Julia's code generator only creates types in a context of its own LLVM.
Lower the type in a temporary context of Julia's LLVM instead, and rebuild
the result (literal types only) in the active context, so that code moved
to another library, e.g. libLLVMGPU, can keep using the same call.

isboxed uses the same temporary context, which is created and disposed
of directly so that it is also freed while an exception is being handled.
Add LLVM.processors and LLVM.isprocessor, backed by new LLVMExtra functions
around MCSubtargetInfo's processor table and isCPUStringValid. LLVM only warns
about an unknown CPU name and falls back to a default processor, so callers
that want to reject such names otherwise need their own table of processors.
LLVM.clopts used LLVMParseCommandLineOptions, which ignores parse failures, so
an unknown option or an invalid value went unnoticed. Use
LLVMParseCommandLineOptionsWithError (LLVM 24) instead, which LLVMExtra
provides for older versions, and throw an LLVMException with its message.
Add an `abi` keyword to the TargetMachine constructor, which sets the ABI name
of the machine's options (LLVM 18 and later), like `llc -target-abi`. Back-ends
that support several ABIs select one this way, e.g., an AMDGPU back-end that
keeps the 8-byte alignment of i128 that Julia needs before 1.12. Without it,
the machine is created as before, also on older versions of LLVM.
@maleadt
maleadt force-pushed the tb/interop-libraries branch 2 times, most recently from adf4bde to 8a56a3b Compare November 26, 2026 14:10
@maleadt
maleadt merged commit 54bc781 into main Nov 26, 2026
59 checks passed
@maleadt
maleadt deleted the tb/interop-libraries branch November 26, 2026 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant