<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64724>64724</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Invalid parser generated for optional parameter in attribute
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mortbopet
      </td>
    </tr>
</table>

<pre>
    git version: [5bf8de8](https://github.com/llvm/llvm-project/tree/5bf8de882ae96a53fdd15eef1682b9168d3d16a2)

In the following, `PathStepAttr` describes an attribute with an optional field `child` - this compiles successfully.

```tablegen
def PathDirection : I32EnumAttr<"PathDirection", "path direction", [
 I32EnumAttrCase<"Parent", 0, "parent">,
  I32EnumAttrCase<"Child", 1, "child">
]>;

def PathStepAttr : AttrDef<IbisDialect, "PathStep", [TypedAttrInterface]> {
  let description = "Used to describe a single step in a path";
  let parameters = (ins
 "PathDirection":$direction,
 AttributeSelfTypeParameter<"">:$type,
 OptionalParameter<"mlir::FlatSymbolRefAttr">:$child
  );
  let mnemonic = "step";
  let assemblyFormat = "`<` $direction ($child^)? `:` $type  `>`";
}
```

However, trying to pass what to my eyes is valid IR, fails:
```mlir
  %some_parent = ibis.path [#ibis.step<parent : !ibis.scoperef>]
  %some_child = ibis.path [#ibis.step<child @a : !ibis.scoperef>]

// fails with

round-trip.mlir:48:45: error: expected attribute value
 %some_child = ibis.path [#ibis.step<parent  : !ibis.scoperef>]
 ^
round-trip.mlir:48:47: error: failed to parse PathStepAttr parameter 'child' which is to be a `mlir::FlatSymbolRefAttr`
  %some_child = ibis.path [#ibis.step<parent  : !ibis.scoperef>]
```

If we instead insert some keyword before `$child`, e.g. `,`:
```td
  let assemblyFormat = "`<` $direction (`,` $child^)? `:` $type  `>`";
```

things parse successfully:
```mlir
  %some_parent = ibis.path [#ibis.step<parent : !ibis.scoperef>]
  %some_child = ibis.path [#ibis.step<child , @a : !ibis.scoperef>]
```

I presume that correct behavior here would either be that a valid parser is generated or that the tablegen backend fails to generate a valid parser for the assembly format specified.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVk-Po7gT_TTOpdQRmADJIYd0_uiX0280vXteGVyAdwxGtkk2335lY9Ik6t2e1pxWighxys-8V6-KYsaIukPckvSVpIcFG2yj9LZV2haqR7soFL9ta2HhgtoI1ZFkByR9TYtqzXFN0gOh68ba3pBkR-iJ0FMtbDMUy1K1hJ6kvExfL71Wf2JpCT1ZjUjoKYCsKcNNxtKk4jxOEas4W9NiE2drnvA4Y5TQDYkOJNqN13MHtkGolJTqKrqa0D2QLPrGbPNmsd9Zq0kWAUdTalGgAdYBs1aLYrAIV2Ebt6J6K1THJFQCJXcAZSMkdztfwDbCQKnaXkg0YIayRGOqQcrbcv4kJIvGj2WFxBq7cZljBe5pDkJj6U4Bp9o5ocduaP3jJXtC6UMIodTzoLRntgH-vJ6-jthzmD0zOEFp7GyIje5A02JyJHQf9n8MsPfcx_1x2F9Oa8kx0E0P7j55nWswkZ2k91zdzQErkuzPhTAHwaRPvIedgt-Z_Xbrkbst586irliJ40lA8ok1SLQho31Q9ODAfjfIwap7soGBEV0tEYzFHkQHDJygnsYDWM80a9GiNgFrLToTAj5KjrP3apaWSc_d5Kw3lJVj8m0CHpUNAvrt9tbj-87_Bwc-bWil0C482Z0ks2-3tlDyO1beNzOsMTuBkKuQR3pth63qRDkJZYLij1HMGGwLeTsp3TI7xTpPJ3tXCXPKTqL7uenRH3kCH7sLsY4fjEtHd52dR_LDU83MTfQ_dcULaucHq2-iq11Oe2YMXBtm3Y_2BnhDA8LAhUnB4fzdRVdMSN96HrG9hndtUqNa_GMsB89RFMIsfZ25uqKJ_-0VSvb3MGeDePynVD1qZ-ejM-YTrhfkU9gQtYrYp9Dh6pvpSNA3rfmfWg0df7Fa9Mtgl9XaXVKHjVor7W_-6rG0yGfd78LkgHeXf4VA0OV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