I32Attr and other attribute type constraints

I’m playing around with my own dialect and looking at providing type restrcitions on attributes. This is done in the TestOps.td but when I introduce to my operations I get a compiler error:

sources/mlir-hello/build/include/Hello/HelloOps.cpp.inc:640:42: error: ‘::mlir::Builder’ is an incomplete type (*this)->setAttr(getI32attrAttrName(), ::mlir::Builder((*this)->getContext()).getIntegerAttr(::mlir::Builder((*this)->getContext()).getIntegerType(32), attrValue));

What am I missing from my ODS to make this work correctly?

include "HelloDialect.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/BuiltinTypes.td"
include "mlir/IR/OpBase.td"

...

def NonNegIntAttrOp : Hello_Op<"non_negative_int_attr"> {
let arguments = (ins
ConfinedAttr<I32Attr, [IntNonNegative]>:$i32attr,
ConfinedAttr<I64Attr, [IntNonNegative]>:$i64attr
);
}

Thanks in advance

My mental model without knowing the implementation details is that when dealing with tablegen files and using them there are several steps:

  1. tablegen files specify various data and include files that behave like C header includes, textually including the file and its data
  2. Generated files are then created based on parsing of the tablegen files
  3. Generated files are then included in non-generated files and that file is compiled.

You are receiving an error in HelloOps.cpp.inc which means that tablegen successfully created something. This means that your file is syntactically correct, so you have finished steps 1 and 2.

This means that either:

  1. You are successfully generating bad code
  2. You are unsuccessfully using good generated code from your non-generated file.

The issue now is that you seem to be hitting problem #2. This problem is that generated code is not self-contained like you would normally expect from your .h and .cpp files. Instead, headers that are included in the non-generated code like here: llvm-project/TestDialect.cpp at main · llvm/llvm-project · GitHub are also sometimes important for the generated code. The generated code cannot be compiled on its own. So in your case, you need to take the file that is including HelloOps.cpp.inc, and before that, you need to include a header file that defines ::mlir::Builder and potentially update your build system to link the implementation.

Ah ok. I was expecting a missing include in my td ODS file to solve this, but i’ll look to solve via your route. Note that my mlir dialect executable compiles and runs fine without this attribute addition.

Thanks for the pointers @tpopp problems solved by including #include "mlir/IR/Builders.h" just prior to auto generated #include "Hello/HelloOps.cpp.inc"