[PATCH] D138072: [llvm-reduce] Do not crash when accessing landingpads of invokes.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 17 14:04:41 PST 2022


fhahn updated this revision to Diff 476236.
fhahn added a comment.

In D138072#3929124 <https://reviews.llvm.org/D138072#3929124>, @arsenm wrote:

> If it's temporarily invalid as in will fall the verifier, probably should try to avoid getting there in the first place

Fair point! I updated the code to only remove the landing pad instruction if the current `invoke` is the only `invoke` using it. This also requires extra care when we cannot remove the landing pad and it would be the destination for the branch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D138072/new/

https://reviews.llvm.org/D138072

Files:
  llvm/test/tools/llvm-reduce/invoke-with-missing-landingpad.ll
  llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp


Index: llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
===================================================================
--- llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
+++ llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
@@ -48,8 +48,20 @@
   bool IsBranch = isa<BranchInst>(Term);
   if (InvokeInst *Invoke = dyn_cast<InvokeInst>(Term)) {
     LandingPadInst *LP = Invoke->getLandingPadInst();
-    LP->replaceAllUsesWith(getDefaultValue(LP->getType()));
-    LP->eraseFromParent();
+    // Remove landingpad instruction if the containing block isn't used by other
+    // invokes.
+    if (none_of(LP->getParent()->users(), [Invoke](User *U) {
+          return U != Invoke && isa<InvokeInst>(U);
+        })) {
+      LP->replaceAllUsesWith(getDefaultValue(LP->getType()));
+      LP->eraseFromParent();
+    } else if (!ChunkSuccessors.empty() &&
+               ChunkSuccessors[0] == LP->getParent()) {
+      // If the selected successor is the landing pad, clear the chunk
+      // successors to avoid creating a regular branch to the landing pad which
+      // would result in invalid IR.
+      ChunkSuccessors.clear();
+    }
     IsBranch = true;
   }
 
Index: llvm/test/tools/llvm-reduce/invoke-with-missing-landingpad.ll
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-reduce/invoke-with-missing-landingpad.ll
@@ -0,0 +1,37 @@
+; RUN: llvm-reduce --delta-passes=basic-blocks --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
+; RUN: FileCheck <%t %s
+
+; CHECK-INTERESTINGNESS: call void @foo()
+
+; CHECK: define void @test() personality ptr null {
+; CHECK-NEXT: entry:
+; CHECK-NEXT:   br label %cont
+; CHECK-EMPTY:
+; CHECK-NEXT: cont:
+; CHECK-NEXT:   br label %exit
+; CHECK-EMPTY:
+; CHECK-NEXT: exit:
+; CHECK-NEXT:   call void @foo()
+; CHECK-NEXT:   ret void
+; CHECK-NEXT: }
+
+define void @test() personality ptr null {
+entry:
+  invoke void @foo()
+          to label %cont unwind label %lpad
+
+cont:
+  invoke void @foo()
+          to label %exit unwind label %lpad
+
+lpad:
+  %0 = landingpad { ptr, i32 }
+          cleanup
+  ret void
+
+exit:
+  call void @foo()
+  ret void
+}
+
+declare void @foo()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138072.476236.patch
Type: text/x-patch
Size: 2279 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221117/06b5fd72/attachment.bin>


More information about the llvm-commits mailing list