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 "ReducerWorkItem.h" 19 #include "TestRunner.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/CodeGen/CommandFlags.h" 22 #include "llvm/CodeGen/MachineFunction.h" 23 #include "llvm/CodeGen/MachineModuleInfo.h" 24 #include "llvm/IR/LLVMContext.h" 25 #include "llvm/IR/Verifier.h" 26 #include "llvm/IRReader/IRReader.h" 27 #include "llvm/MC/TargetRegistry.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/Host.h" 30 #include "llvm/Support/InitLLVM.h" 31 #include "llvm/Support/SourceMgr.h" 32 #include "llvm/Support/TargetSelect.h" 33 #include "llvm/Support/WithColor.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include <system_error> 36 #include <vector> 37 38 using namespace llvm; 39 40 cl::OptionCategory LLVMReduceOptions("llvm-reduce options"); 41 42 static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden, 43 cl::cat(LLVMReduceOptions)); 44 static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden, 45 cl::cat(LLVMReduceOptions)); 46 47 static cl::opt<bool> 48 PrintDeltaPasses("print-delta-passes", 49 cl::desc("Print list of delta passes, passable to " 50 "--delta-passes as a comma separated list"), 51 cl::cat(LLVMReduceOptions)); 52 53 static cl::opt<std::string> InputFilename(cl::Positional, cl::Required, 54 cl::desc("<input llvm ll/bc file>"), 55 cl::cat(LLVMReduceOptions)); 56 57 static cl::opt<std::string> 58 TestFilename("test", cl::Required, 59 cl::desc("Name of the interesting-ness test to be run"), 60 cl::cat(LLVMReduceOptions)); 61 62 static cl::list<std::string> 63 TestArguments("test-arg", 64 cl::desc("Arguments passed onto the interesting-ness test"), 65 cl::cat(LLVMReduceOptions)); 66 67 static cl::opt<std::string> OutputFilename( 68 "output", cl::desc("Specify the output file. default: reduced.ll|mir")); 69 static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"), 70 cl::aliasopt(OutputFilename), 71 cl::cat(LLVMReduceOptions)); 72 73 static cl::opt<bool> 74 ReplaceInput("in-place", 75 cl::desc("WARNING: This option will replace your input file " 76 "with the reduced version!"), 77 cl::cat(LLVMReduceOptions)); 78 79 enum class InputLanguages { None, IR, MIR }; 80 81 static cl::opt<InputLanguages> 82 InputLanguage("x", cl::ValueOptional, 83 cl::desc("Input language ('ir' or 'mir')"), 84 cl::init(InputLanguages::None), 85 cl::values(clEnumValN(InputLanguages::IR, "ir", ""), 86 clEnumValN(InputLanguages::MIR, "mir", "")), 87 cl::cat(LLVMReduceOptions)); 88 89 static cl::opt<int> 90 MaxPassIterations("max-pass-iterations", 91 cl::desc("Maximum number of times to run the full set " 92 "of delta passes (default=5)"), 93 cl::init(5), cl::cat(LLVMReduceOptions)); 94 95 static codegen::RegisterCodeGenFlags CGF; 96 97 static void initializeTargetInfo() { 98 InitializeAllTargets(); 99 InitializeAllTargetMCs(); 100 InitializeAllAsmPrinters(); 101 InitializeAllAsmParsers(); 102 } 103 104 void writeOutput(ReducerWorkItem &M, StringRef Message) { 105 if (ReplaceInput) // In-place 106 OutputFilename = InputFilename.c_str(); 107 else if (OutputFilename.empty() || OutputFilename == "-") 108 OutputFilename = M.isMIR() ? "reduced.mir" : "reduced.ll"; 109 std::error_code EC; 110 raw_fd_ostream Out(OutputFilename, EC); 111 if (EC) { 112 errs() << "Error opening output file: " << EC.message() << "!\n"; 113 exit(1); 114 } 115 M.print(Out, /*AnnotationWriter=*/nullptr); 116 errs() << Message << OutputFilename << "\n"; 117 } 118 119 int main(int Argc, char **Argv) { 120 InitLLVM X(Argc, Argv); 121 122 cl::HideUnrelatedOptions({&LLVMReduceOptions, &getColorCategory()}); 123 cl::ParseCommandLineOptions(Argc, Argv, "LLVM automatic testcase reducer.\n"); 124 125 bool ReduceModeMIR = false; 126 if (InputLanguage != InputLanguages::None) { 127 if (InputLanguage == InputLanguages::MIR) 128 ReduceModeMIR = true; 129 } else if (StringRef(InputFilename).endswith(".mir")) { 130 ReduceModeMIR = true; 131 } 132 133 if (PrintDeltaPasses) { 134 printDeltaPasses(errs()); 135 return 0; 136 } 137 138 if (ReduceModeMIR) 139 initializeTargetInfo(); 140 141 LLVMContext Context; 142 std::unique_ptr<TargetMachine> TM; 143 144 std::unique_ptr<ReducerWorkItem> OriginalProgram = 145 parseReducerWorkItem(Argv[0], InputFilename, Context, TM, ReduceModeMIR); 146 if (!OriginalProgram) { 147 return 1; 148 } 149 150 // Initialize test environment 151 TestRunner Tester(TestFilename, TestArguments, std::move(OriginalProgram), 152 std::move(TM)); 153 154 // Try to reduce code 155 runDeltaPasses(Tester, MaxPassIterations); 156 157 // Print reduced file to STDOUT 158 if (OutputFilename == "-") 159 Tester.getProgram().print(outs(), nullptr); 160 else 161 writeOutput(Tester.getProgram(), "\nDone reducing! Reduced testcase: "); 162 163 return 0; 164 } 165