brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · 67084b8 Raw
99 lines · cpp
1//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//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 file defines an interface that allows bugpoint to choose different10// combinations of optimizations to run on the selected input. Bugpoint will11// run these optimizations and record the success/failure of each. This way12// we can hopefully spot bugs in the optimizations.13//14//===----------------------------------------------------------------------===//15 16#include "BugDriver.h"17#include "llvm/Support/FileSystem.h"18#include "llvm/Support/raw_ostream.h"19#include <random>20using namespace llvm;21 22Error23BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {24  setPassesToRun(AllPasses);25  outs() << "Starting bug finding procedure...\n\n";26 27  // Creating a reference output if necessary28  if (Error E = initializeExecutionEnvironment())29    return E;30 31  outs() << "\n";32  if (ReferenceOutputFile.empty()) {33    outs() << "Generating reference output from raw program: \n";34    if (Error E = createReferenceFile(*Program))35      return E;36  }37 38  std::mt19937 randomness(std::random_device{}());39  unsigned num = 1;40  while (true) {41    //42    // Step 1: Randomize the order of the optimizer passes.43    //44    llvm::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);45 46    //47    // Step 2: Run optimizer passes on the program and check for success.48    //49    outs() << "Running selected passes on program to test for crash: ";50    for (const std::string &Pass : PassesToRun)51      outs() << "-" << Pass << " ";52 53    std::string Filename;54    if (runPasses(*Program, PassesToRun, Filename, false)) {55      outs() << "\n";56      outs() << "Optimizer passes caused failure!\n\n";57      return debugOptimizerCrash();58    } else {59      outs() << "Combination " << num << " optimized successfully!\n";60    }61 62    //63    // Step 3: Compile the optimized code.64    //65    outs() << "Running the code generator to test for a crash: ";66    if (Error E = compileProgram(*Program)) {67      outs() << "\n*** compileProgram threw an exception: ";68      outs() << toString(std::move(E));69      return debugCodeGeneratorCrash();70    }71    outs() << '\n';72 73    //74    // Step 4: Run the program and compare its output to the reference75    // output (created above).76    //77    outs() << "*** Checking if passes caused miscompliation:\n";78    Expected<bool> Diff = diffProgram(*Program, Filename, "", false);79    if (Error E = Diff.takeError()) {80      errs() << toString(std::move(E));81      return debugCodeGeneratorCrash();82    }83    if (*Diff) {84      outs() << "\n*** diffProgram returned true!\n";85      Error E = debugMiscompilation();86      if (!E)87        return Error::success();88    }89    outs() << "\n*** diff'd output matches!\n";90 91    sys::fs::remove(Filename);92 93    outs() << "\n\n";94    num++;95  } // end while96 97  // Unreachable.98}99