[Mlir-commits] [mlir] ff5696c - Document CMake Changes Needed to Generate Attribute Code from TableGen

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Aug 15 14:30:18 PDT 2023


Author: max
Date: 2023-08-15T16:29:46-05:00
New Revision: ff5696cec96e7755d1a3ebb5fbbbfe7999c5386a

URL: https://github.com/llvm/llvm-project/commit/ff5696cec96e7755d1a3ebb5fbbbfe7999c5386a
DIFF: https://github.com/llvm/llvm-project/commit/ff5696cec96e7755d1a3ebb5fbbbfe7999c5386a.diff

LOG: Document CMake Changes Needed to Generate Attribute Code from TableGen

I've been struggling with generating the C++ class declarations and definitions for custom attributes from TableGen, as described on this documentation page: https://mlir.llvm.org/docs/DefiningDialects/AttributesAndTypes/#adding-a-new-attribute-or-type-definition

The code for custom types is automatically generated when the MLIR Dialect is added with `add_mlir_dialect()` in the CMake file.

The same is not the case for custom attributes. I think people could benefit from learning how to adjsut their CMakeLists.txt to automatically generate the classes as described on that documentation page. This change adds the necessary information for this.

makslevental on Discord was so kind to help me figure this out myself

Reviewed By: makslevental

Differential Revision: https://reviews.llvm.org/D155249

Added: 
    

Modified: 
    mlir/docs/DefiningDialects/AttributesAndTypes.md

Removed: 
    


################################################################################
diff  --git a/mlir/docs/DefiningDialects/AttributesAndTypes.md b/mlir/docs/DefiningDialects/AttributesAndTypes.md
index e9f13e7cecb15d..0302d274a65387 100644
--- a/mlir/docs/DefiningDialects/AttributesAndTypes.md
+++ b/mlir/docs/DefiningDialects/AttributesAndTypes.md
@@ -48,6 +48,10 @@ describes the process for defining both Attributes and Types side-by-side with
 examples for both. If necessary, a section will explicitly call out any
 distinct 
diff erences.
 
+One 
diff erence is that generating C++ classes from declarative TableGen 
+definitions will require adding additional targets to your `CMakeLists.txt`.
+This is not necessary for custom types. The details are outlined further below.
+
 ### Adding a new Attribute or Type definition
 
 As described above, C++ Attribute and Type objects in MLIR are value-typed and
@@ -168,6 +172,26 @@ was provided to `MyDialect_Attr` and `MyDialect_Type`. For the definitions we
 added above, we would get C++ classes named `IntegerType` and `IntegerAttr`
 respectively. This can be explicitly overridden via the `cppClassName` field.
 
+### CMake Targets
+
+If you added your dialect using `add_mlir_dialect()` in your `CMakeLists.txt`,
+the above mentioned classes will automatically get generated for custom 
+_types_. They will be output in a file named `<Your Dialect>Types.h.inc`. 
+
+To also generate the classes for custom _attributes_, you will need to add
+two additional TableGen targets to your `CMakeLists.txt`: 
+
+```cmake
+mlir_tablegen(<Your Dialect>AttrDefs.h.inc -gen-attrdef-decls 
+              -attrdefs-dialect=<Your Dialect>)
+mlir_tablegen(<Your Dialect>AttrDefs.cpp.inc -gen-attrdef-defs 
+              -attrdefs-dialect=<Your Dialect>)
+add_public_tablegen_target(<Your Dialect>AttrDefsIncGen)
+```
+
+The generated `<Your Dialect>AttrDefs.h.inc` will need to be included whereever
+you are referencing the custom attribute types.
+
 ### Documentation
 
 The `summary` and `description` fields allow for providing user documentation


        


More information about the Mlir-commits mailing list