[Mlir-commits] [mlir] 2092d14 - [mlir] Change the syntax of dense arrays
Jeff Niu
llvmlistbot at llvm.org
Thu Aug 11 17:56:51 PDT 2022
Author: Jeff Niu
Date: 2022-08-11T20:56:42-04:00
New Revision: 2092d1438c36606d82df20fd36f16de033d31147
URL: https://github.com/llvm/llvm-project/commit/2092d1438c36606d82df20fd36f16de033d31147
DIFF: https://github.com/llvm/llvm-project/commit/2092d1438c36606d82df20fd36f16de033d31147.diff
LOG: [mlir] Change the syntax of dense arrays
Follow-up to D123774, where the syntax of dense arrays was discussed. It
was included that the syntax should be changed to `array<i32: 1, 2>`.
This patch changes the syntax but importantly preserves the `[1, 2]`
syntax when embedding these attributes in assembly formats through ODS.
Reviewed By: mehdi_amini, jpienaar
Differential Revision: https://reviews.llvm.org/D131738
Added:
Modified:
mlir/include/mlir/IR/BuiltinAttributes.td
mlir/lib/AsmParser/AttributeParser.cpp
mlir/lib/AsmParser/Parser.h
mlir/lib/AsmParser/TokenKinds.def
mlir/lib/IR/AsmPrinter.cpp
mlir/test/Dialect/LLVMIR/dynamic-gep-index.mlir
mlir/test/Dialect/LLVMIR/invalid.mlir
mlir/test/IR/attribute.mlir
mlir/test/IR/elements-attr-interface.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/IR/BuiltinAttributes.td b/mlir/include/mlir/IR/BuiltinAttributes.td
index 74bdc35dea367..293a7b9197707 100644
--- a/mlir/include/mlir/IR/BuiltinAttributes.td
+++ b/mlir/include/mlir/IR/BuiltinAttributes.td
@@ -176,9 +176,9 @@ def Builtin_DenseArrayBase : Builtin_Attr<
Examples:
```mlir
- [:i8]
- [:i32 10, 42]
- [:f64 42., 12.]
+ array<i8:>
+ array<i32: 10, 42>
+ array<f64: 42., 12.>
```
when a specific subclass is used as argument of an operation, the declarative
diff --git a/mlir/lib/AsmParser/AttributeParser.cpp b/mlir/lib/AsmParser/AttributeParser.cpp
index 8e827462ee218..817624980f89b 100644
--- a/mlir/lib/AsmParser/AttributeParser.cpp
+++ b/mlir/lib/AsmParser/AttributeParser.cpp
@@ -71,8 +71,6 @@ Attribute Parser::parseAttribute(Type type) {
// Parse an array attribute.
case Token::l_square: {
consumeToken(Token::l_square);
- if (consumeIf(Token::colon))
- return parseDenseArrayAttr();
SmallVector<Attribute, 4> elements;
auto parseElt = [&]() -> ParseResult {
elements.push_back(parseAttribute());
@@ -100,6 +98,10 @@ Attribute Parser::parseAttribute(Type type) {
case Token::kw_dense_resource:
return parseDenseResourceElementsAttr(type);
+ // Parse a dense array attribute.
+ case Token::kw_array:
+ return parseDenseArrayAttr(type);
+
// Parse a dictionary attribute.
case Token::l_brace: {
NamedAttrList elements;
@@ -833,15 +835,19 @@ class CustomAsmParser : public AsmParserImpl<AsmParser> {
} // namespace
/// Parse a dense array attribute.
-Attribute Parser::parseDenseArrayAttr() {
- auto typeLoc = getToken().getLoc();
- auto type = parseType();
- if (!type)
+Attribute Parser::parseDenseArrayAttr(Type type) {
+ consumeToken(Token::kw_array);
+ SMLoc typeLoc = getToken().getLoc();
+ if (parseToken(Token::less, "expected '<' after 'array'"))
+ return {};
+ if (!type &&
+ (!(type = parseType()) ||
+ parseToken(Token::colon, "expected ':' after dense array type")))
return {};
CustomAsmParser parser(*this);
Attribute result;
// Check for empty list.
- bool isEmptyList = getToken().is(Token::r_square);
+ bool isEmptyList = getToken().is(Token::greater);
if (auto intType = type.dyn_cast<IntegerType>()) {
switch (type.getIntOrFloatBitWidth()) {
@@ -901,10 +907,8 @@ Attribute Parser::parseDenseArrayAttr() {
emitError(typeLoc, "expected integer or float type, got: ") << type;
return {};
}
- if (!consumeIf(Token::r_square)) {
- emitError("expected ']' to close an array attribute");
+ if (parseToken(Token::greater, "expected '>' to close an array attribute"))
return {};
- }
return result;
}
diff --git a/mlir/lib/AsmParser/Parser.h b/mlir/lib/AsmParser/Parser.h
index 45c0ad9de1def..fd87ff9960661 100644
--- a/mlir/lib/AsmParser/Parser.h
+++ b/mlir/lib/AsmParser/Parser.h
@@ -274,7 +274,7 @@ class Parser {
Attribute parseDenseResourceElementsAttr(Type attrType);
/// Parse a DenseArrayAttr.
- Attribute parseDenseArrayAttr();
+ Attribute parseDenseArrayAttr(Type type);
/// Parse a sparse elements attribute.
Attribute parseSparseElementsAttr(Type attrType);
diff --git a/mlir/lib/AsmParser/TokenKinds.def b/mlir/lib/AsmParser/TokenKinds.def
index a184676cc2f25..2d746d902335d 100644
--- a/mlir/lib/AsmParser/TokenKinds.def
+++ b/mlir/lib/AsmParser/TokenKinds.def
@@ -82,6 +82,7 @@ TOK_PUNCTUATION(file_metadata_end, "#-}")
// this list and to cater to OCD.
TOK_KEYWORD(affine_map)
TOK_KEYWORD(affine_set)
+TOK_KEYWORD(array)
TOK_KEYWORD(attributes)
TOK_KEYWORD(bf16)
TOK_KEYWORD(ceildiv)
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index 42858ad53ebe9..4e04e661ca8bd 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -1860,11 +1860,11 @@ void AsmPrinter::Impl::printAttribute(Attribute attr,
}
} else if (auto denseArrayAttr = attr.dyn_cast<DenseArrayBaseAttr>()) {
typeElision = AttrTypeElision::Must;
- os << "[:" << denseArrayAttr.getType().getElementType();
+ os << "array<" << denseArrayAttr.getType().getElementType() << ":";
if (denseArrayAttr.size())
os << " ";
denseArrayAttr.printWithoutBraces(os);
- os << "]";
+ os << ">";
} else if (auto resourceAttr = attr.dyn_cast<DenseResourceElementsAttr>()) {
os << "dense_resource<";
printResourceHandle(resourceAttr.getRawHandle());
diff --git a/mlir/test/Dialect/LLVMIR/dynamic-gep-index.mlir b/mlir/test/Dialect/LLVMIR/dynamic-gep-index.mlir
index b71fa84e05096..9e14b1db3432b 100644
--- a/mlir/test/Dialect/LLVMIR/dynamic-gep-index.mlir
+++ b/mlir/test/Dialect/LLVMIR/dynamic-gep-index.mlir
@@ -6,7 +6,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<i1, dense<8> : ve
// CHECK: %[[C0:.+]] = llvm.mlir.constant(0 : i32)
%0 = llvm.mlir.constant(0 : i32) : i32
// CHECK: llvm.getelementptr %[[ARG0]][%[[C0]], 1, %[[ARG1]]]
- %1 = "llvm.getelementptr"(%arg0, %0, %arg1) {rawConstantIndices = [:i32 -2147483648, 1, -2147483648]} : (!llvm.ptr<struct<"my_struct", (struct<"sub_struct", (i32, i8)>, array<4 x i32>)>>, i32, i32) -> !llvm.ptr<i32>
+ %1 = "llvm.getelementptr"(%arg0, %0, %arg1) {rawConstantIndices = array<i32: -2147483648, 1, -2147483648>} : (!llvm.ptr<struct<"my_struct", (struct<"sub_struct", (i32, i8)>, array<4 x i32>)>>, i32, i32) -> !llvm.ptr<i32>
llvm.return
}
}
diff --git a/mlir/test/Dialect/LLVMIR/invalid.mlir b/mlir/test/Dialect/LLVMIR/invalid.mlir
index 6bd130dd6a578..457cc5dc76f39 100644
--- a/mlir/test/Dialect/LLVMIR/invalid.mlir
+++ b/mlir/test/Dialect/LLVMIR/invalid.mlir
@@ -148,7 +148,7 @@ func.func @gep_non_function_type(%pos : i64, %base : !llvm.ptr<f32>) {
func.func @gep_too_few_dynamic(%base : !llvm.ptr<f32>) {
// expected-error at +1 {{expected as many dynamic indices as specified in 'rawConstantIndices'}}
- %1 = "llvm.getelementptr"(%base) {rawConstantIndices = [:i32 -2147483648]} : (!llvm.ptr<f32>) -> !llvm.ptr<f32>
+ %1 = "llvm.getelementptr"(%base) {rawConstantIndices = array<i32: -2147483648>} : (!llvm.ptr<f32>) -> !llvm.ptr<f32>
}
// -----
@@ -414,7 +414,7 @@ func.func @insertvalue_wrong_nesting() {
func.func @insertvalue_invalid_type(%a : !llvm.array<1 x i32>) -> !llvm.array<1 x i32> {
// expected-error at +1 {{'llvm.insertvalue' op Type mismatch: cannot insert '!llvm.array<1 x i32>' into '!llvm.array<1 x i32>'}}
- %b = "llvm.insertvalue"(%a, %a) {position = [:i64 0]} : (!llvm.array<1 x i32>, !llvm.array<1 x i32>) -> !llvm.array<1 x i32>
+ %b = "llvm.insertvalue"(%a, %a) {position = array<i64: 0>} : (!llvm.array<1 x i32>, !llvm.array<1 x i32>) -> !llvm.array<1 x i32>
return %b : !llvm.array<1 x i32>
}
@@ -422,7 +422,7 @@ func.func @insertvalue_invalid_type(%a : !llvm.array<1 x i32>) -> !llvm.array<1
func.func @extractvalue_invalid_type(%a : !llvm.array<4 x vector<8xf32>>) -> !llvm.array<4 x vector<8xf32>> {
// expected-error at +1 {{'llvm.extractvalue' op Type mismatch: extracting from '!llvm.array<4 x vector<8xf32>>' should produce 'vector<8xf32>' but this op returns '!llvm.array<4 x vector<8xf32>>'}}
- %b = "llvm.extractvalue"(%a) {position = [:i64 1]}
+ %b = "llvm.extractvalue"(%a) {position = array<i64: 1>}
: (!llvm.array<4 x vector<8xf32>>) -> !llvm.array<4 x vector<8xf32>>
return %b : !llvm.array<4 x vector<8xf32>>
}
diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir
index 4b17194ec4f57..14c9002076bb9 100644
--- a/mlir/test/IR/attribute.mlir
+++ b/mlir/test/IR/attribute.mlir
@@ -522,34 +522,34 @@ func.func @simple_scalar_example() {
// CHECK-LABEL: func @dense_array_attr
func.func @dense_array_attr() attributes {
-// CHECK-SAME: emptyf32attr = [:f32],
- emptyf32attr = [:f32],
-// CHECK-SAME: emptyf64attr = [:f64],
- emptyf64attr = [:f64],
-// CHECK-SAME: emptyi16attr = [:i16],
- emptyi16attr = [:i16],
-// CHECK-SAME: emptyi1attr = [:i1],
- emptyi1attr = [:i1],
-// CHECK-SAME: emptyi32attr = [:i32],
- emptyi32attr = [:i32],
-// CHECK-SAME: emptyi64attr = [:i64],
- emptyi64attr = [:i64],
-// CHECK-SAME: emptyi8attr = [:i8],
- emptyi8attr = [:i8],
-// CHECK-SAME: f32attr = [:f32 1.024000e+03, 4.530000e+02, -6.435000e+03],
- f32attr = [:f32 1024., 453., -6435.],
-// CHECK-SAME: f64attr = [:f64 -1.420000e+02],
- f64attr = [:f64 -142.],
-// CHECK-SAME: i16attr = [:i16 3, 5, -4, 10],
- i16attr = [:i16 3, 5, -4, 10],
-// CHECK-SAME: i1attr = [:i1 true, false, true],
- i1attr = [:i1 true, false, true],
-// CHECK-SAME: i32attr = [:i32 1024, 453, -6435],
- i32attr = [:i32 1024, 453, -6435],
-// CHECK-SAME: i64attr = [:i64 -142],
- i64attr = [:i64 -142],
-// CHECK-SAME: i8attr = [:i8 1, -2, 3]
- i8attr = [:i8 1, -2, 3]
+// CHECK-SAME: emptyf32attr = array<f32:>,
+ emptyf32attr = array<f32:>,
+// CHECK-SAME: emptyf64attr = array<f64:>,
+ emptyf64attr = array<f64:>,
+// CHECK-SAME: emptyi16attr = array<i16:>,
+ emptyi16attr = array<i16:>,
+// CHECK-SAME: emptyi1attr = array<i1:>,
+ emptyi1attr = array<i1:>,
+// CHECK-SAME: emptyi32attr = array<i32:>,
+ emptyi32attr = array<i32:>,
+// CHECK-SAME: emptyi64attr = array<i64:>,
+ emptyi64attr = array<i64:>,
+// CHECK-SAME: emptyi8attr = array<i8:>,
+ emptyi8attr = array<i8:>,
+// CHECK-SAME: f32attr = array<f32: 1.024000e+03, 4.530000e+02, -6.435000e+03>,
+ f32attr = array<f32: 1024., 453., -6435.>,
+// CHECK-SAME: f64attr = array<f64: -1.420000e+02>,
+ f64attr = array<f64: -142.>,
+// CHECK-SAME: i16attr = array<i16: 3, 5, -4, 10>,
+ i16attr = array<i16: 3, 5, -4, 10>,
+// CHECK-SAME: i1attr = array<i1: true, false, true>,
+ i1attr = array<i1: true, false, true>,
+// CHECK-SAME: i32attr = array<i32: 1024, 453, -6435>,
+ i32attr = array<i32: 1024, 453, -6435>,
+// CHECK-SAME: i64attr = array<i64: -142>,
+ i64attr = array<i64: -142>,
+// CHECK-SAME: i8attr = array<i8: 1, -2, 3>
+ i8attr = array<i8: 1, -2, 3>
} {
// CHECK: test.dense_array_attr
test.dense_array_attr
diff --git a/mlir/test/IR/elements-attr-interface.mlir b/mlir/test/IR/elements-attr-interface.mlir
index d094edec24bfa..81fa495b45c7b 100644
--- a/mlir/test/IR/elements-attr-interface.mlir
+++ b/mlir/test/IR/elements-attr-interface.mlir
@@ -28,19 +28,19 @@ arith.constant dense<[10, 11, 12, 13, 14]> : tensor<5xi64>
arith.constant dense<> : tensor<0xi64>
// expected-error at below {{Test iterating `bool`: true, false, true, false, true, false}}
-arith.constant [:i1 true, false, true, false, true, false]
+arith.constant array<i1: true, false, true, false, true, false>
// expected-error at below {{Test iterating `int8_t`: 10, 11, -12, 13, 14}}
-arith.constant [:i8 10, 11, -12, 13, 14]
+arith.constant array<i8: 10, 11, -12, 13, 14>
// expected-error at below {{Test iterating `int16_t`: 10, 11, -12, 13, 14}}
-arith.constant [:i16 10, 11, -12, 13, 14]
+arith.constant array<i16: 10, 11, -12, 13, 14>
// expected-error at below {{Test iterating `int32_t`: 10, 11, -12, 13, 14}}
-arith.constant [:i32 10, 11, -12, 13, 14]
+arith.constant array<i32: 10, 11, -12, 13, 14>
// expected-error at below {{Test iterating `int64_t`: 10, 11, -12, 13, 14}}
-arith.constant [:i64 10, 11, -12, 13, 14]
+arith.constant array<i64: 10, 11, -12, 13, 14>
// expected-error at below {{Test iterating `float`: 10.00, 11.00, -12.00, 13.00, 14.00}}
-arith.constant [:f32 10., 11., -12., 13., 14.]
+arith.constant array<f32: 10., 11., -12., 13., 14.>
// expected-error at below {{Test iterating `double`: 10.00, 11.00, -12.00, 13.00, 14.00}}
-arith.constant [:f64 10., 11., -12., 13., 14.]
+arith.constant array<f64: 10., 11., -12., 13., 14.>
// Check that we handle an external constant parsed from the config.
// expected-error at below {{Test iterating `int64_t`: unable to iterate type}}
More information about the Mlir-commits
mailing list