Types declaration in MLIR

I’m interested in declaring new types in MLIR. So I followed the tutorial in chapter 7, and one of the main steps in this is to override ParseType and PrintType functions. They were already overridden in “Dialect.h.inc”, and I want to know how this file was generated and in which file in Chapter 7 there’s the specification of it.

In fact, I know that we can declare our dialect from the .td file. For example, in the Toy documentation, we use Ops.td to specify our dialect. However, to generate the parser and printer for our types, we need to set the variable useDefaultTypePrinterParser to 1. Can anyone confirm this? Additionally, can someone show how we can generate the .inc file from the .td file?

I suspect that the tutorial may need to be updated, overriding these methods isn’t needed anymore they can be auto-generated now.

Okay, but for the *.inc files should I generate them manually for all my *.td files by the command line: ../llvm-project/build/bin/mlir-tblgen -gen-dialect-decls include/Adt/AdtDialect.td -I ../llvm-project/mlir/include/ -o build/include/Adt/and put them in my build dir, or it should be automatic ?

cmake should invoke mlir-tblgen for you.

I tried using mlir-tblgen in my CMake file located in the include directory, but it doesn’t seem to generate anything in the build directory. Is there a specific CMake command that I should run to automate this process? Please explain how you generate this file automatically ?
(This is my cmake in the “include” path)
mlir_tablegen(AdtDialect.h.inc -gen-op-decls)
mlir_tablegen(AdtDialect.cpp.inc -gen-op-defs)
add_mlir_dialect(AdtOps adt)
add_mlir_doc(AdtDialect AdtDialect Adt/ -gen-dialect-doc)
add_mlir_doc(AdtOps AdtOps Adt/ -gen-op-doc)

Is there an add_directory(include) in the CMakeLists.txt in the directory above?

Yes, I added add_subdirectory(include) in the CMake file located in the project repository.

The code seems to be missing two things:

  • a add_public_tablegen_target that actually creates the cmake target for running tablegen after mlir_tablegen that merely populates a variable,
  • a dependency from the library target that uses the .inc files on the target created by add_public_tablegen_target.

This is demonstrated in the toy tutorial, e.g. here llvm-project/CMakeLists.txt at main · llvm/llvm-project · GitHub.

The first point you mentioned, and I did add it to my CMake file in the include directory with the following line add_public_tablegen_target(AdtDialectIncGen). However, I’m afraid I didn’t understand your second point well because I did add a dependency for this target in the lib CMake file, as you can see in the screenshot. But despite that, the *.inc files are not generated automatically in the build directory.
(The build command I’m using: cmake -G Ninja -DLLVM_DIR=/home/aymaen/llvm-project/build/lib/cmake/llvm/ -DMLIR_DIR=/home/aymaen/llvm-project/build/lib/cmake/mlir/ ..)
Screenshot from 2023-06-07 12-09-14

This is extremely surprising. Are you sure that no .inc files are generated?

Can you remove the build directory and configure from scratch, then run ninja AdtDialectIncGen. This should generate the files requested by mlir_tablegen commands before add_public_tablegen_target. If it does, you will have to go up the dependency chain, erasing the build every time, to see at which point the generation breaks. For example, ninja MLIRAdt should trigger the generation as well. But we don’t know how the rest of your dependency graph looks and which target are you building.

1 Like

Yes, it’s very weird because after building my project, when I execute the command ninja MLIRAdt, all my files *.inc are generated correctly in the build directory.

Well, this sounds like your project doesn’t depend on MLIRAdt so the commands associated with that target are never run as part of building your project.

1 Like

Thank you! Now I can generate my .inc files using the following command: cmake --build . --target MLIRAdt. It successfully generates the corresponding files.

You are probably missing a add_dependencies in your codebase.

I don’t believe that add_dependencies is having any effect because even after adding it to my main CMake file like this:

add_executable(main lib/Adt/main.cpp)
add_dependencies(main MLIRAdt)

When I run cmake -G Ninja -DLLVM_DIR=/home/aymaen/llvm-project/build/lib/cmake/llvm/ -DMLIR_DIR=/home/aymaen/llvm-project/build/lib/cmake/mlir/ .., the .inc files are not generated. It seems like I need to run cmake --build . --target MLIRAdt every time to generate the .inc files.

If ninja main does not build the .inc files, then you are missing a dependency between main and the td building process.

> ninja clean
> ninja main

May give you issues, because .inc files are missing.
I never used it before, but ninja provides a web interface:

> ninja -t browse --port=8000 --no-browser mytarget

They are not supposed to be generated when you run cmake configuration, which is what the snippet above does. They are supposed to be generated when you are building the actual target, using either ninja MLIRAdt from within build or cmake --build build --target MLIRAdt from above build. I suppose if you run ninja main, they would be generated as expected.

1 Like