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

    <tr>
        <th>Summary</th>
        <td>
            [mlir][ods] Failed assert in generated printer with optional attributes 
        </td>
    </tr>

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

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

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

<pre>
    ```
def TestOptionalAttr : TEST_Op<"optional_attr"> {
  let arguments = (ins OptionalAttr<I64Attr>:$attr);
  let assemblyFormat = "$attr attr-dict";  // no optional group
}
```

Fails assert when trying to print `test.optional_attr`:
```console
$ echo 'test.optional_attr' | mlir-opt
mlir-opt: /home/rk/tmp/llvm-project/llvm/include/llvm/Support/Casting.h:648: decltype(auto) llvm::dyn_cast(const From &) [To = mlir::StringAttr, From = mlir::Attribute]: Assertion `detail::isPresent(Val) && "dyn_cast on a non-existent value"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: mlir-opt
 #0 0x00007f978ac69113 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/rk/tmp/llvm-project/llvm/lib/Support/Unix/Signals.inc:569:13
 #1 0x00007f978ac67340 llvm::sys::RunSignalHandlers() /home/rk/tmp/llvm-project/llvm/lib/Support/Signals.cpp:105:18
 #2 0x00007f978ac695df SignalHandler(int) /home/rk/tmp/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
 #3 0x00007f978f16b420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007f978a53300b raise /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #5 0x00007f978a512859 abort /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:81:7
 #6 0x00007f978a512729 get_sysdep_segment_value /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:509:8
 #7 0x00007f978a512729 _nl_load_domain /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:970:34
 #8 0x00007f978a523fd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #9 0x00007f978d3a7140 mlir::AttributeStorage::getAbstractAttribute() const /home/rk/tmp/llvm-project/mlir/include/mlir/IR/AttributeSupport.h:134:5
#10 0x00007f978d3a7140 mlir::Attribute::getTypeID() /home/rk/tmp/llvm-project/mlir/include/mlir/IR/Attributes.h:68:37
#11 0x00007f978d3a7140 bool mlir::detail::StorageUserBase<mlir::StringAttr, mlir::Attribute, mlir::detail::StringAttrStorage, mlir::detail::AttributeUniquer, mlir::TypedAttr::Trait>::classof<mlir::Attribute>(mlir::Attribute) /home/rk/tmp/llvm-project/mlir/include/mlir/IR/StorageUniquerSupport.h:112:16
#12 0x00007f978d3a7140 llvm::CastInfo<mlir::StringAttr, mlir::Attribute const, void>::isPossible(mlir::Attribute) /home/rk/tmp/llvm-project/mlir/include/mlir/IR/Attributes.h:340:43
#13 0x00007f978d3a7140 llvm::DefaultDoCastIfPossible<mlir::StringAttr, mlir::Attribute const, llvm::CastInfo<mlir::StringAttr, mlir::Attribute const, void> >::doCastIfPossible(mlir::Attribute) /home/rk/tmp/llvm-project/llvm/include/llvm/Support/Casting.h:312:10
#14 0x00007f978d3a7140 decltype(auto) llvm::dyn_cast<mlir::StringAttr, mlir::Attribute>(mlir::Attribute const&) /home/rk/tmp/llvm-project/llvm/include/llvm/Support/Casting.h:649:10
#15 0x00007f978d3a7140 mlir::StringAttr mlir::Attribute::dyn_cast<mlir::StringAttr>() const /home/rk/tmp/llvm-project/mlir/include/mlir/IR/Attributes.h:128:10
#16 0x00007f978d3a7140 (anonymous namespace)::TestOpAsmInterface::getAlias(mlir::Attribute, llvm::raw_ostream&) const /home/rk/tmp/llvm-project/mlir/test/lib/Dialect/Test/TestDialect.cpp:81:31
#17 0x00007f978ae0524c mlir::LogicalResult (anonymous namespace)::AliasInitializer::generateAlias<mlir::Attribute>(mlir::Attribute, llvm::MapVector<llvm::StringRef, std::vector<mlir::Attribute, std::allocator<mlir::Attribute> >, llvm::DenseMap<llvm::StringRef, unsigned int, llvm::DenseMapInfo<llvm::StringRef, void>, llvm::detail::DenseMapPair<llvm::StringRef, unsigned int> >, std::vector<std::pair<llvm::StringRef, std::vector<mlir::Attribute, std::allocator<mlir::Attribute> > >, std::allocator<std::pair<llvm::StringRef, std::vector<mlir::Attribute, std::allocator<mlir::Attribute> > > > > >&) /home/rk/tmp/llvm-project/mlir/lib/IR/AsmPrinter.cpp:730:16
#18 0x00007f978ae0524c (anonymous namespace)::AliasInitializer::visit(mlir::Attribute, bool) /home/rk/tmp/llvm-project/mlir/lib/IR/AsmPrinter.cpp:695:17
#19 0x00007f978d39f0e7 mlir::Operation::getAttrs() /home/rk/tmp/llvm-project/mlir/include/mlir/IR/Operation.h:356:48
#20 0x00007f978d39f0e7 test::TestOptionalAttr::print(mlir::OpAsmPrinter&) /home/rk/tmp/llvm-project/build/tools/mlir/test/lib/Dialect/Test/TestOps.cpp.inc:54434:46
...
```

ODS should probably just error out on this asm format and point users to `($attr^)?` instead
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNWFtvozgU_jXJi1UEmEvykIe0abWVdtVq2tnXyGAn8Qxg1jadZn79HtsQIJt00pmptFVFwJdzvvP5XGxngu4Xk8Rv__3VxF9StkHPTOmHWnNRkWKptUQTvETPt0_P64d6gm8mYSja3jWBbvie4Fs0Sa-dCIQKphGR26ZklVYwe4Um4YxXCg2lgqT7JHJvt6BhEkZO2nyCx5KUYmVW7O-ELIluxYXtcGQeV5Tn2sK4RtB3B_-oEqhDibZSNLUTOUlX7cvYbve8I7xQVqHU6NuOVUjLPa-2SAtUS16B9sTXQI83ZgCk4OWR3FxUShSsbQ0jxPKdAHTpiflhCvTdoLLg8gp63JzDF7APFu1EyeBHfoWHLmt4FsV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