<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57475>57475</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] Update passes to leverage the new autogenerated files capabilities
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir:core
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mscuttari
      </td>
    </tr>
</table>

<pre>
    With 32c5578 and 13ed6958df40b85fcc80250bb3f819863904ecee, the passes declared through tablegen started emitting individual guards for the pass declarations and definitions, together with an additional options struct that can be used to configure the pass itself through C++.
With 67d0d7a I have updated all the existing passes so they leverage the new macros, dropped the old autogenerated code and at the same time erased almost all the `PassDetail.h` files, which are not needed anymore.
However, there is still some work to do and it is too much to be covered by myself alone, so I am here asking for help.

## Pass names and constructors
Constructors are now optional in tablegen, and will have a fallback naming pattern in case they are not specified.
In order to keep things working as much as possible after these three commits, I've decided to keep them explicit, and also the pass names have been kept untouched.
However, a lot of constructors are trivial and can be autogenerated (also wrt the pass options topic, which we will examine later).
If we consider for example the `Arithmetic -> LLVM` conversion pass, removing the explicit constructor from tablegen would lead to a default constructor named `createConvertArithmeticToLLVM`. This may sound good, but I feel like `createConvertArithmeticToLLVMPass`, or even `createArithmeticToLLVMConversionPass`, would sound better. For this specific example, this requires changing the name of the pass from `ConvertArithmeticToLLVM` to `ArithmeticToLLVMConversionPass`, in case we adopt the last naming convention. When changing the pass name, also the pass base class name will change and thus the implementation code has to be updated. Also, if the constructor is autogenerated, the actual pass class must have the same name of the pass itself (see the example).

## Constructors and namespaces
This is not a core topic for this issue, but I feel it should be solved while doing this work in order to avoid redoing the same things again and again.
While patching the codebase, I've noticed that some passes live in a namespace that is nested inside the `mlir` one. This convention is adopted everywhere (at least from what I remember) among dialect specific passes (e.g. linalg transforms), but it is not among the conversion ones. Some of them have a dedicated namespace, others don't. If I'd have to choose, I would like to keep dialect transformation passes into the specialized namespaces (as they already are), but I would avoid specific namespaces for passes that are explicitely thought to "link" two different dialects (such as for conversions).
Independently from my opinion, it must be noted that the constructor must be explicitely defined in tablegen (and also declared and defined in C++) if it leaves in a different namespace with respect to the registration code (i.e. the one enabled with the `GEN_PASS_REGISTRATION` macro). Also for this reason I would prefer the conversion passes to live under the plain `mlir` namespace.

## Pass options
Pass options declared in tablegen now lead to an options struct that can be passed to the pass base class constructor.
Again, the code has been updated to use the new macros but the options structs are still not used. So, the task would be to update the pass implementations and make them inheriting the base class constructors, among which there will be also the options-accepting one (if the pass has at least one option). If the constructor has been declared explicitely, also a options-accepting method has to be defined.
It must be noted that some passes already have an options struct that was used to overcome the lack of this changes. In such cases, the existing struct should be replaced with the new autogenerated one.

## Example
Here is an example to sum things up. Documentation can be found [here](https://mlir.llvm.org/docs/PassManagement/#declarative-pass-specification) (feel free to improve it if not clear enough).
```c++
namespace mlir {
/** PassName.h */
#define GEN_PASS_DECL_PASSNAME
#include "Passes.h.inc"

// If constructor is explicit
std::unique_ptr<mlir::Pass> createPassName(); // Explicit constructor is needed if mlir::Pass has to be replaced with, for example, mlir::OperationPass<>

// If constructor is explicit and pass has options
std::unique_ptr<mlir::Pass> createPassName(const PassNameOptions &options); // Explicit constructor is needed if mlir::Pass has to be replaced with, for example, mlir::OperationPass<>
}


/** Passes.h */
#include "PassName.h"

namespace mlir {
#define GEN_PASS_REGISTRATION
#include "Passes.h.inc"
}


/** PassName.cpp */
namespace mlir {
#define GEN_PASS_DEF_PASSNAME
#include "Passes.h.inc"
}

// Name of the class must match the tablegen name if the constructor is autogenerated
struct PassName : public mlir::impl::PassNameBase {
  using PassNameBase::PassNameBase;
  ...
}

// If the constructor is explicit
std::unique_ptr<mlir::Pass> createPassName() {
  ...
}

std::unique_ptr<mlir::Pass> createPassName(const mlir::PassNameOptions& options) {
  ...
}
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNWE1z2zg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