Skip to content

Commit 913d44d

Browse files
authored
[mlir][IR] Fix enum attribute handling by using parseKeywordOrString instead of parseKeyword (#156662)
Change enum attribute parsing to handle special characters and multi-word identifiers. This allows enum attrs to use symbols like "+" and strings with separators like "dash-separated-sentence" instead of being limited to valid identifiers. This also aligns enum attribute parsing with how enums are already handled by the `FieldParser`: https://github.com/llvm/llvm-project/blob/main/mlir/tools/mlir-tblgen/EnumsGen.cpp#L108 Signed-off-by: Fabian Mora <[email protected]>
1 parent 5e924fa commit 913d44d

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

mlir/include/mlir/IR/EnumAttr.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ class IntEnumAttr<I intType, string name, string summary,
314314
// symbol is not valid.
315315
let parameterParser = [{[&]() -> ::mlir::FailureOr<}] # cppType # [{> {
316316
auto loc = $_parser.getCurrentLocation();
317-
::llvm::StringRef enumKeyword;
318-
if (::mlir::failed($_parser.parseKeyword(&enumKeyword)))
317+
std::string enumKeyword;
318+
if (::mlir::failed($_parser.parseKeywordOrString(&enumKeyword)))
319319
return ::mlir::failure();
320320
auto maybeEnum = }] # cppNamespace # "::" #
321321
stringToSymbolFnName # [{(enumKeyword);
@@ -436,9 +436,9 @@ class BitEnumAttr<I intType, string name, string summary,
436436
let parameterParser = [{[&]() -> ::mlir::FailureOr<}] # cppType # [{> {
437437
}] # cppType # [{ flags = {};
438438
auto loc = $_parser.getCurrentLocation();
439-
::llvm::StringRef enumKeyword;
439+
std::string enumKeyword;
440440
do {
441-
if (::mlir::failed($_parser.parseKeyword(&enumKeyword)))
441+
if (::mlir::failed($_parser.parseKeywordOrString(&enumKeyword)))
442442
return ::mlir::failure();
443443
auto maybeEnum = }] # cppNamespace # "::" #
444444
stringToSymbolFnName # [{(enumKeyword);

mlir/test/IR/array-of-attr.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ test.array_of_attr_op
66
a = [begin 0 : index end, begin 2 : index end],
77
// CHECK-SAME: [0, 1, -42, 42]
88
b = [0, 1, -42, 42],
9-
// CHECK-SAME: [a, b, b, a]
10-
c = [a, b, b, a]
9+
// CHECK-SAME: [a, b, b, a, "+"]
10+
c = [a, b, b, a, "+"]
1111

1212
// CHECK: test.array_of_attr_op
1313
// CHECK-SAME: a = [], b = [], c = []

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def TestEnum
4949

5050
def TestSimpleEnum : I32Enum<"SimpleEnum", "", [
5151
I32EnumCase<"a", 0>,
52-
I32EnumCase<"b", 1>
52+
I32EnumCase<"b", 1>,
53+
I32EnumCase<"Plus", 2, "+">,
54+
I32EnumCase<"LongString", 3, "dash-separated-sentence">,
5355
]> {
5456
let cppNamespace = "::test";
5557
}

mlir/test/mlir-tblgen/attr-or-type-format-roundtrip.mlir

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ attributes {
3232
// CHECK: #test.attr_with_optional_enum<a>
3333
attr_12 = #test.attr_with_optional_enum<a>,
3434
// CHECK: #test.attr_with_optional_enum<b>
35-
attr_13 = #test.attr_with_optional_enum<b>
35+
attr_13 = #test.attr_with_optional_enum<b>,
36+
// CHECK: #test<simple_enum"+">
37+
attr_14 = #test<simple_enum "+">,
38+
// CHECK: #test<simple_enum"dash-separated-sentence">
39+
attr_15 = #test<simple_enum "dash-separated-sentence">
3640
}
3741

3842
// CHECK-LABEL: @test_roundtrip_default_parsers_struct

mlir/test/mlir-tblgen/attr-or-type-format.td

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def AttrE : TestAttr<"TestH"> {
200200

201201
def TestEnum : I32EnumAttr<"TestEnum", "TestEnumType", [
202202
I32EnumAttrCase<"first", 0>,
203-
I32EnumAttrCase<"second", 1>
203+
I32EnumAttrCase<"second", 1>,
204204
]> {
205205
let genSpecializedAttr = 0;
206206
}
@@ -215,6 +215,20 @@ def EnumAttrA : EnumAttr<Test_Dialect, TestEnum, "EnumAttrA"> {
215215
let assemblyFormat = "custom<Foo>($value)";
216216
}
217217

218+
def TestEnumB : I32EnumAttr<"TestEnumB", "TestEnumType", [
219+
I32EnumAttrCase<"Plus", 0, "+">,
220+
I32EnumAttrCase<"LongString", 1, "dash-separated-sentence">,
221+
I32EnumAttrCase<"Other", 2>
222+
]> {
223+
let genSpecializedAttr = 0;
224+
}
225+
226+
// ATTR-LABEL: TestEnumBAttr::parse
227+
// ATTR: parseKeywordOrString(
228+
def EnumAttrB : EnumAttr<Test_Dialect, TestEnumB, "EnumAttrB"> {
229+
let assemblyFormat = "$value";
230+
}
231+
218232
/// Test type parser and printer that mix variables and struct are generated
219233
/// correctly.
220234

0 commit comments

Comments
 (0)