How to execute various MLIR programs?

Hi everyone. Now I have lots of MLIR programs that are represented by different dialects. I want to execute them to see if their behaviors are as expected.

Referring to the test/Integration/ directory in the codebase, the solution seems clear: I need to lower those ops to LLVM/SPIRV dialect with different pass sequences first, and then the mlir-cpu-runner to execute.

However, as a beginner to MLIR, the first step seems difficult to me:

It’s not easy to find the correct pass sequence for lowering. Some passes’ names are quite confusing to me, for example, the --convert-linalg-to-llvm pass, which I thought may lower the entire ops in linalg dialect, only has one ConversionPattern YieldOpConversion. For some passes, I don’t know when and where to use them. the pass sequence seems to be order-sensitive, which makes exploring space huge.

I also try to learn from tests in test/Integration/, but they are insufficient: the lowering process lacks bufferization and some dialects/ops ( index dialect, affine dialect, etc…) are not involved.

I believe these questions will be solved as I go deeper into MLIR in the future. But for now, my question is: is there a direct way to compile MLIR (containing ops of many dialects) to LLVM IR, just like compiling C to LLVM IR?

In general, no. There is more than one way to lower a mix of dialects to LLVM while respecting high-level semantic contracts. For example, the math dialect can be lowered to implementations of mathematical functions, intrinsics or libm calls. However, there is the --test-lower-to-llvm pass pipeline that lowers the commonly used subset of linalg/vector/memref/arithmetic dialect that should work for simple cases.

1 Like