Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 1 | //===- 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 Blaikie | aac114c | 2019-09-10 22:31:35 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ToolOutputFile.h" |
| 18 | #include "llvm/Transforms/Utils/Cloning.h" |
| 19 | #include <fstream> |
| 20 | #include <set> |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 21 | |
David Blaikie | 345fbfd | 2019-09-10 22:10:00 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame] | 24 | bool IsReduced(Module &M, TestRunner &Test, SmallString<128> &CurrentFilepath) { |
| 25 | // Write Module to tmp file |
| 26 | int FD; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 27 | std::error_code EC = |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame] | 28 | sys::fs::createTemporaryFile("llvm-reduce", "ll", FD, CurrentFilepath); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 29 | if (EC) { |
| 30 | errs() << "Error making unique filename: " << EC.message() << "!\n"; |
| 31 | exit(1); |
| 32 | } |
| 33 | |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame] | 34 | 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 Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 39 | exit(1); |
| 40 | } |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame] | 41 | |
| 42 | // Current Chunks aren't interesting |
| 43 | return Test.run(CurrentFilepath); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 46 | /// Counts the amount of lines for a given file |
David Blaikie | c4da7ee | 2019-09-18 22:30:25 +0000 | [diff] [blame^] | 47 | static int getLines(StringRef Filepath) { |
| 48 | int Lines = 0; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 49 | 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. |
| 60 | static bool increaseGranularity(std::vector<Chunk> &Chunks) { |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 61 | errs() << "Increasing granularity..."; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 62 | 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 Blaikie | c4da7ee | 2019-09-18 22:30:25 +0000 | [diff] [blame^] | 69 | int Half = (C.begin + C.end) / 2; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 70 | 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 Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 77 | errs() << "Success! New Chunks:\n"; |
| 78 | for (auto C : Chunks) { |
| 79 | errs() << '\t'; |
| 80 | C.print(); |
| 81 | errs() << '\n'; |
| 82 | } |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 83 | } |
| 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. |
| 90 | void llvm::runDeltaPass( |
David Blaikie | c4da7ee | 2019-09-18 22:30:25 +0000 | [diff] [blame^] | 91 | TestRunner &Test, int Targets, |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 92 | std::function<void(const std::vector<Chunk> &, Module *)> |
| 93 | ExtractChunksFromModule) { |
David Blaikie | c4da7ee | 2019-09-18 22:30:25 +0000 | [diff] [blame^] | 94 | assert(Targets >= 0); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 95 | if (!Targets) { |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 96 | errs() << "\nNothing to reduce\n"; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame] | 99 | |
| 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 Ferrer | 376f642 | 2019-08-14 20:34:12 +0000 | [diff] [blame] | 106 | } |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 107 | |
| 108 | std::vector<Chunk> Chunks = {{1, Targets}}; |
| 109 | std::set<Chunk> UninterestingChunks; |
| 110 | std::unique_ptr<Module> ReducedProgram; |
| 111 | |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 112 | if (!increaseGranularity(Chunks)) { |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 113 | errs() << "\nAlready at minimum size. Cannot reduce anymore.\n"; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 114 | 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 Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 133 | |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 134 | errs() << "Ignoring: "; |
| 135 | Chunks[I].print(); |
| 136 | for (auto C : UninterestingChunks) |
| 137 | C.print(); |
| 138 | |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 139 | |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame] | 140 | |
| 141 | SmallString<128> CurrentFilepath; |
| 142 | if (!IsReduced(*Clone, Test, CurrentFilepath)) { |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 143 | errs() << "\n"; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 144 | continue; |
| 145 | } |
| 146 | |
| 147 | UninterestingChunks.insert(Chunks[I]); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 148 | ReducedProgram = std::move(Clone); |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 149 | errs() << " **** SUCCESS | lines: " << getLines(CurrentFilepath) << "\n"; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 150 | } |
| 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 Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 161 | errs() << "Couldn't increase anymore.\n"; |
David Blaikie | 345fbfd | 2019-09-10 22:10:00 +0000 | [diff] [blame] | 162 | } |