[Mlir-commits] [mlir] [MLIR] Add ODS support for generating helpers for dialect (discardable) attributes (PR #77024)

Mehdi Amini llvmlistbot at llvm.org
Fri Jan 5 08:05:57 PST 2024


================
@@ -181,6 +196,37 @@ static const char *const operationInterfaceFallbackDecl = R"(
                                       mlir::OperationName opName) override;
 )";
 
+/// The code block for the discardable attribute helper.
+static const char *const discardableAttrHelperDecl = R"(
+    /// Helper to manage the discardable attribute `{1}`.
+    class {0}AttrHelper {{
+      mlir::StringAttr name;
+    public:
+      static constexpr llvm::StringLiteral getNameStr() {{
+        return "{4}.{1}";
+      }
+      constexpr mlir::StringAttr getName() {{
+        return name;
+      }
+
+      {0}AttrHelper(mlir::MLIRContext *ctx)
+        : name(mlir::StringAttr::get(ctx, getNameStr())) {{}
+
+     {2} getAttr(::mlir::Operation *op) {{
+       return op->getAttrOfType<{2}>(getName());
+     }
+     void setAttr(::mlir::Operation *op, {2} val) {{
+       op->setAttr(getName(), val);
+     }
+   };
----------------
joker-eph wrote:

`hasAttr` does not read well to me: because the "has" apply to "op" but the method would be on this object. So you would have something like:

```
        flatWorkGroupSizeAttrHelper.hasAttr(op);
```

https://github.com/llvm/llvm-project/pull/77024


More information about the Mlir-commits mailing list