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