Using MLIR::Parser: minimum skeleton

Trying to build a graph visualizer and analyzer and for that need to start with an MLIR txt and byte code reader.

I got the following structure:

#include <iostream>
#include <iomanip>
#include <filesystem>

#include "mlir/Parser/Parser.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/BuiltinOps.h"
#include "llvm/Support/SourceMgr.h"

int main(int argc, char* argv[]) {
    mlir::MLIRContext context;
    llvm::SourceMgr sourceMgr;

    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <path-to-mlirbc-file>\n";
        return 1;
    }

    std::string filepath = argv[1];

    // Load your MLIR source into sourceMgr here.
    mlir::OwningOpRef<mlir::ModuleOp> module =
        mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &context);
    if (!module) {
        // Handle parsing error.
        return 1;
    }

    // Use the parsed module as needed.

    return 0;
}

It has all the right includes, but how do I find all the libraries that need to be loaded, and second, what is the CMake infrastructure that I need to pull in to name these libs symbolically? This must run across platforms, such as Windows/MacOS/Linux.

Ok, by looking in the lib directory of the LLVM build I could see all the libraries that were built.

I guessed I would need:
MLIRIR
MLIRDialect
MLIRParser

Some more googling also suggested
MLIRSupport

Then I created this CMakeLists.txt file:

# file (GLOB SOURCES "./*.cpp")
set(SOURCES cli.cpp matmul.cpp)

####
# Set dfa tools specific include directories
find_package(MLIR REQUIRED CONFIG)

message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})

list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
link_directories(${LLVM_BUILD_LIBRARY_DIR})
link_directories(${MLIR_BUILD_LIBRARY_DIR})
add_definitions(${LLVM_DEFINITIONS})

compile_all("true" "dfa" "dfa/tools" "${SOURCES}")

set(DFA_MLIR_IMPORTER dfa_mlir)
set(DFA_MLIR_SRC mlir_txt_importer.cpp)
add_executable(${DFA_MLIR_IMPORTER} ${DFA_MLIR_SRC})
message(STATUS "Add test ${DFA_MLIR_IMPORTER} from source ${DFA_MLIR_SRC}.")
target_link_libraries(${DFA_MLIR_IMPORTER} MLIRIR MLIRDialect MLIRParser MLIRSupport)  # Link to MLIR libs
set_target_properties(${DFA_MLIR_IMPORTER} PROPERTIES FOLDER "dfa/tools")

and that is giving me a working skeleton.
compile_all() is a macro for automating a GLOB into a set of tests.

I wasn’t able to figure out how to generalize the target_link_libraries so that it would associate programmatic targets to a set of MLIR libs, so I split it out explicitely.

Now let’s flesh this out.

In general there is a 1:1 mapping from the header path to a library: so without looking at the code I would say you’d need MLIRIR, MLIRParse, and LLVMSupport here :slight_smile:

Not sure I understand the question exactly, but have you seen this example: it shows how an external project can be setup to use MLIR.

1 Like