blob: 0642241ddebd66402014dbb7a1c6b31c74234cc5 [file] [log] [blame]
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +00001//===- Delta.cpp - Delta Debugging Algorithm Implementation ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation for the Delta Debugging Algorithm:
10// it splits a given set of Targets (i.e. Functions, Instructions, BBs, etc.)
11// into chunks and tries to reduce the number chunks that are interesting.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Delta.h"
16#include "llvm/ADT/STLExtras.h"
David Blaikieaac114c2019-09-10 22:31:35 +000017#include "llvm/Support/ToolOutputFile.h"
18#include "llvm/Transforms/Utils/Cloning.h"
19#include <fstream>
20#include <set>
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000021
David Blaikie345fbfd2019-09-10 22:10:00 +000022using namespace llvm;
23
David Blaikieaaef97a2019-09-12 00:31:57 +000024bool IsReduced(Module &M, TestRunner &Test, SmallString<128> &CurrentFilepath) {
25 // Write Module to tmp file
26 int FD;
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000027 std::error_code EC =
David Blaikieaaef97a2019-09-12 00:31:57 +000028 sys::fs::createTemporaryFile("llvm-reduce", "ll", FD, CurrentFilepath);
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000029 if (EC) {
30 errs() << "Error making unique filename: " << EC.message() << "!\n";
31 exit(1);
32 }
33
David Blaikieaaef97a2019-09-12 00:31:57 +000034 ToolOutputFile Out(CurrentFilepath, FD);
35 M.print(Out.os(), /*AnnotationWriter=*/nullptr);
36 Out.os().close();
37 if (Out.os().has_error()) {
38 errs() << "Error emitting bitcode to file '" << CurrentFilepath << "'!\n";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000039 exit(1);
40 }
David Blaikieaaef97a2019-09-12 00:31:57 +000041
42 // Current Chunks aren't interesting
43 return Test.run(CurrentFilepath);
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000044}
45
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000046/// Counts the amount of lines for a given file
David Blaikiec4da7ee2019-09-18 22:30:25 +000047static int getLines(StringRef Filepath) {
48 int Lines = 0;
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000049 std::string CurrLine;
50 std::ifstream FileStream(Filepath);
51
52 while (std::getline(FileStream, CurrLine))
53 ++Lines;
54
55 return Lines;
56}
57
58/// Splits Chunks in half and prints them.
59/// If unable to split (when chunk size is 1) returns false.
60static bool increaseGranularity(std::vector<Chunk> &Chunks) {
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +000061 errs() << "Increasing granularity...";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000062 std::vector<Chunk> NewChunks;
63 bool SplitOne = false;
64
65 for (auto &C : Chunks) {
66 if (C.end - C.begin == 0)
67 NewChunks.push_back(C);
68 else {
David Blaikiec4da7ee2019-09-18 22:30:25 +000069 int Half = (C.begin + C.end) / 2;
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000070 NewChunks.push_back({C.begin, Half});
71 NewChunks.push_back({Half + 1, C.end});
72 SplitOne = true;
73 }
74 }
75 if (SplitOne) {
76 Chunks = NewChunks;
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +000077 errs() << "Success! New Chunks:\n";
78 for (auto C : Chunks) {
79 errs() << '\t';
80 C.print();
81 errs() << '\n';
82 }
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000083 }
84 return SplitOne;
85}
86
87/// Runs the Delta Debugging algorithm, splits the code into chunks and
88/// reduces the amount of chunks that are considered interesting by the
89/// given test.
90void llvm::runDeltaPass(
David Blaikiec4da7ee2019-09-18 22:30:25 +000091 TestRunner &Test, int Targets,
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000092 std::function<void(const std::vector<Chunk> &, Module *)>
93 ExtractChunksFromModule) {
David Blaikiec4da7ee2019-09-18 22:30:25 +000094 assert(Targets >= 0);
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000095 if (!Targets) {
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +000096 errs() << "\nNothing to reduce\n";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +000097 return;
98 }
David Blaikieaaef97a2019-09-12 00:31:57 +000099
100 if (Module *Program = Test.getProgram()) {
101 SmallString<128> CurrentFilepath;
102 if (!IsReduced(*Program, Test, CurrentFilepath)) {
103 errs() << "\nInput isn't interesting! Verify interesting-ness test\n";
104 exit(1);
105 }
Diego Trevino Ferrer376f6422019-08-14 20:34:12 +0000106 }
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000107
108 std::vector<Chunk> Chunks = {{1, Targets}};
109 std::set<Chunk> UninterestingChunks;
110 std::unique_ptr<Module> ReducedProgram;
111
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000112 if (!increaseGranularity(Chunks)) {
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +0000113 errs() << "\nAlready at minimum size. Cannot reduce anymore.\n";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000114 return;
115 }
116
117 do {
118 UninterestingChunks = {};
119 for (int I = Chunks.size() - 1; I >= 0; --I) {
120 std::vector<Chunk> CurrentChunks;
121
122 for (auto C : Chunks)
123 if (!UninterestingChunks.count(C) && C != Chunks[I])
124 CurrentChunks.push_back(C);
125
126 if (CurrentChunks.empty())
127 continue;
128
129 // Clone module before hacking it up..
130 std::unique_ptr<Module> Clone = CloneModule(*Test.getProgram());
131 // Generate Module with only Targets inside Current Chunks
132 ExtractChunksFromModule(CurrentChunks, Clone.get());
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000133
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +0000134 errs() << "Ignoring: ";
135 Chunks[I].print();
136 for (auto C : UninterestingChunks)
137 C.print();
138
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000139
David Blaikieaaef97a2019-09-12 00:31:57 +0000140
141 SmallString<128> CurrentFilepath;
142 if (!IsReduced(*Clone, Test, CurrentFilepath)) {
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +0000143 errs() << "\n";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000144 continue;
145 }
146
147 UninterestingChunks.insert(Chunks[I]);
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000148 ReducedProgram = std::move(Clone);
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +0000149 errs() << " **** SUCCESS | lines: " << getLines(CurrentFilepath) << "\n";
Diego Trevino Ferrerddc64eb2019-08-08 22:16:33 +0000150 }
151 // Delete uninteresting chunks
152 erase_if(Chunks, [&UninterestingChunks](const Chunk &C) {
153 return UninterestingChunks.count(C);
154 });
155
156 } while (!UninterestingChunks.empty() || increaseGranularity(Chunks));
157
158 // If we reduced the testcase replace it
159 if (ReducedProgram)
160 Test.setProgram(std::move(ReducedProgram));
Diego Trevino Ferrerc2689252019-08-15 22:39:43 +0000161 errs() << "Couldn't increase anymore.\n";
David Blaikie345fbfd2019-09-10 22:10:00 +0000162}