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

    <tr>
        <th>Summary</th>
        <td>
            AArch64 global isel "Assertion failed: (HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry && "Scope imbalance!")" on valid code
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AArch64,
            llvm:codegen,
            globalisel,
            crash-on-valid
      </td>
    </tr>

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

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

<pre>
    this function:
```llvm
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-linux-gnu"

define i32 @f() {
  %1 = icmp sge i16 0, 0
  %2 = sext i1 true to i8
  %3 = sext i8 %2 to i32
  %4 = sext i1 %1 to i32
  %5 = udiv i32 %3, %4
  %6 = sext i1 %1 to i32
  %7 = sext i1 true to i32
  %8 = urem i32 %6, %7
  %9 = trunc i32 %8 to i8
  %10 = zext i32 %5 to i64
  %11 = sext i8 %9 to i64
  %12 = or i64 %10, %11
  %13 = trunc i64 %12 to i32
  ret i32 %13
}
```

is causing global isel to assert out:
```
Johns-MacBook-Pro:tmp regehr$ llc -global-isel reduced.ll 
Assertion failed: (HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry && "Scope imbalance!"), function ~ScopedHashTableScope, file ScopedHashTable.h, line 244.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: llc -global-isel reduced.ll
1.      Running pass 'Function Pass Manager' on module 'reduced.ll'.
2.      Running pass 'Machine Common Subexpression Elimination' on function '@f'
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  libLLVMSupport.dylib 0x000000010c981594 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  libLLVMSupport.dylib     0x000000010c980418 llvm::sys::RunSignalHandlers() + 112
2  libLLVMSupport.dylib 0x000000010c981c20 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x00000001876742a4 _sigtramp + 56
4  libsystem_pthread.dylib 0x0000000187645cec pthread_kill + 288
5  libsystem_c.dylib 0x000000018757e2c8 abort + 180
6  libsystem_c.dylib 0x000000018757d620 err + 0
7  libLLVMCodeGen.dylib     0x000000010d15f780 llvm::ScopedHashTableScope<llvm::MachineInstr*, unsigned int, llvm::MachineInstrExpressionTrait, llvm::RecyclingAllocator<llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, 4096ul, 4096ul, 128ul>, llvm::ScopedHashTableVal<llvm::MachineInstr*, unsigned int>, 32ul, 8ul>>::~ScopedHashTableScope() + 440
8 libLLVMCodeGen.dylib     0x000000010d15f550 (anonymous namespace)::MachineCSE::ExitScope(llvm::MachineBasicBlock*) + 360
9 libLLVMCodeGen.dylib     0x000000010d15bc5c (anonymous namespace)::MachineCSE::runOnMachineFunction(llvm::MachineFunction&) + 9932
10 libLLVMCodeGen.dylib     0x000000010d194b2c llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 616
11 libLLVMCore.dylib        0x000000010e21e298 llvm::FPPassManager::runOnFunction(llvm::Function&) + 704
12 libLLVMCore.dylib        0x000000010e224fc8 llvm::FPPassManager::runOnModule(llvm::Module&) + 60
13 libLLVMCore.dylib 0x000000010e21e86c llvm::legacy::PassManagerImpl::run(llvm::Module&) + 764
14 llc                      0x0000000104b7d208 compileModule(char**, llvm::LLVMContext&) + 5672
15 llc                      0x0000000104b7b594 main + 1128
16 dyld                     0x000000018731be50 start + 2544
Abort trap: 6
Johns-MacBook-Pro:tmp regehr$ 
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV0tv67gV_jX05sCGSL0XXth59N42wQQ3wQDtJqAoRmJDkQJJZeJZ9LcXpCRHdjx3Mt3UcByb_HjOd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