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

    <tr>
        <th>Summary</th>
        <td>
            [Matrix] Crash in llvm::Instruction::eraseFromParent
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    `opt -p lower-matrix-intrinsics` on the IR below crashes (https://llvm.godbolt.org/z/13EM3Wfb5)


```
declare <8 x i32> @llvm.matrix.transpose.v8i32(<8 x i32>, i32 immarg, i32 immarg) #0

declare <1 x i32> @llvm.matrix.multiply.v1i32.v8i32.v8i32(<8 x i32>, <8 x i32>, i32 immarg, i32 immarg, i32 immarg) #0

define void @test(ptr %p, <8 x i32> %x) {
  %l = load <8 x i32>, ptr %p, align 4
  %t = tail call <8 x i32> @llvm.matrix.transpose.v8i32(<8 x i32> %x, i32 1, i32 8)
  %m = tail call <1 x i32> @llvm.matrix.multiply.v1i32.v8i32.v8i32(<8 x i32> %l, <8 x i32> %t, i32 1, i32 8, i32 1)
  %e = extractelement <1 x i32> %m, i64 0
 store i32 %e, ptr %p, align 4
  ret void
}
```

```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/opt -o /app/output.s -S -passes=lower-matrix-intrinsics <source>
1.      Running pass "function(lower-matrix-intrinsics<>)" on module "<source>"
2.      Running pass "lower-matrix-intrinsics<>" on function "test"
 #0 0x00000000051ca038 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x51ca038)
 #1 0x00000000051c7a2c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007e6703042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x0000000004f2919e llvm::Instruction::eraseFromParent() (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4f2919e)
 #4 0x0000000003eb1ea0 (anonymous namespace)::LowerMatrixIntrinsics::Visit() LowerMatrixIntrinsics.cpp:0:0
 #5 0x0000000003eb2b55 llvm::LowerMatrixIntrinsicsPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x3eb2b55)
 #6 0x0000000002fdc35e llvm::detail::PassModel<llvm::Function, llvm::LowerMatrixIntrinsicsPass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x2fdc35e)
 #7 0x0000000004fc9688 llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fc9688)
 #8 0x0000000000e45c8e llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe45c8e)
 #9 0x0000000004fc80ae llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fc80ae)
#10 0x0000000000e4533e llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0xe4533e)
#11 0x0000000004fc7ae0 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x4fc7ae0)
#12 0x000000000090f90a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x90f90a)
#13 0x0000000000901fa9 optMain (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x901fa9)
#14 0x00007e6703029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#15 0x00007e6703029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#16 0x00000000008f97fe _start (/opt/compiler-explorer/clang-assertions-trunk/bin/opt+0x8f97fe)
Program terminated with signal: SIGSEGV
Compiler returned: 139
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWEFv2zwS_TX0hbBBkbIsHXxQk_jbYBts0BTdY0BJI5lbihRIKnH21y9IybHsONmmzQcULVzEIjkzb968oUVya0WjANZo-QktL2e8d1tt1vWWb9Ws0NXTGiVEdw7POyz1I5h5y50Ru7lQzghlRWlRQrBW2G0BX3_BBUj9iEvD7RYsRjTdOtdZxHJEN4hupHxoF42uCi3dQpsG0c1_Ed1E7OqG_bsulohmiOTjJyHjh-QVlJIbwIhdpHiHBaOIXWEUk-BwwLRwhivbaQuLh9SvoOnRckQv_Dcs2pb7yMdPGUaUkSHyJFr0arS2l0508mnxEAlGh5CvB34Hkrdw1UIBftCi8mgcWIdo2jmDEV12L8L40V3wsPqESI79s8SIXWKpefUS0tQRl6JRON6buWDmuJC45FL-Uh1GWEOa0f5LOpQ-RGtfRvuIOoT8z7LkzsF5HnnGBQEX7JzhpQMJLSh3Co4u22C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