[Clang][CodeGen]`vtable`, `typeinfo` et al. are globals
[llvm-project.git] / mlir / python / CMakeLists.txt
blobe5d37b2289862d94d336590e197520c10491b85f
1 include(AddMLIRPython)
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
18   SOURCES
19     _mlir_libs/__init__.py
20     ir.py
21     passmanager.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
33   SOURCES
34     execution_engine.py
35     _mlir_libs/_mlirExecutionEngine.pyi
36   SOURCES_GLOB
37     runtime/*.py
40 declare_mlir_python_sources(MLIRPythonCAPI.HeaderSources
41   ROOT_DIR "${MLIR_SOURCE_DIR}/include"
42   SOURCES_GLOB "mlir-c/*.h"
45 ################################################################################
46 # Dialect bindings
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
60   SOURCES
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
69   SOURCES
70     dialects/builtin.py
71     dialects/_builtin_ops_ext.py
72   DIALECT_NAME builtin)
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
78   SOURCES
79     dialects/complex.py
80   DIALECT_NAME complex)
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
86   SOURCES
87     dialects/cf.py
88   DIALECT_NAME cf)
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
94   SOURCES
95     dialects/func.py
96     dialects/_func_ops_ext.py
97   DIALECT_NAME func)
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
104   DIALECT_NAME gpu)
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
110   SOURCES
111     dialects/_linalg_ops_ext.py
112   SOURCES_GLOB
113     dialects/linalg/*.py
114   DIALECT_NAME linalg
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
121   SOURCES
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
131   SOURCES
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
141   SOURCES
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
150   SOURCES
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
159   SOURCES
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
169   SOURCES
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
180   DIALECT_NAME math)
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
186   SOURCES
187     dialects/arith.py
188     dialects/_arith_ops_ext.py
189   DIALECT_NAME arith)
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
195   SOURCES
196     dialects/memref.py
197     dialects/_memref_ops_ext.py
198   DIALECT_NAME memref)
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
204   SOURCES
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"
213   SOURCES
214     dialects/quant.py
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
221   SOURCES
222     dialects/pdl.py
223     dialects/_pdl_ops_ext.py
224     _mlir_libs/_mlir/dialects/pdl.pyi
225   DIALECT_NAME pdl)
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
231   SOURCES
232     dialects/scf.py
233     dialects/_scf_ops_ext.py
234   DIALECT_NAME scf)
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
241   DIALECT_NAME shape)
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
254   SOURCES
255     dialects/tensor.py
256     dialects/_tensor_ops_ext.py
257   DIALECT_NAME tensor)
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
264   DIALECT_NAME tosa)
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
271   DIALECT_NAME vector)
273 ################################################################################
274 # Python extensions.
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
278 # dependencies.
279 ################################################################################
281 set(PYTHON_SOURCE_DIR "${MLIR_SOURCE_DIR}/lib/Bindings/Python")
282 declare_mlir_python_extension(MLIRPythonExtension.Core
283   MODULE_NAME _mlir
284   ADD_TO_PARENT MLIRPythonSources.Core
285   ROOT_DIR "${PYTHON_SOURCE_DIR}"
286   SOURCES
287     MainModule.cpp
288     IRAffine.cpp
289     IRAttributes.cpp
290     IRCore.cpp
291     IRInterfaces.cpp
292     IRModule.cpp
293     IRTypes.cpp
294     Pass.cpp
296     # Headers must be included explicitly so they are installed.
297     Globals.h
298     IRModule.h
299     Pass.h
300     PybindUtils.h
301   PRIVATE_LINK_LIBS
302     LLVMSupport
303   EMBED_CAPI_LINK_LIBS
304     MLIRCAPIDebug
305     MLIRCAPIIR
306     MLIRCAPIInterfaces
308     # Dialects
309     MLIRCAPIFunc
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}"
322   SOURCES
323     RegisterEverything.cpp
324   PRIVATE_LINK_LIBS
325     LLVMSupport
326   EMBED_CAPI_LINK_LIBS
327     MLIRCAPIConversion
328     MLIRCAPITransforms
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}"
336   SOURCES
337     DialectLinalg.cpp
338   PRIVATE_LINK_LIBS
339     LLVMSupport
340   EMBED_CAPI_LINK_LIBS
341     MLIRCAPIIR
342     MLIRCAPILinalg
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}"
349   SOURCES
350     DialectQuant.cpp
351   PRIVATE_LINK_LIBS
352     LLVMSupport
353   EMBED_CAPI_LINK_LIBS
354     MLIRCAPIIR
355     MLIRCAPIQuant
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}"
362   SOURCES
363     DialectPDL.cpp
364   PRIVATE_LINK_LIBS
365     LLVMSupport
366   EMBED_CAPI_LINK_LIBS
367     MLIRCAPIIR
368     MLIRCAPIPDL
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}"
375   SOURCES
376     DialectSparseTensor.cpp
377   PRIVATE_LINK_LIBS
378     LLVMSupport
379   EMBED_CAPI_LINK_LIBS
380     MLIRCAPIIR
381     MLIRCAPISparseTensor
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}"
388   SOURCES
389     DialectTransform.cpp
390   PRIVATE_LINK_LIBS
391     LLVMSupport
392   EMBED_CAPI_LINK_LIBS
393     MLIRCAPIIR
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}"
401   SOURCES
402     AsyncPasses.cpp
403   PRIVATE_LINK_LIBS
404     LLVMSupport
405   EMBED_CAPI_LINK_LIBS
406     MLIRCAPIAsync
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}"
414     SOURCES
415       ExecutionEngineModule.cpp
416     PRIVATE_LINK_LIBS
417       LLVMSupport
418     EMBED_CAPI_LINK_LIBS
419       MLIRCAPIExecutionEngine
420   )
421 endif()
423 declare_mlir_python_extension(MLIRPythonExtension.GPUDialectPasses
424   MODULE_NAME _mlirGPUPasses
425   ADD_TO_PARENT MLIRPythonSources.Dialects.gpu
426   ROOT_DIR "${PYTHON_SOURCE_DIR}"
427   SOURCES
428     GPUPasses.cpp
429   PRIVATE_LINK_LIBS
430     LLVMSupport
431   EMBED_CAPI_LINK_LIBS
432     MLIRCAPIGPU
435 declare_mlir_python_extension(MLIRPythonExtension.LinalgPasses
436   MODULE_NAME _mlirLinalgPasses
437   ADD_TO_PARENT MLIRPythonSources.Dialects.linalg
438   ROOT_DIR "${PYTHON_SOURCE_DIR}"
439   SOURCES
440     LinalgPasses.cpp
441   PRIVATE_LINK_LIBS
442     LLVMSupport
443   EMBED_CAPI_LINK_LIBS
444     MLIRCAPILinalg
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}"
451   SOURCES
452     SparseTensorPasses.cpp
453   PRIVATE_LINK_LIBS
454     LLVMSupport
455   EMBED_CAPI_LINK_LIBS
456     MLIRCAPISparseTensor
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")
479   mlir_tablegen(
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"
494     SOURCES
495       PythonTestModule.cpp
496     PRIVATE_LINK_LIBS
497       LLVMSupport
498     EMBED_CAPI_LINK_LIBS
499       MLIRCAPIPythonTestDialect
500   )
501 endif()
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
513 # once ready.
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 "../../../.."
521   DECLARED_HEADERS
522     MLIRPythonCAPI.HeaderSources
523   DECLARED_SOURCES
524     MLIRPythonSources
525     MLIRPythonExtension.RegisterEverything
526     ${_ADDL_TEST_SOURCES}
529 ################################################################################
530 # Custom targets.
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
539   )
540 endif()
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"
550   DECLARED_SOURCES
551     MLIRPythonSources
552     MLIRPythonExtension.RegisterEverything
553     ${_ADDL_TEST_SOURCES}
554   COMMON_CAPI_LINK_LIBS
555     MLIRPythonCAPI