[mlir][OpAsmFormat] Add support for an "else" group on optional elements
authorRiver Riddle <riddleriver@gmail.com>
Tue, 23 Mar 2021 01:07:09 +0000 (18:07 -0700)
committerRiver Riddle <riddleriver@gmail.com>
Tue, 23 Mar 2021 01:19:23 +0000 (18:19 -0700)
commit6d6fe9ccc43d23286b764016bc8b5a4a3ab8f675
tree1bb9a5320f871e37164186a43142ad8ae69ee092
parent0524a09cc7e1a0797982feacf505825231efbee7
[mlir][OpAsmFormat] Add support for an "else" group on optional elements

The "else" group of an optional element is a collection of elements that get parsed/printed when the anchor of the main element group is *not* present. This is useful when there is a special syntax when an element is not present. The new syntax for an optional element is shown below:

```
optional-group: `(` elements `)` (`:` `(` else-elements `)`)? `?`
```

An example of how this might be used is shown below:

```tablegen
def FooOp : ... {
  let arguments = (ins UnitAttr:$foo);

  let assemblyFormat = "attr-dict (`foo_is_present` $foo^):(`foo_is_absent`)?";
}
```

would be formatted as such:

```mlir
// When the `foo` attribute is present:
foo.op foo_is_present

// When the `foo` attribute is not present:
foo.op foo_is_absent
```

Differential Revision: https://reviews.llvm.org/D99129
mlir/docs/OpDefinitions.md
mlir/test/lib/Dialect/Test/TestOps.td
mlir/test/mlir-tblgen/op-format-spec.td
mlir/test/mlir-tblgen/op-format.mlir
mlir/tools/mlir-tblgen/OpFormatGen.cpp