First step in LLVM was to produce an LLVM IR using a command like
clang++ -O3 -emit-llvm -S matmul.cpp -o matmul.ll
but what is the first step in MLIR? How do I get my .cpp code appearing as a module with blocks with regions with ops (is that the correct hierarchy?) in the top dialect?
Do I have to rewrite my code in MLIR syntax myself?
Having a convo with an expert on LLVM, I was told:
Me:
“What is the point of MLIR if I cannot get MLIR representation of my own source code in Python, C++, etc?”
"to rewrite existing high-level language code (like Python or C++) directly into MLIR. Instead, the idea is to:
- Use frontend compilers (like Clang for C++) to convert the high-level language code to LLVM IR.
- Optionally, use tools (under active development) to translate LLVM IR to MLIR for advanced optimizations, especially those that LLVM might not handle well (like certain loop transformations or targeting specific accelerators).
- Lower the optimized MLIR back to LLVM IR or directly to machine code for execution.
- Current State of Tooling: As of now, tooling for converting LLVM IR to MLIR and vice versa is in development. This means that for many practical applications, especially in common programming environments, MLIR is not yet a drop-in solution for optimization.
- Why MLIR Exists: The purpose of MLIR is to explore and enable optimizations that are difficult or impossible to express in LLVM IR or high-level languages. It’s particularly useful for research, compiler development, and specialized domains like high-performance computing, machine learning, and hardware design.
- Relevance to General Programming: For most developers, especially those working in high-level languages for general-purpose applications, MLIR might not yet offer direct benefits. Its advantages become apparent in specialized areas and advanced compiler research.
In summary, MLIR isn’t intended to replace traditional compilers or make you rewrite existing high-level language code into MLIR directly. Its strengths lie in specialized optimization scenarios and as a research tool in compiler technology. For general programming needs, traditional compilation and optimization workflows remain the go-to approach."
Is that the gist of it?
So for example, I learn information on how to optimize matmul for matrices of varying sizes on my GTX 1650, then the final MLIR is what I have to read and then go back and refactor my CUDA C++ or other code that I wrote the original matmul.cpp in using the MLIR as a blueprint?