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/AffineOps.td
58 declare_mlir_dialect_python_bindings(
59 ADD_TO_PARENT MLIRPythonSources.Dialects
60 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
61 TD_FILE dialects/AMDGPUOps.td
67 declare_mlir_dialect_python_bindings(
68 ADD_TO_PARENT MLIRPythonSources.Dialects
69 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
70 TD_FILE dialects/AsyncOps.td
71 SOURCES_GLOB dialects/async_dialect/*.py
72 DIALECT_NAME async_dialect)
74 declare_mlir_dialect_python_bindings(
75 ADD_TO_PARENT MLIRPythonSources.Dialects
76 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
77 TD_FILE dialects/BufferizationOps.td
79 dialects/bufferization.py
80 DIALECT_NAME bufferization
81 GEN_ENUM_BINDINGS_TD_FILE
82 "../../include/mlir/Dialect/Bufferization/IR/BufferizationEnums.td"
85 declare_mlir_dialect_python_bindings(
86 ADD_TO_PARENT MLIRPythonSources.Dialects
87 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
88 TD_FILE dialects/BuiltinOps.td
93 declare_mlir_dialect_python_bindings(
94 ADD_TO_PARENT MLIRPythonSources.Dialects
95 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
96 TD_FILE dialects/ComplexOps.td
101 declare_mlir_dialect_python_bindings(
102 ADD_TO_PARENT MLIRPythonSources.Dialects
103 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
104 TD_FILE dialects/ControlFlowOps.td
109 declare_mlir_dialect_python_bindings(
110 ADD_TO_PARENT MLIRPythonSources.Dialects
111 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
112 TD_FILE dialects/FuncOps.td
117 declare_mlir_dialect_python_bindings(
118 ADD_TO_PARENT MLIRPythonSources.Dialects
119 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
120 TD_FILE dialects/GPUOps.td
121 SOURCES_GLOB dialects/gpu/*.py
125 declare_mlir_dialect_python_bindings(
126 ADD_TO_PARENT MLIRPythonSources.Dialects
127 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
128 TD_FILE dialects/LinalgOps.td
136 declare_mlir_dialect_python_bindings(
137 ADD_TO_PARENT MLIRPythonSources.Dialects
138 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
139 TD_FILE dialects/LLVMOps.td
145 declare_mlir_dialect_extension_python_bindings(
146 ADD_TO_PARENT MLIRPythonSources.Dialects
147 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
148 TD_FILE dialects/TransformPDLExtensionOps.td
150 dialects/transform/pdl.py
151 DIALECT_NAME transform
152 EXTENSION_NAME transform_pdl_extension)
154 declare_mlir_dialect_python_bindings(
155 ADD_TO_PARENT MLIRPythonSources.Dialects
156 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
157 TD_FILE dialects/TransformOps.td
159 dialects/transform/__init__.py
160 _mlir_libs/_mlir/dialects/transform/__init__.pyi
161 DIALECT_NAME transform
162 GEN_ENUM_BINDINGS_TD_FILE
163 "../../include/mlir/Dialect/Transform/IR/TransformAttrs.td"
166 declare_mlir_dialect_extension_python_bindings(
167 ADD_TO_PARENT MLIRPythonSources.Dialects
168 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
169 TD_FILE dialects/BufferizationTransformOps.td
171 dialects/transform/bufferization.py
172 DIALECT_NAME transform
173 EXTENSION_NAME bufferization_transform)
175 declare_mlir_dialect_extension_python_bindings(
176 ADD_TO_PARENT MLIRPythonSources.Dialects
177 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
178 TD_FILE dialects/GPUTransformOps.td
180 dialects/transform/gpu.py
181 DIALECT_NAME transform
182 EXTENSION_NAME gpu_transform)
184 declare_mlir_dialect_extension_python_bindings(
185 ADD_TO_PARENT MLIRPythonSources.Dialects
186 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
187 TD_FILE dialects/SCFLoopTransformOps.td
189 dialects/transform/loop.py
190 DIALECT_NAME transform
191 EXTENSION_NAME loop_transform)
193 declare_mlir_dialect_extension_python_bindings(
194 ADD_TO_PARENT MLIRPythonSources.Dialects
195 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
196 TD_FILE dialects/MemRefTransformOps.td
198 dialects/transform/memref.py
199 DIALECT_NAME transform
200 EXTENSION_NAME memref_transform)
202 declare_mlir_dialect_extension_python_bindings(
203 ADD_TO_PARENT MLIRPythonSources.Dialects
204 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
205 TD_FILE dialects/NVGPUTransformOps.td
207 dialects/transform/nvgpu.py
208 DIALECT_NAME transform
209 EXTENSION_NAME nvgpu_transform)
211 declare_mlir_dialect_extension_python_bindings(
212 ADD_TO_PARENT MLIRPythonSources.Dialects
213 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
214 TD_FILE dialects/LinalgStructuredTransformOps.td
216 dialects/transform/structured.py
217 DIALECT_NAME transform
218 EXTENSION_NAME structured_transform
219 GEN_ENUM_BINDINGS_TD_FILE
220 "../../include/mlir/Dialect/Linalg/TransformOps/LinalgTransformEnums.td"
223 declare_mlir_dialect_extension_python_bindings(
224 ADD_TO_PARENT MLIRPythonSources.Dialects
225 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
226 TD_FILE dialects/SparseTensorTransformOps.td
228 dialects/transform/sparse_tensor.py
229 DIALECT_NAME transform
230 EXTENSION_NAME sparse_tensor_transform)
232 declare_mlir_dialect_extension_python_bindings(
233 ADD_TO_PARENT MLIRPythonSources.Dialects
234 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
235 TD_FILE dialects/TensorTransformOps.td
237 dialects/transform/tensor.py
238 DIALECT_NAME transform
239 EXTENSION_NAME tensor_transform)
241 declare_mlir_dialect_extension_python_bindings(
242 ADD_TO_PARENT MLIRPythonSources.Dialects
243 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
244 TD_FILE dialects/VectorTransformOps.td
246 dialects/transform/vector.py
247 DIALECT_NAME transform
248 EXTENSION_NAME vector_transform
249 GEN_ENUM_BINDINGS_TD_FILE
250 "../../include/mlir/Dialect/Vector/Transforms/VectorTransformsBase.td"
253 declare_mlir_dialect_python_bindings(
254 ADD_TO_PARENT MLIRPythonSources.Dialects
255 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
256 TD_FILE dialects/MathOps.td
257 SOURCES dialects/math.py
260 declare_mlir_dialect_python_bindings(
261 ADD_TO_PARENT MLIRPythonSources.Dialects
262 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
263 TD_FILE dialects/ArithOps.td
269 declare_mlir_dialect_python_bindings(
270 ADD_TO_PARENT MLIRPythonSources.Dialects
271 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
272 TD_FILE dialects/MemRefOps.td
277 declare_mlir_dialect_python_bindings(
278 ADD_TO_PARENT MLIRPythonSources.Dialects
279 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
280 TD_FILE dialects/MLProgramOps.td
282 dialects/ml_program.py
283 DIALECT_NAME ml_program)
285 declare_mlir_dialect_python_bindings(
286 ADD_TO_PARENT MLIRPythonSources.Dialects
287 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
288 TD_FILE dialects/NVGPUOps.td
294 declare_mlir_dialect_python_bindings(
295 ADD_TO_PARENT MLIRPythonSources.Dialects
296 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
297 TD_FILE dialects/NVVMOps.td
303 declare_mlir_dialect_python_bindings(
304 ADD_TO_PARENT MLIRPythonSources.Dialects
305 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
306 TD_FILE dialects/ROCDLOps.td
311 declare_mlir_python_sources(
312 MLIRPythonSources.Dialects.quant
313 ADD_TO_PARENT MLIRPythonSources.Dialects
314 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
318 _mlir_libs/_mlir/dialects/quant.pyi)
320 declare_mlir_dialect_python_bindings(
321 ADD_TO_PARENT MLIRPythonSources.Dialects
322 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
323 TD_FILE dialects/PDLOps.td
326 _mlir_libs/_mlir/dialects/pdl.pyi
329 declare_mlir_dialect_python_bindings(
330 ADD_TO_PARENT MLIRPythonSources.Dialects
331 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
332 TD_FILE dialects/OpenMPOps.td
337 declare_mlir_dialect_python_bindings(
338 ADD_TO_PARENT MLIRPythonSources.Dialects
339 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
340 TD_FILE dialects/SCFOps.td
345 declare_mlir_dialect_python_bindings(
346 ADD_TO_PARENT MLIRPythonSources.Dialects
347 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
348 TD_FILE dialects/ShapeOps.td
349 SOURCES dialects/shape.py
352 declare_mlir_dialect_python_bindings(
353 ADD_TO_PARENT MLIRPythonSources.Dialects
354 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
355 TD_FILE dialects/SparseTensorOps.td
356 SOURCES dialects/sparse_tensor.py
357 DIALECT_NAME sparse_tensor
358 GEN_ENUM_BINDINGS_TD_FILE
359 "../../include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td"
362 declare_mlir_dialect_python_bindings(
363 ADD_TO_PARENT MLIRPythonSources.Dialects
364 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
365 TD_FILE dialects/TensorOps.td
370 declare_mlir_dialect_python_bindings(
371 ADD_TO_PARENT MLIRPythonSources.Dialects
372 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
373 TD_FILE dialects/TosaOps.td
374 SOURCES dialects/tosa.py
378 declare_mlir_dialect_python_bindings(
379 ADD_TO_PARENT MLIRPythonSources.Dialects
380 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
381 TD_FILE dialects/VectorOps.td
382 SOURCES dialects/vector.py
384 GEN_ENUM_BINDINGS_TD_FILE
385 "dialects/VectorAttributes.td")
387 ################################################################################
389 # The sources for these are all in lib/Bindings/Python, but since they have to
390 # be rebuilt for each package and integrate with the source setup here, we
391 # just reference them here instead of having ordered, cross package target
393 ################################################################################
395 set(PYTHON_SOURCE_DIR "${MLIR_SOURCE_DIR}/lib/Bindings/Python")
396 declare_mlir_python_extension(MLIRPythonExtension.Core
398 ADD_TO_PARENT MLIRPythonSources.Core
399 ROOT_DIR "${PYTHON_SOURCE_DIR}"
410 # Headers must be included explicitly so they are installed.
426 # This extension exposes an API to register all dialects, extensions, and passes
427 # packaged in upstream MLIR and it is used for the upstream "mlir" Python
428 # package. Downstreams will likely want to provide their own and not depend
429 # on this one, since it links in the world.
430 # Note that this is not added to any top-level source target for transitive
431 # inclusion: It must be included explicitly by downstreams if desired. Note that
432 # this has a very large impact on what gets built/packaged.
433 declare_mlir_python_extension(MLIRPythonExtension.RegisterEverything
434 MODULE_NAME _mlirRegisterEverything
435 ROOT_DIR "${PYTHON_SOURCE_DIR}"
437 RegisterEverything.cpp
443 MLIRCAPIRegisterEverything
446 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Linalg.Pybind
447 MODULE_NAME _mlirDialectsLinalg
448 ADD_TO_PARENT MLIRPythonSources.Dialects.linalg
449 ROOT_DIR "${PYTHON_SOURCE_DIR}"
459 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Quant.Pybind
460 MODULE_NAME _mlirDialectsQuant
461 ADD_TO_PARENT MLIRPythonSources.Dialects.quant
462 ROOT_DIR "${PYTHON_SOURCE_DIR}"
472 declare_mlir_python_extension(MLIRPythonExtension.Dialects.PDL.Pybind
473 MODULE_NAME _mlirDialectsPDL
474 ADD_TO_PARENT MLIRPythonSources.Dialects.pdl
475 ROOT_DIR "${PYTHON_SOURCE_DIR}"
485 declare_mlir_python_extension(MLIRPythonExtension.Dialects.SparseTensor.Pybind
486 MODULE_NAME _mlirDialectsSparseTensor
487 ADD_TO_PARENT MLIRPythonSources.Dialects.sparse_tensor
488 ROOT_DIR "${PYTHON_SOURCE_DIR}"
490 DialectSparseTensor.cpp
498 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Transform.Pybind
499 MODULE_NAME _mlirDialectsTransform
500 ADD_TO_PARENT MLIRPythonSources.Dialects.transform
501 ROOT_DIR "${PYTHON_SOURCE_DIR}"
508 MLIRCAPITransformDialect
511 declare_mlir_python_extension(MLIRPythonExtension.AsyncDialectPasses
512 MODULE_NAME _mlirAsyncPasses
513 ADD_TO_PARENT MLIRPythonSources.Dialects.async_dialect
514 ROOT_DIR "${PYTHON_SOURCE_DIR}"
523 if(MLIR_ENABLE_EXECUTION_ENGINE)
524 declare_mlir_python_extension(MLIRPythonExtension.ExecutionEngine
525 MODULE_NAME _mlirExecutionEngine
526 ADD_TO_PARENT MLIRPythonSources.ExecutionEngine
527 ROOT_DIR "${PYTHON_SOURCE_DIR}"
529 ExecutionEngineModule.cpp
533 MLIRCAPIExecutionEngine
537 declare_mlir_python_extension(MLIRPythonExtension.GPUDialectPasses
538 MODULE_NAME _mlirGPUPasses
539 ADD_TO_PARENT MLIRPythonSources.Dialects.gpu
540 ROOT_DIR "${PYTHON_SOURCE_DIR}"
549 declare_mlir_python_extension(MLIRPythonExtension.LinalgPasses
550 MODULE_NAME _mlirLinalgPasses
551 ADD_TO_PARENT MLIRPythonSources.Dialects.linalg
552 ROOT_DIR "${PYTHON_SOURCE_DIR}"
561 declare_mlir_python_extension(MLIRPythonExtension.SparseTensorDialectPasses
562 MODULE_NAME _mlirSparseTensorPasses
563 ADD_TO_PARENT MLIRPythonSources.Dialects.sparse_tensor
564 ROOT_DIR "${PYTHON_SOURCE_DIR}"
566 SparseTensorPasses.cpp
573 # TODO: Figure out how to put this in the test tree.
574 # This should not be included in the main Python extension. However,
575 # putting it into MLIRPythonTestSources along with the dialect declaration
576 # above confuses Python module loader when running under lit.
577 set(_ADDL_TEST_SOURCES)
578 if(MLIR_INCLUDE_TESTS)
579 set(_ADDL_TEST_SOURCES MLIRPythonTestSources)
580 declare_mlir_python_sources(MLIRPythonTestSources)
581 declare_mlir_python_sources(MLIRPythonTestSources.Dialects
582 ADD_TO_PARENT MLIRPythonTestSources)
584 # TODO: this uses a tablegen file from the test directory and should be
585 # decoupled from here.
586 declare_mlir_python_sources(
587 MLIRPythonTestSources.Dialects.PythonTest
588 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
589 ADD_TO_PARENT MLIRPythonTestSources.Dialects
590 SOURCES dialects/python_test.py)
591 set(LLVM_TARGET_DEFINITIONS
592 "${MLIR_MAIN_SRC_DIR}/test/python/python_test_ops.td")
594 "dialects/_python_test_ops_gen.py"
595 -gen-python-op-bindings
596 -bind-dialect=python_test)
597 add_public_tablegen_target(PythonTestDialectPyIncGen)
598 declare_mlir_python_sources(
599 MLIRPythonTestSources.Dialects.PythonTest.ops_gen
600 ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
601 ADD_TO_PARENT MLIRPythonTestSources.Dialects.PythonTest
602 SOURCES "dialects/_python_test_ops_gen.py")
604 declare_mlir_python_extension(MLIRPythonTestSources.PythonTestExtension
605 MODULE_NAME _mlirPythonTest
606 ADD_TO_PARENT MLIRPythonTestSources.Dialects
607 ROOT_DIR "${MLIR_SOURCE_DIR}/test/python/lib"
613 MLIRCAPIPythonTestDialect
617 ################################################################################
618 # Common CAPI dependency DSO.
619 # All python extensions must link through one DSO which exports the CAPI, and
620 # this must have a globally unique name amongst all embeddors of the python
621 # library since it will effectively have global scope.
623 # The presence of this aggregate library is part of the long term plan, but its
624 # use needs to be made more flexible.
626 # TODO: Upgrade to the aggregate utility in https://reviews.llvm.org/D106419
628 ################################################################################
630 add_mlir_python_common_capi_library(MLIRPythonCAPI
631 INSTALL_COMPONENT MLIRPythonModules
632 INSTALL_DESTINATION python_packages/mlir_core/mlir/_mlir_libs
633 OUTPUT_DIRECTORY "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir/_mlir_libs"
634 RELATIVE_INSTALL_ROOT "../../../.."
636 MLIRPythonCAPI.HeaderSources
639 MLIRPythonExtension.RegisterEverything
640 ${_ADDL_TEST_SOURCES}
643 ################################################################################
645 ################################################################################
647 _flatten_mlir_python_targets(mlir_python_sources_deps MLIRPythonSources)
648 add_custom_target("mlir-python-sources" DEPENDS ${mlir_python_sources_deps})
649 if(NOT LLVM_ENABLE_IDE)
650 add_llvm_install_targets(install-mlir-python-sources
651 DEPENDS mlir-python-sources
652 COMPONENT mlir-python-sources
656 ################################################################################
657 # The fully assembled package of modules.
658 # This must come last.
659 ################################################################################
661 add_mlir_python_modules(MLIRPythonModules
662 ROOT_PREFIX "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir"
663 INSTALL_PREFIX "python_packages/mlir_core/mlir"
666 MLIRPythonExtension.RegisterEverything
667 ${_ADDL_TEST_SOURCES}
668 COMMON_CAPI_LINK_LIBS