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

    <tr>
        <th>Summary</th>
        <td>
            Wasm EH errors out after FixIrreducibleControlFlow
        </td>
    </tr>

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

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

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

<pre>
    test.ll:
```ll
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target triple = "wasm32-unknown-emscripten"

declare i32 @__gxx_wasm_personality_v0(...)

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.wasm.get.exception(token) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare i32 @llvm.wasm.get.ehselector(token) #0

declare void @foo()

define void @test() personality ptr @__gxx_wasm_personality_v0 {
bb0:
  invoke void @foo()
          to label %bb1 unwind label %bb4

bb1: ; preds = %bb7, %bb0
  unreachable

bb2: ; preds = %bb7, %bb5, %bb4
  %catchswitch0 = catchswitch within none [label %bb3] unwind to caller

bb3: ; preds = %bb2
  %catchpad0 = catchpad within %catchswitch0 [ptr null]
  %t0 = call ptr @llvm.wasm.get.exception(token %catchpad0)
  %t1 = call i32 @llvm.wasm.get.ehselector(token %catchpad0)
  catchret from %catchpad0 to label %bb7

bb4:                                              ; preds = %bb0
  %catchswitch1 = catchswitch within none [label %bb5] unwind label %bb2

bb5:                                              ; preds = %bb4
  %catchpad1 = catchpad within %catchswitch1 [ptr null]
  %t2 = call ptr @llvm.wasm.get.exception(token %catchpad1)
  %t3 = call i32 @llvm.wasm.get.ehselector(token %catchpad1)
  invoke void @foo() [ "funclet"(token %catchpad1) ]
          to label %bb6 unwind label %bb2

bb6:                                              ; preds = %bb5
  catchret from %catchpad1 to label %bb7

bb7: ; preds = %bb6, %bb3
  invoke void @foo()
 to label %bb1 unwind label %bb2
}

attributes #0 = { nocallback nofree nosync nounwind willreturn }
```
![image](https://github.com/llvm/llvm-project/assets/8726997/ae1933ff-ac4c-4e61-919a-ac2ed6f94c46)

```console
$ llc -mattr=+exception-handling -wasm-enable-eh -exception-model=wasm -verify-machineinstrs test.ll

`*** Bad machine code: MBB has unexpected successors which are not branch targets, fallthrough, EHPads, or inlineasm_br targets. ***
- function:    test
- basic block: %bb.3 bb5 (0x560373183d28)

*** Bad machine code: MBB has unexpected successors which are not branch targets, fallthrough, EHPads, or inlineasm_br targets. ***
- function:    test
- basic block: %bb.6 bb7 (0x560373183f28)
LLVM ERROR: Found 2 machine code errors.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /usr/local/google/home/aheejin/llvm-git/install.debug/bin/llc -mattr=+exception-handling -wasm-enable-eh -exception-model=wasm -verify-machineinstrs min.ll
1.      Running pass 'Function Pass Manager' on module 'min.ll'.
2.      Running pass 'Verify generated machine code' on function '@test'
 #0 0x00007f6978d9628a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /usr/local/google/home/aheejin/llvm-git/llvm/lib/Support/Unix/Signals.inc:602:11
 #1 0x00007f6978d9643b PrintStackTraceSignalHandler(void*) /usr/local/google/home/aheejin/llvm-git/llvm/lib/Support/Unix/Signals.inc:675:1
 #2 0x00007f6978d94c46 llvm::sys::RunSignalHandlers() /usr/local/google/home/aheejin/llvm-git/llvm/lib/Support/Signals.cpp:104:5
 #3 0x00007f6978d96a45 SignalHandler(int) /usr/local/google/home/aheejin/llvm-git/llvm/lib/Support/Unix/Signals.inc:413:1
 #4 0x00007f697885af90 (/lib/x86_64-linux-gnu/libc.so.6+0x3bf90)
 #5 0x00007f69788a9ccc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #6 0x00007f697885aef2 raise ./signal/../sysdeps/posix/raise.c:27:6
 #7 0x00007f6978845472 abort ./stdlib/abort.c:81:7
 #8 0x00007f6978c66dc4 llvm::report_fatal_error(llvm::Twine const&, bool) /usr/local/google/home/aheejin/llvm-git/llvm/lib/Support/ErrorHandling.cpp:125:5
 #9 0x00007f697c9ef832 (anonymous namespace)::MachineVerifierPass::runOnMachineFunction(llvm::MachineFunction&) /usr/local/google/home/aheejin/llvm-git/llvm/lib/CodeGen/MachineVerifier.cpp:315:7
#10 0x00007f697c86981f llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /usr/local/google/home/aheejin/llvm-git/llvm/lib/CodeGen/MachineFunctionPass.cpp:91:8
#11 0x00007f69795e886a llvm::FPPassManager::runOnFunction(llvm::Function&) /usr/local/google/home/aheejin/llvm-git/llvm/lib/IR/LegacyPassManager.cpp:1435:23
#12 0x00007f69795ed612 llvm::FPPassManager::runOnModule(llvm::Module&) /usr/local/google/home/aheejin/llvm-git/llvm/lib/IR/LegacyPassManager.cpp:1481:16
#13 0x00007f69795e9129 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /usr/local/google/home/aheejin/llvm-git/llvm/lib/IR/LegacyPassManager.cpp:1550:23
#14 0x00007f69795e8c9a llvm::legacy::PassManagerImpl::run(llvm::Module&) /usr/local/google/home/aheejin/llvm-git/llvm/lib/IR/LegacyPassManager.cpp:535:16
#15 0x00007f69795ed8f1 llvm::legacy::PassManager::run(llvm::Module&) /usr/local/google/home/aheejin/llvm-git/llvm/lib/IR/LegacyPassManager.cpp:1677:3
#16 0x000056037167d3a7 compileModule(char**, llvm::LLVMContext&) /usr/local/google/home/aheejin/llvm-git/llvm/tools/llc/llc.cpp:759:41
#17 0x000056037167b6ed main /usr/local/google/home/aheejin/llvm-git/llvm/tools/llc/llc.cpp:420:13
#18 0x00007f697884618a __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#19 0x00007f6978846245 call_init ./csu/../csu/libc-start.c:128:20
#20 0x00007f6978846245 __libc_start_main ./csu/../csu/libc-start.c:368:5
#21 0x000056037167aed1 _start (/usr/local/google/home/aheejin/llvm-git/install.debug/bin/llc+0x2eed1)
Aborted
```

This program was created while trying to reduce the example shown in #62961, so this can be due to the same cause. At the same time, this can be also related to #63182 (because this has an unwind backedge `bb7`->`bb2`) or #49292 (because this also has an irreducible CFG related to an unwind edge), or both. Before investigating it's hard to ensure if any of these are actually the same bug, so will file this separately for now.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWV1v27jS_jXMzcCGRH1f5CJp4u0CLbZo99330qCokcUNTQokVSf__oCUbEtu0o_dbM_iBIFjUeTD55kZDocMs1bsFOI1yW5JdnfFBtdpc806xD-Fuqp183Tt0Lq1lCS5IdEdiW5IHo2_Uo4NjpkdOmiYY5I96cEBSe6AUIqrPUlucNWT5Cah4WMl8pQkN3m6UqElT1efYloSShdYzohe4hHnwOw-oatBPSh9UCvcW25E71Cdho2fDXLJDIJIKJA02m53j49bP3jbo7FaMSnc0_ZzRGi5Xq8JreaDSXILm0FxJ7SCG-eMJckNKM2ZlDXjD6B0axBBafukOCg9qINQDRyElAbdYNSSRO-MJyHl5_3ac1jv0K3xkWPvZyC0dPrBK6iA0CT6J5lM5rhg0lmUyJ02X6NyhPisReMxWq2J91a17NQKde7j42XsBDOzH-3xolOAFLcjXl1Hp2ADEOqzfniRAZx-nAbJapRAaFbXMUxGmbWlc9J1HXurelv3Bhs7xVpW1wWhb8Zv0XGOQRlkvGO1xCUG_SZGdvqWHtEIzThzvLMH4XgXhVGzBjgI1wkFSisEkt3OFCQkuzsKcxp8QKBZMkpeYEQvZ-9ZM5u6Z81x3i_oZbfed2qQkmR3Mxh3HC_ld0b7YvKZDz1YfAb7zoB9CS20GXTQGr1f6l0GSbE0nU9M8EM_z9g5et7L8Q94OZt5edZMl2yz12D7RUz2rIm_Iyrir0QF_TtREV9ERfK3omKO9lIe8Ur8JtMOikt0fkt5AQlmQp_POvm33Za_htuyb4Z6_PVQL17IEvkpWyXfmX-_nXaPBiju5hyYc0bUg0MbNp2RQnH7Q9scnDGPNcn0SGOS3Yo926H3GS0753q_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