Skip to content

Commit e8c242a

Browse files
jeanPeriervdonaldson
authored andcommitted
[mlir][flang] Do not prevent integer types from being parsed as MLIR keywords
DialectAsmParser::parseKeyword is rejecting `'i' digit+` while it is a valid identifier according to mlir/docs/LangRef.md. Integer types actually used to be TOK_KEYWORD a while back before the change: llvm@6af866c. This patch Modifies `isCurrentTokenAKeyword` to return true for tokens that match integer types too. The motivation for this change is the parsing of `!fir.type<{` `component-name: component-type,`+ `}>` type in FIR that represent Fortran derived types. The component-names are parsed as keywords, and can very well be i32 or any ixxx (which are valid Fortran derived type component names). The Quant dialect type parser had to be modified since it relied on `iw` not being parsed as keywords. Differential Revision: https://reviews.llvm.org/D108913
1 parent 82c7d3d commit e8c242a

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

flang/test/Fir/fir-types.fir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ func private @it7() -> !fir.char<4,?>
2525
// CHECK-LABEL: func private @dvd4() -> !fir.type<derived4(p:i8){f:f32}>
2626
// CHECK-LABEL: func private @dvd5() -> !fir.type<derived5(p1:i8,p2:i8,p3:i8,p4:i8,p5:i8){f1:f32,f2:f32,f3:f32,f4:f32,f5:f32,f6:f32,f7:f32,f8:f32}>
2727
// CHECK-LABEL: func private @dvd6() -> !fir.type<derived6{f:!fir.ptr<!fir.type<derived6>>}>
28+
// CHECK-LABEL: func private @dvd7() -> !fir.type<derived_with_field_name_same_as_integer_type{i32:f32}>
2829
func private @dvd1() -> !fir.type<derived1>
2930
func private @dvd2() -> !fir.type<derived2(p:i32)>
3031
func private @dvd3() -> !fir.type<derived3{f:f32}>
3132
func private @dvd4() -> !fir.type<derived4(p:i8){f:f32}>
3233
func private @dvd5() -> !fir.type<derived5(p1:i8,p2:i8,p3:i8,p4:i8,p5:i8){f1:f32,f2:f32,f3:f32,f4:f32,f5:f32,f6:f32,f7:f32,f8:f32}>
3334
func private @dvd6() -> !fir.type<derived6{f:!fir.ptr<!fir.type<derived6>>}>
35+
func private @dvd7() -> !fir.type<derived_with_field_name_same_as_integer_type{i32:f32}>
3436

3537
// FIR array types
3638
// CHECK-LABEL: func private @arr1() -> !fir.array<10xf32>

mlir/lib/Dialect/Quant/IR/TypeParser.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ static IntegerType parseStorageType(DialectAsmParser &parser, bool &isSigned) {
2929
// Parse storage type (alpha_ident, integer_literal).
3030
StringRef identifier;
3131
unsigned storageTypeWidth = 0;
32-
if (failed(parser.parseOptionalKeyword(&identifier))) {
33-
// If we didn't parse a keyword, this must be a signed type.
34-
if (parser.parseType(type))
32+
OptionalParseResult result = parser.parseOptionalType(type);
33+
if (result.hasValue()) {
34+
if (!succeeded(*result)) {
35+
parser.parseType(type);
3536
return nullptr;
36-
isSigned = true;
37+
}
38+
isSigned = !type.isUnsigned();
3739
storageTypeWidth = type.getWidth();
38-
40+
} else if (succeeded(parser.parseKeyword(&identifier))) {
3941
// Otherwise, this must be an unsigned integer (`u` integer-literal).
40-
} else {
4142
if (!identifier.consume_front("u")) {
4243
parser.emitError(typeLoc, "illegal storage type prefix");
4344
return nullptr;
@@ -48,6 +49,8 @@ static IntegerType parseStorageType(DialectAsmParser &parser, bool &isSigned) {
4849
}
4950
isSigned = false;
5051
type = parser.getBuilder().getIntegerType(storageTypeWidth);
52+
} else {
53+
return nullptr;
5154
}
5255

5356
if (storageTypeWidth == 0 ||

mlir/lib/Parser/DialectSymbolParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class CustomDialectAsmParser : public DialectAsmParser {
250250

251251
/// Returns true if the current token corresponds to a keyword.
252252
bool isCurrentTokenAKeyword() const {
253-
return parser.getToken().is(Token::bare_identifier) ||
253+
return parser.getToken().isAny(Token::bare_identifier, Token::inttype) ||
254254
parser.getToken().isKeyword();
255255
}
256256

mlir/test/mlir-tblgen/types.mlir

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,27 @@ func @elements_attr_not_index() {
504504
"test.indexElementsAttr"() {attr = dense<[1, 2]>:tensor<2xi32>} : () -> ()
505505
return
506506
}
507+
508+
// -----
509+
510+
// CHECK-LABEL: @struct_success
511+
func @struct_success() {
512+
"test.simple_struct"() : () -> (!test.struct<{a, i32}, {b, f64}>)
513+
return
514+
}
515+
516+
// -----
517+
518+
// CHECK-LABEL: @struct_with_field_names_like_types
519+
func @struct_with_field_names_like_types() {
520+
"test.struct_with_field_names_like_types"() : () -> (!test.struct<{i32, i32}, {f64, f64}>)
521+
return
522+
}
523+
524+
// -----
525+
526+
func @struct_bad_keywords() {
527+
// expected-error@+1 {{expected valid keyword}}
528+
"test.struct_bad_keywords"() : () -> (!test.struct<{42, i32}>)
529+
return
530+
}

0 commit comments

Comments
 (0)