[Paper Review] High Performance Code Generation in MLIR: An Early Case Study with GEMM
This paper presents an early case study demonstrating that MLIR, a compiler infrastructure with affine and polyhedral abstractions, can automatically generate high-performance GEMM kernels competitive with hand-optimized libraries like OpenBLAS and MKL. By leveraging MLIR's memref, affine dialect, and polyhedral passes—including unroll-and-jam, tiling, and vectorization—it achieves 92% of peak performance on an Intel Skylake CPU, showing that modular, automated code generation is viable for high-performance linear algebra kernels.
This article is primarily meant to present an early case study on using MLIR, a new compiler intermediate representation infrastructure, for high-performance code generation. Aspects of MLIR covered in particular include memrefs, the affine dialect, and polyhedral utilities and pass infrastructure surrounding those. This article is also aimed at showing the role compiler infrastructure could play in generating code that is competitive with highly tuned manually developed libraries, albeit in a more modular, reusable, and automatable way.
Motivation & Objective
- To demonstrate that MLIR can automate the generation of high-performance GEMM kernels without hand-written assembly.
- To evaluate whether MLIR’s polyhedral and affine infrastructure can achieve performance competitive with hand-tuned libraries like OpenBLAS and MKL.
- To explore the feasibility of replacing manual optimization with a modular, reusable, and composable compiler-based approach for dense linear algebra kernels.
- To assess the role of MLIR’s pass infrastructure, including tiling, vectorization, and scalar replacement, in achieving near-peak performance.
Proposed method
- The authors use MLIR’s affine dialect to express matrix multiplication as an affine operation with explicit tiling parameters (e.g., M_R=3, N_R=16, K_U=4).
- They apply a sequence of MLIR passes: tiling, unroll-and-jam, vectorization, and scalar replacement, all operating on affine operations.
- The code generation pipeline starts from a high-level MLIR operation and progressively lowers through the MLIR infrastructure to target-specific code.
- Memref types with affine layout maps are used to express strides, transposes, and memory access patterns, enabling precise data layout analysis.
- The transformation sequence is encoded as a C++ pass, allowing programmatic exploration and automation of optimization choices.
- The final code is generated using MLIR’s infrastructure, avoiding hand-written C or inline assembly.
Experimental results
Research questions
- RQ1Can MLIR’s polyhedral and affine infrastructure automatically generate GEMM kernels that achieve performance close to hand-optimized libraries like OpenBLAS and MKL?
- RQ2How effective are MLIR’s built-in passes—such as unroll-and-jam, vectorization, and tiling—in achieving high performance without manual tuning?
- RQ3To what extent can MLIR’s modular infrastructure replace the need for hand-written assembly and domain-specific optimization expertise in high-performance linear algebra?
- RQ4How does MLIR handle non-uniform problem sizes (e.g., when tile dimensions do not divide matrix dimensions) without sacrificing performance?
- RQ5Can analytical models for tile parameters be effectively integrated into MLIR’s transformation pipeline to guide automatic performance optimization?
Key findings
- MLIR-generated GEMM kernels achieved 67.49 GFLOPS on an Intel i7-8700K CPU, representing 92% of the theoretical peak performance (75.2 GFLOPS).
- The performance of the MLIR-generated kernel matched that of OpenBLAS (67.49 GFLOPS) and was within 0.3% of MKL (67.70 GFLOPS).
- Clang and GCC with -O3 and -ffast-math achieved only 0.47 GFLOPS and 4.53 GFLOPS respectively, highlighting the limitations of general-purpose compilers.
- The polyhedral passes in MLIR—including unroll-and-jam and vectorization—were critical in achieving high performance, especially when combined with proper tiling.
- MLIR’s support for affine layout maps enabled clean modeling of strides, transposes, and memory access patterns, simplifying code generation.
- The study shows that MLIR can automate the entire optimization pipeline from high-level GEMM to high-performance code, reducing reliance on manual low-level tuning.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.