[llvm] 8f3127c - [LoopDeletion] Don't delete loop exiting to EH pad (PR62160)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 17 06:49:14 PDT 2023


Author: Nikita Popov
Date: 2023-04-17T15:49:05+02:00
New Revision: 8f3127cdea6e772954466554b3d51eeb6762270f

URL: https://github.com/llvm/llvm-project/commit/8f3127cdea6e772954466554b3d51eeb6762270f
DIFF: https://github.com/llvm/llvm-project/commit/8f3127cdea6e772954466554b3d51eeb6762270f.diff

LOG: [LoopDeletion] Don't delete loop exiting to EH pad (PR62160)

We can't branch directly to the EH pad, which is what the current
loop deletion code would try to do. We would need a different
approach here, which retains the invoke. This edge case does not
look worth bothering with.

Fixes https://github.com/llvm/llvm-project/issues/62160.

Added: 
    llvm/test/Transforms/LoopDeletion/pr62160.ll

Modified: 
    llvm/lib/Transforms/Scalar/LoopDeletion.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
index e0dc376e8f04a..c041e3621a16b 100644
--- a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
@@ -486,6 +486,14 @@ static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT,
     LLVM_DEBUG(dbgs() << "Deletion requires at most one exit block.\n");
     return LoopDeletionResult::Unmodified;
   }
+
+  // We can't directly branch to an EH pad. Don't bother handling this edge
+  // case.
+  if (ExitBlock && ExitBlock->isEHPad()) {
+    LLVM_DEBUG(dbgs() << "Cannot delete loop exiting to EH pad.\n");
+    return LoopDeletionResult::Unmodified;
+  }
+
   // Finally, we have to check that the loop really is dead.
   bool Changed = false;
   if (!isLoopDead(L, SE, ExitingBlocks, ExitBlock, Changed, Preheader, LI)) {

diff  --git a/llvm/test/Transforms/LoopDeletion/pr62160.ll b/llvm/test/Transforms/LoopDeletion/pr62160.ll
new file mode 100644
index 0000000000000..cf6d34d888d4d
--- /dev/null
+++ b/llvm/test/Transforms/LoopDeletion/pr62160.ll
@@ -0,0 +1,39 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -S -passes=loop-deletion < %s | FileCheck %s
+
+; Do not try to delete a loop that exits to an EH pad, as we can't directly
+; branch to it. We would need a 
diff erent approach here that still retains
+; the invoke.
+
+define i32 @test() mustprogress personality ptr poison {
+; CHECK-LABEL: define i32 @test
+; CHECK-SAME: () #[[ATTR0:[0-9]+]] personality ptr poison {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[LOOP:%.*]]
+; CHECK:       loop:
+; CHECK-NEXT:    invoke void @llvm.donothing()
+; CHECK-NEXT:    to label [[LOOP_LATCH:%.*]] unwind label [[LPAD:%.*]]
+; CHECK:       lpad:
+; CHECK-NEXT:    [[LP:%.*]] = landingpad { ptr, i32 }
+; CHECK-NEXT:    cleanup
+; CHECK-NEXT:    resume { ptr, i32 } [[LP]]
+; CHECK:       loop.latch:
+; CHECK-NEXT:    br label [[LOOP]]
+;
+entry:
+  br label %loop
+
+loop:
+  invoke void @llvm.donothing()
+  to label %loop.latch unwind label %lpad
+
+lpad:
+  %lp = landingpad { ptr, i32 }
+  cleanup
+  resume { ptr, i32 } %lp
+
+loop.latch:
+  br label %loop
+}
+
+declare void @llvm.donothing()


        


More information about the llvm-commits mailing list