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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR][linalg] Parser crashes on transpose
        </td>
    </tr>

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

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

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

<pre>
    My mlir lsp crashed when working with linalg.transpose. The issue I was can be reproduced with `mlir-opt`, which crashes when parsing this mlir:

```
module {
  func.func private @transpose(%arg0 : tensor<10x10xi32>){
    %0 = linalg.transpose permutation = [0, 1]
 }
}
```

This crash occurs for both `linalg.transpose` and `linalg.broadcast` ops. It looks like the reason is that this occurs is that the call to `buildIdentityRegion` implicitly requires that the op has at least 1 bb arg. https://github.com/llvm/llvm-project/blob/c2fbc701aaf826e2015a5dcab36e3ba792e7da7f/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp#L1780 Defined here https://github.com/llvm/llvm-project/blob/c2fbc701aaf826e2015a5dcab36e3ba792e7da7f/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp#L1737-L1744

Trace:

```
STLExtras.h:1272: ReferenceT llvm::detail::indexed_accessor_range_base<mlir::ValueRange, llvm::PointerUnion<const 
mlir::Value *, mlir::OpOperand *, mlir::detail::OpResultImpl *>, mlir::Value, mlir::Value, mlir::Value>::operator[](size_t) const [DerivedT = mlir::ValueRange, BaseT = llvm::PointerUnion<const mlir::Value *, mlir::OpOperand *, mlir::detail::OpResultImpl *>, T = mlir::Value, PointerT = mlir::Value, ReferenceT = mlir::Value]: Assertion `Index < size() && "invalid index for value 
range"' failed. 

 #0 0x00005ef82a40f887 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /iree/third_party/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:13 
 #1 0x00005ef82a40dad0 llvm::sys::RunSignalHandlers() /iree/third_party/llvm-project/llvm/lib/Support/Signals.cpp:106:18 
 #2 0x00005ef82a41020a SignalHandler(int) /iree/third_party/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1 
 #3 0x0000709d4f442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520) 
 #4 0x0000709d4f4969fc __pthread_kill_implementation ./nptl/./nptl/pthread_kill.c:44:76 
 #5 0x0000709d4f4969fc __pthread_kill_internal ./nptl/./nptl/pthread_kill.c:78:10
 #6 0x0000709d4f4969fc pthread_kill ./nptl/./nptl/pthread_kill.c:89:10
 #7 0x0000709d4f442476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x0000709d4f4287f3 abort ./stdlib/./stdlib/abort.c:81:7
 #9 0x0000709d4f42871b _nl_load_domain ./intl/./intl/loadmsgcat.c:1177:9
