Igor Laevsky | faacdf8 | 2017-11-30 15:24:41 +0000 | [diff] [blame^] | 1 | //===- RandomIRBuilderTest.cpp - Tests for injector strategy --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/FuzzMutate/RandomIRBuilder.h" |
| 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/AsmParser/Parser.h" |
| 13 | #include "llvm/AsmParser/SlotMapping.h" |
| 14 | #include "llvm/FuzzMutate/IRMutator.h" |
| 15 | #include "llvm/FuzzMutate/OpDescriptor.h" |
| 16 | #include "llvm/FuzzMutate/Operations.h" |
| 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Instructions.h" |
| 19 | #include "llvm/IR/LLVMContext.h" |
| 20 | #include "llvm/IR/Module.h" |
| 21 | #include "llvm/IR/Verifier.h" |
| 22 | #include "llvm/Support/SourceMgr.h" |
| 23 | |
| 24 | #include "gtest/gtest.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | static constexpr int Seed = 5; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | std::unique_ptr<Module> parseAssembly( |
| 33 | const char *Assembly, LLVMContext &Context) { |
| 34 | |
| 35 | SMDiagnostic Error; |
| 36 | std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context); |
| 37 | |
| 38 | std::string ErrMsg; |
| 39 | raw_string_ostream OS(ErrMsg); |
| 40 | Error.print("", OS); |
| 41 | |
| 42 | assert(M && !verifyModule(*M, &errs())); |
| 43 | return M; |
| 44 | } |
| 45 | |
| 46 | TEST(RandomIRBuilderTest, ShuffleVectorIncorrectOperands) { |
| 47 | // Test that we don't create load instruction as a source for the shuffle |
| 48 | // vector operation. |
| 49 | |
| 50 | LLVMContext Ctx; |
| 51 | const char *Source = |
| 52 | "define <2 x i32> @test(<2 x i1> %cond, <2 x i32> %a) {\n" |
| 53 | " %A = alloca <2 x i32>\n" |
| 54 | " %I = insertelement <2 x i32> %a, i32 1, i32 1\n" |
| 55 | " ret <2 x i32> undef\n" |
| 56 | "}"; |
| 57 | auto M = parseAssembly(Source, Ctx); |
| 58 | |
| 59 | fuzzerop::OpDescriptor Descr = fuzzerop::shuffleVectorDescriptor(1); |
| 60 | |
| 61 | // Empty known types since we ShuffleVector descriptor doesn't care about them |
| 62 | RandomIRBuilder IB(Seed, {}); |
| 63 | |
| 64 | // Get first basic block of the first function |
| 65 | Function &F = *M->begin(); |
| 66 | BasicBlock &BB = *F.begin(); |
| 67 | |
| 68 | SmallVector<Instruction *, 32> Insts; |
| 69 | for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I) |
| 70 | Insts.push_back(&*I); |
| 71 | |
| 72 | // Pick first and second sources |
| 73 | SmallVector<Value *, 2> Srcs; |
| 74 | ASSERT_TRUE(Descr.SourcePreds[0].matches(Srcs, Insts[1])); |
| 75 | Srcs.push_back(Insts[1]); |
| 76 | ASSERT_TRUE(Descr.SourcePreds[1].matches(Srcs, Insts[1])); |
| 77 | Srcs.push_back(Insts[1]); |
| 78 | |
| 79 | // Create new source. Check that it always matches with the descriptor. |
| 80 | // Run some iterations to account for random decisions. |
| 81 | for (int i = 0; i < 10; ++i) { |
| 82 | Value *LastSrc = IB.newSource(BB, Insts, Srcs, Descr.SourcePreds[2]); |
| 83 | ASSERT_TRUE(Descr.SourcePreds[2].matches(Srcs, LastSrc)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } |