[llvm] a0ff26e - [GlobalOpt] Fix assertion failure during instruction deletion

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 2 02:58:47 PST 2021


Author: Nikita Popov
Date: 2021-12-02T11:58:39+01:00
New Revision: a0ff26e08c0cca4917a08c8b8383b8338aa8e73e

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

LOG: [GlobalOpt] Fix assertion failure during instruction deletion

This fixes the assertion failure reported in https://reviews.llvm.org/D114889#3166417,
by making RecursivelyDeleteTriviallyDeadInstructionsPermissive()
more permissive. As the function accepts a WeakTrackingVH, even if
originally only Instructions were inserted, we may end up with
different Value types after a RAUW operation. As such, we should
not assume that the vector only contains instructions.

Notably this matches the behavior of the
RecursivelyDeleteTriviallyDeadInstructions() function variant which
accepts a single value rather than vector.

Added: 
    llvm/test/Transforms/GlobalOpt/recursively-delete-dead-inst-assertion.ll

Modified: 
    llvm/lib/Transforms/Utils/Local.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 99eed9e065f87..ec926b1f5a94a 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -529,8 +529,8 @@ bool llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive(
     std::function<void(Value *)> AboutToDeleteCallback) {
   unsigned S = 0, E = DeadInsts.size(), Alive = 0;
   for (; S != E; ++S) {
-    auto *I = cast<Instruction>(DeadInsts[S]);
-    if (!isInstructionTriviallyDead(I)) {
+    auto *I = dyn_cast<Instruction>(DeadInsts[S]);
+    if (!I || !isInstructionTriviallyDead(I)) {
       DeadInsts[S] = nullptr;
       ++Alive;
     }

diff  --git a/llvm/test/Transforms/GlobalOpt/recursively-delete-dead-inst-assertion.ll b/llvm/test/Transforms/GlobalOpt/recursively-delete-dead-inst-assertion.ll
new file mode 100644
index 0000000000000..63782798d652c
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/recursively-delete-dead-inst-assertion.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -globalopt < %s | FileCheck %s
+
+; In this case an instruction queued for recursive deletion gets RAUWd with
+; a constant in the meantime. Make sure this does not cause an assertion
+; failure.
+
+ at a = internal global i32** null
+ at b = internal global i32*** @a
+
+define void @test() {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:    ret void
+;
+  %v1 = load i32***, i32**** @b
+  %v2 = load i32**, i32*** %v1
+  store i32** %v2, i32*** @a
+  ret void
+}
+


        


More information about the llvm-commits mailing list