[Mlir-commits] [mlir] [MLIR] Add DefaultValuedEnumAttr decorator (PR #172916)
Henrich Lauko
llvmlistbot at llvm.org
Thu Dec 18 14:31:20 PST 2025
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/172916
>From e231e9530a34874cf8e35d1949ef60c6cb6d5741 Mon Sep 17 00:00:00 2001
From: xlauko <xlauko at mail.muni.cz>
Date: Thu, 18 Dec 2025 23:14:35 +0100
Subject: [PATCH] [MLIR] Add DefaultValuedEnumAttr decorator
Introduce DefaultValuedEnumAttr, which similarly to DefaultValuedAttr
decorates an enum attribute to have a default value from a specific enum
case when not present. The default is constructed as the fully-qualified
enum case symbol.
In comparison to DefaultValuedAttr, this allows using a TableGen EnumCase
variable instead of a raw string.
---
mlir/docs/DefiningDialects/Operations.md | 5 +++++
mlir/include/mlir/IR/EnumAttr.td | 6 ++++++
mlir/test/lib/Dialect/Test/TestOpsSyntax.td | 5 +++++
mlir/test/mlir-tblgen/op-format.mlir | 7 +++++++
4 files changed, 23 insertions(+)
diff --git a/mlir/docs/DefiningDialects/Operations.md b/mlir/docs/DefiningDialects/Operations.md
index 7c1be84727476..08fb08998437e 100644
--- a/mlir/docs/DefiningDialects/Operations.md
+++ b/mlir/docs/DefiningDialects/Operations.md
@@ -287,6 +287,11 @@ like `"0.5f"`, and an integer array default value should be specified as like
The generated operation printing function will not print default-valued
attributes when the attribute value is equal to the default.
+For enum attributes, you can use `DefaultValuedEnumAttr<EnumAttr, EnumCase>`
+instead of `DefaultValuedAttr`. This allows specifying the default value using a
+TableGen `EnumCase` variable instead of a raw string. For example
+`DefaultValuedEnumAttr<SomeI64Enum, I64Case5>`.
+
#### Confining attributes
`ConfinedAttr` is provided as a general mechanism to help modelling further
diff --git a/mlir/include/mlir/IR/EnumAttr.td b/mlir/include/mlir/IR/EnumAttr.td
index 4bc94809ed3ce..ec57626ebde65 100644
--- a/mlir/include/mlir/IR/EnumAttr.td
+++ b/mlir/include/mlir/IR/EnumAttr.td
@@ -278,6 +278,12 @@ class EnumAttrInfo<
string parameterPrinter = ?;
}
+// Decorates an enum attribute to have a default (unvalidated) default value
+// from a specific enum case if not present. The default value is constructed as
+// the fully-qualified enum case symbol.
+class DefaultValuedEnumAttr<EnumAttrInfo attr, EnumCase case>
+ : DefaultValuedAttr<attr, attr.cppType # "::" # case.symbol>;
+
// An attribute holding a single integer value.
class IntEnum<string name, string summary, list<EnumCase> cases, int width>
: EnumInfo<name,
diff --git a/mlir/test/lib/Dialect/Test/TestOpsSyntax.td b/mlir/test/lib/Dialect/Test/TestOpsSyntax.td
index 9a96fe8b11c14..73de8295380a6 100644
--- a/mlir/test/lib/Dialect/Test/TestOpsSyntax.td
+++ b/mlir/test/lib/Dialect/Test/TestOpsSyntax.td
@@ -432,6 +432,11 @@ def FormatOptionalDefaultAttrs : TEST_Op<"format_optional_default_attrs"> {
let assemblyFormat = "($str^)? ($sym^)? ($e^)? attr-dict";
}
+def FormatOptionalDefaultEnumAttrs : TEST_Op<"format_optional_default_enum_attr"> {
+ let arguments = (ins DefaultValuedEnumAttr<SomeI64Enum, I64Case5>:$e);
+ let assemblyFormat = "($e^)? attr-dict";
+}
+
def FormatOptionalWithElse : TEST_Op<"format_optional_else"> {
let arguments = (ins UnitAttr:$isFirstBranchPresent);
let assemblyFormat = "(`then` $isFirstBranchPresent^):(`else`)? attr-dict";
diff --git a/mlir/test/mlir-tblgen/op-format.mlir b/mlir/test/mlir-tblgen/op-format.mlir
index fa929c2df9d10..ac0931f18f042 100644
--- a/mlir/test/mlir-tblgen/op-format.mlir
+++ b/mlir/test/mlir-tblgen/op-format.mlir
@@ -221,6 +221,13 @@ test.format_optional_default_attrs "foo" @foo case10
// CHECK-NOT: case5
test.format_optional_default_attrs "default" @default case5
+// CHECK: test.format_optional_default_enum_attr case10
+test.format_optional_default_enum_attr case10
+
+// CHECK: test.format_optional_default_enum_attr
+// CHECK-NOT: case5
+test.format_optional_default_enum_attr case5
+
//===----------------------------------------------------------------------===//
// Format optional operands and results
//===----------------------------------------------------------------------===//
More information about the Mlir-commits
mailing list