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

via flang-commits flang-commits at lists.llvm.org
Wed Jan 10 17:52:17 PST 2024


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

>From 80a650763278061630e6ff5635bbc3a2dacc093e Mon Sep 17 00:00:00 2001
From: Haohai Wen <haohai.wen at intel.com>
Date: Wed, 10 Jan 2024 16:09:00 +0800
Subject: [PATCH 1/4] [BranchFolding][SEH] Add test to track SEH CFG
 optimization

This test tracks BranchFolding pass which removes fall through jump and
leaves landing-pad to be machine basic block of no predecessors. It
would raise bug as introduced in #77441.
---
 .../X86/branchfolding-landingpad-cfg.mir      | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir

diff --git a/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir b/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir
new file mode 100644
index 00000000000000..a494701c2a3997
--- /dev/null
+++ b/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir
@@ -0,0 +1,49 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
+# RUN: llc -mtriple=x86_64-pc-windows-msvc -run-pass=branch-folder -o - %s | FileCheck %s
+---
+name:            main
+body:             |
+  ; CHECK-LABEL: name: main
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.1(0x7ffff800), %bb.3(0x00000800)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   RET 0
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.2 (machine-block-address-taken, landing-pad, ehfunclet-entry):
+  ; CHECK-NEXT:   successors: %bb.3(0x80000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   CLEANUPRET
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.3 (landing-pad, ehfunclet-entry):
+  ; CHECK-NEXT:   CLEANUPRET
+  bb.0:
+    successors: %bb.1(0x7ffff800), %bb.5(0x00000800)
+    JMP_1 %bb.1
+
+  bb.1:
+    successors: %bb.2(0x7ffff800), %bb.4(0x00000800)
+
+    JMP_1 %bb.2
+
+  bb.2:
+    successors: %bb.3(0x7ffff800), %bb.4(0x00000800)
+
+    JMP_1 %bb.3
+
+  bb.3:
+    successors: %bb.6(0x7ffff800)
+
+    JMP_1 %bb.6
+
+  bb.4 (machine-block-address-taken, landing-pad, ehfunclet-entry):
+    successors: %bb.5(0x80000000)
+    CLEANUPRET
+
+  bb.5 (landing-pad, ehfunclet-entry):
+    CLEANUPRET
+
+  bb.6:
+    RET 0
+...

>From 34cce741218ed6f719214556b8e260c94b20deee Mon Sep 17 00:00:00 2001
From: Haohai Wen <haohai.wen at intel.com>
Date: Wed, 10 Jan 2024 20:50:24 +0800
Subject: [PATCH 2/4] [BranchFolding] Fix missing predecessors of landing-pad

When removing an empty machine basic block, all of its successors should
be inherited by its fall through MBB. This keeps CFG as only have one
entry which is required by LiveDebugValues.
---
 llvm/lib/CodeGen/BranchFolding.cpp              | 17 +++++++++++++++++
 .../X86/branchfolding-landingpad-cfg.mir        |  2 ++
 2 files changed, 19 insertions(+)

diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp
index 0801296cab49f8..599b7c72b2f5c6 100644
--- a/llvm/lib/CodeGen/BranchFolding.cpp
+++ b/llvm/lib/CodeGen/BranchFolding.cpp
@@ -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);
+        }
       // If MBB was the target of a jump table, update jump tables to go to the
       // fallthrough instead.
       if (MachineJumpTableInfo *MJTI = MF.getJumpTableInfo())
@@ -1624,6 +1632,15 @@ bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
             } else {
               DidChange = true;
               PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB);
+              // Add rest successors of MBB to successors of CurTBB. 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 != CurTBB && !CurTBB->isSuccessor(*SI)) {
+                  assert((*SI)->isEHPad() && "Bad CFG");
+                  CurTBB->copySuccessor(MBB, SI);
+                }
               // If this change resulted in PMBB ending in a conditional
               // branch where both conditions go to the same destination,
               // change this to an unconditional branch.
diff --git a/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir b/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir
index a494701c2a3997..8eef5450e252a6 100644
--- a/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir
+++ b/llvm/test/CodeGen/X86/branchfolding-landingpad-cfg.mir
@@ -9,6 +9,8 @@ body:             |
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   successors: %bb.2(0x00000800)
+  ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT:   RET 0
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.2 (machine-block-address-taken, landing-pad, ehfunclet-entry):

