[MLIR] Add C API for Enzyme dialect (MLIRCAPIEnzyme)#2859
Conversation
Adds the missing C API layer for the Enzyme MLIR dialect, following the same pattern as upstream MLIR's mlir-c/ libraries: - `MLIR_DECLARE/DEFINE_CAPI_DIALECT_REGISTRATION` for the enzyme dialect - `enzymeActivityAttrGet` — constructs an `enzyme.activity` attr from the C-side `EnzymeActivity` enum (enzyme_active, enzyme_dup, enzyme_const, …) - `enzymeAutoDiffOpCreate` / `enzymeForwardDiffOpCreate` — construct unattached `enzyme.autodiff` / `enzyme.fwddiff` ops from C - `Integrations/c/CMakeLists.txt` wiring up `MLIRCAPIEnzyme` as an `add_mlir_public_c_api_library` target - `Integrations/CMakeLists.txt` + hook in `MLIR/CMakeLists.txt` This enables foreign-language bindings (Rust, Python, etc.) to emit Enzyme dialect ops without depending on C++ headers.
|
@mofeing note once this lands, we should adapt reactantextra to remove its copy of similar apis |
|
@agarret7 minor, but clang-format fails |
…D on all definitions
|
Updated this PR with the following additions:
Motivation: I'm planning a PR to Melior to expose Enzyme-backed autodiff. Melior upgraded to MLIR 22 a couple months ago, so I validated this C API end-to-end against MLIR 22 via a small Rust test harness using bindgen. |
wsmoses
left a comment
There was a problem hiding this comment.
sorry one more nit for the activity one, then lgtm
I'm not super familiar with bazel, what would that entail? |
Looking at enzyme/BUILD, the CInterface target (BUILD:1052-1053) already builds |
Add C API for Enzyme MLIR dialect (
MLIRCAPIEnzyme)The Enzyme MLIR dialect has no C API, making it inaccessible to
foreign-language bindings (Rust, Python, etc.) without depending on C++
headers. This PR adds the missing layer, following the same pattern used
by upstream MLIR's own dialect C APIs (
mlir-c/Dialect/Arith.h, etc.).What's added
MLIR_DECLARE/DEFINE_CAPI_DIALECT_REGISTRATIONso the dialect can be loaded into an
MlirContextfrom CenzymeActivityAttrGet— constructs anenzyme.activityattributefrom a C-side
EnzymeActivityenumenzymeAutoDiffOpCreate/enzymeForwardDiffOpCreate— constructunattached
enzyme.autodiff/enzyme.fwddiffops from C, taking thesame arguments as the TableGen-generated C++ builders
Integrations/c/CMakeLists.txtwith anadd_mlir_public_c_api_library(MLIRCAPIEnzyme ...)target, wired intoIntegrations/CMakeLists.txtandMLIR/CMakeLists.txtNot included (deferred)
enzymeRegisterPassesis not included becauseMLIREnzymeTransformsdepends on
MLIREnzymeAnalysis, which currently fails to build due to apre-existing MLIR API change (
getSuccessorInputs→getSuccessorRegions), tracked in #2858.Testing
MLIRCAPIEnzymebuilds cleanly against LLVM/MLIR 22. The round-trip test will pass once theMLIREnzymeAnalysisbuild failures are resolved.Edit: The test/MLIR/CAPI/roundtrip.mlir I included is a round-trip test for the ops themselves, not the C API. The existing ForwardMode/ReverseMode tests already cover that. A proper C API test requires a compiled C binary and lit infrastructure that doesn't exist in Enzyme's test suite yet. I've removed the placeholder. Happy to add proper C test infrastructure as a follow-up if that's the preferred direction.