summaryrefslogtreecommitdiff
path: root/mlir/examples
AgeCommit message (Collapse)Author
2025-11-19[MLIR][Python] make sure stubs get installed with ↵Maksim Levental
LLVM_DISTRIBUTION_COMPONENTS (#168407) Fixes https://github.com/llvm/llvm-project/issues/168393. Also adds top-level `MLIR_PYTHON_STUBGEN_ENABLED` CMake option.
2025-10-23[MLIR][Python] Skip stubgen while any sanitizer is enabled (#164661)Twice
The intention of this PR is described https://github.com/llvm/llvm-project/issues/164197#issuecomment-3432843709 (and https://github.com/llvm/llvm-project/issues/164197#issuecomment-3432935838). When sanitizers are enabled, some additional setup (such as preloading certain libraries) seems to be required for the stubgen step to work properly (https://github.com/llvm/llvm-project/issues/164197#issuecomment-3432924034). In this case, I chose to simply skip the stubgen process, as supporting it would likely require some extra CMake logic, and type stubs don’t appear to be particularly necessary in this configuration.
2025-10-11[mlir] Use llvm accumulate wrappers. NFCI. (#162957)Jakub Kuderski
Use wrappers around `std::accumulate` to make the code more concise and less bug-prone: https://github.com/llvm/llvm-project/pull/162129. With `std::accumulate`, it's the initial value that determines the accumulator type. `llvm::sum_of` and `llvm::product_of` pick the right accumulator type based on the range element type. Found some funny bugs like a local accumulate helper that calculated a sum with initial value of 1 -- we didn't hit the bug because the code was actually dead...
2025-10-07Revert "[MLIR][Python] use `FetchContent_Declare` for nanobind and remove ↵Maksim Levental
pybind (#161230)" (#162309) This reverts commit 84a214856ad989f37af19f5e8aaa9ec2346dde6f. This gives us more time to work out the alternative and also people to migrate
2025-10-06[MLIR][Python] use `FetchContent_Declare` for nanobind and remove pybind ↵Maksim Levental
(#161230) Inspired by this comment https://github.com/llvm/llvm-project/pull/157930#issuecomment-3346634290 (and long-standing issues related to finding nanobind/pybind in the right place), this PR moves to using `FetchContent_Declare` to get the nanobind dependency. This is pretty standard (see e.g., [IREE](https://github.com/iree-org/iree/blob/cf60359b7443b0e92e15fb6ffc011525dc40e772/CMakeLists.txt#L842-L848)). This PR also removes pybind which has been deprecated for almost a year (https://github.com/llvm/llvm-project/pull/117922) and which isn't compatible (for whatever reason) with `FetchContent_Declare`. --------- Co-authored-by: Jacques Pienaar <jpienaar@google.com>
2025-09-27[mlir] Fix typo in StandalonePasses.td doc string (#161009)Corbin Robeck
2025-09-25[MLIR][Python] don't generate type stubs when cross-compiling (#160793)Maksim Levental
Stubgen doesn't work when cross-compiling (stubgen will run in the host interpreter and then fail to find the extension module for the host arch). So disable it when `CMAKE_CROSSCOMPILING=ON`.
2025-09-24[MLIR][Standalone] use narrow registration instead of RegisterEverything ↵Maksim Levental
(#160469) This PR cleans up a long-standing TODO by avoiding `MLIRPythonExtension.RegisterEverything` in the Standalone example and registering the necessary dialects explicitly instead.
2025-09-24Reland "[MLIR][Python] add Python wheel build demo/test" (#160481) (#160488)Maksim Levental
Reland standalone wheel build. The fix is to gate the test behind `BUILD_SHARED_LIBS=OFF` (because bundling all libs in the wheel requires valid rpaths which is not the case under `BUILD_SHARED_LIBS=ON`).
2025-09-24Revert "[MLIR][Python] add Python wheel build demo/test" (#160481)Maksim Levental
Reverts llvm/llvm-project#160388 because it broke [mlir-nvidia](https://lab.llvm.org/buildbot/#/builders/138) builder.
2025-09-24[MLIR][Python] add Python wheel build demo/test (#160388)Maksim Levental
This PR demos and tests building Python wheels using [scikit-build-core](https://scikit-build-core.readthedocs.io/en/latest/). The test is added to standalone and thus demos "out-of-tree" use cases but the same `pyproject.toml` will work for in-tree builds. Note, one can easily pair this with [cibuildwheel](https://github.com/scikit-build/scikit-build-core/blob/326490975510b2af888e0f319292fc4a9084a033/docs/guide/build.md?plain=1#L221-L226) to build for all Python versions, OSs, architectures, etc.
2025-09-23[MLIR][Python] quiet stubgen (#160368)Maksim Levental
Silence all stdout from stubgen unless `VERBOSE` is passed to `mlir_generate_type_stubs`.
2025-09-20[MLIR][Python] reland (narrower) type stub generation (#157930)Maksim Levental
This a reland of https://github.com/llvm/llvm-project/pull/155741 which was reverted at https://github.com/llvm/llvm-project/pull/157831. This version is narrower in scope - it only turns on automatic stub generation for `MLIRPythonExtension.Core._mlir` and **does not do anything automatically**. Specifically, the only CMake code added to `AddMLIRPython.cmake` is the `mlir_generate_type_stubs` function which is then used only in a manual way. The API for `mlir_generate_type_stubs` is: ``` Arguments: MODULE_NAME: The fully-qualified name of the extension module (used for importing in python). DEPENDS_TARGETS: List of targets these type stubs depend on being built; usually corresponding to the specific extension module (e.g., something like StandalonePythonModules.extension._standaloneDialectsNanobind.dso) and the core bindings extension module (e.g., something like StandalonePythonModules.extension._mlir.dso). OUTPUT_DIR: The root output directory to emit the type stubs into. OUTPUTS: List of expected outputs. DEPENDS_TARGET_SRC_DEPS: List of cpp sources for extension library (for generating a DEPFILE). IMPORT_PATHS: List of paths to add to PYTHONPATH for stubgen. PATTERN_FILE: (Optional) Pattern file (see https://nanobind.readthedocs.io/en/latest/typing.html#pattern-files). Outputs: NB_STUBGEN_CUSTOM_TARGET: The target corresponding to generation which other targets can depend on. ``` Downstream users should use `mlir_generate_type_stubs` in coordination with `declare_mlir_python_sources` to turn on stub generation for their own downstream dialect extensions and upstream dialect extensions if they so choose. Standalone example shows an example. Note, downstream will also need to set `-DMLIR_PYTHON_PACKAGE_PREFIX=...` correctly for their bindings.
2025-09-12[MLIR] enable Standalone example test for Windows (#158183)Maksim Levental
This PR turns on all Standalone tests for Windows except for the plugins (which aren't enabled by default).
2025-09-10Revert stubgen-related patches (#157831)Oleksandr "Alex" Zinenko
Despite several hotfixes, things remain broken, in particular: - installation/distribution (`ninja install / install-distribution`); - downstream projects with bindings exposed. See https://github.com/llvm/llvm-project/pull/157583#issuecomment-3274164357 for more details. Reverts #155741, #157583, #157697. Let's make sure things are fixed and re-land as a unit.
2025-09-05[MLIR][Python] enable type stub auto-generation (#155741)Maksim Levental
This PR turns on automatic type stub generation (rather than relying on hand-written/updated stubs). It uses nanobind's [stubgen facility](https://nanobind.readthedocs.io/en/latest/typing.html#stub-generation). If you would like to enable this functionality you can add `GENERATE_TYPE_STUBS` to `declare_mlir_python_extension` .
2025-08-30[MLIR][Python] fix standalone example/test (#156197)Maksim Levental
Fix some things in `standalone` in order to unblock https://github.com/llvm/llvm-project/pull/155741.
2025-08-22[MLIR] Apply clang-tidy fixes for misc-use-internal-linkage in toy Tutorial ↵Mehdi Amini
(NFC)
2025-08-15[MLIR] Set LLVM_LIT_ARGS in Standalone Example CMake (#152423)Aiden Grossman
Setting LLVM_LIT_ARGS to include --quiet and then running check-mlir in a standard checkout will otherwise cause test failures here because LLVM_LIT_ARGS gets propagated into this project.
2025-07-29[mlir] Reland `Move InitAll*** implementation into static library` (#151150)Ivan Butygin
Reland https://github.com/llvm/llvm-project/pull/150805 Shared libs build was broken. Add `${dialect_libs}` and `${conversion_libs}` to `MLIRRegisterAllExtensions` because it depends on `registerConvert***ToLLVMInterface` functions.
2025-07-29Revert "[mlir][core] Move `InitAll***` implementation into static library." ↵Mehdi Amini
(#151118) Reverts llvm/llvm-project#150805 Some bots are failing.
2025-07-29[mlir][core] Move `InitAll***` implementation into static library. (#150805)Ivan Butygin
`InitAll***` functions are used by `opt`-style tools to init all MLIR dialects/passes/extensions. Currently they are implemeted as inline functions and include essentially the entire MLIR header tree. Each file which includes this header (~10 currently) takes 10+ sec and multiple GB of ram to compile (tested with clang-19), which limits amount of parallel compiler jobs which can be run. Also, flang just includes this file into one of its headers. Move the actual registration code to the static library, so it's compiled only once. Discourse thread https://discourse.llvm.org/t/rfc-moving-initall-implementation-into-static-library/87559
2025-07-27[mlir][toy] Update dialect conversion example (#150826)Matthias Springer
The Toy tutorial used outdated API. Update the example to: * Use the `OpAdaptor` in all places. * Do not mix `RewritePattern` and `ConversionPattern`. This cannot always be done safely and should not be advertised in the example code.
2025-07-27[MLIR] Update MLIR tutorial to use LDBG() macro (#150763)Mehdi Amini
2025-07-27[MLIR] Migrate some "transform dialect" source to use the standard LDBG ↵Mehdi Amini
macro (NFC) (#150695)
2025-07-26[mlir][doc] Fix transform dialect tutorial ch3 (#150456)lonely eagle
Fixed some bugs in documentation. Add CallOpInterfaceHandle to the arguments of ChangeCallTargetOp, after doing so the section described in the documentation works correctly, Otherwise the following code reports an error. ``` // Cast to our new type. %casted = transform.cast %call : !transform.any_op to !transform.my.call_op_interface // Using our new operation. transform.my.change_call_target %casted, "microkernel" : !transform.my.call_op_interface ```
2025-07-25[mlir][NFC] update `mlir/examples` create APIs (31/n) (#150652)Maksim Levental
See https://github.com/llvm/llvm-project/pull/147168 for more info.
2025-07-03[mlir][examples] Use {} instead of std::nullopt (NFC) (#146949)Kazu Hirata
This patch fixes deprecation warnings like: mlir/include/mlir/IR/TypeRange.h:51:19: error: 'ArrayRef' is deprecated: Use {} or ArrayRef<T>() instead [-Werror,-Wdeprecated-declarations]
2025-07-03[mlir][toy] Use `make_early_inc_range` when erasing ops during iteration ↵Matthias Springer
(#146892) Use `make_early_inc_range` when erasing operations from a block to make sure that the iterator is not invalidated. The previous implementation happened to work on a "normal" dialect conversion because some IR modifications are delayed. It no longer works with a One-Shot Dialect Conversion. The new One-Shot Dialect Conversion API is more similar to the normal rewriter API.
2025-06-30[mlir][examples] Fix deprecation warnings for `ArrayRef(std::nullopt)` (#146420)Jakub Kuderski
This used to print: ``` llvm-project/mlir/include/mlir/IR/ValueRange.h:401:20: warning: 'ArrayRef' is deprecated: Use {} or ArrayRef<T>() instead [-Wdeprecated-declarations] 401 | : ValueRange(ArrayRef<Value>(std::forward<Arg>(arg))) {} | ^~~~~~~~ | {} build/tools/mlir/examples/toy/Ch7/include/toy/Ops.cpp.inc:2221:30: note: in instantiation of function template specialization 'mlir::ValueRange::ValueRange<const std::nullopt_t &, void>' requested here 2221 | build(odsBuilder, odsState, std::nullopt); | ^ llvm-project/llvm/include/llvm/ADT/ArrayRef.h:70:18: note: 'ArrayRef' has been explicitly marked deprecated here 70 | /*implicit*/ LLVM_DEPRECATED("Use {} or ArrayRef<T>() instead", "{}") | ^ llvm-project/llvm/include/llvm/Support/Compiler.h:249:50: note: expanded from macro 'LLVM_DEPRECATED' 249 | #define LLVM_DEPRECATED(MSG, FIX) __attribute__((deprecated(MSG, FIX))) ```
2025-04-14[mlir] Remove deprecated cast member functions (#135556)Jakub Kuderski
These have been deprecated for over two years now in favor of free functions. See the relevant discourse thread: https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443 and the deprecation notice: https://mlir.llvm.org/deprecation/.
2025-04-10[mlir] implement `-verify-diagnostics=only-expected` (#135131)Maksim Levental
This PR implements `verify-diagnostics=only-expected` which is a "best effort" verification - i.e., `unexpected`s and `near-misses` will not be considered failures. The purpose is to enable narrowly scoped checking of verification remarks (just as we have for lit where only a subset of lines get `CHECK`ed).
2025-02-03[mlir] share argument attributes interface between calls and callables (#123176)jeanPerier
This patch shares core interface methods dealing with argument and result attributes from CallableOpInterface with the CallOpInterface and makes them mandatory to gives more consistent guarantees about concrete operations using these interfaces. This allows adding argument attributes on call like operations, which is sometimes required to get proper ABI, like with llvm.call (and llvm.invoke). The patch adds optional `arg_attrs` and `res_attrs` attributes to operations using these interfaces that did not have that already. They can then re-use the common "rich function signature" printing/parsing helpers if they want (for the LLVM dialect, this is done in the next patch). Part of RFC: https://discourse.llvm.org/t/mlir-rfc-adding-argument-and-result-attributes-to-llvm-call/84107
2025-01-28[MLIR] Define `getArgument()` for Toy tutorial passesMehdi Amini
This is important during debugging to be able to dump a pass pipeline. It is also what is used by `--mlir-print-ir-tree-dir` to compute filenames during dumps.
2025-01-25[mlir] Fix deprecated pointer union casts in toy example (#124422)Jakub Kuderski
2025-01-13Remove StandaloneExtensionPybind11.cpp FT update as does not work with ↵vfdev
python 3.8 and old pybind11 (#122697) Description: - Remove StandaloneExtensionPybind11.cpp FT update as does not work with python 3.8 and old pybind11 This should also fix the failing toy.test: https://github.com/llvm/llvm-project/pull/122684#issuecomment-2586802692 cc @jpienaar
2025-01-13Enabled freethreading support in MLIR python bindings (#122684)vfdev
Reland reverted https://github.com/llvm/llvm-project/pull/107103 with the fixes for Python 3.8 cc @jpienaar Co-authored-by: Peter Hawkins <phawkins@google.com>
2025-01-11[mlir] Migrate away from PointerUnion::{is,get} (NFC) (#122591)Kazu Hirata
Note that PointerUnion::{is,get} have been soft deprecated in PointerUnion.h: // FIXME: Replace the uses of is(), get() and dyn_cast() with // isa<T>, cast<T> and the llvm::dyn_cast<T> I'm not touching PointerUnion::dyn_cast for now because it's a bit complicated; we could blindly migrate it to dyn_cast_if_present, but we should probably use dyn_cast when the operand is known to be non-null.
2025-01-06[mlir][GPU] Add NVVM-specific `cf.assert` lowering (#120431)Matthias Springer
This commit add an NVIDIA-specific lowering of `cf.assert` to to `__assertfail`. Note: `getUniqueFormatGlobalName`, `getOrCreateFormatStringConstant` and `getOrDefineFunction` are moved to `GPUOpsLowering.h`, so that they can be reused.
2024-12-20[mlir python] Port in-tree dialects to nanobind. (#119924)Peter Hawkins
This is a companion to #118583, although it can be landed independently because since #117922 dialects do not have to use the same Python binding framework as the Python core code. This PR ports all of the in-tree dialect and pass extensions to nanobind, with the exception of those that remain for testing pybind11 support. This PR also: * removes CollectDiagnosticsToStringScope from NanobindAdaptors.h. This was overlooked in a previous PR and it is duplicated in Diagnostics.h. --------- Co-authored-by: Jacques Pienaar <jpienaar@google.com>
2024-12-20[mlir] Enable decoupling two kinds of greedy behavior. (#104649)Jacques Pienaar
The greedy rewriter is used in many different flows and it has a lot of convenience (work list management, debugging actions, tracing, etc). But it combines two kinds of greedy behavior 1) how ops are matched, 2) folding wherever it can. These are independent forms of greedy and leads to inefficiency. E.g., cases where one need to create different phases in lowering and is required to applying patterns in specific order split across different passes. Using the driver one ends up needlessly retrying folding/having multiple rounds of folding attempts, where one final run would have sufficed. Of course folks can locally avoid this behavior by just building their own, but this is also a common requested feature that folks keep on working around locally in suboptimal ways. For downstream users, there should be no behavioral change. Updating from the deprecated should just be a find and replace (e.g., `find ./ -type f -exec sed -i 's|applyPatternsAndFoldGreedily|applyPatternsGreedily|g' {} \;` variety) as the API arguments hasn't changed between the two.
2024-12-09Revert "Revert "[mlir python] Add nanobind support (#119232)Maksim Levental
Reverts revert #118517 after (hopefully) fixing builders (https://github.com/llvm/llvm-zorg/pull/328, https://github.com/llvm/llvm-zorg/pull/327) This reverts commit 61bf308cf2fc32452f14861c102ace89f5f36fec.
2024-12-03Revert "[mlir python] Add nanobind support for standalone dialects." (#118517)Maksim Levental
Reverts llvm/llvm-project#117922 because deps aren't met on some of the post-commit build bots.
2024-12-03[mlir python] Add nanobind support for standalone dialects. (#117922)Peter Hawkins
This PR allows out-of-tree dialects to write Python dialect modules using nanobind instead of pybind11. It may make sense to migrate in-tree dialects and some of the ODS Python infrastructure to nanobind, but that is a topic for a future change. This PR makes the following changes: * adds nanobind to the CMake and Bazel build systems. We also add robin_map to the Bazel build, which is a dependency of nanobind. * adds a PYTHON_BINDING_LIBRARY option to various CMake functions, such as declare_mlir_python_extension, allowing users to select a Python binding library. * creates a fork of mlir/include/mlir/Bindings/Python/PybindAdaptors.h named NanobindAdaptors.h. This plays the same role, using nanobind instead of pybind11. * splits CollectDiagnosticsToStringScope out of PybindAdaptors.h and into a new header mlir/include/mlir/Bindings/Python/Diagnostics.h, since it is code that is no way related to pybind11 or for that matter, Python. * changed the standalone Python extension example to have both pybind11 and nanobind variants. * changed mlir/python/mlir/dialects/python_test.py to have both pybind11 and nanobind variants. Notes: * A slightly unfortunate thing that I needed to do in the CMake integration was to use FindPython in addition to FindPython3, since nanobind's CMake integration expects the Python_ names for variables. Perhaps there's a better way to do this.
2024-09-30Include <cstdlib> in Toy Lexer examples. (#110449)Alexey Samsonov
These files all use `strtod` - make sure to include a proper header for this function. Otherwise, building MLIR fails on some systems after the recent commit 1b5f6916199ce09244cdb52c6911f2028e6ca95a which removed inclusion of `<cmath>` and thus broke transitive inclusion of `<stdlib.h>` in these headers.
2024-08-14[MLIR][LLVM] Turn the inliner interface into a promised interface (#103927)Christian Ulmann
This commit changes the LLVM dialect's inliner interface to no longer be registered at dialect initialization. Instead, it is now a promised interface, that needs to be registered explicitly. This change is desired to avoid pulling in a lot of dependencies into the `MLIRLLVMDialect` library, especially considering future patches that plan to extend it further with strong IR analysis.
2024-08-06[mlir] Support DialectRegistry extension comparison (#101119)Nikhil Kalra
`PassManager::run` loads the dependent dialects for each pass into the current context prior to invoking the individual passes. If the dependent dialect is already loaded into the context, this should be a no-op. However, if there are extensions registered in the `DialectRegistry`, the dependent dialects are unconditionally registered into the context. This poses a problem for dynamic pass pipelines, however, because they will likely be executing while the context is in an immutable state (because of the parent pass pipeline being run). To solve this, we'll update the extension registration API on `DialectRegistry` to require a type ID for each extension that is registered. Then, instead of unconditionally registered dialects into a context if extensions are present, we'll check against the extension type IDs already present in the context's internal `DialectRegistry`. The context will only be marked as dirty if there are net-new extension types present in the `DialectRegistry` populated by `PassManager::getDependentDialects`. Note: this PR removes the `addExtension` overload that utilizes `std::function` as the parameter. This is because `std::function` is copyable and potentially allocates memory for the contained function so we can't use the function pointer as the unique type ID for the extension. Downstream changes required: - Existing `DialectExtension` subclasses will need a type ID to be registered for each subclass. More details on how to register a type ID can be found here: https://github.com/llvm/llvm-project/blob/8b68e06731e0033ed3f8d6fe6292ae671611cfa1/mlir/include/mlir/Support/TypeID.h#L30 - Existing uses of the `std::function` overload of `addExtension` will need to be refactored into dedicated `DialectExtension` classes with associated type IDs. The attached `std::function` can either be inlined into or called directly from `DialectExtension::apply`. --------- Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
2024-07-02mlir/LogicalResult: move into llvm (#97309)Ramkumar Ramachandra
This patch is part of a project to move the Presburger library into LLVM.
2024-06-19[mlir][side effect] refactor(*): Include more precise side effects (#94213)donald chen
This patch adds more precise side effects to the current ops with memory effects, allowing us to determine which OpOperand/OpResult/BlockArgument the operation reads or writes, rather than just recording the reading and writing of values. This allows for convenient use of precise side effects to achieve analysis and optimization. Related discussions: https://discourse.llvm.org/t/rfc-add-operandindex-to-sideeffect-instance/79243
2024-05-25[mlir] Revise IDE folder structure (#89749)Michael Kruse
Update the folder titles for targets in the monorepository that have not seen taken care of for some time. These are the folders that targets are organized in Visual Studio and XCode (`set_property(TARGET <target> PROPERTY FOLDER "<title>")`) when using the respective CMake's IDE generator. * Ensure that every target is in a folder * Use a folder hierarchy with each LLVM subproject as a top-level folder * Use consistent folder names between subprojects * When using target-creating functions from AddLLVM.cmake, automatically deduce the folder. This reduces the number of `set_property`/`set_target_property`, but are still necessary when `add_custom_target`, `add_executable`, `add_library`, etc. are used. A LLVM_SUBPROJECT_TITLE definition is used for that in each subproject's root CMakeLists.txt.