Commit Graph

53 Commits

Author SHA1 Message Date
Benjamin Kramer
daa350c199 [mlir] Work around MSVC bug
MSVC fails to parse this construct, leading to
MlirTranslateMain.cpp(70): error C2065: 'inputSplitMarker': undeclared identifier

Just switching to brace init works around the issue
2024-03-17 14:05:41 +01:00
Mehdi Amini
2a547f0f6c [MLIR] Fix mlir-opt --show-dialects to not require any input (as documented) 2024-03-14 22:55:25 -07:00
Ingo Müller
516ccce7fa [mlir] Make the split markers of splitAndProcessBuffer configurable. (#84765)
This allows to define custom splitters, which is interesting for
non-MLIR inputs and outputs to `mlir-translate`. For example, one may
use `; -----` as a splitter of `.ll` files. The splitters are now passed
as arguments into `splitAndProcessBuffer`, the input splitter defaulting
to the previous default (`// -----`) and the output splitter defaulting
to the empty string, which also corresponds to the previous default. The
behavior of the input split marker should not change at all; however,
outputs now have one new line *more* than before if there is no splitter
(old: `insertMarkerInOutput = false`, new: `outputSplitMarker = ""`) and
one new line *less* if there is one. The value of the input splitter is
exposed as a command line options of `mlir-translate` and other tools as
an optional value to the previously existing flag `-split-input-file`,
which defaults to the default splitter if not specified; the value of
the output splitter is exposed with the new `-output-split-marker`,
which default to the empty string in `mlir-translate` and the default
splitter in the other tools. In short, the previous usage or omission of
the flags should result in previous behavior (modulo the new lines
mentioned before).
2024-03-14 13:55:50 +01:00
Mehdi Amini
6594f428de Split the llvm::ThreadPool into an abstract base class and an implementation (#82094)
This decouples the public API used to enqueue tasks and wait for
completion from the actual implementation, and opens up the possibility
for clients to set their own thread pool implementation for the pool.

https://discourse.llvm.org/t/construct-threadpool-from-vector-of-existing-threads/76883
2024-03-02 19:10:50 -08:00
Matteo Franciolini
ce4da0c4ee Enables textual IR roundtripping through --verifyRoundtrip (#82946)
The patch enables roundtrip to textual file when running
`--verifyRoundtrip`. The verification is successful if both textual and
bytecode formats can roundtrip successfully.
2024-02-25 20:34:56 -08:00
Mehdi Amini
820e438e70 Apply clang-tidy fixes for readability-identifier-naming in MlirOptMain.cpp (NFC) 2024-02-16 18:07:10 -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
Matteo Franciolini
4488f4933e [mlir][bytecode] Add bytecode writer config API to skip serialization of resources (#71991)
When serializing to bytecode, users can select the option to elide
resources from the bytecode file. This will instruct the bytecode writer
to serialize only the key and resource kind, while skipping
serialization of the data buffer. At parsing, the IR is built in memory
with valid (but empty) resource handlers.
2023-11-13 12:59:30 -06:00
weiwei chen
77c6339197 [MLIR][mlir-opt] Add registerationAndParseCLIOptions for MlirOptMain. (#70581)
Seprate registeration and CLI parsing from `MlirOptMain` to
`mlir::registrationAndParseCLIOptions` and a new `MlirOptMain` so that
external tools to drive `mlir-opt` can call these two new APIs to get CLI
option values before running `MlirOptMain`.
2023-10-29 22:58:33 -04:00
Mehdi Amini
a6d09d4b1a Add a -verify-roundtrip option to mlir-opt intended to validate custom printer/parser completeness
Running:

  MLIR_OPT_CHECK_IR_ROUNDTRIP=1 ninja check-mlir

will now exercises all of our test with a round-trip to bytecode and a comparison for equality.

Reviewed By: rriddle, ftynse, jpienaar

Differential Revision: https://reviews.llvm.org/D90088
2023-05-25 15:15:47 -07:00
Mehdi Amini
7f00ba08aa Restore mlir-opt --run-reproducer option to opt-in running a reproducer
When tooling out there produces a reproducer that is archived, the first thing
a user is likely to expect is to process this as they do with any MLIR file.
However https://reviews.llvm.org/D126447 changed the behavior of mlir-opt to
eliminate the `--run-reproducer` option and instead automatically run it when
present in the input file. This creates a discrepancy in how mlir-opt behaves
when fed with an input file, and is surprising for users.
The explicit passing of `--run-reproducer` to express user-intent seems more
in line with what is expected from `mlir-opt`.

Reviewed By: rriddle, jpienaar

Differential Revision: https://reviews.llvm.org/D149820
2023-05-04 11:08:03 -07:00
Jacques Pienaar
5c90e1ffb0 [mlir][bytecode] Return error instead of min version
Can't return a well-formed IR output while enabling version to be bumped
up during emission. Previously it would return min version but
potentially invalid IR which was confusing, instead make it return
error and abort immediately instead.

Differential Revision: https://reviews.llvm.org/D149569
2023-04-30 22:11:02 -07:00
Jacques Pienaar
0610e2f6a2 [mlir][bytecode] Allow client to specify a desired version.
Add method to set a desired bytecode file format to generate. Change
write method to be able to return status including the minimum bytecode
version needed by reader. This enables generating an older version of
the bytecode (not dialect ops, attributes or types). But this does not
guarantee that an older version can always be generated, e.g., if a
dialect uses a new encoding only available at later bytecode version.
This clamps setting to at most current version.

Differential Revision: https://reviews.llvm.org/D146555
2023-04-29 05:35:53 -07:00
Mehdi Amini
9c8db444bc Remove deprecated preloadDialectInContext flag for MlirOptMain that has been deprecated for 2 years
See https://discourse.llvm.org/t/psa-preloaddialectincontext-has-been-deprecated-for-1y-and-will-be-removed/68992

Differential Revision: https://reviews.llvm.org/D149039
2023-04-24 14:37:31 -07:00
Mehdi Amini
ffd6f6b91a Remove deprecated entry point for MlirOptMain
See: https://discourse.llvm.org/t/psa-migrating-mlir-opt-like-tools-to-use-mliroptmainconfig/68991

Differential Revision: https://reviews.llvm.org/D149038
2023-04-24 14:37:31 -07:00
Mehdi Amini
cca510640b Refactor the mlir-opt command line options related to debugging in a helper
This makes it reusable across various tooling and reduces the amount of
boilerplate needed.

Differential Revision: https://reviews.llvm.org/D144818
2023-04-24 14:34:15 -07:00
Mehdi Amini
1020150e7a Add a GDB/LLDB interface for interactive debugging of MLIR Actions
This includes a small runtime acting as callback for the ExecutionEngine
and a C API that makes it possible to control from the debugger.

A python script for LLDB is included that hook a new `mlir` subcommand
and allows to set breakpoints and inspect the current action, the context
and the stack.

Differential Revision: https://reviews.llvm.org/D144817
2023-04-24 14:34:15 -07:00
Mathieu Fehr
8ac8c922fb [mlir][irdl] Add IRDL registration
This patch add support for loading IRDL dialects at runtime
with `mlir-opt`.

Given the following `dialect.irdl` file:
```mlir
module {
  irdl.dialect @cmath {
    irdl.type @complex {
      %0 = irdl.is f32
      %1 = irdl.is f64
      %2 = irdl.any_of(%0, %1)
      irdl.parameters(%2)
    }

    irdl.operation @norm {
      %0 = irdl.any
      %1 = irdl.parametric @complex<%0>
      irdl.operands(%1)
      irdl.results(%0)
    }
}
```

the IRDL file can be loaded with the `mlir-opt --irdl-file=dialect.irdl`
command, and the following file can then be parsed:

```mlir
func.func @conorm(%p: !cmath.complex<f32>, %q: !cmath.complex<f32>) -> f32 {
  %norm_p = "cmath.norm"(%p) : (!cmath.complex<f32>) -> f32
  %norm_q = "cmath.norm"(%q) : (!cmath.complex<f32>) -> f32
  %pq = arith.mulf %norm_p, %norm_q : f32
  return %pq : f32
}
```

To minimize the size of this patch, the operation, attribute, and type verifier are all always returning `success()`.

Depends on D144692

Reviewed By: rriddle, Mogball, mehdi_amini

Differential Revision: https://reviews.llvm.org/D144693
2023-04-23 17:28:44 +01:00
Mehdi Amini
7f069f5ef4 Add a breakpoint manager that matches based on File/Line/Col Locations
This will match the locations attached to the IRunits passed in as context
with an action.

This is a recommit of d09c80515d after fixing the test on Windows.

Differential Revision: https://reviews.llvm.org/D144815
2023-04-21 23:54:20 -06:00
Mehdi Amini
6fb4c9fdc1 Revert "Add a breakpoint manager that matches based on File/Line/Col Locations"
This reverts commit d09c80515d.

This is broken on Windows
2023-04-21 23:24:05 -06:00
Mehdi Amini
d09c80515d Add a breakpoint manager that matches based on File/Line/Col Locations
This will match the locations attached to the IRunits passed in as context
with an action.

Differential Revision: https://reviews.llvm.org/D144815
2023-04-21 22:28:27 -06:00
Arjun P
5115ede632 Revert "[mlir][irdl] Add IRDL registration"
This reverts commit e0d884de36.

Reverting due to buildbot failure.
2023-04-20 15:55:57 +01:00
Mathieu Fehr
e0d884de36 [mlir][irdl] Add IRDL registration
This patch add support for loading IRDL dialects at runtime
with `mlir-opt`.

Given the following `dialect.irdl` file:
```mlir
module {
  irdl.dialect @cmath {
    irdl.type @complex {
      %0 = irdl.is f32
      %1 = irdl.is f64
      %2 = irdl.any_of(%0, %1)
      irdl.parameters(%2)
    }

    irdl.operation @norm {
      %0 = irdl.any
      %1 = irdl.parametric @complex<%0>
      irdl.operands(%1)
      irdl.results(%0)
    }
}
```

the IRDL file can be loaded with the `mlir-opt --irdl-file=dialect.irdl`
command, and the following file can then be parsed:

```mlir
func.func @conorm(%p: !cmath.complex<f32>, %q: !cmath.complex<f32>) -> f32 {
  %norm_p = "cmath.norm"(%p) : (!cmath.complex<f32>) -> f32
  %norm_q = "cmath.norm"(%q) : (!cmath.complex<f32>) -> f32
  %pq = arith.mulf %norm_p, %norm_q : f32
  return %pq : f32
}
```

To minimize the size of this patch, the operation, attribute, and type verifier are all always returning `success()`.

Depends on D144692

Reviewed By: rriddle, Mogball, mehdi_amini

Differential Revision: https://reviews.llvm.org/D144693
2023-04-20 15:35:41 +01:00
Fabian Mora
5e2afe5c66 Implement Pass and Dialect plugins for mlir-opt
Implementation of Pass and Dialect Plugins that mirrors LLVM Pass Plugin
implementation from the new pass manager.

Currently the implementation only supports using the pass-pipeline option
for adding passes. This restriction is imposed by the `PassPipelineCLParser`
variable in mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:114 that loads the
parse options statically before parsing the cmd line args.

```
mlir-opt stanalone-plugin.mlir --load-dialect-plugin=lib/libStandalonePlugin.so --pass-pipeline="builtin.module(standalone-switch-bar-foo)"
```

Reviewed By: rriddle, mehdi_amini

Differential Revision: https://reviews.llvm.org/D147053
2023-04-06 18:28:50 -07:00
Mehdi Amini
713e815f96 Revert "Implement Pass and Dialect plugins for mlir-opt"
This reverts commit e9b415870d.

This landed without the right authorship.
2023-04-06 18:28:11 -07:00
Mehdi Amini
e9b415870d Implement Pass and Dialect plugins for mlir-opt
Implementation of Pass and Dialect Plugins that mirrors LLVM Pass Plugin  implementation from the new pass manager.

Currently the implementation only supports using the pass-pipeline option for adding passes. This restriction is imposed by the `PassPipelineCLParser` variable in mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:114 that loads the parse options statically before parsing the cmd line args.

```
mlir-opt stanalone-plugin.mlir --load-dialect-plugin=lib/libStandalonePlugin.so --pass-pipeline="builtin.module(standalone-switch-bar-foo)"
```

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D147053
2023-04-06 18:27:54 -07:00
Jacques Pienaar
c8525e980b Revert "Implement Pass and Dialect plugins for mlir-opt"
Breaks bot.

This reverts commit d4c873b044.
2023-04-06 09:41:51 -07:00
Fabian Mora
d4c873b044 Implement Pass and Dialect plugins for mlir-opt
Implementation of Pass and Dialect Plugins that mirrors LLVM Pass Plugin  implementation from the new pass manager.

Currently the implementation only supports using the pass-pipeline option for adding passes. This restriction is imposed by the `PassPipelineCLParser` variable in mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:114 that loads the parse options statically before parsing the cmd line args.

```
mlir-opt stanalone-plugin.mlir --load-dialect-plugin=lib/libStandalonePlugin.so --pass-pipeline="builtin.module(standalone-switch-bar-foo)"
```

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D147053
2023-04-06 09:33:21 -07:00
Kai Sasaki
470f3cee35 [mlir] Catch the case using ir print without disabling multithread
-mlir-print-ir-module-scope option cannot be used without disabling multithread for pass manager. For the usability, we can throw a validation error in mlir-opt instead of assertion failure.

Issue: https://github.com/llvm/llvm-project/issues/61578

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D146785
2023-04-02 15:47:11 +09: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
Mehdi Amini
5c41a7334d Fix a small typo in a comment in MlitOptMain.cpp (NFC) 2023-03-13 18:15:27 +01:00
Mehdi Amini
554e40d6ba Add a message to mlir-opt when reading from stdin to avoid being waiting for nothing
It happens from time to time that one may run mlir-opt expecting something to
happen, but the process is waiting on stdin. Print a message when reading from
stdin to warn developers.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D145469
2023-03-13 14:28:11 +01:00
Mehdi Amini
7de0804ea3 Make mlir-opt --show-dialects option print on a single line
Differential Revision: https://reviews.llvm.org/D145398
2023-03-08 01:17:51 +01:00
Mehdi Amini
9b1fe5649e Delete ActionManager and replace it with a simple callback on the Context
The concept of the ActionManager acts as a sort of "Hub" that can receive
various types of action and dispatch them to a set of registered handlers.
One handler will handle the action or it'll cascade to other handlers.

This model does not really fit the current evolution of the Action tracing
and debugging: we can't foresee a good case where this behavior compose with
the use-case behind the handlers. Instead we simplify it with a single
callback installed on the Context.

Differential Revision: https://reviews.llvm.org/D144811
2023-03-07 08:25:34 +01:00
Goran Flegar
f2cdccc034 [mlir-opt] Fix dialect preload after fb1bb6a
Also pipe empty string to the commandline test to make sure it does
not hang on some configurations.
2023-03-06 19:30:14 +01:00
Mehdi Amini
28d04c564d Rename DebugAction to tracing::Action and move related code from lib/Support to lib/IR and lib/Debug
This is a preparation for adding support for more infrastructure around the concept
of Action and make tracing Action more of a first class concept.
The doc will be updated later in a subsequent revision after the changes are
completed.

Action belongs to IR because of circular dependency: Actions are dispatched through
the MLIRContext but Action will learn to encapsulate IR construct.

Differential Revision: https://reviews.llvm.org/D144809
2023-03-06 16:12:30 +01:00
Mehdi Amini
fb1bb6a0b0 Expose a convenient registerCLOptions() for MlirOptMainConfig
This allows for downstream *-opt tools to stay always aligned with the options
exposed by mlir-opt.
It aligns the "generic" options with the more "components" ones like the
pass manager options or the context options.

Differential Revision: https://reviews.llvm.org/D144782
2023-03-06 15:16:46 +01:00
Mehdi Amini
01cfdf1f8d [mlir-opt] Rename internally the variables backing --no-implicit-module to ExplicitModule (NFC)
The flag name isn't the best: avoiding negative is in general more readable
and conveys the intent better.
2023-02-26 09:32:23 -05:00
Mehdi Amini
8d2e0c16e6 Refactor a MlirOptMainConfig class to hold the configuration of MlirOptMain (NFC)
The list of boolean flags and others is becoming unresonnably long.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D143829
2023-02-25 17:04:29 -05:00
Mehdi Amini
5cf549e6b1 Add info about the "testing only" aspect of --allow-unregistered-dialect in the cl::opt desc (NFC) 2023-02-24 17:37:05 -07: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
River Riddle
18546ff8dd [mlir:Bytecode] Add shared_ptr<SourceMgr> overloads to allow safe mmap of data
The bytecode reader currently has no mechanism that allows for directly referencing
data from the input buffer safely. This commit adds shared_ptr<SourceMgr> overloads
that provide an explicit and safe way of extending the lifetime of the input. The usage of
these new overloads is adopted in all of our tooling, and is implicitly used in the filename
only parser methods.

Differential Revision: https://reviews.llvm.org/D139366
2022-12-11 22:45:34 -08:00
rkayaith
40e7741d2d [mlir-opt] Add '-p' as an alias for '-pass-pipeline'
The pipeline strings have been getting more verbose over time, adding an
alias for the option should help improve the ergonomics a bit.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D137347
2022-11-08 15:17:27 -05:00
Emilio Cota
17dbd80ff7 [mlir] Fix typo s/utilties/utilities/ (including in file name)
Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D136887
2022-10-27 17:14:33 -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
River Riddle
1ae60e044e [mlir] Making verification after parsing optional
This is very useful when you want to parse IR even if
its invalid (e.g. bytecode). It's also useful if you don't
want to pay the cost of verification in certain situations.

Differential Revision: https://reviews.llvm.org/D134847
2022-09-28 20:38:12 -07:00
rkayaith
4b27825ba3 [mlir-opt] Support parsing operations other than 'builtin.module' as top-level
This adds a `--no-implicit-module` option, which disables the insertion
of a top-level `builtin.module` during parsing. In this mode any op may
be top-level, however it's required that there be exactly one top-level
op in the source.

`parseSource{File,String}` now support `Operation *` as the container op
type, which disables the top-level-op-insertion behaviour.

Following patches will add the same option to the other tools as well.

Depends on D133644

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D133645
2022-09-27 21:13:47 -04:00
rkayaith
7e22179d38 [mlir-opt] Delay pass manager creation until after parsing
Currently the pass manager is created before parsing, which requires an
assumption that the top-level operation will be `builtin.module`.
Delaying the creation allows for using the parsed top-level operation as
the PassManager operation instead.

A followup change will allow for parsing top-level operations other than
`builtin.module`.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D133644
2022-09-27 20:34:20 -04:00
River Riddle
34300ee369 [mlir] Add fallback support for parsing/printing unknown external resources
This is necessary/useful for building generic tooling that can roundtrip external
resources without needing to explicitly handle them. For example, this allows
for viewing the resources encoded within a bytecode file without having to
explicitly know how to process them (e.g. making it easier to interact with a
reproducer encoded in bytecode).

Differential Revision: https://reviews.llvm.org/D133460
2022-09-13 11:39:20 -07:00
River Riddle
f3acb54c1b [mlir] Add initial support for a binary serialization format
This commit adds a new bytecode serialization format for MLIR.
The actual serialization of MLIR to binary is relatively straightforward,
given the very very general structure of MLIR. The underlying basis for
this format is a variable-length encoding for integers, which gets heavily
used for nearly all aspects of the encoding (given that most of the encoding
is just indexing into lists).

The format currently does not provide support for custom attribute/type
serialization, and thus always uses an assembly format fallback. It also
doesn't provide support for resources. These will be added in followups,
the intention for this patch is to provide something that supports the
basic cases, and can be built on top of.

https://discourse.llvm.org/t/rfc-a-binary-serialization-format-for-mlir/63518

Differential Revision: https://reviews.llvm.org/D131747
2022-08-22 00:36:26 -07:00