This guide walks through the process of adding support for a new dialect to the MLIR Interpreter (MLI), using the Arith dialect as an example. How exciting! 🎉
Adding a new dialect requires modifications to several parts of the codebase:
- CMake configuration files
- Main interpreter source files
- Directory structure for the new dialect
- Registration of dialect and interpreter implementations
Update the following CMake files to include the new dialect:
target_link_libraries(MLIRInterpreter
PRIVATE
MLIRIR
MLIRFuncDialect
MLIRArithDialect # Add the new dialect
MLIRLLVMDialect
MLIRSupport
)
- Add the new source file:
add_library(MLIRInterpreterLib SHARED
Interpreter/InterpreterOpInterface.cpp
Interpreter/Dialects/Func/FuncInterpreter.cpp
Interpreter/Dialects/LLVM/LLVMInterpreter.cpp
Interpreter/Dialects/Arith/ArithInterpreter.cpp # Add new interpreter file
)
- Update target linkage:
target_link_libraries(MLIRInterpreterLib
PUBLIC
MLIRIR
MLIRArithDialect # Add new dialect
MLIRFuncDialect
MLIRLLVMDialect
MLIRSupport
)
add_subdirectory(Func)
add_subdirectory(LLVM)
add_subdirectory(Arith) # Add new dialect directory
target_link_libraries(mli
PRIVATE
MLIRInterpreterLib
MLIRIR
MLIRFuncDialect
MLIRLLVMDialect
MLIRArithDialect # Add new dialect
MLIRSupport
MLIRParser
MLIRTransforms
)
Modify src/MLI.cpp
to:
- Include the new dialect header:
#include "mlir/Dialect/Arith/IR/Arith.h" // May vary between dialects
#include "mlir/Interpreter/Dialects/ArithInterpreter.h"
- Register the dialect and interpreter:
context.getOrLoadDialect<mlir::arith::ArithDialect>();
interpreter.registerDialectInterpreter<mlir::ArithInterpreter>();
Add a new test directory under test/
for the dialect-specific tests:
if (ENABLE_TESTING)
add_subdirectory(bytecode)
add_subdirectory(llvm_ir)
add_subdirectory(mem_manager)
add_subdirectory(arith) # Add new test directory
endif()
The new dialect should follow this directory structure:
lib/Interpreter/Dialects/Arith/
├── ArithInterpreter.cpp
└── CMakeLists.txt
include/mlir/Interpreter/Dialects/
├── ArithInterpreter.cpp
├── FuncInterpreter.cpp
├── LLVMInterpreter.cpp
└── CMakeLists.txt
test/arith/
├── CMakeLists.txt
└── [test directories]
- Follow the existing pattern for interpreter implementation
- Add appropriate tests in the new test directory
- Make sure all new files are properly included in version control
- Missing library dependencies in CMake files
- Forgetting to register the dialect or interpreter in
MLI.cpp
- Incorrect include paths for new headers
- Missing test directory configuration
Remember to run the test suite after adding the new dialect to ensure everything works as expected.