[llvm] r323409 - [FuzzMutate] Inst deleter doesn't work with PhiNodes

Igor Laevsky via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 01:22:18 PST 2018


Author: igor.laevsky
Date: Thu Jan 25 01:22:18 2018
New Revision: 323409

URL: http://llvm.org/viewvc/llvm-project?rev=323409&view=rev
Log:
[FuzzMutate] Inst deleter doesn't work with PhiNodes

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


Modified:
    llvm/trunk/lib/FuzzMutate/IRMutator.cpp
    llvm/trunk/unittests/FuzzMutate/StrategiesTest.cpp

Modified: llvm/trunk/lib/FuzzMutate/IRMutator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/FuzzMutate/IRMutator.cpp?rev=323409&r1=323408&r2=323409&view=diff
==============================================================================
--- llvm/trunk/lib/FuzzMutate/IRMutator.cpp (original)
+++ llvm/trunk/lib/FuzzMutate/IRMutator.cpp Thu Jan 25 01:22:18 2018
@@ -152,10 +152,14 @@ uint64_t InstDeleterIRStrategy::getWeigh
 
 void InstDeleterIRStrategy::mutate(Function &F, RandomIRBuilder &IB) {
   auto RS = makeSampler<Instruction *>(IB.Rand);
-  // Avoid terminators so we don't have to worry about keeping the CFG coherent.
-  for (Instruction &Inst : instructions(F))
-    if (!Inst.isTerminator())
-      RS.sample(&Inst, /*Weight=*/1);
+  for (Instruction &Inst : instructions(F)) {
+    // TODO: We can't handle these instructions.
+    if (Inst.isTerminator() || Inst.isEHPad() ||
+        Inst.isSwiftError() || isa<PHINode>(Inst))
+      continue;
+
+    RS.sample(&Inst, /*Weight=*/1);
+  }
   if (RS.isEmpty())
     return;
 
@@ -191,4 +195,5 @@ void InstDeleterIRStrategy::mutate(Instr
     RS.sample(IB.newSource(*BB, InstsBefore, {}, Pred), /*Weight=*/1);
 
   Inst.replaceAllUsesWith(RS.getSelection());
+  Inst.eraseFromParent();
 }

Modified: llvm/trunk/unittests/FuzzMutate/StrategiesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/FuzzMutate/StrategiesTest.cpp?rev=323409&r1=323408&r2=323409&view=diff
==============================================================================
--- llvm/trunk/unittests/FuzzMutate/StrategiesTest.cpp (original)
+++ llvm/trunk/unittests/FuzzMutate/StrategiesTest.cpp Thu Jan 25 01:22:18 2018
@@ -64,6 +64,18 @@ std::unique_ptr<Module> parseAssembly(
   return M;
 }
 
+void IterateOnSource(StringRef Source, IRMutator &Mutator) {
+  LLVMContext Ctx;
+
+  for (int i = 0; i < 10; ++i) {
+    auto M = parseAssembly(Source.data(), Ctx);
+    ASSERT_TRUE(M && !verifyModule(*M, &errs()));
+
+    Mutator.mutateModule(*M, Seed, Source.size(), Source.size() + 100);
+    EXPECT_TRUE(!verifyModule(*M, &errs()));
+  }
+}
+
 TEST(InjectorIRStrategyTest, EmptyModule) {
   // Test that we can inject into empty module
 
@@ -81,7 +93,6 @@ TEST(InjectorIRStrategyTest, EmptyModule
 TEST(InstDeleterIRStrategyTest, EmptyFunction) {
   // Test that we don't crash even if we can't remove from one of the functions.
 
-  LLVMContext Ctx;
   StringRef Source = ""
       "define <8 x i32> @func1() {\n"
         "ret <8 x i32> undef\n"
@@ -96,15 +107,33 @@ TEST(InstDeleterIRStrategyTest, EmptyFun
   auto Mutator = createDeleterMutator();
   ASSERT_TRUE(Mutator);
 
-  // We need to choose 'func1' in order for the crash to appear.
-  // Loop 10 times and assume we are lucky.
-  for (int i = 0; i < 10; ++i) {
-    auto M = parseAssembly(Source.data(), Ctx);
-    ASSERT_TRUE(M && !verifyModule(*M, &errs()));
+  IterateOnSource(Source, *Mutator);
+}
 
-    Mutator->mutateModule(*M, Seed, Source.size(), Source.size() + 100);
-    EXPECT_TRUE(!verifyModule(*M, &errs()));
-  }
+TEST(InstDeleterIRStrategyTest, PhiNodes) {
+  // Test that inst deleter works correctly with the phi nodes.
+
+  LLVMContext Ctx;
+  StringRef Source = "\n\
+      define i32 @earlyreturncrash(i32 %x) {\n\
+      entry:\n\
+        switch i32 %x, label %sw.epilog [\n\
+          i32 1, label %sw.bb1\n\
+        ]\n\
+      \n\
+      sw.bb1:\n\
+        br label %sw.epilog\n\
+      \n\
+      sw.epilog:\n\
+        %a.0 = phi i32 [ 7, %entry ],  [ 9, %sw.bb1 ]\n\
+        %b.0 = phi i32 [ 10, %entry ], [ 4, %sw.bb1 ]\n\
+        ret i32 %a.0\n\
+      }";
+
+  auto Mutator = createDeleterMutator();
+  ASSERT_TRUE(Mutator);
+
+  IterateOnSource(Source, *Mutator);
 }
 
 }




More information about the llvm-commits mailing list