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 | |
Roman Lebedev | 34b9bbb | 2020-07-21 15:36:31 +0300 | [diff] [blame] | 31 | static cl::OptionCategory Options("llvm-reduce options"); |
| 32 | |
| 33 | static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden, |
| 34 | cl::cat(Options)); |
| 35 | static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden, |
| 36 | cl::cat(Options)); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 37 | |
| 38 | static cl::opt<std::string> InputFilename(cl::Positional, cl::Required, |
Roman Lebedev | 34b9bbb | 2020-07-21 15:36:31 +0300 | [diff] [blame] | 39 | cl::desc("<input llvm ll/bc file>"), |
| 40 | cl::cat(Options)); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 41 | |
| 42 | static cl::opt<std::string> |
| 43 | TestFilename("test", cl::Required, |
Roman Lebedev | 34b9bbb | 2020-07-21 15:36:31 +0300 | [diff] [blame] | 44 | cl::desc("Name of the interesting-ness test to be run"), |
| 45 | cl::cat(Options)); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 46 | |
| 47 | static cl::list<std::string> |
| 48 | TestArguments("test-arg", cl::ZeroOrMore, |
Roman Lebedev | 34b9bbb | 2020-07-21 15:36:31 +0300 | [diff] [blame] | 49 | cl::desc("Arguments passed onto the interesting-ness test"), |
| 50 | cl::cat(Options)); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 51 | |
| 52 | static cl::opt<std::string> |
| 53 | OutputFilename("output", |
| 54 | cl::desc("Specify the output file. default: reduced.ll")); |
| 55 | static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"), |
Roman Lebedev | 34b9bbb | 2020-07-21 15:36:31 +0300 | [diff] [blame] | 56 | cl::aliasopt(OutputFilename), |
| 57 | cl::cat(Options)); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 58 | |
| 59 | static cl::opt<bool> |
| 60 | ReplaceInput("in-place", |
George Burgess IV | 9b9327f | 2019-12-05 19:42:33 -0800 | [diff] [blame] | 61 | cl::desc("WARNING: This option will replace your input file " |
Roman Lebedev | 34b9bbb | 2020-07-21 15:36:31 +0300 | [diff] [blame] | 62 | "with the reduced version!"), |
| 63 | cl::cat(Options)); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 64 | |
| 65 | // Parses IR into a Module and verifies it |
| 66 | static std::unique_ptr<Module> parseInputFile(StringRef Filename, |
| 67 | LLVMContext &Ctxt) { |
| 68 | SMDiagnostic Err; |
| 69 | std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt); |
| 70 | if (!Result) { |
| 71 | Err.print("llvm-reduce", errs()); |
| 72 | return Result; |
| 73 | } |
| 74 | |
| 75 | if (verifyModule(*Result, &errs())) { |
| 76 | errs() << "Error: " << Filename << " - input module is broken!\n"; |
| 77 | return std::unique_ptr<Module>(); |
| 78 | } |
| 79 | |
| 80 | return Result; |
| 81 | } |
| 82 | |
Tyker | 7fef40d | 2020-08-22 19:04:20 +0200 | [diff] [blame] | 83 | void writeOutput(Module *M, StringRef Message) { |
| 84 | if (ReplaceInput) // In-place |
| 85 | OutputFilename = InputFilename.c_str(); |
| 86 | else if (OutputFilename.empty() || OutputFilename == "-") |
| 87 | OutputFilename = "reduced.ll"; |
| 88 | |
| 89 | std::error_code EC; |
| 90 | raw_fd_ostream Out(OutputFilename, EC); |
| 91 | if (EC) { |
| 92 | errs() << "Error opening output file: " << EC.message() << "!\n"; |
| 93 | exit(1); |
| 94 | } |
| 95 | M->print(Out, /*AnnotationWriter=*/nullptr); |
| 96 | errs() << Message << OutputFilename << "\n"; |
| 97 | } |
| 98 | |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 99 | int main(int argc, char **argv) { |
| 100 | InitLLVM X(argc, argv); |
| 101 | |
| 102 | cl::ParseCommandLineOptions(argc, argv, "LLVM automatic testcase reducer.\n"); |
| 103 | |
| 104 | LLVMContext Context; |
| 105 | std::unique_ptr<Module> OriginalProgram = |
| 106 | parseInputFile(InputFilename, Context); |
| 107 | |
| 108 | // Initialize test environment |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame] | 109 | TestRunner Tester(TestFilename, TestArguments); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 110 | Tester.setProgram(std::move(OriginalProgram)); |
| 111 | |
| 112 | // Try to reduce code |
| 113 | runDeltaPasses(Tester); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 114 | |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame] | 115 | if (!Tester.getProgram()) { |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 116 | errs() << "\nCouldnt reduce input :/\n"; |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 117 | } else { |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 118 | // Print reduced file to STDOUT |
| 119 | if (OutputFilename == "-") |
| 120 | Tester.getProgram()->print(outs(), nullptr); |
Tyker | 7fef40d | 2020-08-22 19:04:20 +0200 | [diff] [blame] | 121 | else |
| 122 | writeOutput(Tester.getProgram(), "\nDone reducing! Reduced testcase: "); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |