Add parser support for internal named attributes. These are attributes with names starting with ':'.

PiperOrigin-RevId: 235774810
This commit is contained in:
River Riddle 2019-02-26 12:52:51 -08:00 committed by jpienaar
parent bac3eece66
commit 2d4b0e2c00
4 changed files with 19 additions and 6 deletions

View File

@ -867,7 +867,7 @@ Syntax:
``` {.ebnf}
attribute-dict ::= `{` `}`
| `{` attribute-entry (`,` attribute-entry)* `}`
attribute-entry ::= bare-id `:` attribute-value
attribute-entry ::= `:`? bare-id `:` attribute-value
```
Attributes are the mechanism for specifying constant data in MLIR in places

View File

@ -1022,8 +1022,8 @@ void ModulePrinter::printOptionalAttrDict(ArrayRef<NamedAttribute> attrs,
SmallVector<NamedAttribute, 8> filteredAttrs;
for (auto attr : attrs) {
auto attrName = attr.first.strref();
// Never print attributes that start with a colon. These are internal
// attributes that represent location or other internal metadata.
// By default, never print attributes that start with a colon. These are
// attributes represent location or other internal metadata.
if (!printInternalAttributes && attrName.startswith(":"))
return;

View File

@ -1433,7 +1433,7 @@ ParseResult Parser::parseLocationInstance(llvm::Optional<Location> *loc) {
///
/// attribute-dict ::= `{` `}`
/// | `{` attribute-entry (`,` attribute-entry)* `}`
/// attribute-entry ::= bare-id `:` attribute-value
/// attribute-entry ::= `:`? bare-id `:` attribute-value
///
ParseResult
Parser::parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes) {
@ -1441,11 +1441,17 @@ Parser::parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes) {
return ParseFailure;
auto parseElt = [&]() -> ParseResult {
// Check for an internal attribute.
bool isInternalAttr = consumeIf(Token::colon);
// We allow keywords as attribute names.
if (getToken().isNot(Token::bare_identifier, Token::inttype) &&
!getToken().isKeyword())
return emitError("expected attribute name");
auto nameId = builder.getIdentifier(getTokenSpelling());
Identifier nameId =
isInternalAttr
? builder.getIdentifier(Twine(":" + getTokenSpelling()).str())
: builder.getIdentifier(getTokenSpelling());
consumeToken();
if (parseToken(Token::colon, "expected ':' in attribute list"))

View File

@ -1,4 +1,4 @@
// RUN: mlir-opt %s | FileCheck %s
// RUN: mlir-opt -mlir-print-internal-attributes %s | FileCheck %s
// CHECK-DAG: #map{{[0-9]+}} = (d0, d1, d2, d3, d4)[s0] -> (d0, d1, d2, d4, d3)
#map0 = (d0, d1, d2, d3, d4)[s0] -> (d0, d1, d2, d4, d3)
@ -802,3 +802,10 @@ func @unregistered_term(%arg0 : i1) -> i1 {
^bb1(%arg1 : i1):
return %arg1 : i1
}
// CHECK-LABEL: func @internal_attrs
func @internal_attrs()
// CHECK-NEXT: attributes {:internal.attr: 10
attributes {:internal.attr: 10} {
return
}