[Mlir-commits] [flang] [clang] [clang-tools-extra] [lldb] [libunwind] [openmp] [llvm] [lld] [mlir] [compiler-rt] [BranchFolding] Fix missing predecessors of landing-pad (PR #77608)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jan 10 18:15:32 PST 2024


================
@@ -1363,6 +1363,14 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
         MachineBasicBlock *Pred = *(MBB->pred_end()-1);
         Pred->ReplaceUsesOfBlockWith(MBB, &*FallThrough);
       }
+      // Add rest successors of MBB to successors of FallThrough. Those
+      // successors are not directly reachable via MBB, so it should be
+      // landing-pad.
+      for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE; ++SI)
+        if (*SI != &*FallThrough && !FallThrough->isSuccessor(*SI)) {
+          assert((*SI)->isEHPad() && "Bad CFG");
+          FallThrough->copySuccessor(MBB, SI);
+        }
----------------
HaohaiWen wrote:

Let's see what happened before/after this code:
Before:
```
bb.0:
  successors: %bb.3(0x7ffff800), %bb.5(0x00000800); %bb.3(100.00%), %bb.5(0.00%)

  JMP_1 %bb.3

bb.2:
  successors: %bb.3(0x7ffff800), %bb.4(0x00000800); %bb.3(100.00%), %bb.4(0.00%)


bb.3:
; predecessors: %bb.2, %bb.0
  successors: %bb.6(0x80000000); %bb.6(100.00%)

  JMP_1 %bb.6

bb.4 (machine-block-address-taken, landing-pad, ehfunclet-entry):
; predecessors: %bb.2
  successors: %bb.5(0x80000000); %bb.5(100.00%)

  CLEANUPRET

bb.5 (landing-pad, ehfunclet-entry):
; predecessors: %bb.0, %bb.4

  CLEANUPRET

bb.6:
; predecessors: %bb.3

  RET 0

# End machine code for function main.
```
After
```
bb.0:
  successors: %bb.3(0x7ffff800), %bb.5(0x00000800); %bb.3(100.00%), %bb.5(0.00%)

  JMP_1 %bb.3

bb.2:
  successors: %bb.3(0x7ffff800), %bb.4(0x00000800); %bb.3(100.00%), %bb.4(0.00%)


bb.3:
; predecessors: %bb.2, %bb.0
  successors: %bb.6(0x80000000), %bb.4(0x00000800); %bb.6(100.00%), %bb.4(0.00%)

  JMP_1 %bb.6

bb.4 (machine-block-address-taken, landing-pad, ehfunclet-entry):
; predecessors: %bb.2, %bb.3
  successors: %bb.5(0x80000000); %bb.5(100.00%)

  CLEANUPRET

bb.5 (landing-pad, ehfunclet-entry):
; predecessors: %bb.0, %bb.4

  CLEANUPRET

bb.6:
; predecessors: %bb.3

  RET 0

# End machine code for function main.
```
As we can see, No BB is removed at this time. We're adding edge from one live BB to another live BB.
In fact, dead BB was removed by BranchFolder::RemoveDeadBlock later. The CHECK is the final result after many optimization.

https://github.com/llvm/llvm-project/pull/77608


More information about the Mlir-commits mailing list