aboutsummaryrefslogtreecommitdiff
path: root/mlir/python
AgeCommit message (Collapse)AuthorFilesLines
2025-12-05[python] Fix loc_tracebacks() (#170831)James Molloy1-1/+2
There were two bugs lurking in mlir.ir.loc_tracebacks(): 1) The default None parameter was not handled correctly (passed to a C++ function that expects ints. 2) The `yield` was incorrectly indented meaning loc_tracebacks() could not be nested (a "generator didn't yield" exception would be raised). Added testing of loc_tracebacks by replacing the custom contextmanager in the auto_location.py test with the loc_tracebacks() API. Had to harden the test to line number differences. --------- Co-authored-by: James Molloy <jmolloy@google.com>
2025-11-30[mlir][linalg] Restrict fill initial value type to output element type (#169567)Jakub Kuderski1-4/+4
Disallow implicit casting, which is surprising, and, IME, usually indicative of copy-paste errors. Because the initial value must be a scalar, I don't expect this to affect any data movement.
2025-11-22[MLIR][Python] remove PyYAML as a dep (#169145)Maksim Levental2-25/+43
PyYAML is not an actual use-time/runtime dependency of our bindings. It is necessary only if someone wants to regenerate `LinalgNamedStructuredOps.yaml`: https://github.com/llvm/llvm-project/blob/93097b2d47c87bf5eee0a2612d961c7a01831eab/mlir/tools/mlir-linalg-ods-gen/update_core_linalg_named_ops.sh.in#L29 This PR does the minimal refactor to remove the need during actual run/use time.
2025-11-21[MLIR][XeGPU][TransformOps] Add slice_dims argument to set_op_layout_attr ↵Tuomas Kärnä1-0/+8
and set_desc_layout (#168929) `set_op_layout_attr` and `set_desc_layout` transform ops wrap `xegpu.layout` in an `xegpu.slice` attribute if `slice_dims` argument is set.
2025-11-19[MLIR][Python] make sure stubs get installed with ↵Maksim Levental1-19/+12
LLVM_DISTRIBUTION_COMPONENTS (#168407) Fixes https://github.com/llvm/llvm-project/issues/168393. Also adds top-level `MLIR_PYTHON_STUBGEN_ENABLED` CMake option.
2025-11-18[MLIR][Python] Add arg_attrs and res_attrs to gpu func (#168475)Asher Mancinelli1-2/+4
I missed these attributes when I added the wrapper for GPUFuncOp in fbdd98f74f0d.
2025-11-15[MLIR][Transform][Python] Expose applying named_sequences as a method (#168223)Rolf Morel1-0/+20
Makes it so that a NamedSequenceOp can be directly applied to a Module, via a method `apply(...)`.
2025-11-12[MLIR][XeGPU][TransformOps] Add convert_layout op (#167342)Tuomas Kärnä1-1/+146
Adds `transform.xegpu.convert_layout` transform op that inserts an `xegpu.convert_layout` op for a given `Value`.
2025-11-12[MLIR][XeGPU][TransformOps] Add insert_prefetch op (#167356)Tuomas Kärnä1-0/+43
Adds `transform.xegpu.insert_prefetch` transform op that inserts `xegpu.prefetch_nd` ops for the given `Value` in an `scf.for` loop.
2025-11-11[MLIR][Python] Add region_op wrappers for linalg (#167616)Asher Mancinelli1-0/+4
Makes linalg.reduce and linalg.map region_ops so they can be constructed from functions and be called as decorators.
2025-11-11[MLIR][Python] Add wrappers for scf.index_switch (#167458)Asher Mancinelli1-0/+75
The C++ index switch op has utilities for `getCaseBlock(int i)` and `getDefaultBlock()`, so these have been added. Optional body builder args have been added: one for the default case and one for the switch cases.
2025-11-11[MLIR][XeGPU][TransformOps] Add set_gpu_launch_threads op (#166865)Tuomas Kärnä1-0/+36
Adds `transform.xegpu.set_gpu_launch_threads` that overrides `gpu.launch` operation threads.
2025-11-10[MLIR][XeGPU][TransformOps] Add set_op_layout_attr op (#166854)Tuomas Kärnä1-0/+47
Adds `transform.xegpu.set_op_layout_attr` transform op that attaches `xegpu.layout` attribute to the target op.
2025-11-10[MLIR][XeGPU][TransformOps] Add get_desc_op (#166801)Tuomas Kärnä1-0/+21
Add `transform.xegpu.get_desc_op` transform op that finds a `xegpu.create_nd_tdesc` producer op of a `Value`.
2025-11-07[MLIR][Transform][Python] Sync derived classes and their wrappers (#166871)Rolf Morel1-7/+156
Updates the derived Op-classes for the main transform ops to have all the arguments, etc, from the auto-generated classes. Additionally updates and adds missing snake_case wrappers for the derived classes which shadow the snake_case wrappers of the auto-generated classes, which were hitherto exposed alongside the derived classes.
2025-11-06[MLIR][XeGPU][Transform] add xegpu.set_desc_layout transform op (#165615)Tuomas Kärnä3-0/+94
Adds the first XeGPU transform op, `xegpu.set_desc_layout`, which attachs a `xegpu.layout` attribute to the descriptor that a `xegpu.create_nd_tdesc` op returns.
2025-11-04[MLIR][Linalg][Transform] Expose more args in VectorizeChildren[...] op's ↵Tuomas Kärnä1-0/+4
Python bindings (#166134) Expose missing boolean arguments in `VectorizeChildrenAndApplyPatternsOp` Python bindings.
2025-10-23[MLIR][Python] Skip stubgen while any sanitizer is enabled (#164661)Twice1-3/+10
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-21[MLIR][Python] Add shard Dialect Python Bindings (#162578)Siavash Nazari3-0/+29
Add Python bindings for `shard` dialect. Provide means for creating constructs in this dialect in Python.
2025-10-20[mlir][python] Add Pythonic wrappers for gpu ops (#163883)Asher Mancinelli1-1/+183
Add builders on the Python side that match builders in the C++ side, add tests for launching GPU kernels and regions, and correct some small documentation mistakes. This reflects the API decisions already made in the func dialect's Python bindings and makes use of the GPU dialect's bindings work more similar to C++ interface.
2025-10-20[MLIR][Python] expose translate_module_to_llvmir (#163881)Maksim Levental1-0/+2
This PR exposes `translate_module_to_llvmir` in the Python bindings.
2025-10-18[MLIR][Transform][SMT] Allow for declarative computations in schedules (#160895)Rolf Morel1-0/+12
By allowing `transform.smt.constrain_params`'s region to yield SMT-vars, op instances can declare relationships, through constraints, on incoming params-as-SMT-vars and outgoing SMT-vars-as-params. This makes it possible to declare that computations on params should be performed. The semantics are that the yielded SMT-vars should be from any valid satisfying assignment/model of the constraints in the region.
2025-10-16[mlir][python] Add bindings for OpenACC dialect (#163620)Asher Mancinelli3-0/+29
Adds initial support for Python bindings to the OpenACC dialect. * The bindings do not provide any niceties yet, just the barebones exposure of the dialect to Python. Construction of OpenACC ops is therefore verbose and somewhat inconvenient, as evidenced by the test. * The test only constructs one module, but I attempted to use enough operations to be meaningful. It does not test all the ops exposed, but does contain a realistic example of a memcpy idiom.
2025-10-16[mlir][python] Add pythonic interface for GPUFuncOp (#163596)Asher Mancinelli1-0/+146
The func dialect provides a more pythonic interface for constructing operations, but the gpu dialect does not; this is the first PR to provide the same conveniences for the gpu dialect, starting with the gpu.func op.
2025-10-13[MLIR][Transform] FuseOp: accept transform params, add use_forall argument ↵Tuomas Kärnä1-14/+28
(#161883) Changes to linalg `structured.fuse` transform op: * Adds an optional `use_forall` boolean argument which generates a tiled `scf.forall` loop instead of `scf.for` loops. * `tile_sizes` can now be any parameter or handle. * `tile_interchange` can now be any parameter or handle. * IR formatting changes from `transform.structured.fuse %0 [4, 8] ...` to `transform.structured.fuse %0 tile_sizes [4, 8] ...` - boolean arguments are now `UnitAttrs` and should be set via the op attr-dict: `{apply_cleanup, use_forall}`
2025-10-13[MLIR][Python] Pass OpView subclasses instead of Operation in rewrite ↵Twice1-1/+1
patterns (#163080) This is a follow-up PR for #162699. Currently, in the function where we define rewrite patterns, the `op` we receive is of type `ir.Operation` rather than a specific `OpView` type (such as `arith.AddIOp`). This means we can’t conveniently access certain parts of the operation — for example, we need to use `op.operands[0]` instead of `op.lhs`. The following example code illustrates this situation. ```python def to_muli(op, rewriter): # op is typed ir.Operation instead of arith.AddIOp pass patterns.add(arith.AddIOp, to_muli) ``` In this PR, we convert the operation to its corresponding `OpView` subclass before invoking the rewrite pattern callback, making it much easier to write patterns. --------- Co-authored-by: Maksim Levental <maksim.levental@gmail.com>
2025-10-07[MLIR][Python] hide globals in ir.py (#162339)Maksim Levental1-7/+7
We're shadowing the Python builtin function `globals` in `ir.py` and therefore anywhere someone does `from mlir.ir import *`. So hide it.
2025-10-07Revert "[MLIR][Python] use `FetchContent_Declare` for nanobind and remove ↵Maksim Levental3-18/+56
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 Levental3-56/+18
(#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-10-01[MLIR][Transform][Tune] Introduce `transform.tune.alternatives` op (#160724)Rolf Morel1-3/+63
This op enables expressing uncertainty regarding what should be happening at particular places in transform-dialect schedules. In particular, it enables representing a choice among alternative regions. This choice is resolved through providing a `selected_region` argument. When this argument is provided, the semantics are such that it is valid to rewrite the op through substituting in the selected region -- with the op's interpreted semantics corresponding to exactly this. This op represents another piece of the puzzle w.r.t. a toolkit for expressing autotuning problems with the transform dialect. Note that this goes beyond tuning knobs _on_ transforms, going further by making it tunable which (sequences of) transforms are to be applied.
2025-10-01[mlir][transform] Add PromoteTensorOp (#158318)Hendrik_Klug1-6/+0
Transform op to request a tensor value to live in a specific memory space after bufferization Co-authored-by: Nicolas Vasilache <Nico.Vasilache@amd.com> Co-authored-by: Alex Zinenko <ftynse@gmail.com>
2025-09-25[MLIR][Python] don't generate type stubs when cross-compiling (#160793)Maksim Levental1-78/+87
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-21[MLIR][Transform][SMT] Introduce transform.smt.constrain_params (#159450)Rolf Morel4-0/+67
Introduces a Transform-dialect SMT-extension so that we can have an op to express constrains on Transform-dialect params, in particular when these params are knobs -- see transform.tune.knob -- and can hence be seen as symbolic variables. This op allows expressing joint constraints over multiple params/knobs together. While the op's semantics are clearly defined, per SMTLIB, the interpreted semantics -- i.e. the `apply()` method -- for now just defaults to failure. In the future we should support attaching an implementation so that users can Bring Your Own Solver and thereby control performance of interpreting the op. For now the main usage is to walk schedule IR and collect these constraints so that knobs can be rewritten to constants that satisfy the constraints.
2025-09-20[MLIR][Python] reland (narrower) type stub generation (#157930)Maksim Levental7-2905/+101
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-19[MLIR][Python] Add python bindings for IRDL dialect (#158488)Twice3-0/+129
In this PR we add basic python bindings for IRDL dialect, so that python users can create and load IRDL dialects in python. This allows users, to some extent, to define dialects in Python without having to modify MLIR’s CMake/TableGen/C++ code and rebuild, making prototyping more convenient. A basic example is shown below (and also in the added test case): ```python # create a module with IRDL dialects module = Module.create() with InsertionPoint(module.body): dialect = irdl.DialectOp("irdl_test") with InsertionPoint(dialect.body): op = irdl.OperationOp("test_op") with InsertionPoint(op.body): f32 = irdl.is_(TypeAttr.get(F32Type.get())) irdl.operands_([f32], ["input"], [irdl.Variadicity.single]) # load the module irdl.load_dialects(module) # use the op defined in IRDL m = Module.parse(""" module { %a = arith.constant 1.0 : f32 "irdl_test.test_op"(%a) : (f32) -> () } """) ```
2025-09-10Revert stubgen-related patches (#157831)Oleksandr "Alex" Zinenko10-51/+3162
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-09[MLIR] [Python] Added a context manager for enabling traceback-based ↵Sergei Lebedev1-1/+34
locations (#157562) Previously this functionality was not surfaced in the public API.
2025-09-08[MLIR][Python] fix stubgen (#157583)Maksim Levental2-0/+22
In https://github.com/llvm/llvm-project/pull/155741 I broke the cardinal rule of CMake: nothing happens when you think it happens 🤷. Meaning: `declare_mlir_python_sources(SOURCES_GLOB "_mlir_libs/${_module_name}/**/*.pyi")` wasn't picking up any sources _because they aren't generated yet_. This of course makes sense in retrospect (the stubs are generated as part of the build process, post extension compile, rather than the configure process). Thus, the API needs to be: ``` GENERATE_TYPE_STUBS: List of generated type stubs expected from stubgen, relative to _mlir_libs. ``` Partially as a result of this omission, the stubs weren't being installed into either the build dir nor the install dir. That is also fixed now: **Source dir (for easy reference):** <img width="300" height="674" alt="image" src="https://github.com/user-attachments/assets/a569f066-c2bd-4361-91f3-1c75181e51da" /> **Build dir (for forthcoming typechecker tests):** <img width="398" height="551" alt="image" src="https://github.com/user-attachments/assets/017859f9-fddb-49ee-85e5-915f5b5f7377" /> **Install dir:** <img width="456" height="884" alt="image" src="https://github.com/user-attachments/assets/8051be7e-898c-4ec8-a11e-e2408b241a56" />
2025-09-05[MLIR][Python] enable type stub auto-generation (#155741)Maksim Levental10-3162/+29
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-09-05[mlir][python] UB dialect python bindings (#157127)Ivan Butygin3-1/+27
2025-08-17[MLIR] Split ExecutionEngine Initialization out of ctor into an explicit ↵Shenghang Tsai1-0/+1
method call (#153524) Retry landing https://github.com/llvm/llvm-project/pull/153373 ## Major changes from previous attempt - remove the test in CAPI because no existing tests in CAPI deal with sanitizer exemptions - update `mlir/docs/Dialects/GPU.md` to reflect the new behavior: load GPU binary in global ctors, instead of loading them at call site. - skip the test on Aarch64 since we have an issue with initialization there --------- Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
2025-08-12[mlir][python] automatic location inference (#151246)Maksim Levental1-4/+4
This PR implements "automatic" location inference in the bindings. The way it works is it walks the frame stack collecting source locations (Python captures these in the frame itself). It is inspired by JAX's [implementation](https://github.com/jax-ml/jax/blob/523ddcfbcad005deab5a7d542df4c706f5ee5e9c/jax/_src/interpreters/mlir.py#L462) but moves the frame stack traversal into the bindings for better performance. The system supports registering "included" and "excluded" filenames; frames originating from functions in included filenames **will not** be filtered and frames originating from functions in excluded filenames **will** be filtered (in that order). This allows excluding all the generated `*_ops_gen.py` files. The system is also "toggleable" and off by default to save people who have their own systems (such as JAX) from the added cost. Note, the system stores the entire stacktrace (subject to `locTracebackFramesLimit`) in the `Location` using specifically a `CallSiteLoc`. This can be useful for profiling tools (flamegraphs etc.). Shoutout to the folks at JAX for coming up with a good system. --------- Co-authored-by: Jacques Pienaar <jpienaar@google.com>
2025-08-11[mlir][python] expose isAttached (#153045)Maksim Levental1-0/+7
2025-08-08[MLIR][Linalg] Remove matmul_transpose variants (#147961)Renato Golin1-93/+0
Removes the `(batch_)matmul_transpose_{a|b}` variants from OpDSL and replace it with `matmul affine_maps [...]` whenever appropriate. This is in line with the [plan](https://discourse.llvm.org/t/rfc-op-explosion-in-linalg/82863), and can be done since #104783 merged. See: https://discourse.llvm.org/t/deprecate-batch-matmul-transpose-a-b-linalg-operations/87245 Issues investigated: * pad transform tests that could use `matmul` instead, so change to that. * ArmSME test using transpose actually needed it, so changed to `matmul` + affine maps. Arm tests validated by @banach-space (thanks!!).
2025-07-18[MLIR][SCF] Add dedicated Python bindings for ForallOp (#149416)Colin De Vlieghere1-1/+118
This patch specializes the Python bindings for ForallOp and InParallelOp, similar to the existing one for ForOp. These bindings create the regions and blocks properly and expose some additional helpers.
2025-07-17[MLIR][Python] Support eliding large resource strings in PassManager (#149187)Akshay Khadse2-0/+8
- Introduces a `large_resource_limit` parameter across Python bindings, enabling the eliding of resource strings exceeding a specified character limit during IR printing. - To maintain backward compatibilty, when using `operation.print()` API, if `large_resource_limit` is None and the `large_elements_limit` is set, the later will be used to elide the resource string as well. This change was introduced by https://github.com/llvm/llvm-project/pull/125738. - For printing using pass manager, the `large_resource_limit` and `large_elements_limit` are completely independent of each other.
2025-07-11[mlir] Fix TransformTuneExtensionOps.td include guardsChristian Sigg1-3/+3
2025-07-08[MLIR][Transform] Introduce `transform.tune.knob` op (#146732)Rolf Morel3-0/+110
A new transform op to represent that an attribute is to be chosen from a set of alternatives and that this choice is made available as a `!transform.param`. When a `selected` argument is provided, the op's `apply()` semantics is that of just making this selected attribute available as the result. When `selected` is not provided, `apply()` complains that nothing has resolved the non-determinism that the op is representing.
2025-07-07[mlir] Add `isStatic`* size check for `ShapedType`s. NFCI. (#147085)Jakub Kuderski1-0/+13
The motivation is to avoid having to negate `isDynamic*` checks, avoid double negations, and allow for `ShapedType::isStaticDim` to be used in ADT functions without having to wrap it in a lambda performing the negation. Also add the new functions to C and Python bindings.
2025-07-07[MLIR][Linalg] Remove elemwise_unary and elemwise_binary (#147082)Renato Golin1-31/+0
RFC: https://discourse.llvm.org/t/rfc-deprecate-linalg-elemwise-unary-and-elemwise-binary/87144 Remove the two operations and fix the tests by: * Cleaning simple operation tests of the old ops * Changing `linalg.elemwise_{u|bi}nary` with `linalg.{exp|add}` on transform tests * Changing some of the tests with `linalg.elementwise` instead, to broaden test coverage * Surgically removing the `elemwise_*` part in the Python tests * Update MLIR transform examples (text and tests) with `linalg.elementwise` instead Nothing else changed.