Examples
The purpose of the examples is to give users a better understanding of how to use the passes and the interfaces in buddy-mlir. Currently, we provide three types of examples.
- IR level conversion and transformation examples.
- Domain-specific application level examples.
- Testing and demonstrating examples.
IR Level Examples
The IR level examples show how to use the passes in upstream MLIR and buddy-mlir, some of these examples come from the MLIR integration test. Most cases can be run directly with the MLIR JIT engine mlir-cpu-runner
. The lowering pipeline and the toolchain configuration are specified in makefile target. Before you start trying the IR level examples, please make sure you have completed the get started part.
Then you can find a Dialect that you are interested in, and you can go to the corresponding directory to find the target you want to run. The naming convention for target is <Dialect Name>-<Operation Name>-<Target Type>
. For most examples, we provide the
following targets:
-
Lower Target (
<Dialect Name>-<Operation Name>-lower
): the lower target is designed to show the lowering pipeline. You can also remove some of these passes to see the different generated MLIR code. The name of the output file islog.mlir
. -
Translate Target (
<Dialect Name>-<Operation Name>-translate
): the translate target is designed to show the LLVM IR that generate from MLIR. The name of the output file islog.ll
. -
Executable Target (
<Dialect Name>-<Operation Name>-run
): the executable target uses MLIR JIT engine to show the result of the cases.
You can run these targets with the make
command. Take an example with the memref.dim
operation, you can run the following command:
Feel free to send PR when you write a new example.
Domain-specific Application Level Examples
The domain-specific application level examples show how to use MLIR in domain applications and how to use the interfaces we provide. Currently, we provide the following examples. Before you start trying the application level examples, please make sure you have completed the get started part.
Convolution Vectorization Examples
So far, we provide the 2D convolution vectorization pass conv-vectorization
. The pass implements the Coefficients Broadcasting algorithm with Strip Mining strategy, and the strip mining size is configurable. Take the size of 256 as an example, you can use the tool with the following configuration.
- Conversion example
We provide a function with linalg.conv_2d
operation. You can use the following commands to print the conversion result.
- Edge detection example
We also provide an edge detection example to show the optimization.
The conv-vectorization
pass is responsible for lowering the linalg.conv_2d
with our algorithm. And then we use mlir-translate
and llc
tools to generate the object file. At last, we call the MLIR convolution function in a C++ program.
Please make sure OpenCV is installed to play around.
This example can also show the “magic” of AutoConfig mechanism that can help you specify the strip mining size
, ISA SIMD/Vector extension
, and target triple
. You only need to enable the BUDDY_EXAMPLES
option and don’t worry about the toolchain configuration.
Of course, you can also use your own configuration assigning values -DBUDDY_CONV_OPT_STRIP_MINING
(e.g. 64) and -DBUDDY_OPT_ATTR
(e.g. avx2).
We provide an image at buddy-mlir/examples/ConvOpt/images/YuTu.png
, which is the robotic lunar rover that formed part of the Chinese Chang’e 3 mission.
You can detect the edge of the image with edge-detection
.
We also provide the performance comparison between our buddy-opt
tool and other state-of-the-art approaches.
For more details, please see the benchamrk in the buddy-benchmark repo.
Digital Image Processing Examples
- Conversion example:
- Edge detection example:
Build and run the example.
Note: Please make sure OpenCV, libjpeg-dev and libpng-dev is installed to play around.
This example can also show the “magic” of AutoConfig mechanism that can help you specify the strip mining size
, ISA SIMD/Vector extension
, and target triple
. You only need to enable the BUDDY_EXAMPLES
option and don’t worry about the toolchain configuration.
To generate build files for these examples
Using Fast Fourier Transform:
Of course, you can also use your own configuration assigning values -DBUDDY_DIP_OPT_STRIP_MINING
(e.g. 64) and -DBUDDY_OPT_ATTR
(e.g. avx2).
Note: Maximum allowed value of BUDDY_DIP_OPT_STRIP_MINING
for producing correct result is equal to image width.
- Rotation example:
- Resize example:
- Morphological Operations example:
We also provide the performance comparison between our buddy-opt
tool and other state-of-the-art approaches.
For more details, please see the benchamrk in the buddy-benchmark repo.
Digital Audio Processing Examples
- Fir Lowpass example:
Build and run the example.
Note: No external library required.
This example shows how FIR is acheived using our library and MLIR-based convolution method. It uses basic lowering pipelines so performance is poor by default. Different windows could be applied and cutoff frequency could be altered. The result could be saved to any specified destination available, or saved to current working directory by default. Notice that you must specify input file first than the output destination could be specified.
Specify nothing to process default NASA audio.
- Biquad example:
Build and run the example.
Note: No external library required.
This example shows how Biquad is acheived using our library. The result could be saved to any specified destination available, or saved to current working directory by default. Notice that you must specify input file first than the output destination could be specified.
You can also use your own configuration assigning values -DBUDDY_DAP_OPT_VECTOR_SPLITTING
(e.g. 64) and -DBUDDY_OPT_ATTR
(e.g. avx2).
Specify nothing to process default NASA audio.
- Iir Lowpass example:
Build and run the example.
Note: No external library required.
This example shows how IIR is acheived using our library. The result could be saved to any specified destination available, or saved to current working directory by default. Notice that you must specify input file first than the output destination could be specified.
You can also use your own configuration assigning values -DBUDDY_DAP_OPT_VECTOR_SPLITTING
(e.g. 64) and -DBUDDY_OPT_ATTR
(e.g. avx2).
Specify nothing to process default NASA audio.
Testing and Demonstrating Examples
Example:
DSL Examples
We use Antlr as the frontend framework. Please make sure Java is installed in your environment. You can check it with $ java -version
.
- Build LLVM/MLIR with RTTI and EH enabled.
- Build buddy-mlir with
BUDDY_DSL_EXAMPLES
enabled.
- Run the example.
For example, you can run the example of constant printing through makefile target buddy-toy-constant-run
:
All Toy DSL executable targets can be found in the buddy-mlir/examples/ToyDSL/makefile
.