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::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)); 37 38 static cl::opt<std::string> InputFilename(cl::Positional, cl::Required, 39 cl::desc("<input llvm ll/bc file>"), 40 cl::cat(Options)); 41 42 static cl::opt<std::string> 43 TestFilename("test", cl::Required, 44 cl::desc("Name of the interesting-ness test to be run"), 45 cl::cat(Options)); 46 47 static cl::list<std::string> 48 TestArguments("test-arg", cl::ZeroOrMore, 49 cl::desc("Arguments passed onto the interesting-ness test"), 50 cl::cat(Options)); 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"), 56 cl::aliasopt(OutputFilename), 57 cl::cat(Options)); 58 59 static cl::opt<bool> 60 ReplaceInput("in-place", 61 cl::desc("WARNING: This option will replace your input file " 62 "with the reduced version!"), 63 cl::cat(Options)); 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 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 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 109 TestRunner Tester(TestFilename, TestArguments); 110 Tester.setProgram(std::move(OriginalProgram)); 111 112 // Try to reduce code 113 runDeltaPasses(Tester); 114 115 if (!Tester.getProgram()) { 116 errs() << "\nCouldnt reduce input :/\n"; 117 } else { 118 // Print reduced file to STDOUT 119 if (OutputFilename == "-") 120 Tester.getProgram()->print(outs(), nullptr); 121 else 122 writeOutput(Tester.getProgram(), "\nDone reducing! Reduced testcase: "); 123 } 124 125 return 0; 126 } 127