167 Commits

Author SHA1 Message Date
Will Froom
4b1d5b8d4f
[MLIR] Fix pipelineInitializationKey never being correctly updated (#150948)
Prior to this change `pipelineInitializationKey` would never be updated
so `initialize` would always be called even if the pipeline didn't
change
2025-07-28 15:47:12 +01:00
Kazu Hirata
606e7f90b1
[mlir] Remove unused includes (NFC) (#148872)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-07-15 20:47:53 -07:00
Longsheng Mou
5a8e60e724
[mlir] Use llvm::fill instead of std::fill(NFC) (#146889) 2025-07-07 09:12:38 +08:00
Jeremy Kun
b1b84a629d
Pretty print on -dump-pass-pipeline (#143223)
This PR makes `dump-pass-pipeline` pretty-print the dumped pipeline. For
large pipelines the current behavior produces a wall of text that is
hard to visually navigate.

For the command

```bash
mlir-opt --pass-pipeline="builtin.module(flatten-memref, expand-strided-metadata,func.func(arith-expand,func.func(affine-scalrep)))" --dump-pass-pipeline
```

Before:

```bash
Pass Manager with 3 passes:
builtin.module(flatten-memref,expand-strided-metadata,func.func(arith-expand{include-bf16=false include-f8e8m0=false},func.func(affine-scalrep)))
```

After:

```bash
Pass Manager with 3 passes:
builtin.module(
  flatten-memref,
  expand-strided-metadata,
  func.func(
    arith-expand{include-bf16=false include-f8e8m0=false},
    func.func(
      affine-scalrep
    )
  )
)
```

Another nice feature of this is that the pretty-printed string can still
be copy/pasted into `-pass-pipeline` using a quote:

```bash
$ bin/mlir-opt --dump-pass-pipeline test.mlir --pass-pipeline='
builtin.module(
  flatten-memref,
  expand-strided-metadata,
  func.func(
    arith-expand{include-bf16=false include-f8e8m0=false},
    func.func(
      affine-scalrep
    )
  )
)'
```

---------

Co-authored-by: Jeremy Kun <j2kun@users.noreply.github.com>
2025-06-08 12:23:38 -07:00
Iris Shi
78af0f3ab8
[mlir][NFC] Use llvm::sort (#140261) 2025-05-16 23:35:13 +08:00
Reid Kleckner
880de1cae2
[mlir] Declare explicit typeid to fix dylib build (#138357)
This makes the dyn_cast<PassExecutionAction> work outside the dylib,
i.e. from the unit test binary, when the CMake setting
MLIR_LINK_MLIR_DYLIB is ON.

Fixes #138202
2025-05-02 16:48:24 -07:00
Peter Collingbourne
d58c793673
Pass: Do not use llvm::array_pod_sort to sort OpPassManagers.
OpPassManager contains a field of type std::unique_ptr which
is not guaranteed to be trivially relocatable so we cannot use
llvm::array_pod_sort.

Reviewers: River707, joker-eph

Reviewed By: joker-eph

Pull Request: https://github.com/llvm/llvm-project/pull/129968
2025-03-06 11:20:23 -08:00
Billy Zhu
5b21fd298c
[MLIR][Pass] Full & deterministic diagnostics (#110311)
Today, when the pass infra schedules a pass/nested-pipeline on a set of
ops, it exits early as soon as it fails on one of the ops. This leads to
non-exhaustive, and more importantly, non-deterministic error reporting
(under async).

This PR removes the early termination behavior so that all ops have a
chance to run through the current pass/nested-pipeline, and all errors
are reported (async diagnostics are already ordered). This guarantees
deterministic & full error reporting. As a result, it's also no longer
necessary to -split-input-file with one error per split when testing
with -verify-diagnostics.
2024-10-01 19:07:52 -07:00
JOE1994
884221eddb [mlir] Tidy uses of llvm::raw_stream_ostream (NFC)
As specified in the docs,
1) raw_string_ostream is always unbuffered and
2) the underlying buffer may be used directly

( 65b13610a5226b84889b923bae884ba395ad084d for further reference )

* Don't call raw_string_ostream::flush(), which is essentially a no-op.
* Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
2024-09-16 23:23:25 -04:00
Kazu Hirata
0078d4b5a4
[Pass] Avoid repeated hash lookups (NFC) (#107761) 2024-09-08 09:14:08 -07:00
Ivan Butygin
1079fc4f54
[mlir][pass] Add errorHandler param to Pass::initializeOptions (#87289)
There is no good way to report detailed errors from inside
`Pass::initializeOptions` function as context may not be available at
this point and writing directly to `llvm::errs()` is not composable.

See
https://github.com/llvm/llvm-project/pull/87166#discussion_r1546426763

* Add error handler callback to `Pass::initializeOptions`
* Update `PassOptions::parseFromString` to support custom error stream
instead of using `llvm::errs()` directly.
* Update default `Pass::initializeOptions` implementation to propagate
error string from `parseFromString` to new error handler.
* Update `MapMemRefStorageClassPass` to report error details using new
API.
2024-04-02 02:43:04 +03:00
Mehdi Amini
744616b3ae
Rename ThreadPool::getThreadCount() to getMaxConcurrency() (NFC) (#82296)
This is addressing a long-time TODO to rename this misleading API. The
old one is preserved for now but marked deprecated.
2024-02-19 18:07:12 -08:00
Puyan Lotfi
03e29a49d9
[mlir][Pass] Enable the option for reproducer generation without crashing (#75421)
This PR adds API `makeReproducer` and cl::opt flag
`--mlir-generate-reproducer=<filename>` in order to allow for mlir
reproducer dumps even when the pipeline doesn't crash.

This PR also decouples the code that handles generation of an MLIR
reproducer from the crash recovery portion. The purpose is to allow for
generating reproducers outside of the context of a compiler crash.

This will be useful for frameworks and runtimes that use MLIR where it
is needed to reproduce the pipeline behavior for reasons outside of
diagnosing crashes. An example is for diagnosing performance issues
using offline tools, where being able to dump the reproducer from a
runtime compiler would be helpful.
2024-01-03 12:36:43 -05:00
Aman LaChapelle
46708a5bcb
[mlir][Pass] Move PassExecutionAction to Pass.h, NFC. (#74850)
This patch moves PassExecutionAction to Pass.h so that it can be used by
the action framework to introspect and intercede in pass managers that
might be set up opaquely. This provides for a very particular use case,
which essentially involves being able to intercede in a PassManager and
skip or apply individual passes. Because of this, this patch also adds a
test for this use case to verify that it could in fact work.
2023-12-08 11:22:08 -08:00
Mehdi Amini
ec5e947f97 Fix MSAN error: use of unitialized value when hashing the MLIR pass manager (NFC) 2023-08-22 16:27:44 -07:00
Mehdi Amini
809b440395 Fix MLIR pass manager initialization: hash the pass pipeline to detect when initialization is needed
The current logic hashes the context to detect registration changes and re-run
the pass initialization. However it wasn't checking for changes to the
pipeline, so a pass that would get added after a first run would not be
initialized during subsequent runs.

Reviewed By: Mogball

Differential Revision: https://reviews.llvm.org/D158377
2023-08-22 12:55:07 -07:00
Mehdi Amini
0620f99de5 Fix minor typos in comments in MLIR Pass.cpp (NFC) 2023-08-07 19:01:14 -07:00
Mehdi Amini
3e2e10b552 Clarify the invariant of the MLIR pass pipeline around Pass::initialize()
This method should not load new dialect or affect the context itself.

Differential Revision: https://reviews.llvm.org/D157198
2023-08-07 18:46:08 -07:00
Matthias Springer
87da4b6fa6 [mlir][Pass] Check supported op types before running a pass
Add extra error checking to prevent passes from being run on unsupported ops through the pass manager infrastructure.

Differential Revision: https://reviews.llvm.org/D153144
2023-06-20 14:40:11 +02:00
Mehdi Amini
f406adf134 Add capture of "IRUnits" as context for an MLIR Action
IRUnit is defined as:

  using IRUnit = PointerUnion<Operation *, Region *, Block *, Value>;

The tracing::Action is extended to take an ArrayRef<IRUnit> as context to
describe an Action. It is demonstrated in the "ActionLogging" observer.

Reviewed By: rriddle, Mogball

Differential Revision: https://reviews.llvm.org/D144814
2023-03-20 13:40:55 +01:00
Mehdi Amini
930744fcda Add an Observer for logging actions application to a stream
Integrate the `tracing::ExecutionContext()` into mlir-opt with a new
--log-action-to=<file> option to demonstrate the feature.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D144813
2023-03-20 13:39:01 +01:00
rkayaith
94a309284d [mlir][Pass] Make PassManager default to op-agnostic
Currently `PassManager` defaults to being anchored on `builtin.module`.
Switching the default makes `PassManager` consistent with
`OpPassManager` and avoids the implicit dependency on `builtin.module`.

Specifying the anchor op type isn't strictly necessary when using
explicit nesting (existing pipelines will continue to work), but I've
updated most call sites to specify the anchor since it allows for better
error-checking during pipeline construction.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D137731
2023-01-25 15:38:19 -05:00
Kazu Hirata
0a81ace004 [mlir] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to remove #include "llvm/ADT/Optional.h".

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 01:25:58 -08:00
Kazu Hirata
a1fe1f5f77 [mlir] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-13 21:05:06 -08:00
Kazu Hirata
1a36588ec6 [mlir] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 18:50:27 -08:00
rkayaith
13bd410962 [mlir][Pass] Include anchor op in -pass-pipeline
In D134622 the printed form of a pass manager is changed to include the
name of the op that the pass manager is anchored on. This updates the
`-pass-pipeline` argument format to include the anchor op as well, so
that the printed form of a pipeline can be directly passed to
`-pass-pipeline`. In most cases this requires updating
`-pass-pipeline='pipeline'` to
`-pass-pipeline='builtin.module(pipeline)'`.

This also fixes an outdated assert that prevented running a
`PassManager` anchored on `'any'`.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D134900
2022-11-03 11:36:12 -04:00
rkayaith
cad61e49b2 [mlir-opt] Add a '-dump-pass-pipeline' option
Add an option to dump the pipeline that will be run to stderr. A
dedicated option is needed since the existing `test-dump-pipeline`
pipeline won't be usable with `-pass-pipeline` after D135745.

Reviewed By: rriddle, mehdi_amini

Differential Revision: https://reviews.llvm.org/D135747
2022-10-20 19:20:24 -04:00
rkayaith
e874bbc292 [mlir] Include anchor op when printing pass managers
Previously a pipeline nested on `anchor-op` would print as just
`'pipeline'`, now it will print as `'anchor-op(pipeline)'`. This ensures
the text form includes all information needed to reconstruct the pass
manager.

Reviewed By: rriddle, mehdi_amini

Differential Revision: https://reviews.llvm.org/D134622
2022-10-20 19:17:45 -04:00
Kazu Hirata
0a706be316 [mlir] Don't include SetVector.h (NFC) 2022-09-17 13:36:16 -07:00
Kazu Hirata
87d627b623 Remove redundant string initialization (NFC)
Identified with readability-redundant-string-init.
2022-07-24 12:27:11 -07:00
Kazu Hirata
30c675878c Use value_or instead of getValueOr (NFC) 2022-06-19 10:34:41 -07:00
River Riddle
c2fb9c29b4 [mlir:Pass] Add support for op-agnostic pass managers
This commit refactors the current pass manager support to allow for
operation agnostic pass managers. This allows for a series of passes
to be executed on any viable pass manager root operation, instead
of one specific operation type. Op-agnostic/generic pass managers
only allow for adding op-agnostic passes.

These types of pass managers are extremely useful when constructing
pass pipelines that can apply to many different types of operations,
e.g., the default inliner simplification pipeline. With the advent of
interface/trait passes, this support can be used to define FunctionOpInterface
pass managers, or other pass managers that effectively operate on
specific interfaces/traits/etc (see #52916 for an example).

Differential Revision: https://reviews.llvm.org/D123536
2022-05-12 13:12:59 -07:00
River Riddle
36d3efea15 [mlir][NFC] Drop a few unnecessary includes from Pass.h 2022-04-07 23:42:47 -07:00
River Riddle
0d8df98035 [mlir] Allow for using OpPassManager in pass options
This significantly simplifies the boilerplate necessary for passes
to define nested pass pipelines.

Differential Revision: https://reviews.llvm.org/D122880
2022-04-02 00:45:11 -07:00
River Riddle
50f82e6847 [mlir] Fix missing verification after running an OpToOpAdaptorPass
The current decision of when to run the verifier is running on the
assumption that nested passes can't affect the validity of the parent
operation, which isn't true. Parent operations may attach any number
of constraints on nested operations, which may not necessarily be
captured (or shouldn't be captured) at a smaller granularity.

This commit rectifies this by properly running the verifier after an
OpToOpAdaptor pass. To avoid an explosive increase in compile time,
we only run verification on the parent operation itself. To do this, a
flag to mlir::verify is added to avoid recursive verification if it isn't
desired.

Fixes #54288

Differential Revision: https://reviews.llvm.org/D121836
2022-03-16 14:53:41 -07:00
River Riddle
9c9a431735 [mlir][Pass] Add support for an InterfacePass and pass filtering based on OperationName
This commit adds a new hook Pass `bool canScheduleOn(RegisteredOperationName)` that
indicates if the given pass can be scheduled on operations of the given type. This makes it
easier to define constraints on generic passes without a) adding conditional checks to
the beginning of the `runOnOperation`, or b) defining a new pass type that forwards
from `runOnOperation` (after checking the invariants) to a new hook. This new hook is
used to implement an `InterfacePass` pass class, that represents a  generic pass that
runs on operations of the given interface type.

The PassManager will also verify that passes added to a pass manager can actually be
scheduled on that pass manager, meaning that we will properly error when an Interface
is scheduled on an operation that doesn't actually implement that interface.

Differential Revision: https://reviews.llvm.org/D120791
2022-03-04 15:14:04 -08:00
River Riddle
56f62fbf73 [mlir] Finish removing Identifier from the C++ API
There have been a few API pieces remaining to allow for a smooth transition for
downstream users, but these have been up for a few months now. After this only
the C API will have reference to "Identifier", but those will be reworked in a followup.

The main updates are:
* Identifier -> StringAttr
* StringAttr::get requires the context as the first parameter
  - i.e. `Identifier::get("...", ctx)` -> `StringAttr::get(ctx, "...")`

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D116626
2022-01-12 11:58:23 -08:00
Mehdi Amini
e5639b3fa4 Fix more clang-tidy cleanups in mlir/ (NFC) 2021-12-22 20:53:11 +00:00
Mehdi Amini
02b6fb218e Fix clang-tidy issues in mlir/ (NFC)
Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D115956
2021-12-20 20:25:01 +00:00
Mehdi Amini
be0a7e9f27 Adjust "end namespace" comment in MLIR to match new agree'd coding style
See D115115 and this mailing list discussion:
https://lists.llvm.org/pipermail/llvm-dev/2021-December/154199.html

Differential Revision: https://reviews.llvm.org/D115309
2021-12-08 06:05:26 +00:00
River Riddle
195730a650 [mlir][NFC] Replace references to Identifier with StringAttr
This is part of the replacement of Identifier with StringAttr.

Differential Revision: https://reviews.llvm.org/D113953
2021-11-16 17:36:26 +00:00
Mehdi Amini
67453c8941 Use std::make_unique instead of new to reinitalize a unique_ptr (NFC)
Fix a clang-tidy warning.
2021-11-14 22:28:54 +00:00
Mehdi Amini
a8c1d9d63e Add a clear() method on the PassManager (NFC)
This allows to clear an OpPassManager and populated it again with a new
pipeline, while preserving all the other options (including instrumentations).

Differential Revision: https://reviews.llvm.org/D112393
2021-10-25 04:39:00 +00:00
Mehdi Amini
1a406cd5f2 Remove unused llvm/Support/Parallel.h from MLIR (NFC)
This header aren't needed anymore: MLIR is using a thread pool
injected in the context instead of a global one.
2021-09-14 23:30:42 +00:00
Mehdi Amini
20113d66c7 Fix mismatch between the provisioning of asyncExecutors and the actual thread count currently in the context (NFC)
This fixes an assert in some deployment where the threadpool is
customized.
2021-07-16 06:51:06 +00:00
River Riddle
84bd07aff9 [mlir] Fix GCC5 build after D104516
GCC5 isn't able to implicitly capture `this` properly in an `auto` lambda.
2021-06-23 03:16:34 +00:00
River Riddle
6569cf2a44 [mlir] Add a ThreadPool to MLIRContext and refactor MLIR threading usage
This revision refactors the usage of multithreaded utilities in MLIR to use a common
thread pool within the MLIR context, in addition to a new utility that makes writing
multi-threaded code in MLIR less error prone. Using a unified thread pool brings about
several advantages:

* Better thread usage and more control
We currently use the static llvm threading utilities, which do not allow multiple
levels of asynchronous scheduling (even if there are open threads). This is due to
how the current TaskGroup structure works, which only allows one truly multithreaded
instance at a time. By having our own ThreadPool we gain more control and flexibility
over our job/thread scheduling, and in a followup can enable threading more parts of
the compiler.

* The static nature of TaskGroup causes issues in certain configurations
Due to the static nature of TaskGroup, there have been quite a few problems related to
destruction that have caused several downstream projects to disable threading. See
D104207 for discussion on some related fallout. By having a ThreadPool scoped to
the context, we don't have to worry about destruction and can ensure that any
additional MLIR thread usage ends when the context is destroyed.

Differential Revision: https://reviews.llvm.org/D104516
2021-06-23 01:29:24 +00:00
Chris Lattner
a490ca8e01 [PassManager] Save compile time by not running the verifier unnecessarily. NFC
This changes the pass manager to not rerun the verifier when a pass says it
didn't change anything or after an OpToOpPassAdaptor, since neither of those
cases need verification (and if the pass lied, then there will be much larger
semantic problems than will be caught by the verifier).

This maintains behavior in EXPENSIVE_CHECKS mode.

Differential Revision: https://reviews.llvm.org/D104243
2021-06-14 11:43:52 -07:00
River Riddle
92469ca027 [mlir] Refactor the implementation of pass crash reproducers
The current implementation has several key limitations and weirdness, e.g local reproducers don't support dynamic pass pipelines, error messages don't include the passes that failed, etc. This revision refactors the implementation to support more use cases, and also be much cleaner.

The main change in this revision, aside from moving the implementation out of Pass.cpp and into its own file, is the addition of a crash recovery pass instrumentation. For local reproducers, this instrumentation handles setting up the recovery context before executing each pass. For global reproducers, the instrumentation is used to provide a more detailed error message, containing information about which passes are running and on which operations.

Example of new message:

```
error: Failures have been detected while processing an MLIR pass pipeline
note: Pipeline failed while executing [`TestCrashRecoveryPass` on 'module' operation: @foo]: reproducer generated at `crash-recovery.mlir.tmp`
```

Differential Revision: https://reviews.llvm.org/D101854
2021-05-19 16:59:53 -07:00
Fabian Schuiki
33f908c428
[MLIR] Factor pass timing out into a dedicated timing manager
This factors out the pass timing code into a separate `TimingManager`
that can be plugged into the `PassManager` from the outside. Users are
able to provide their own implementation of this manager, and use it to
time additional code paths outside of the pass manager. Also allows for
multiple `PassManager`s to run and contribute to a single timing report.

More specifically, moves most of the existing infrastructure in
`Pass/PassTiming.cpp` into a new `Support/Timing.cpp` file and adds a
public interface in `Support/Timing.h`. The `PassTiming` instrumentation
becomes a wrapper around the new timing infrastructure which adapts the
instrumentation callbacks to the new timers.

Reviewed By: rriddle, lattner

Differential Revision: https://reviews.llvm.org/D100647
2021-05-12 18:14:51 +02:00