Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame^] | 1 | //===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===// |
| 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 program tries to reduce an IR test case for a given interesting-ness |
| 10 | // test. It runs multiple delta debugging passes in order to minimize the input |
| 11 | // file. It's worth noting that this is a part of the bugpoint redesign |
| 12 | // proposal, and thus a *temporary* tool that will eventually be integrated |
| 13 | // into the bugpoint tool itself. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "DeltaManager.h" |
| 18 | #include "llvm/ADT/SmallString.h" |
| 19 | #include "llvm/IR/LLVMContext.h" |
| 20 | #include "llvm/IR/Verifier.h" |
| 21 | #include "llvm/IRReader/IRReader.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/InitLLVM.h" |
| 24 | #include "llvm/Support/SourceMgr.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include <system_error> |
| 27 | #include <vector> |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); |
| 32 | static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden); |
| 33 | |
| 34 | static cl::opt<std::string> InputFilename(cl::Positional, cl::Required, |
| 35 | cl::desc("<input llvm ll/bc file>")); |
| 36 | |
| 37 | static cl::opt<std::string> |
| 38 | TestFilename("test", cl::Required, |
| 39 | cl::desc("Name of the interesting-ness test to be run")); |
| 40 | |
| 41 | static cl::list<std::string> |
| 42 | TestArguments("test-arg", cl::ZeroOrMore, |
| 43 | cl::desc("Arguments passed onto the interesting-ness test")); |
| 44 | |
| 45 | static cl::opt<std::string> |
| 46 | OutputFilename("output", |
| 47 | cl::desc("Specify the output file. default: reduced.ll")); |
| 48 | static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"), |
| 49 | cl::aliasopt(OutputFilename)); |
| 50 | |
| 51 | static cl::opt<bool> |
| 52 | ReplaceInput("in-place", |
| 53 | cl::desc("WARNING: This option will replace your input file" |
| 54 | "with the reduced version!")); |
| 55 | |
| 56 | // Parses IR into a Module and verifies it |
| 57 | static std::unique_ptr<Module> parseInputFile(StringRef Filename, |
| 58 | LLVMContext &Ctxt) { |
| 59 | SMDiagnostic Err; |
| 60 | std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt); |
| 61 | if (!Result) { |
| 62 | Err.print("llvm-reduce", errs()); |
| 63 | return Result; |
| 64 | } |
| 65 | |
| 66 | if (verifyModule(*Result, &errs())) { |
| 67 | errs() << "Error: " << Filename << " - input module is broken!\n"; |
| 68 | return std::unique_ptr<Module>(); |
| 69 | } |
| 70 | |
| 71 | return Result; |
| 72 | } |
| 73 | |
| 74 | int main(int argc, char **argv) { |
| 75 | InitLLVM X(argc, argv); |
| 76 | |
| 77 | cl::ParseCommandLineOptions(argc, argv, "LLVM automatic testcase reducer.\n"); |
| 78 | |
| 79 | LLVMContext Context; |
| 80 | std::unique_ptr<Module> OriginalProgram = |
| 81 | parseInputFile(InputFilename, Context); |
| 82 | |
| 83 | // Initialize test environment |
| 84 | TestRunner Tester(TestFilename, TestArguments, InputFilename); |
| 85 | Tester.setProgram(std::move(OriginalProgram)); |
| 86 | |
| 87 | // Try to reduce code |
| 88 | runDeltaPasses(Tester); |
| 89 | StringRef ReducedFilename = sys::path::filename(Tester.getReducedFilepath()); |
| 90 | |
| 91 | if (ReducedFilename == sys::path::filename(InputFilename)) { |
| 92 | outs() << "\nCouldnt reduce input :/\n"; |
| 93 | } else { |
| 94 | if (ReplaceInput) // In-place |
| 95 | OutputFilename = InputFilename.c_str(); |
| 96 | else if (OutputFilename.empty()) |
| 97 | OutputFilename = "reduced.ll"; |
| 98 | else |
| 99 | OutputFilename += ".ll"; |
| 100 | |
| 101 | sys::fs::copy_file(Tester.getReducedFilepath(), OutputFilename); |
| 102 | outs() << "\nDone reducing! Reduced IR to file: " << OutputFilename << "\n"; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |