[llvm] 5b6575d - [llvm-reduce] Do not crash when accessing landingpads of invokes.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 18 07:20:36 PST 2022


Author: Florian Hahn
Date: 2022-11-18T15:19:50Z
New Revision: 5b6575d50eab705b6d3b20c9f92b9aece5e1bd24

URL: https://github.com/llvm/llvm-project/commit/5b6575d50eab705b6d3b20c9f92b9aece5e1bd24
DIFF: https://github.com/llvm/llvm-project/commit/5b6575d50eab705b6d3b20c9f92b9aece5e1bd24.diff

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

Unconditionally removing landing pads results in invalid IR,
if there is a different `invoke` that uses it. Update the code
to only remove the landing pad if the current invoke is the only
user. Also carefully avoid creating plain branches to bbs with
landing pads we couldn't remove.

Reviewed By: arsenm, aeubanks

Differential Revision: https://reviews.llvm.org/D138072

Added: 
    llvm/test/tools/llvm-reduce/invoke-with-missing-landingpad.ll

Modified: 
    llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-reduce/invoke-with-missing-landingpad.ll b/llvm/test/tools/llvm-reduce/invoke-with-missing-landingpad.ll
new file mode 100644
index 0000000000000..6c306c1d3fafd
--- /dev/null
+++ b/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  -abort-on-invalid-reduction -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()

diff  --git a/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp b/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
index bc7a23d99eabe..cb518a9c4313f 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceBasicBlocks.cpp
@@ -48,8 +48,20 @@ static void replaceBranchTerminator(BasicBlock &BB,
   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;
   }
 


        


More information about the llvm-commits mailing list