Writing a basic Conversion Pass

I am exploring MLIR, and am looking at writing my own conversion pass for the Linalg dialect, replacing matmul calls with something else. I am still to add the implementation of the pass, however I would like to be sure that the pass works as a stub, before developing it further.

I am following how LinalgToSPIRV appears to be doing it, however when I try and compile I get an error (see below). I’ve got all my changes as a single commit on the dev/external_pass branch.

/app/llvm-project-public/mlir/lib/Conversion/LinalgToExternal/LinalgToExternal.cpp:75:27: error: use of undeclared identifier 'ConvertLinalgToExternalPass'; did you mean 'ConvertLinalgToExternalBase'?
  return std::make_unique<ConvertLinalgToExternalPass>();
tools/mlir/include/mlir/Conversion/Passes.h.inc:1012:7: note: 'ConvertLinalgToExternalBase' declared here
class ConvertLinalgToExternalBase : public ::mlir::OperationPass<ModuleOp> {

To me, this suggests that the code generation step in Passes.h is not registering my pass properly. Is there something I’m missing?

To explain a bit more of what I’ve done:

  1. I have created a directory for my pass in lib, and added the directory to CMakeLists.txt, as well as having a basic C++ file. external_pass/mlir/lib/Conversion/LinalgToExternal

  2. I have also added my pass to the include directory, adding the header to mlir/include/mlir/Conversion/Passes.h, as well as to the tablegen file mlir/include/mlir/Conversion/Passes.td. mlir/include/mlir/Conversion/LinalgToExternal

Is there anything else I need here? Is my intuition about there being an issue with the tablegen for Passes.h reasonable, or is it more likely to be something else?

Adding your pass to Passes.td will result in a base class being generated (ConvertLinalgToExternalBase). To actually define your pass you will need to create a new class that inherits from this base class, i.e. define ConvertLinalgToExternalPass that inherits from ConvertLinalgToExternalBase<ConvertLinalgToExternalPass>.

The docs are here: Pass Infrastructure - MLIR
And here is where that happens for LinalgToSPRIV: llvm-project/LinalgToSPIRVPass.cpp at main · llvm/llvm-project · GitHub