← Back to Pull requests

Ship only the MLIR dialects numba-cuda-mlir uses #463

Closed
unknown closed this pull request without merging on Nov 18, 2026 20:46 UTC
View on GitHub
unknown unknown commented on Nov 13, 2026 04:03 UTC
Author

xref https://github.com/NVIDIA/numba-cuda-mlir/issues/133#issuecomment-5025522204

numba-cuda-mlir bundles upstream's stock MLIR Python package. Its _mlirRegisterEverything links every upstream dialect, conversion and pass into the MLIR Python CAPI library, and the LLVM build compiles all of MLIR to get there. numba-cuda-mlir imports 18 dialects and runs one pass pipeline.

This PR builds our own numba_cuda_mlir._mlir instead, in cext/mlir-python/, the LLVM external project that #466 added. It takes upstream's declared Python sources for those 18 dialects and adds a _site_initialize_0 module that registers only the dialects, interface models, pipeline passes and LLVM IR translations numba-cuda-mlir needs. No MLIR source patch is involved. The CAPI library keeps the unique name and Windows LLVM-C exports from #466. ci/build-llvm-modern.sh then builds and installs only LLVM distribution components: the package plus the libraries cext/ links via find_package(MLIR). A first configure computes that list from the CMake link graph.

Linux x86_64, same LLVM pin and flags, -j8, no compiler cache:

| | main | this PR |
|---|---|---|
| LLVM build, wall / CPU | 29m24s / 3h52m | 21m44s / 2h50m |
| MLIR Python CAPI library | 172 MB | 92 MB |
| wheel without libLLVM-7.so, compressed | 57.9 MB | 28.3 MB |
| LLVM install dir (CI cache and artifact) | 980 MB | 636 MB |

CI wheel artifacts for cp312, main at f77f830 vs this PR:

| | main | this PR |
|---|---|---|
| linux-64 | 70.6 MB | 41.7 MB |
| linux-aarch64 | 66.6 MB | 39.4 MB |
| win-64 | 41.5 MB | 21.5 MB |
| win-arm64 | 28.3 MB | 11.2 MB |

The full test suite gives identical results on both local Linux wheels. tests/mlir/test_registration.py checks that the base pipeline parses and that the dialects numba-cuda-mlir uses are registered, so a pass added to mlir_optimization.py without being registered fails there.

Notes:

  • SPIR-V and ROCDL are still compiled and linked, although cext/mlir-python/CMakeLists.txt does not name them. At this LLVM pin they come in through upstream libraries we do need; see "How SPIR-V and ROCDL get in" below.

  • On Windows, CMAKE_OBJECT_PATH_MAX is lowered for cext/mlir-python. Upstream's binding sources are outside that directory, so their object paths embed the absolute source path, and on the GitHub-hosted runners one went past MSVC's 260-character limit.

  • #386 would need its ExecutionEngine pieces added to the package, and #383 touches the same build scripts.

  • Related: conda-forge/numba-cuda-mlir-feedstock#6. The feedstock builds LLVM for every Python version, so the build-time saving applies there too.
  • <details>
    <summary>How SPIR-V and ROCDL get in</summary>

    Why they are compiled. CMake builds the whole link closure of the libraries we list.

  • MLIRGPUTransforms is needed for gpu-kernel-outlining and the GPU interface models, and cext/mlir-llvm70 links it. At this pin it also contains SPIRVAttachTarget.cpp, ROCDLAttachTarget.cpp and XeVMAttachTarget.cpp, and links MLIRSPIRVTarget, MLIRROCDLTarget, MLIRROCDLDialect, MLIRAMDGPUDialect and MLIRXeVMDialect. The GPU-to-NVVM conversions we register reach the same libraries through their own dependencies.

  • MLIRToLLVMIRTranslationRegistration, which llvm70-translate in cext/mlir-llvm70 links, links every LLVM IR translation, SPIR-V and ROCDL included.
  • Why they end up in the CAPI library. The linker keeps only the objects something references, but C API libraries are embedded whole.

  • The gpu Python dialect brings in upstream's _mlirDialectsGPU extension, which embeds MLIRCAPIGPU.

  • MLIRCAPIGPU includes GPUPasses.cpp, which generates an exported C create and register function for every GPU pass, spirv-attach-target and rocdl-attach-target included.

  • Resolving those pulls in the attach-target objects, and through them the SPIR-V and ROCDL dialects. In the subset CAPI library on Linux, nm -D shows about 3 MB of exported mlir::spirv symbols and 2.6 MB of mlir::ROCDL.
  • What dropping them would take. Declaring our own gpu Python sources, without upstream's _mlirDialectsGPU and _mlirGPUPasses extensions, plus a GPU C API built from upstream's lib/CAPI/Dialect/GPU.cpp alone, should keep them out of the CAPI library; this has not been tried. They would still be compiled unless the attach-target passes were in a library of their own, which they are not at this pin.

    </details>

    <details>
    <summary>Upstream patterns this follows</summary>

    Package assembly follows upstream's mlir/examples/standalone:

  • It builds the CAPI library and the Python modules from an explicit list of source groups, MLIRPythonSources.Core and MLIRPythonSources.Dialects.builtin plus its own dialect, with no _mlirRegisterEverything. cext/mlir-python/CMakeLists.txt does the same with 18 dialect groups.

  • It can build as an LLVM external project, which is how cext/mlir-python plugs into the LLVM build.

  • The bindings doc describes this layout as allowing "downstream integrators to completely customize what is included 'in the box'".
  • Dialect registration differs from the example. The example registers its dialects through a register_dialects(context) function the user calls. Here every Context gets them automatically, as with the stock package, through the hook in _mlir_libs/__init__.py: when _mlirRegisterEverything is absent, it loads _site_initialize_0, _1, ... and calls their register_dialects for each new context. What Registration.cpp registers follows upstream's RegisterAllDialects.cpp and RegisterAllExtensions.cpp, filtered to these dialects.

    Building only what is needed is LLVM's LLVM_DISTRIBUTION_COMPONENTS mechanism, described in BuildingADistribution.rst.

    </details>

    -- Leo's bot

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: atmnp Review duration: 8202m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #463"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%