>From 00c6c0dc42688531edec8efd65aeabd5ffbfec98 Mon Sep 17 00:00:00 2001
From: HaohaiWen <haohai.wen at intel.com>
Date: Tue, 9 Jan 2024 22:30:13 +0800
Subject: [PATCH 3/4] [SEH][CodeGen] Add test to track CFG optimization bug for
 SEH (#77441)

LiveDebugValues requires CFG only has one entry. BranchFolding and
MachineBlockPlacement may remove all predecessors of landing pad which
leaves it to be another entry.
---
 .../X86/windows-seh-EHa-PreserveCFG.ll        | 81 +++++++++++++++++++
 1 file changed, 81 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll

diff --git a/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll b/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll
new file mode 100644
index 00000000000000..bd6743f7c414b4
--- /dev/null
+++ b/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll
@@ -0,0 +1,81 @@
+; XFAIL: *
+; RUN: llc -mtriple=x86_64-pc-windows-msvc %s
+define dso_local void @main(ptr %addr, ptr %src, ptr %dst) personality ptr @__CxxFrameHandler3 !dbg !11 {
+entry:
+  %tmp0 = load float, ptr %src
+  %src1 = getelementptr inbounds float, ptr %src, i64 1
+  %tmp1 = load float, ptr %src1
+  %src2 = getelementptr inbounds float, ptr %src, i64 2
+  %tmp2 = load float, ptr %src2
+  %src3 = getelementptr inbounds float, ptr %src, i64 3
+  %tmp3 = load float, ptr %src3
+  %src4 = getelementptr inbounds float, ptr %src, i64 4
+  %tmp4 = load float, ptr %src4
+  %src5 = getelementptr inbounds float, ptr %src, i64 5
+  %tmp5 = load float, ptr %src5
+  %src6 = getelementptr inbounds float, ptr %src, i64 6
+  %tmp6 = load float, ptr %src6
+  invoke void @foo(ptr %addr)
+          to label %scope_begin unwind label %ehcleanup1, !dbg !13
+
+scope_begin:
+  invoke void @llvm.seh.scope.begin()
+          to label %scope_end unwind label %ehcleanup, !dbg !13
+
+scope_end:
+  invoke void @llvm.seh.scope.end()
+          to label %finish unwind label %ehcleanup, !dbg !13
+
+ehcleanup:
+  %0 = cleanuppad within none [], !dbg !13
+  call void @llvm.dbg.value(metadata ptr %addr, metadata !12, metadata !DIExpression()), !dbg !13
+  call void @foo(ptr %addr) [ "funclet"(token %0) ], !dbg !13
+  cleanupret from %0 unwind label %ehcleanup1, !dbg !13
+
+ehcleanup1:
+  %1 = cleanuppad within none [], !dbg !13
+  call void @foo(ptr %addr) [ "funclet"(token %1) ], !dbg !13
+  cleanupret from %1 unwind to caller, !dbg !13
+
+finish:
+  store float %tmp0, ptr %dst
+  %dst1 = getelementptr inbounds float, ptr %dst, i64 1
+  store float %tmp1, ptr %dst1
+  %dst2 = getelementptr inbounds float, ptr %dst, i64 2
+  store float %tmp2, ptr %dst2
+  %dst3 = getelementptr inbounds float, ptr %dst, i64 3
+  store float %tmp3, ptr %dst3
+  %dst4 = getelementptr inbounds float, ptr %dst, i64 4
+  store float %tmp4, ptr %dst4
+  %dst5 = getelementptr inbounds float, ptr %dst, i64 5
+  store float %tmp5, ptr %dst5
+  %dst6 = getelementptr inbounds float, ptr %dst, i64 6
+  store float %tmp6, ptr %dst6
+  ret void
+}
+
+declare dso_local void @llvm.seh.scope.begin()
+declare dso_local void @llvm.seh.scope.end()
+declare dso_local i32 @__CxxFrameHandler3(...)
+declare dso_local void @foo(ptr %addr)
+declare void @llvm.dbg.value(metadata, metadata, metadata)
+
+!llvm.module.flags = !{!0, !1, !2, !3}
+!llvm.dbg.cu = !{!14}
+
+!0 = !{i32 2, !"eh-asynch", i32 1}
+!1 = !{i32 2, !"CodeView", i32 1}
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = !{i32 7, !"uwtable", i32 2}
+
+!4 = !DIBasicType(name: "float", size: 32, encoding: DW_ATE_float)
+!5 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 64)
+!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, size: 64)
+!8 = !DISubroutineType(types: !9)
+!9 = !{null, !7, !5, !5}
+!10 = !DIFile(filename: "c:/main.cpp", directory: "")
+!11 = distinct !DISubprogram(name: "main", scope: !10, file: !10, line: 5, type: !8, scopeLine: 11, unit: !14)
+!12 = !DILocalVariable(name: "addr", scope: !11, file: !10, line: 5, type: !7)
+!13 = !DILocation(line: 7, scope: !11)
+!14 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !10, isOptimized: true, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)

>From 7f84b74e1829d9a2c61da0a672dedf1ce8d29c94 Mon Sep 17 00:00:00 2001
From: Haohai Wen <haohai.wen at intel.com>
Date: Thu, 11 Jan 2024 09:48:29 +0800
Subject: [PATCH 4/4] Reland #77441

---
 llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll b/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll
index bd6743f7c414b4..38461dcbe53f34 100644
--- a/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll
+++ b/llvm/test/CodeGen/X86/windows-seh-EHa-PreserveCFG.ll
@@ -1,4 +1,3 @@
-; XFAIL: *
 ; RUN: llc -mtriple=x86_64-pc-windows-msvc %s
 define dso_local void @main(ptr %addr, ptr %src, ptr %dst) personality ptr @__CxxFrameHandler3 !dbg !11 {
 entry:



More information about the flang-commits mailing list