#10 0x0000709d4f439e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x00005ef82abe42c1 (./build/RelWithDebInfo/llvm-project/bin/mlir-opt+0x443d2c1)
#12 0x00005ef82aafc726 mlir::OpBuilder::InsertPoint::isSet() const /iree/third_party/llvm-project/mlir/include/mlir/IR/Builders.h:339:40
#13 0x00005ef82aafc726 mlir::OpBuilder::restoreInsertionPoint(mlir::OpBuilder::InsertPoint) /iree/third_party/llvm-project/mlir/include/mlir/IR/Builders.h:393:12
#14 0x00005ef82aafc726 mlir::OpBuilder::InsertionGuard::~InsertionGuard() /iree/third_party/llvm-project/mlir/include/mlir/IR/Builders.h:357:18
#15 0x00005ef82aafc726 buildGenericRegion(mlir::OpBuilder&, mlir::Location, mlir::Region&, mlir::ValueRange, mlir::ValueRange, llvm::function_ref<void (mlir::OpBuilder&, mlir::Location, mlir
::ValueRange)>) /iree/third_party/llvm-project/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp:916:1
#16 0x00005ef82ab04619 buildIdentityRegion(mlir::OpBuilder&, mlir::Location, mlir::Region&, mlir::ValueRange, mlir::ValueRange) /iree/third_party/llvm-project/mlir/lib/Dialect/L
inalg/IR/LinalgOps.cpp:0:3
#17 0x00005ef82ab04619 mlir::linalg::TransposeOp::parse(mlir::OpAsmParser&, mlir::OperationState&) /iree/third_party/llvm-project/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp:1780:3
#18 0x00005ef82e35e2cb llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#19 0x00005ef82e35e2cb llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#20 0x00005ef82e35e2cb (anonymous namespace)::CustomOpAsmParser::parseOperation(mlir::OperationState&) /iree/third_party/llvm-project/mlir/lib/AsmParser/Parser.cpp:1521:9
#21 0x00005ef82e35e2cb (anonymous namespace)::OperationParser::parseCustomOperation(llvm::ArrayRef<std::tuple<llvm::StringRef, unsigned int, llvm::SMLoc> >) /iree/third_party/ll
vm-project/mlir/lib/AsmParser/Parser.cpp:2017:19
#22 0x00005ef82e35e2cb (anonymous namespace)::OperationParser::parseOperation() /iree/third_party/llvm-project/mlir/lib/AsmParser/Parser.cpp:1174:10
#23 0x00005ef82e367f80 llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#24 0x00005ef82e367f80 llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#25 0x00005ef82e367f80 (anonymous namespace)::OperationParser::parseBlockBody(mlir::Block*) /iree/third_party/llvm-project/mlir/lib/AsmParser/Parser.cpp:2296:9
#26 0x00005ef82e367f80 (anonymous namespace)::OperationParser::parseBlock(mlir::Block*&) /iree/third_party/llvm-project/mlir/lib/AsmParser/Parser.cpp:2226:12
#27 0x00005ef82e365f22 llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#28 0x00005ef82e365f22 llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#29 0x00005ef82e365f22 (anonymous namespace)::OperationParser::parseRegionBody(mlir::Region&, llvm::SMLoc, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) /iree/third_party/l
lvm-project/mlir/lib/AsmParser/Parser.cpp:2184:7
#30 0x00005ef82e365f22 (anonymous namespace)::OperationParser::parseRegion(mlir::Region&, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) /iree/third_party/llvm-project/mlir/
lib/AsmParser/Parser.cpp:2121:7
#31 0x00005ef82e3644a9 llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#32 0x00005ef82e3644a9 llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#33 0x00005ef82e3644a9 (anonymous namespace)::CustomOpAsmParser::parseRegion(mlir::Region&, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) /iree/third_party/llvm-project/mli
r/lib/AsmParser/Parser.cpp:1785:9 
#34 0x00005ef82e3644ec (anonymous namespace)::CustomOpAsmParser::parseOptionalRegion(mlir::Region&, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) /iree/third_party/llvm-pro
ject/mlir/lib/AsmParser/Parser.cpp:1796:5 
#35 0x00005ef82e3227ef std::_Optional_base_impl<llvm::ParseResult, std::_Optional_base<llvm::ParseResult, true, true> >::_M_is_engaged() const /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../.
./../include/c++/11/optional:433:58 
#36 0x00005ef82e3227ef std::optional<llvm::ParseResult>::has_value() const /usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/optional:945:22 
#37 0x00005ef82e3227ef mlir::OptionalParseResult::has_value() const /iree/third_party/llvm-project/mlir/include/mlir/IR/OpDefinition.h:49:40 
#38 0x00005ef82e3227ef mlir::function_interface_impl::parseFunctionOp(mlir::OpAsmParser&, mlir::OperationState&, bool, mlir::StringAttr, llvm::function_ref<mlir::Type (mlir::Builder&, llvm::
ArrayRef<mlir::Type>, llvm::ArrayRef<mlir::Type>, mlir::function_interface_impl::VariadicFlag, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)>, mlir::Strin
gAttr, mlir::StringAttr) /iree/third_party/llvm-project/mlir/lib/Interfaces/FunctionImplementation.cpp:234:19
#39 0x00005ef82a939348 mlir::func::FuncOp::parse(mlir::OpAsmParser&, mlir::OperationState&) /iree/third_party/llvm-project/mlir/lib/Dialect/Func/IR/FuncOps.cpp:203:10
#40 0x00005ef82e35e2cb llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#41 0x00005ef82e35e2cb llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#42 0x00005ef82e35e2cb (anonymous namespace)::CustomOpAsmParser::parseOperation(mlir::OperationState&) /iree/third_party/llvm-project/mlir/lib/AsmParser/Parser.cpp:1521:9
#43 0x00005ef82e35e2cb (anonymous namespace)::OperationParser::parseCustomOperation(llvm::ArrayRef<std::tuple<llvm::StringRef, unsigned int, llvm::SMLoc> >) /iree/third_party/ll
vm-project/mlir/lib/AsmParser/Parser.cpp:2017:19
#44 0x00005ef82e35e2cb (anonymous namespace)::OperationParser::parseOperation() /iree/third_party/llvm-project/mlir/lib/AsmParser/Parser.cpp:1174:10
#45 0x00005ef82e367f80 llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#46 0x00005ef82e367f80 llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#47 0x00005ef82e367f80 (anonymous namespace)::OperationParser::parseBlockBody(mlir::Block*) /iree/third_party/llvm-project/mlir/lib/AsmParser/Parser.cpp:2296:9
#48 0x00005ef82e367f80 (anonymous namespace)::OperationParser::parseBlock(mlir::Block*&) /iree/third_party/llvm-project/mlir/lib/AsmParser/Parser.cpp:2226:12
#49 0x00005ef82e365d79 llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#50 0x00005ef82e365d79 llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#51 0x00005ef82e365d79 (anonymous namespace)::OperationParser::parseRegionBody(mlir::Region&, llvm::SMLoc, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) /iree/third_party/l
lvm-project/mlir/lib/AsmParser/Parser.cpp:2184:7
#52 0x00005ef82e365d79 (anonymous namespace)::OperationParser::parseRegion(mlir::Region&, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) /iree/third_party/llvm-project/mlir/
lib/AsmParser/Parser.cpp:2121:7
#53 0x00005ef82e3644a9 llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#54 0x00005ef82e3644a9 llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#55 0x00005ef82e3644a9 (anonymous namespace)::CustomOpAsmParser::parseRegion(mlir::Region&, llvm::ArrayRef<mlir::OpAsmParser::Argument>, bool) /iree/third_party/llvm-project/mli
r/lib/AsmParser/Parser.cpp:1785:9
#56 0x00005ef82cb8f249 llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
#57 0x00005ef82cb8f249 llvm::ParseResult::operator bool() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:124:53
#58 0x00005ef82cb8f249 mlir::ModuleOp::parse(mlir::OpAsmParser&, mlir::OperationState&) /iree/build/RelWithDebInfo/llvm-project/tools/mlir/include/mlir/IR/BuiltinOps.cpp.inc:293
:7
#59 0x00005ef82e35e2cb llvm::LogicalResult::failed() const /iree/third_party/llvm-project/llvm/include/llvm/Support/LogicalResult.h:43:43
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.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