Skip to content

Commit 32243a5

Browse files
authored
[MLIR] Add DefaultValuedEnumAttr decorator (#172916)
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.
1 parent 2c98c6e commit 32243a5

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

mlir/docs/DefiningDialects/Operations.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ like `"0.5f"`, and an integer array default value should be specified as like
287287
The generated operation printing function will not print default-valued
288288
attributes when the attribute value is equal to the default.
289289

290+
For enum attributes, you can use `DefaultValuedEnumAttr<EnumAttr, EnumCase>`
291+
instead of `DefaultValuedAttr`. This allows specifying the default value using a
292+
TableGen `EnumCase` variable instead of a raw string. For example
293+
`DefaultValuedEnumAttr<SomeI64Enum, I64Case5>`.
294+
290295
#### Confining attributes
291296

292297
`ConfinedAttr` is provided as a general mechanism to help modelling further

mlir/include/mlir/IR/EnumAttr.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ class EnumAttrInfo<
278278
string parameterPrinter = ?;
279279
}
280280

281+
// Decorates an enum attribute to have a default (unvalidated) default value
282+
// from a specific enum case if not present. The default value is constructed as
283+
// the fully-qualified enum case symbol.
284+
class DefaultValuedEnumAttr<EnumAttrInfo attr, EnumCase case>
285+
: DefaultValuedAttr<attr, attr.cppType # "::" # case.symbol>;
286+
281287
// An attribute holding a single integer value.
282288
class IntEnum<string name, string summary, list<EnumCase> cases, int width>
283289
: EnumInfo<name,

mlir/test/lib/Dialect/Test/TestOpsSyntax.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ def FormatOptionalDefaultAttrs : TEST_Op<"format_optional_default_attrs"> {
432432
let assemblyFormat = "($str^)? ($sym^)? ($e^)? attr-dict";
433433
}
434434

435+
def FormatOptionalDefaultEnumAttrs : TEST_Op<"format_optional_default_enum_attr"> {
436+
let arguments = (ins DefaultValuedEnumAttr<SomeI64Enum, I64Case5>:$e);
437+
let assemblyFormat = "($e^)? attr-dict";
438+
}
439+
435440
def FormatOptionalWithElse : TEST_Op<"format_optional_else"> {
436441
let arguments = (ins UnitAttr:$isFirstBranchPresent);
437442
let assemblyFormat = "(`then` $isFirstBranchPresent^):(`else`)? attr-dict";

mlir/test/mlir-tblgen/op-format.mlir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ test.format_optional_default_attrs "foo" @foo case10
221221
// CHECK-NOT: case5
222222
test.format_optional_default_attrs "default" @default case5
223223

224+
// CHECK: test.format_optional_default_enum_attr case10
225+
test.format_optional_default_enum_attr case10
226+
227+
// CHECK: test.format_optional_default_enum_attr
228+
// CHECK-NOT: case5
229+
test.format_optional_default_enum_attr case5
230+
224231
//===----------------------------------------------------------------------===//
225232
// Format optional operands and results
226233
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)