brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.4 KiB · 94bc7e1 Raw
219 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This program tries to reduce an IR test case for a given interesting-ness10// test. It runs multiple delta debugging passes in order to minimize the input11// file. It's worth noting that this is a part of the bugpoint redesign12// proposal, and thus a *temporary* tool that will eventually be integrated13// into the bugpoint tool itself.14//15//===----------------------------------------------------------------------===//16 17#include "DeltaManager.h"18#include "ReducerWorkItem.h"19#include "TestRunner.h"20#include "llvm/Bitcode/BitcodeReader.h"21#include "llvm/CodeGen/CommandFlags.h"22#include "llvm/Support/CommandLine.h"23#include "llvm/Support/InitLLVM.h"24#include "llvm/Support/Process.h"25#include "llvm/Support/WithColor.h"26#include "llvm/Support/raw_ostream.h"27 28#ifdef _WIN3229#include <windows.h>30#endif31 32using namespace llvm;33 34cl::OptionCategory LLVMReduceOptions("llvm-reduce options");35 36static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden,37                          cl::cat(LLVMReduceOptions));38static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden,39                             cl::cat(LLVMReduceOptions));40 41static cl::opt<bool> PreserveDebugEnvironment(42    "preserve-debug-environment",43    cl::desc("Don't disable features used for crash "44             "debugging (crash reports, llvm-symbolizer and core dumps)"),45    cl::cat(LLVMReduceOptions));46 47static 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 53static cl::opt<std::string> InputFilename(cl::Positional,54                                          cl::desc("<input llvm ll/bc file>"),55                                          cl::cat(LLVMReduceOptions));56 57static cl::opt<std::string>58    TestFilename("test",59                 cl::desc("Name of the interesting-ness test to be run"),60                 cl::cat(LLVMReduceOptions));61 62static cl::list<std::string>63    TestArguments("test-arg",64                  cl::desc("Arguments passed onto the interesting-ness test"),65                  cl::cat(LLVMReduceOptions));66 67static cl::opt<std::string> OutputFilename(68    "output",69    cl::desc("Specify the output file. default: reduced.ll|.bc|.mir"));70static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"),71                                 cl::aliasopt(OutputFilename),72                                 cl::cat(LLVMReduceOptions));73 74static cl::opt<bool>75    ReplaceInput("in-place",76                 cl::desc("WARNING: This option will replace your input file "77                          "with the reduced version!"),78                 cl::cat(LLVMReduceOptions));79 80enum class InputLanguages { None, IR, MIR };81 82static cl::opt<InputLanguages>83    InputLanguage("x", cl::ValueOptional,84                  cl::desc("Input language ('ir' or 'mir')"),85                  cl::init(InputLanguages::None),86                  cl::values(clEnumValN(InputLanguages::IR, "ir", ""),87                             clEnumValN(InputLanguages::MIR, "mir", "")),88                  cl::cat(LLVMReduceOptions));89 90static cl::opt<bool> ForceOutputBitcode(91    "output-bitcode",92    cl::desc("Emit final result as bitcode instead of text IR"), cl::Hidden,93    cl::cat(LLVMReduceOptions));94 95static 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 101static codegen::RegisterCodeGenFlags CGF;102 103/// Turn off crash debugging features104///105/// Crash is expected, so disable crash reports and symbolization to reduce106/// output clutter and avoid potentially slow symbolization.107static void disableEnvironmentDebugFeatures() {108  sys::Process::PreventCoreFiles();109 110  // TODO: Copied from not. Should have a wrapper around setenv.111#ifdef _WIN32112  SetEnvironmentVariableA("LLVM_DISABLE_CRASH_REPORT", "1");113  SetEnvironmentVariableA("LLVM_DISABLE_SYMBOLIZATION", "1");114#else115  setenv("LLVM_DISABLE_CRASH_REPORT", "1", /*overwrite=*/1);116  setenv("LLVM_DISABLE_SYMBOLIZATION", "1", /*overwrite=*/1);117#endif118}119 120static std::pair<StringRef, bool> determineOutputType(bool IsMIR,121                                                      bool InputIsBitcode) {122  bool OutputBitcode = ForceOutputBitcode || InputIsBitcode;123 124  if (ReplaceInput) { // In-place125    OutputFilename = InputFilename.c_str();126  } else if (OutputFilename.empty()) {127    // Default to producing bitcode if the input was bitcode, if not explicitly128    // requested.129 130    OutputFilename =131        IsMIR ? "reduced.mir" : (OutputBitcode ? "reduced.bc" : "reduced.ll");132  }133 134  return {OutputFilename, OutputBitcode};135}136 137int main(int Argc, char **Argv) {138  InitLLVM X(Argc, Argv);139  const StringRef ToolName(Argv[0]);140 141  cl::HideUnrelatedOptions({&LLVMReduceOptions, &getColorCategory()});142  cl::ParseCommandLineOptions(143      Argc, Argv,144      "LLVM automatic testcase reducer.\n"145      "See https://llvm.org/docs/CommandGuide/llvm-reduce.html for more "146      "information.\n");147 148  if (Argc == 1) {149    cl::PrintHelpMessage();150    return 0;151  }152 153  if (PrintDeltaPasses) {154    printDeltaPasses(outs());155    return 0;156  }157 158  bool ReduceModeMIR = false;159  if (InputLanguage != InputLanguages::None) {160    if (InputLanguage == InputLanguages::MIR)161      ReduceModeMIR = true;162  } else if (StringRef(InputFilename).ends_with(".mir")) {163    ReduceModeMIR = true;164  }165 166  if (InputFilename.empty()) {167    WithColor::error(errs(), ToolName)168        << "reduction testcase positional argument must be specified\n";169    return 1;170  }171 172  if (TestFilename.empty()) {173    WithColor::error(errs(), ToolName) << "--test option must be specified\n";174    return 1;175  }176 177  if (!PreserveDebugEnvironment)178    disableEnvironmentDebugFeatures();179 180  LLVMContext Context;181  std::unique_ptr<TargetMachine> TM;182 183  auto [OriginalProgram, InputIsBitcode] =184      parseReducerWorkItem(ToolName, InputFilename, Context, TM, ReduceModeMIR);185  if (!OriginalProgram) {186    return 1;187  }188 189  StringRef OutputFilename;190  bool OutputBitcode;191  std::tie(OutputFilename, OutputBitcode) =192      determineOutputType(ReduceModeMIR, InputIsBitcode);193 194  // Initialize test environment195  TestRunner Tester(TestFilename, TestArguments, std::move(OriginalProgram),196                    std::move(TM), ToolName, OutputFilename, InputIsBitcode,197                    OutputBitcode);198 199  // This parses and writes out the testcase into a temporary file copy for the200  // test, rather than evaluating the source IR directly. This is for the201  // convenience of lit tests; the stripped out comments may have broken the202  // interestingness checks.203  if (!Tester.getProgram().isReduced(Tester)) {204    errs() << "\nInput isn't interesting! Verify interesting-ness test\n";205    return 2;206  }207 208  // Try to reduce code209  runDeltaPasses(Tester, MaxPassIterations);210 211  // Print reduced file to STDOUT212  if (OutputFilename == "-")213    Tester.getProgram().print(outs(), nullptr);214  else215    Tester.writeOutput("Done reducing! Reduced testcase: ");216 217  return 0;218}219