3 ################################################################################
4 # Structural groupings.
5 ################################################################################
7 declare_mlir_python_sources(MLIRPythonSources)
8 declare_mlir_python_sources(MLIRPythonSources.Dialects
9 ADD_TO_PARENT MLIRPythonSources)
11 ################################################################################
12 # Pure python sources and generated code
13 ################################################################################
15 declare_mlir_python_sources(MLIRPythonSources.Core
16 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
17 ADD_TO_PARENT MLIRPythonSources
19 _mlir_libs/__init__.py
22 dialects/_ods_common.py
24 # The main _mlir module has submodules: include stubs from each.
25 _mlir_libs/_mlir/__init__.pyi
26 _mlir_libs/_mlir/ir.pyi
27 _mlir_libs/_mlir/passmanager.pyi
30 declare_mlir_python_sources(MLIRPythonSources.ExecutionEngine
31 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
32 ADD_TO_PARENT MLIRPythonSources
35 _mlir_libs/_mlirExecutionEngine.pyi
40 declare_mlir_python_sources(MLIRPythonCAPI.HeaderSources
41 ROOT_DIR "${MLIR_SOURCE_DIR}/include"
42 SOURCES_GLOB "mlir-c/*.h"
45 ################################################################################
47 ################################################################################
49 declare_mlir_dialect_python_bindings(
50 ADD_TO_PARENT MLIRPythonSources.Dialects
51 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
52 TD_FILE dialects/AsyncOps.td
53 SOURCES_GLOB dialects/async_dialect/*.py
54 DIALECT_NAME async_dialect)
56 declare_mlir_dialect_python_bindings(
57 ADD_TO_PARENT MLIRPythonSources.Dialects
58 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
59 TD_FILE dialects/BufferizationOps.td
61 dialects/bufferization.py
62 dialects/_bufferization_ops_ext.py
63 DIALECT_NAME bufferization)
65 declare_mlir_dialect_python_bindings(
66 ADD_TO_PARENT MLIRPythonSources.Dialects
67 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
68 TD_FILE dialects/BuiltinOps.td
71 dialects/_builtin_ops_ext.py
74 declare_mlir_dialect_python_bindings(
75 ADD_TO_PARENT MLIRPythonSources.Dialects
76 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
77 TD_FILE dialects/ComplexOps.td
82 declare_mlir_dialect_python_bindings(
83 ADD_TO_PARENT MLIRPythonSources.Dialects
84 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
85 TD_FILE dialects/ControlFlowOps.td
90 declare_mlir_dialect_python_bindings(
91 ADD_TO_PARENT MLIRPythonSources.Dialects
92 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
93 TD_FILE dialects/FuncOps.td
96 dialects/_func_ops_ext.py
99 declare_mlir_dialect_python_bindings(
100 ADD_TO_PARENT MLIRPythonSources.Dialects
101 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
102 TD_FILE dialects/GPUOps.td
103 SOURCES_GLOB dialects/gpu/*.py
106 declare_mlir_dialect_python_bindings(
107 ADD_TO_PARENT MLIRPythonSources.Dialects
108 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
109 TD_FILE dialects/LinalgOps.td
111 dialects/_linalg_ops_ext.py
115 DEPENDS LinalgOdsGen)
117 declare_mlir_dialect_extension_python_bindings(
118 ADD_TO_PARENT MLIRPythonSources.Dialects
119 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
120 TD_FILE dialects/TransformPDLExtensionOps.td
122 dialects/_transform_pdl_extension_ops_ext.py
123 dialects/transform/pdl.py
124 DIALECT_NAME transform
125 EXTENSION_NAME transform_pdl_extension)
127 declare_mlir_dialect_python_bindings(
128 ADD_TO_PARENT MLIRPythonSources.Dialects
129 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
130 TD_FILE dialects/TransformOps.td
132 dialects/_transform_ops_ext.py
133 dialects/transform/__init__.py
134 _mlir_libs/_mlir/dialects/transform/__init__.pyi
135 DIALECT_NAME transform)
137 declare_mlir_dialect_extension_python_bindings(
138 ADD_TO_PARENT MLIRPythonSources.Dialects
139 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
140 TD_FILE dialects/BufferizationTransformOps.td
142 dialects/transform/bufferization.py
143 DIALECT_NAME transform
144 EXTENSION_NAME bufferization_transform)
146 declare_mlir_dialect_extension_python_bindings(
147 ADD_TO_PARENT MLIRPythonSources.Dialects
148 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
149 TD_FILE dialects/GPUTransformOps.td
151 dialects/transform/gpu.py
152 DIALECT_NAME transform
153 EXTENSION_NAME gpu_transform)
155 declare_mlir_dialect_extension_python_bindings(
156 ADD_TO_PARENT MLIRPythonSources.Dialects
157 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
158 TD_FILE dialects/SCFLoopTransformOps.td
160 dialects/_loop_transform_ops_ext.py
161 dialects/transform/loop.py
162 DIALECT_NAME transform
163 EXTENSION_NAME loop_transform)
165 declare_mlir_dialect_extension_python_bindings(
166 ADD_TO_PARENT MLIRPythonSources.Dialects
167 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
168 TD_FILE dialects/LinalgStructuredTransformOps.td
170 dialects/_structured_transform_ops_ext.py
171 dialects/transform/structured.py
172 DIALECT_NAME transform
173 EXTENSION_NAME structured_transform)
175 declare_mlir_dialect_python_bindings(
176 ADD_TO_PARENT MLIRPythonSources.Dialects
177 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
178 TD_FILE dialects/MathOps.td
179 SOURCES dialects/math.py
182 declare_mlir_dialect_python_bindings(
183 ADD_TO_PARENT MLIRPythonSources.Dialects
184 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
185 TD_FILE dialects/ArithOps.td
188 dialects/_arith_ops_ext.py
191 declare_mlir_dialect_python_bindings(
192 ADD_TO_PARENT MLIRPythonSources.Dialects
193 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
194 TD_FILE dialects/MemRefOps.td
197 dialects/_memref_ops_ext.py
200 declare_mlir_dialect_python_bindings(
201 ADD_TO_PARENT MLIRPythonSources.Dialects
202 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
203 TD_FILE dialects/MLProgramOps.td
205 dialects/ml_program.py
206 dialects/_ml_program_ops_ext.py
207 DIALECT_NAME ml_program)
209 declare_mlir_python_sources(
210 MLIRPythonSources.Dialects.quant
211 ADD_TO_PARENT MLIRPythonSources.Dialects
212 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
215 _mlir_libs/_mlir/dialects/quant.pyi)
217 declare_mlir_dialect_python_bindings(
218 ADD_TO_PARENT MLIRPythonSources.Dialects
219 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
220 TD_FILE dialects/PDLOps.td
223 dialects/_pdl_ops_ext.py
224 _mlir_libs/_mlir/dialects/pdl.pyi
227 declare_mlir_dialect_python_bindings(
228 ADD_TO_PARENT MLIRPythonSources.Dialects
229 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
230 TD_FILE dialects/SCFOps.td
233 dialects/_scf_ops_ext.py
236 declare_mlir_dialect_python_bindings(
237 ADD_TO_PARENT MLIRPythonSources.Dialects
238 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
239 TD_FILE dialects/ShapeOps.td
240 SOURCES dialects/shape.py
243 declare_mlir_dialect_python_bindings(
244 ADD_TO_PARENT MLIRPythonSources.Dialects
245 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
246 TD_FILE dialects/SparseTensorOps.td
247 SOURCES dialects/sparse_tensor.py
248 DIALECT_NAME sparse_tensor)
250 declare_mlir_dialect_python_bindings(
251 ADD_TO_PARENT MLIRPythonSources.Dialects
252 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
253 TD_FILE dialects/TensorOps.td
256 dialects/_tensor_ops_ext.py
259 declare_mlir_dialect_python_bindings(
260 ADD_TO_PARENT MLIRPythonSources.Dialects
261 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
262 TD_FILE dialects/TosaOps.td
263 SOURCES dialects/tosa.py
266 declare_mlir_dialect_python_bindings(
267 ADD_TO_PARENT MLIRPythonSources.Dialects
268 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
269 TD_FILE dialects/VectorOps.td
270 SOURCES dialects/vector.py
273 ################################################################################
275 # The sources for these are all in lib/Bindings/Python, but since they have to
276 # be rebuilt for each package and integrate with the source setup here, we
277 # just reference them here instead of having ordered, cross package target
279 ################################################################################
281 set(PYTHON_SOURCE_DIR "${MLIR_SOURCE_DIR}/lib/Bindings/Python")
282 declare_mlir_python_extension(MLIRPythonExtension.Core
284 ADD_TO_PARENT MLIRPythonSources.Core
285 ROOT_DIR "${PYTHON_SOURCE_DIR}"
296 # Headers must be included explicitly so they are installed.
312 # This extension exposes an API to register all dialects, extensions, and passes
313 # packaged in upstream MLIR and it is used for the upstream "mlir" Python
314 # package. Downstreams will likely want to provide their own and not depend
315 # on this one, since it links in the world.
316 # Note that this is not added to any top-level source target for transitive
317 # inclusion: It must be included explicitly by downstreams if desired. Note that
318 # this has a very large impact on what gets built/packaged.
319 declare_mlir_python_extension(MLIRPythonExtension.RegisterEverything
320 MODULE_NAME _mlirRegisterEverything
321 ROOT_DIR "${PYTHON_SOURCE_DIR}"
323 RegisterEverything.cpp
329 MLIRCAPIRegisterEverything
332 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Linalg.Pybind
333 MODULE_NAME _mlirDialectsLinalg
334 ADD_TO_PARENT MLIRPythonSources.Dialects.linalg
335 ROOT_DIR "${PYTHON_SOURCE_DIR}"
345 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Quant.Pybind
346 MODULE_NAME _mlirDialectsQuant
347 ADD_TO_PARENT MLIRPythonSources.Dialects.quant
348 ROOT_DIR "${PYTHON_SOURCE_DIR}"
358 declare_mlir_python_extension(MLIRPythonExtension.Dialects.PDL.Pybind
359 MODULE_NAME _mlirDialectsPDL
360 ADD_TO_PARENT MLIRPythonSources.Dialects.pdl
361 ROOT_DIR "${PYTHON_SOURCE_DIR}"
371 declare_mlir_python_extension(MLIRPythonExtension.Dialects.SparseTensor.Pybind
372 MODULE_NAME _mlirDialectsSparseTensor
373 ADD_TO_PARENT MLIRPythonSources.Dialects.sparse_tensor
374 ROOT_DIR "${PYTHON_SOURCE_DIR}"
376 DialectSparseTensor.cpp
384 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Transform.Pybind
385 MODULE_NAME _mlirDialectsTransform
386 ADD_TO_PARENT MLIRPythonSources.Dialects.transform
387 ROOT_DIR "${PYTHON_SOURCE_DIR}"
394 MLIRCAPITransformDialect
397 declare_mlir_python_extension(MLIRPythonExtension.AsyncDialectPasses
398 MODULE_NAME _mlirAsyncPasses
399 ADD_TO_PARENT MLIRPythonSources.Dialects.async_dialect
400 ROOT_DIR "${PYTHON_SOURCE_DIR}"
409 if(MLIR_ENABLE_EXECUTION_ENGINE)
410 declare_mlir_python_extension(MLIRPythonExtension.ExecutionEngine
411 MODULE_NAME _mlirExecutionEngine
412 ADD_TO_PARENT MLIRPythonSources.ExecutionEngine
413 ROOT_DIR "${PYTHON_SOURCE_DIR}"
415 ExecutionEngineModule.cpp
419 MLIRCAPIExecutionEngine
423 declare_mlir_python_extension(MLIRPythonExtension.GPUDialectPasses
424 MODULE_NAME _mlirGPUPasses
425 ADD_TO_PARENT MLIRPythonSources.Dialects.gpu
426 ROOT_DIR "${PYTHON_SOURCE_DIR}"
435 declare_mlir_python_extension(MLIRPythonExtension.LinalgPasses
436 MODULE_NAME _mlirLinalgPasses
437 ADD_TO_PARENT MLIRPythonSources.Dialects.linalg
438 ROOT_DIR "${PYTHON_SOURCE_DIR}"
447 declare_mlir_python_extension(MLIRPythonExtension.SparseTensorDialectPasses
448 MODULE_NAME _mlirSparseTensorPasses
449 ADD_TO_PARENT MLIRPythonSources.Dialects.sparse_tensor
450 ROOT_DIR "${PYTHON_SOURCE_DIR}"
452 SparseTensorPasses.cpp
459 # TODO: Figure out how to put this in the test tree.
460 # This should not be included in the main Python extension. However,
461 # putting it into MLIRPythonTestSources along with the dialect declaration
462 # above confuses Python module loader when running under lit.
463 set(_ADDL_TEST_SOURCES)
464 if(MLIR_INCLUDE_TESTS)
465 set(_ADDL_TEST_SOURCES MLIRPythonTestSources)
466 declare_mlir_python_sources(MLIRPythonTestSources)
467 declare_mlir_python_sources(MLIRPythonTestSources.Dialects
468 ADD_TO_PARENT MLIRPythonTestSources)
470 # TODO: this uses a tablegen file from the test directory and should be
471 # decoupled from here.
472 declare_mlir_python_sources(
473 MLIRPythonTestSources.Dialects.PythonTest
474 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
475 ADD_TO_PARENT MLIRPythonTestSources.Dialects
476 SOURCES dialects/python_test.py)
477 set(LLVM_TARGET_DEFINITIONS
478 "${MLIR_MAIN_SRC_DIR}/test/python/python_test_ops.td")
480 "dialects/_python_test_ops_gen.py"
481 -gen-python-op-bindings
482 -bind-dialect=python_test)
483 add_public_tablegen_target(PythonTestDialectPyIncGen)
484 declare_mlir_python_sources(
485 MLIRPythonTestSources.Dialects.PythonTest.ops_gen
486 ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
487 ADD_TO_PARENT MLIRPythonTestSources.Dialects.PythonTest
488 SOURCES "dialects/_python_test_ops_gen.py")
490 declare_mlir_python_extension(MLIRPythonTestSources.PythonTestExtension
491 MODULE_NAME _mlirPythonTest
492 ADD_TO_PARENT MLIRPythonTestSources.Dialects
493 ROOT_DIR "${MLIR_SOURCE_DIR}/test/python/lib"
499 MLIRCAPIPythonTestDialect
503 ################################################################################
504 # Common CAPI dependency DSO.
505 # All python extensions must link through one DSO which exports the CAPI, and
506 # this must have a globally unique name amongst all embeddors of the python
507 # library since it will effectively have global scope.
509 # The presence of this aggregate library is part of the long term plan, but its
510 # use needs to be made more flexible.
512 # TODO: Upgrade to the aggregate utility in https://reviews.llvm.org/D106419
514 ################################################################################
516 add_mlir_python_common_capi_library(MLIRPythonCAPI
517 INSTALL_COMPONENT MLIRPythonModules
518 INSTALL_DESTINATION python_packages/mlir_core/mlir/_mlir_libs
519 OUTPUT_DIRECTORY "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir/_mlir_libs"
520 RELATIVE_INSTALL_ROOT "../../../.."
522 MLIRPythonCAPI.HeaderSources
525 MLIRPythonExtension.RegisterEverything
526 ${_ADDL_TEST_SOURCES}
529 ################################################################################
531 ################################################################################
533 _flatten_mlir_python_targets(mlir_python_sources_deps MLIRPythonSources)
534 add_custom_target("mlir-python-sources" DEPENDS ${mlir_python_sources_deps})
535 if(NOT LLVM_ENABLE_IDE)
536 add_llvm_install_targets(install-mlir-python-sources
537 DEPENDS mlir-python-sources
538 COMPONENT mlir-python-sources
542 ################################################################################
543 # The fully assembled package of modules.
544 # This must come last.
545 ################################################################################
547 add_mlir_python_modules(MLIRPythonModules
548 ROOT_PREFIX "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir"
549 INSTALL_PREFIX "python_packages/mlir_core/mlir"
552 MLIRPythonExtension.RegisterEverything
553 ${_ADDL_TEST_SOURCES}
554 COMMON_CAPI_LINK_LIBS