*.h.inc files are not built when building MLIR

Hi everyone,

I’m trying to build LLVM + MLIR from source on a Linux EC2 instance - everything builds with no errors, and all of the example binaries (toy, etc.) work fine.

I used the following commands to build MLIR:

> git clone --depth 1 https://github.com/llvm/llvm-project.git
> cd llvm-project
> mkdir build
> cd build
> cmake -G "Unix Makefiles" ../llvm -DLLVM_ENABLE_PROJECTS=mlir -DLLVM_BUILD_EXAMPLES=ON -DLLVM_TARGETS_TO_BUILD="Native" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON
> make -j8

I am currently working on creating a new dialect and a few simple passes for the dialect following the tutorial at MLIR — Defining a New Dialect || Math ∩ Programming, with some slight modifications (mostly just name changes), and am not using bazel.

When I try to create a binary like mlir-opt to test out my passes, I encounter the following error:

> [ec2-user@ip-172-31-65-76 ensemble-compilation]$ g++ ./ensemble-compilation/qe_opt.cpp -o qeOpt -I llvm-project/mlir/include -I ensemble_compilation/
> In file included from llvm-project/mlir/include/mlir/IR/UseDefLists.h:16,
>                  from llvm-project/mlir/include/mlir/IR/Value.h:17,
>                  from llvm-project/mlir/include/mlir/IR/AffineMap.h:18,
>                  from llvm-project/mlir/include/mlir/IR/BuiltinAttributeInterfaces.h:12,
>                  from llvm-project/mlir/include/mlir/IR/BuiltinTypes.h:12,
>                  from llvm-project/mlir/include/mlir/IR/OpImplementation.h:16,
>                  from llvm-project/mlir/include/mlir/IR/DialectImplementation.h:17,
>                  from ./ensemble-compilation/lib/Dialect/QuantumEnsemble/QuantumEnsembleDialect.h:6,
>                  from ./ensemble-compilation/qe_opt.cpp:1:
> llvm-project/mlir/include/mlir/IR/Location.h:137:10: fatal error: mlir/IR/BuiltinLocationAttributes.h.inc: No such file or directory
>   137 | #include "mlir/IR/BuiltinLocationAttributes.h.inc"
>       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> compilation terminated.

After looking through the llvm/mlir/include folder, I found that none of the .h.inc files were generated from the .td files - for example, see the screenshot below, which lists some the files in my llvm/mlir/include/mlir/IR folder. There are some .td files, but none of the associated .h.inc or .cpp.inc files are present.

Could anyone please let me know what I need to change in my setup to fix this?

Thank you in advance, and I apologize if this is a particularly egregious oversight on my part.

The generated headers would be in the build for rather than source sir, and I don’t see an “-I” for it. Are you using cmake for your passes or just manually compiling by hand?

Thank you for your quick reply.

Are you using cmake for your passes or just manually compiling by hand?

I am just manually compiling for now.

The generated headers would be in the build for rather than source sir

I do not see an mlir subfolder in the llvm/build/include folder:

Is it possible that I missed a step during the build process that is required to create the /build/include/mlir folder?

Typically, inc files generated by MLIR are located under the YOUR_BUILD_PATH/tools/mlir/include/mlir/* directory. You can check your project to see if it has this directory.

Thank you so much! This fixed the error I was getting. Could you please point me to a good tutorial that shows how to set up CMake for LLVM / MLIR projects? The current tutorial I am following (MLIR — Defining a New Dialect || Math ∩ Programming) uses bazel, which is a different build system.

I don’t know of any better tutorials either. I follow the implementation of other MLIR dialects to implement new dialects when startup.

Alright, thanks for letting me know. In that case, which dialects do you often look at when setting up a new project?

For me, Arith and SCF dialect is a good startup for this.

Alright, I will take a look. Thank you so much for your help!

FWIW, there is a CMake build for the tutorial, but it was not added until later articles and I didn’t explain it in the prose. You can see the CMakeLists for the dialects at HEAD, e.g., mlir-tutorial/lib/Dialect/Poly/CMakeLists.txt at main · j2kun/mlir-tutorial · GitHub

1 Like

Thank you for pointing this out! I have yet to learn how CMake works (which is why I am manually compiling everything for now), but I will definitely check this out once I spend some time on CMake.

cxy helped me solve the issues I was facing with the .h.inc and .cpp.inc files, but I ran into something else:

> [ec2-user@ip-172-31-65-76 ~]$ g++ ./ensemble-compilation/qe_opt.cpp -o qeOpt -I llvm-project/mlir/include -I ensemble-compilation/ -I llvm-project/build/tools/mlir/include -I llvm-project/llvm/include
> In file included from llvm-project/llvm/include/llvm/IR/Module.h:22,
>                  from llvm-project/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.h:36,
>                  from llvm-project/mlir/include/mlir/InitAllDialects.h:43,
>                  from ./ensemble-compilation/qe_opt.cpp:2:
> llvm-project/llvm/include/llvm/IR/Attributes.h: In static member function ‘static bool llvm::Attribute::isConstantRangeAttrKind(llvm::Attribute::AttrKind)’:
> llvm-project/llvm/include/llvm/IR/Attributes.h:108:20: error: ‘FirstConstantRangeAttr’ was not declared in this scope; did you mean ‘isConstantRangeAttrKind’?
>   108 |     return Kind >= FirstConstantRangeAttr && Kind <= LastConstantRangeAttr;
>       |                    ^~~~~~~~~~~~~~~~~~~~~~
>       |                    isConstantRangeAttrKind
> llvm-project/llvm/include/llvm/IR/Attributes.h:108:54: error: ‘LastConstantRangeAttr’ was not declared in this scope; did you mean ‘isConstantRangeAttrKind’?
>   108 |     return Kind >= FirstConstantRangeAttr && Kind <= LastConstantRangeAttr;
>       |                                                      ^~~~~~~~~~~~~~~~~~~~~
>       |                                                      isConstantRangeAttrKind
> In file included from llvm-project/llvm/include/llvm/IR/Module.h:25,
>                  from llvm-project/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.h:36,
>                  from llvm-project/mlir/include/mlir/InitAllDialects.h:43,
>                  from ./ensemble-compilation/qe_opt.cpp:2:
> llvm-project/llvm/include/llvm/IR/Function.h: In member function ‘bool llvm::Function::isCoroOnlyDestroyWhenComplete() const’:
> llvm-project/llvm/include/llvm/IR/Function.h:528:38: error: ‘CoroDestroyOnlyWhenComplete’ is not a member of ‘llvm::Attribute’
>   528 |     return hasFnAttribute(Attribute::CoroDestroyOnlyWhenComplete);
>       |                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
> llvm-project/llvm/include/llvm/IR/Function.h: In member function ‘void llvm::Function::setCoroDestroyOnlyWhenComplete()’:
> llvm-project/llvm/include/llvm/IR/Function.h:531:26: error: ‘CoroDestroyOnlyWhenComplete’ is not a member of ‘llvm::Attribute’
>   531 |     addFnAttr(Attribute::CoroDestroyOnlyWhenComplete);

It looks to me that something went wrong during my LLVM setup, which caused these issues? Did you face something similar when building your tutorial?

Again, I apologize for taking up everyone’s time with super simple issues - this is my first time attempting anything even slightly nontrivial in C++.