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_python_bindings(
118 ADD_TO_PARENT MLIRPythonSources.Dialects
119 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
120 TD_FILE dialects/TransformOps.td
122 dialects/_transform_ops_ext.py
123 dialects/transform/__init__.py
124 _mlir_libs/_mlir/dialects/transform/__init__.pyi
125 DIALECT_NAME transform)
127 declare_mlir_dialect_extension_python_bindings(
128 ADD_TO_PARENT MLIRPythonSources.Dialects
129 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
130 TD_FILE dialects/SCFLoopTransformOps.td
132 dialects/_loop_transform_ops_ext.py
133 dialects/transform/loop.py
134 DIALECT_NAME transform
135 EXTENSION_NAME loop_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/LinalgStructuredTransformOps.td
142 dialects/_structured_transform_ops_ext.py
143 dialects/transform/structured.py
144 DIALECT_NAME transform
145 EXTENSION_NAME structured_transform)
147 declare_mlir_dialect_python_bindings(
148 ADD_TO_PARENT MLIRPythonSources.Dialects
149 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
150 TD_FILE dialects/MathOps.td
151 SOURCES dialects/math.py
154 declare_mlir_dialect_python_bindings(
155 ADD_TO_PARENT MLIRPythonSources.Dialects
156 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
157 TD_FILE dialects/ArithOps.td
160 dialects/_arith_ops_ext.py
163 declare_mlir_dialect_python_bindings(
164 ADD_TO_PARENT MLIRPythonSources.Dialects
165 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
166 TD_FILE dialects/MemRefOps.td
169 dialects/_memref_ops_ext.py
172 declare_mlir_dialect_python_bindings(
173 ADD_TO_PARENT MLIRPythonSources.Dialects
174 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
175 TD_FILE dialects/MLProgramOps.td
177 dialects/ml_program.py
178 dialects/_ml_program_ops_ext.py
179 DIALECT_NAME ml_program)
181 declare_mlir_python_sources(
182 MLIRPythonSources.Dialects.quant
183 ADD_TO_PARENT MLIRPythonSources.Dialects
184 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
187 _mlir_libs/_mlir/dialects/quant.pyi)
189 declare_mlir_dialect_python_bindings(
190 ADD_TO_PARENT MLIRPythonSources.Dialects
191 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
192 TD_FILE dialects/PDLOps.td
195 dialects/_pdl_ops_ext.py
196 _mlir_libs/_mlir/dialects/pdl.pyi
199 declare_mlir_dialect_python_bindings(
200 ADD_TO_PARENT MLIRPythonSources.Dialects
201 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
202 TD_FILE dialects/SCFOps.td
205 dialects/_scf_ops_ext.py
208 declare_mlir_dialect_python_bindings(
209 ADD_TO_PARENT MLIRPythonSources.Dialects
210 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
211 TD_FILE dialects/ShapeOps.td
212 SOURCES dialects/shape.py
215 declare_mlir_dialect_python_bindings(
216 ADD_TO_PARENT MLIRPythonSources.Dialects
217 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
218 TD_FILE dialects/SparseTensorOps.td
219 SOURCES dialects/sparse_tensor.py
220 DIALECT_NAME sparse_tensor)
222 declare_mlir_dialect_python_bindings(
223 ADD_TO_PARENT MLIRPythonSources.Dialects
224 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
225 TD_FILE dialects/TensorOps.td
228 dialects/_tensor_ops_ext.py
231 declare_mlir_dialect_python_bindings(
232 ADD_TO_PARENT MLIRPythonSources.Dialects
233 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
234 TD_FILE dialects/TosaOps.td
235 SOURCES dialects/tosa.py
238 declare_mlir_dialect_python_bindings(
239 ADD_TO_PARENT MLIRPythonSources.Dialects
240 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
241 TD_FILE dialects/VectorOps.td
242 SOURCES dialects/vector.py
245 ################################################################################
247 # The sources for these are all in lib/Bindings/Python, but since they have to
248 # be rebuilt for each package and integrate with the source setup here, we
249 # just reference them here instead of having ordered, cross package target
251 ################################################################################
253 set(PYTHON_SOURCE_DIR "${MLIR_SOURCE_DIR}/lib/Bindings/Python")
254 declare_mlir_python_extension(MLIRPythonExtension.Core
256 ADD_TO_PARENT MLIRPythonSources.Core
257 ROOT_DIR "${PYTHON_SOURCE_DIR}"
269 # Headers must be included explicitly so they are installed.
285 # This extension exposes an API to register all dialects, extensions, and passes
286 # packaged in upstream MLIR and it is used for the upstream "mlir" Python
287 # package. Downstreams will likely want to provide their own and not depend
288 # on this one, since it links in the world.
289 # Note that this is not added to any top-level source target for transitive
290 # inclusion: It must be included explicitly by downstreams if desired. Note that
291 # this has a very large impact on what gets built/packaged.
292 declare_mlir_python_extension(MLIRPythonExtension.RegisterEverything
293 MODULE_NAME _mlirRegisterEverything
294 ROOT_DIR "${PYTHON_SOURCE_DIR}"
296 RegisterEverything.cpp
302 MLIRCAPIRegisterEverything
305 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Linalg.Pybind
306 MODULE_NAME _mlirDialectsLinalg
307 ADD_TO_PARENT MLIRPythonSources.Dialects.linalg
308 ROOT_DIR "${PYTHON_SOURCE_DIR}"
318 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Quant.Pybind
319 MODULE_NAME _mlirDialectsQuant
320 ADD_TO_PARENT MLIRPythonSources.Dialects.quant
321 ROOT_DIR "${PYTHON_SOURCE_DIR}"
331 declare_mlir_python_extension(MLIRPythonExtension.Dialects.PDL.Pybind
332 MODULE_NAME _mlirDialectsPDL
333 ADD_TO_PARENT MLIRPythonSources.Dialects.pdl
334 ROOT_DIR "${PYTHON_SOURCE_DIR}"
344 declare_mlir_python_extension(MLIRPythonExtension.Dialects.SparseTensor.Pybind
345 MODULE_NAME _mlirDialectsSparseTensor
346 ADD_TO_PARENT MLIRPythonSources.Dialects.sparse_tensor
347 ROOT_DIR "${PYTHON_SOURCE_DIR}"
349 DialectSparseTensor.cpp
357 declare_mlir_python_extension(MLIRPythonExtension.Dialects.Transform.Pybind
358 MODULE_NAME _mlirDialectsTransform
359 ADD_TO_PARENT MLIRPythonSources.Dialects.transform
360 ROOT_DIR "${PYTHON_SOURCE_DIR}"
367 MLIRCAPITransformDialect
370 declare_mlir_python_extension(MLIRPythonExtension.AsyncDialectPasses
371 MODULE_NAME _mlirAsyncPasses
372 ADD_TO_PARENT MLIRPythonSources.Dialects.async_dialect
373 ROOT_DIR "${PYTHON_SOURCE_DIR}"
382 if(MLIR_ENABLE_EXECUTION_ENGINE)
383 declare_mlir_python_extension(MLIRPythonExtension.ExecutionEngine
384 MODULE_NAME _mlirExecutionEngine
385 ADD_TO_PARENT MLIRPythonSources.ExecutionEngine
386 ROOT_DIR "${PYTHON_SOURCE_DIR}"
388 ExecutionEngineModule.cpp
392 MLIRCAPIExecutionEngine
396 declare_mlir_python_extension(MLIRPythonExtension.GPUDialectPasses
397 MODULE_NAME _mlirGPUPasses
398 ADD_TO_PARENT MLIRPythonSources.Dialects.gpu
399 ROOT_DIR "${PYTHON_SOURCE_DIR}"
408 declare_mlir_python_extension(MLIRPythonExtension.LinalgPasses
409 MODULE_NAME _mlirLinalgPasses
410 ADD_TO_PARENT MLIRPythonSources.Dialects.linalg
411 ROOT_DIR "${PYTHON_SOURCE_DIR}"
420 declare_mlir_python_extension(MLIRPythonExtension.SparseTensorDialectPasses
421 MODULE_NAME _mlirSparseTensorPasses
422 ADD_TO_PARENT MLIRPythonSources.Dialects.sparse_tensor
423 ROOT_DIR "${PYTHON_SOURCE_DIR}"
425 SparseTensorPasses.cpp
432 # TODO: Figure out how to put this in the test tree.
433 # This should not be included in the main Python extension. However,
434 # putting it into MLIRPythonTestSources along with the dialect declaration
435 # above confuses Python module loader when running under lit.
436 set(_ADDL_TEST_SOURCES)
437 if(MLIR_INCLUDE_TESTS)
438 set(_ADDL_TEST_SOURCES MLIRPythonTestSources)
439 declare_mlir_python_sources(MLIRPythonTestSources)
440 declare_mlir_python_sources(MLIRPythonTestSources.Dialects
441 ADD_TO_PARENT MLIRPythonTestSources)
443 # TODO: this uses a tablegen file from the test directory and should be
444 # decoupled from here.
445 declare_mlir_python_sources(
446 MLIRPythonTestSources.Dialects.PythonTest
447 ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
448 ADD_TO_PARENT MLIRPythonTestSources.Dialects
449 SOURCES dialects/python_test.py)
450 set(LLVM_TARGET_DEFINITIONS
451 "${MLIR_MAIN_SRC_DIR}/test/python/python_test_ops.td")
453 "dialects/_python_test_ops_gen.py"
454 -gen-python-op-bindings
455 -bind-dialect=python_test)
456 add_public_tablegen_target(PythonTestDialectPyIncGen)
457 declare_mlir_python_sources(
458 MLIRPythonTestSources.Dialects.PythonTest.ops_gen
459 ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
460 ADD_TO_PARENT MLIRPythonTestSources.Dialects.PythonTest
461 SOURCES "dialects/_python_test_ops_gen.py")
463 declare_mlir_python_extension(MLIRPythonTestSources.PythonTestExtension
464 MODULE_NAME _mlirPythonTest
465 ADD_TO_PARENT MLIRPythonTestSources.Dialects
466 ROOT_DIR "${MLIR_SOURCE_DIR}/test/python/lib"
472 MLIRCAPIPythonTestDialect
476 ################################################################################
477 # Common CAPI dependency DSO.
478 # All python extensions must link through one DSO which exports the CAPI, and
479 # this must have a globally unique name amongst all embeddors of the python
480 # library since it will effectively have global scope.
482 # The presence of this aggregate library is part of the long term plan, but its
483 # use needs to be made more flexible.
485 # TODO: Upgrade to the aggregate utility in https://reviews.llvm.org/D106419
487 ################################################################################
489 add_mlir_python_common_capi_library(MLIRPythonCAPI
490 INSTALL_COMPONENT MLIRPythonModules
491 INSTALL_DESTINATION python_packages/mlir_core/mlir/_mlir_libs
492 OUTPUT_DIRECTORY "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir/_mlir_libs"
493 RELATIVE_INSTALL_ROOT "../../../.."
495 MLIRPythonCAPI.HeaderSources
498 MLIRPythonExtension.RegisterEverything
499 ${_ADDL_TEST_SOURCES}
502 ################################################################################
503 # The fully assembled package of modules.
504 # This must come last.
505 ################################################################################
507 add_mlir_python_modules(MLIRPythonModules
508 ROOT_PREFIX "${MLIR_BINARY_DIR}/python_packages/mlir_core/mlir"
509 INSTALL_PREFIX "python_packages/mlir_core/mlir"
512 MLIRPythonExtension.RegisterEverything
513 ${_ADDL_TEST_SOURCES}
514 COMMON_CAPI_LINK_LIBS