MLIR Passes v1.0
|
The following sections describe how expand this collection of MLIR passes by defining custom passes.
Into the MQSS, the passes are classified as transforms, decompositions and code generation passes. To define a new pass you have to register it into the include/Passes/Transforms.hpp
, include/Passes/Decompositions.hpp
or include/Passes/CodeGen.hpp
files, respectively.
Let say, you want to include a custom pass called CustomExamplePass
as part of the decompositions collection. The method to create the pass must be registered in the file include/Passes/Decompositions.hpp
as follows:
If the pass requires arguments, those have to be also declared into the signature of the method that creates the pass. For instance, we declare a CustomExampleArgumentPass
that receives the argument int value
.
Notice that the pass has to be declared into namespace mqss::opt
to be integrated as part of the MQSS.
This example serves as a very simple template for creating custom MLIR passes using QUAKE MLIR dialect and perform some general transformation. In this example, you can create a rewrite pattern that replaces Hadamard
operations with S
operations.
In order to be integrated into this project, the file CustomExamplePass.cpp
must be in the lib/Passes/Decompositions
, lib/Passes/Transforms
or lib/Passes/CodeGen
directory of this repository.
After including your pass in the project, you can build it as follows:
Once your pass is integrated into this project. You can use it to transform any given MLIR/Quake module. Assuming that your MLIR module is into a string named quakeModule
. First, you need to get the context and the module itself as follows:
Apart of getting context and module, the function extractMLIRContext
register the dialects to be used, in our case Quake dialect too. This tells to MLIR to recognize operations and functions belonging to Quake dialect.
Next, you have to declare a pass manager mlir::PassManager
and load your custom pass mqss::opt::createCustomExamplePass
as follows:
To apply your custom pass on the MLIR module mlirModule
, you have to run the pass manager as follows:
If your pass is successfully applied, you can dump your module to visualize the effects of your pass in the module, as follows: