Path: blob/main/contrib/llvm-project/llvm/tools/bugpoint/Miscompilation.cpp
35230 views
//===- Miscompilation.cpp - Debug program miscompilations -----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements optimizer and code generation miscompilation debugging9// support.10//11//===----------------------------------------------------------------------===//1213#include "BugDriver.h"14#include "ListReducer.h"15#include "ToolRunner.h"16#include "llvm/Config/config.h" // for HAVE_LINK_R17#include "llvm/IR/Constants.h"18#include "llvm/IR/DerivedTypes.h"19#include "llvm/IR/Instructions.h"20#include "llvm/IR/Module.h"21#include "llvm/IR/Verifier.h"22#include "llvm/Linker/Linker.h"23#include "llvm/Pass.h"24#include "llvm/Support/CommandLine.h"25#include "llvm/Support/FileUtilities.h"26#include "llvm/Transforms/Utils/Cloning.h"2728using namespace llvm;2930namespace llvm {31extern cl::opt<std::string> OutputPrefix;32extern cl::list<std::string> InputArgv;33} // end namespace llvm3435namespace {36static llvm::cl::opt<bool> DisableLoopExtraction(37"disable-loop-extraction",38cl::desc("Don't extract loops when searching for miscompilations"),39cl::init(false));40static llvm::cl::opt<bool> DisableBlockExtraction(41"disable-block-extraction",42cl::desc("Don't extract blocks when searching for miscompilations"),43cl::init(false));4445class ReduceMiscompilingPasses : public ListReducer<std::string> {46BugDriver &BD;4748public:49ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}5051Expected<TestResult> doTest(std::vector<std::string> &Prefix,52std::vector<std::string> &Suffix) override;53};54} // end anonymous namespace5556/// TestResult - After passes have been split into a test group and a control57/// group, see if they still break the program.58///59Expected<ReduceMiscompilingPasses::TestResult>60ReduceMiscompilingPasses::doTest(std::vector<std::string> &Prefix,61std::vector<std::string> &Suffix) {62// First, run the program with just the Suffix passes. If it is still broken63// with JUST the kept passes, discard the prefix passes.64outs() << "Checking to see if '" << getPassesString(Suffix)65<< "' compiles correctly: ";6667std::string BitcodeResult;68if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false /*delete*/,69true /*quiet*/)) {70errs() << " Error running this sequence of passes"71<< " on the input program!\n";72BD.setPassesToRun(Suffix);73BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);74// TODO: This should propagate the error instead of exiting.75if (Error E = BD.debugOptimizerCrash())76exit(1);77exit(0);78}7980// Check to see if the finished program matches the reference output...81Expected<bool> Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",82true /*delete bitcode*/);83if (Error E = Diff.takeError())84return std::move(E);85if (*Diff) {86outs() << " nope.\n";87if (Suffix.empty()) {88errs() << BD.getToolName() << ": I'm confused: the test fails when "89<< "no passes are run, nondeterministic program?\n";90exit(1);91}92return KeepSuffix; // Miscompilation detected!93}94outs() << " yup.\n"; // No miscompilation!9596if (Prefix.empty())97return NoFailure;9899// Next, see if the program is broken if we run the "prefix" passes first,100// then separately run the "kept" passes.101outs() << "Checking to see if '" << getPassesString(Prefix)102<< "' compiles correctly: ";103104// If it is not broken with the kept passes, it's possible that the prefix105// passes must be run before the kept passes to break it. If the program106// WORKS after the prefix passes, but then fails if running the prefix AND107// kept passes, we can update our bitcode file to include the result of the108// prefix passes, then discard the prefix passes.109//110if (BD.runPasses(BD.getProgram(), Prefix, BitcodeResult, false /*delete*/,111true /*quiet*/)) {112errs() << " Error running this sequence of passes"113<< " on the input program!\n";114BD.setPassesToRun(Prefix);115BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);116// TODO: This should propagate the error instead of exiting.117if (Error E = BD.debugOptimizerCrash())118exit(1);119exit(0);120}121122// If the prefix maintains the predicate by itself, only keep the prefix!123Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false);124if (Error E = Diff.takeError())125return std::move(E);126if (*Diff) {127outs() << " nope.\n";128sys::fs::remove(BitcodeResult);129return KeepPrefix;130}131outs() << " yup.\n"; // No miscompilation!132133// Ok, so now we know that the prefix passes work, try running the suffix134// passes on the result of the prefix passes.135//136std::unique_ptr<Module> PrefixOutput =137parseInputFile(BitcodeResult, BD.getContext());138if (!PrefixOutput) {139errs() << BD.getToolName() << ": Error reading bitcode file '"140<< BitcodeResult << "'!\n";141exit(1);142}143sys::fs::remove(BitcodeResult);144145// Don't check if there are no passes in the suffix.146if (Suffix.empty())147return NoFailure;148149outs() << "Checking to see if '" << getPassesString(Suffix)150<< "' passes compile correctly after the '" << getPassesString(Prefix)151<< "' passes: ";152153std::unique_ptr<Module> OriginalInput =154BD.swapProgramIn(std::move(PrefixOutput));155if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false /*delete*/,156true /*quiet*/)) {157errs() << " Error running this sequence of passes"158<< " on the input program!\n";159BD.setPassesToRun(Suffix);160BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);161// TODO: This should propagate the error instead of exiting.162if (Error E = BD.debugOptimizerCrash())163exit(1);164exit(0);165}166167// Run the result...168Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",169true /*delete bitcode*/);170if (Error E = Diff.takeError())171return std::move(E);172if (*Diff) {173outs() << " nope.\n";174return KeepSuffix;175}176177// Otherwise, we must not be running the bad pass anymore.178outs() << " yup.\n"; // No miscompilation!179// Restore orig program & free test.180BD.setNewProgram(std::move(OriginalInput));181return NoFailure;182}183184namespace {185class ReduceMiscompilingFunctions : public ListReducer<Function *> {186BugDriver &BD;187Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,188std::unique_ptr<Module>);189190public:191ReduceMiscompilingFunctions(BugDriver &bd,192Expected<bool> (*F)(BugDriver &,193std::unique_ptr<Module>,194std::unique_ptr<Module>))195: BD(bd), TestFn(F) {}196197Expected<TestResult> doTest(std::vector<Function *> &Prefix,198std::vector<Function *> &Suffix) override {199if (!Suffix.empty()) {200Expected<bool> Ret = TestFuncs(Suffix);201if (Error E = Ret.takeError())202return std::move(E);203if (*Ret)204return KeepSuffix;205}206if (!Prefix.empty()) {207Expected<bool> Ret = TestFuncs(Prefix);208if (Error E = Ret.takeError())209return std::move(E);210if (*Ret)211return KeepPrefix;212}213return NoFailure;214}215216Expected<bool> TestFuncs(const std::vector<Function *> &Prefix);217};218} // end anonymous namespace219220/// Given two modules, link them together and run the program, checking to see221/// if the program matches the diff. If there is an error, return NULL. If not,222/// return the merged module. The Broken argument will be set to true if the223/// output is different. If the DeleteInputs argument is set to true then this224/// function deletes both input modules before it returns.225///226static Expected<std::unique_ptr<Module>> testMergedProgram(const BugDriver &BD,227const Module &M1,228const Module &M2,229bool &Broken) {230// Resulting merge of M1 and M2.231auto Merged = CloneModule(M1);232if (Linker::linkModules(*Merged, CloneModule(M2)))233// TODO: Shouldn't we thread the error up instead of exiting?234exit(1);235236// Execute the program.237Expected<bool> Diff = BD.diffProgram(*Merged, "", "", false);238if (Error E = Diff.takeError())239return std::move(E);240Broken = *Diff;241return std::move(Merged);242}243244/// split functions in a Module into two groups: those that are under245/// consideration for miscompilation vs. those that are not, and test246/// accordingly. Each group of functions becomes a separate Module.247Expected<bool>248ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function *> &Funcs) {249// Test to see if the function is misoptimized if we ONLY run it on the250// functions listed in Funcs.251outs() << "Checking to see if the program is misoptimized when "252<< (Funcs.size() == 1 ? "this function is" : "these functions are")253<< " run through the pass"254<< (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";255PrintFunctionList(Funcs);256outs() << '\n';257258// Create a clone for two reasons:259// * If the optimization passes delete any function, the deleted function260// will be in the clone and Funcs will still point to valid memory261// * If the optimization passes use interprocedural information to break262// a function, we want to continue with the original function. Otherwise263// we can conclude that a function triggers the bug when in fact one264// needs a larger set of original functions to do so.265ValueToValueMapTy VMap;266std::unique_ptr<Module> Clone = CloneModule(BD.getProgram(), VMap);267std::unique_ptr<Module> Orig = BD.swapProgramIn(std::move(Clone));268269std::vector<Function *> FuncsOnClone;270for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {271Function *F = cast<Function>(VMap[Funcs[i]]);272FuncsOnClone.push_back(F);273}274275// Split the module into the two halves of the program we want.276VMap.clear();277std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);278std::unique_ptr<Module> ToOptimize =279SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap);280281Expected<bool> Broken =282TestFn(BD, std::move(ToOptimize), std::move(ToNotOptimize));283284BD.setNewProgram(std::move(Orig));285286return Broken;287}288289/// Give anonymous global values names.290static void DisambiguateGlobalSymbols(Module &M) {291for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E;292++I)293if (!I->hasName())294I->setName("anon_global");295for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)296if (!I->hasName())297I->setName("anon_fn");298}299300/// Given a reduced list of functions that still exposed the bug, check to see301/// if we can extract the loops in the region without obscuring the bug. If so,302/// it reduces the amount of code identified.303///304static Expected<bool>305ExtractLoops(BugDriver &BD,306Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,307std::unique_ptr<Module>),308std::vector<Function *> &MiscompiledFunctions) {309bool MadeChange = false;310while (true) {311if (BugpointIsInterrupted)312return MadeChange;313314ValueToValueMapTy VMap;315std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);316std::unique_ptr<Module> ToOptimize = SplitFunctionsOutOfModule(317ToNotOptimize.get(), MiscompiledFunctions, VMap);318std::unique_ptr<Module> ToOptimizeLoopExtracted =319BD.extractLoop(ToOptimize.get());320if (!ToOptimizeLoopExtracted)321// If the loop extractor crashed or if there were no extractible loops,322// then this chapter of our odyssey is over with.323return MadeChange;324325errs() << "Extracted a loop from the breaking portion of the program.\n";326327// Bugpoint is intentionally not very trusting of LLVM transformations. In328// particular, we're not going to assume that the loop extractor works, so329// we're going to test the newly loop extracted program to make sure nothing330// has broken. If something broke, then we'll inform the user and stop331// extraction.332AbstractInterpreter *AI = BD.switchToSafeInterpreter();333bool Failure;334Expected<std::unique_ptr<Module>> New = testMergedProgram(335BD, *ToOptimizeLoopExtracted, *ToNotOptimize, Failure);336if (Error E = New.takeError())337return std::move(E);338if (!*New)339return false;340341// Delete the original and set the new program.342std::unique_ptr<Module> Old = BD.swapProgramIn(std::move(*New));343for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)344MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]);345346if (Failure) {347BD.switchToInterpreter(AI);348349// Merged program doesn't work anymore!350errs() << " *** ERROR: Loop extraction broke the program. :("351<< " Please report a bug!\n";352errs() << " Continuing on with un-loop-extracted version.\n";353354BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc",355*ToNotOptimize);356BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc",357*ToOptimize);358BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc",359*ToOptimizeLoopExtracted);360361errs() << "Please submit the " << OutputPrefix362<< "-loop-extract-fail-*.bc files.\n";363return MadeChange;364}365BD.switchToInterpreter(AI);366367outs() << " Testing after loop extraction:\n";368// Clone modules, the tester function will free them.369std::unique_ptr<Module> TOLEBackup =370CloneModule(*ToOptimizeLoopExtracted, VMap);371std::unique_ptr<Module> TNOBackup = CloneModule(*ToNotOptimize, VMap);372373for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)374MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]);375376Expected<bool> Result = TestFn(BD, std::move(ToOptimizeLoopExtracted),377std::move(ToNotOptimize));378if (Error E = Result.takeError())379return std::move(E);380381ToOptimizeLoopExtracted = std::move(TOLEBackup);382ToNotOptimize = std::move(TNOBackup);383384if (!*Result) {385outs() << "*** Loop extraction masked the problem. Undoing.\n";386// If the program is not still broken, then loop extraction did something387// that masked the error. Stop loop extraction now.388389std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions;390for (Function *F : MiscompiledFunctions) {391MisCompFunctions.emplace_back(std::string(F->getName()),392F->getFunctionType());393}394395if (Linker::linkModules(*ToNotOptimize,396std::move(ToOptimizeLoopExtracted)))397exit(1);398399MiscompiledFunctions.clear();400for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {401Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);402403assert(NewF && "Function not found??");404MiscompiledFunctions.push_back(NewF);405}406407BD.setNewProgram(std::move(ToNotOptimize));408return MadeChange;409}410411outs() << "*** Loop extraction successful!\n";412413std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions;414for (Module::iterator I = ToOptimizeLoopExtracted->begin(),415E = ToOptimizeLoopExtracted->end();416I != E; ++I)417if (!I->isDeclaration())418MisCompFunctions.emplace_back(std::string(I->getName()),419I->getFunctionType());420421// Okay, great! Now we know that we extracted a loop and that loop422// extraction both didn't break the program, and didn't mask the problem.423// Replace the current program with the loop extracted version, and try to424// extract another loop.425if (Linker::linkModules(*ToNotOptimize, std::move(ToOptimizeLoopExtracted)))426exit(1);427428// All of the Function*'s in the MiscompiledFunctions list are in the old429// module. Update this list to include all of the functions in the430// optimized and loop extracted module.431MiscompiledFunctions.clear();432for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {433Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);434435assert(NewF && "Function not found??");436MiscompiledFunctions.push_back(NewF);437}438439BD.setNewProgram(std::move(ToNotOptimize));440MadeChange = true;441}442}443444namespace {445class ReduceMiscompiledBlocks : public ListReducer<BasicBlock *> {446BugDriver &BD;447Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,448std::unique_ptr<Module>);449std::vector<Function *> FunctionsBeingTested;450451public:452ReduceMiscompiledBlocks(BugDriver &bd,453Expected<bool> (*F)(BugDriver &,454std::unique_ptr<Module>,455std::unique_ptr<Module>),456const std::vector<Function *> &Fns)457: BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}458459Expected<TestResult> doTest(std::vector<BasicBlock *> &Prefix,460std::vector<BasicBlock *> &Suffix) override {461if (!Suffix.empty()) {462Expected<bool> Ret = TestFuncs(Suffix);463if (Error E = Ret.takeError())464return std::move(E);465if (*Ret)466return KeepSuffix;467}468if (!Prefix.empty()) {469Expected<bool> Ret = TestFuncs(Prefix);470if (Error E = Ret.takeError())471return std::move(E);472if (*Ret)473return KeepPrefix;474}475return NoFailure;476}477478Expected<bool> TestFuncs(const std::vector<BasicBlock *> &BBs);479};480} // end anonymous namespace481482/// TestFuncs - Extract all blocks for the miscompiled functions except for the483/// specified blocks. If the problem still exists, return true.484///485Expected<bool>486ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock *> &BBs) {487// Test to see if the function is misoptimized if we ONLY run it on the488// functions listed in Funcs.489outs() << "Checking to see if the program is misoptimized when all ";490if (!BBs.empty()) {491outs() << "but these " << BBs.size() << " blocks are extracted: ";492for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)493outs() << BBs[i]->getName() << " ";494if (BBs.size() > 10)495outs() << "...";496} else {497outs() << "blocks are extracted.";498}499outs() << '\n';500501// Split the module into the two halves of the program we want.502ValueToValueMapTy VMap;503std::unique_ptr<Module> Clone = CloneModule(BD.getProgram(), VMap);504std::unique_ptr<Module> Orig = BD.swapProgramIn(std::move(Clone));505std::vector<Function *> FuncsOnClone;506std::vector<BasicBlock *> BBsOnClone;507for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) {508Function *F = cast<Function>(VMap[FunctionsBeingTested[i]]);509FuncsOnClone.push_back(F);510}511for (unsigned i = 0, e = BBs.size(); i != e; ++i) {512BasicBlock *BB = cast<BasicBlock>(VMap[BBs[i]]);513BBsOnClone.push_back(BB);514}515VMap.clear();516517std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);518std::unique_ptr<Module> ToOptimize =519SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap);520521// Try the extraction. If it doesn't work, then the block extractor crashed522// or something, in which case bugpoint can't chase down this possibility.523if (std::unique_ptr<Module> New =524BD.extractMappedBlocksFromModule(BBsOnClone, ToOptimize.get())) {525Expected<bool> Ret = TestFn(BD, std::move(New), std::move(ToNotOptimize));526BD.setNewProgram(std::move(Orig));527return Ret;528}529BD.setNewProgram(std::move(Orig));530return false;531}532533/// Given a reduced list of functions that still expose the bug, extract as many534/// basic blocks from the region as possible without obscuring the bug.535///536static Expected<bool>537ExtractBlocks(BugDriver &BD,538Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,539std::unique_ptr<Module>),540std::vector<Function *> &MiscompiledFunctions) {541if (BugpointIsInterrupted)542return false;543544std::vector<BasicBlock *> Blocks;545for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)546for (BasicBlock &BB : *MiscompiledFunctions[i])547Blocks.push_back(&BB);548549// Use the list reducer to identify blocks that can be extracted without550// obscuring the bug. The Blocks list will end up containing blocks that must551// be retained from the original program.552unsigned OldSize = Blocks.size();553554// Check to see if all blocks are extractible first.555Expected<bool> Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)556.TestFuncs(std::vector<BasicBlock *>());557if (Error E = Ret.takeError())558return std::move(E);559if (*Ret) {560Blocks.clear();561} else {562Expected<bool> Ret =563ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)564.reduceList(Blocks);565if (Error E = Ret.takeError())566return std::move(E);567if (Blocks.size() == OldSize)568return false;569}570571ValueToValueMapTy VMap;572std::unique_ptr<Module> ProgClone = CloneModule(BD.getProgram(), VMap);573std::unique_ptr<Module> ToExtract =574SplitFunctionsOutOfModule(ProgClone.get(), MiscompiledFunctions, VMap);575std::unique_ptr<Module> Extracted =576BD.extractMappedBlocksFromModule(Blocks, ToExtract.get());577if (!Extracted) {578// Weird, extraction should have worked.579errs() << "Nondeterministic problem extracting blocks??\n";580return false;581}582583// Otherwise, block extraction succeeded. Link the two program fragments back584// together.585586std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions;587for (Module::iterator I = Extracted->begin(), E = Extracted->end(); I != E;588++I)589if (!I->isDeclaration())590MisCompFunctions.emplace_back(std::string(I->getName()),591I->getFunctionType());592593if (Linker::linkModules(*ProgClone, std::move(Extracted)))594exit(1);595596// Update the list of miscompiled functions.597MiscompiledFunctions.clear();598599for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {600Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);601assert(NewF && "Function not found??");602MiscompiledFunctions.push_back(NewF);603}604605// Set the new program and delete the old one.606BD.setNewProgram(std::move(ProgClone));607608return true;609}610611/// This is a generic driver to narrow down miscompilations, either in an612/// optimization or a code generator.613///614static Expected<std::vector<Function *>> DebugAMiscompilation(615BugDriver &BD,616Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,617std::unique_ptr<Module>)) {618// Okay, now that we have reduced the list of passes which are causing the619// failure, see if we can pin down which functions are being620// miscompiled... first build a list of all of the non-external functions in621// the program.622std::vector<Function *> MiscompiledFunctions;623Module &Prog = BD.getProgram();624for (Function &F : Prog)625if (!F.isDeclaration())626MiscompiledFunctions.push_back(&F);627628// Do the reduction...629if (!BugpointIsInterrupted) {630Expected<bool> Ret = ReduceMiscompilingFunctions(BD, TestFn)631.reduceList(MiscompiledFunctions);632if (Error E = Ret.takeError()) {633errs() << "\n***Cannot reduce functions: ";634return std::move(E);635}636}637outs() << "\n*** The following function"638<< (MiscompiledFunctions.size() == 1 ? " is" : "s are")639<< " being miscompiled: ";640PrintFunctionList(MiscompiledFunctions);641outs() << '\n';642643// See if we can rip any loops out of the miscompiled functions and still644// trigger the problem.645646if (!BugpointIsInterrupted && !DisableLoopExtraction) {647Expected<bool> Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions);648if (Error E = Ret.takeError())649return std::move(E);650if (*Ret) {651// Okay, we extracted some loops and the problem still appears. See if652// we can eliminate some of the created functions from being candidates.653DisambiguateGlobalSymbols(BD.getProgram());654655// Do the reduction...656if (!BugpointIsInterrupted)657Ret = ReduceMiscompilingFunctions(BD, TestFn)658.reduceList(MiscompiledFunctions);659if (Error E = Ret.takeError())660return std::move(E);661662outs() << "\n*** The following function"663<< (MiscompiledFunctions.size() == 1 ? " is" : "s are")664<< " being miscompiled: ";665PrintFunctionList(MiscompiledFunctions);666outs() << '\n';667}668}669670if (!BugpointIsInterrupted && !DisableBlockExtraction) {671Expected<bool> Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions);672if (Error E = Ret.takeError())673return std::move(E);674if (*Ret) {675// Okay, we extracted some blocks and the problem still appears. See if676// we can eliminate some of the created functions from being candidates.677DisambiguateGlobalSymbols(BD.getProgram());678679// Do the reduction...680Ret = ReduceMiscompilingFunctions(BD, TestFn)681.reduceList(MiscompiledFunctions);682if (Error E = Ret.takeError())683return std::move(E);684685outs() << "\n*** The following function"686<< (MiscompiledFunctions.size() == 1 ? " is" : "s are")687<< " being miscompiled: ";688PrintFunctionList(MiscompiledFunctions);689outs() << '\n';690}691}692693return MiscompiledFunctions;694}695696/// This is the predicate function used to check to see if the "Test" portion of697/// the program is misoptimized. If so, return true. In any case, both module698/// arguments are deleted.699///700static Expected<bool> TestOptimizer(BugDriver &BD, std::unique_ptr<Module> Test,701std::unique_ptr<Module> Safe) {702// Run the optimization passes on ToOptimize, producing a transformed version703// of the functions being tested.704outs() << " Optimizing functions being tested: ";705std::unique_ptr<Module> Optimized =706BD.runPassesOn(Test.get(), BD.getPassesToRun());707if (!Optimized) {708errs() << " Error running this sequence of passes"709<< " on the input program!\n";710BD.EmitProgressBitcode(*Test, "pass-error", false);711BD.setNewProgram(std::move(Test));712if (Error E = BD.debugOptimizerCrash())713return std::move(E);714return false;715}716outs() << "done.\n";717718outs() << " Checking to see if the merged program executes correctly: ";719bool Broken;720auto Result = testMergedProgram(BD, *Optimized, *Safe, Broken);721if (Error E = Result.takeError())722return std::move(E);723if (auto New = std::move(*Result)) {724outs() << (Broken ? " nope.\n" : " yup.\n");725// Delete the original and set the new program.726BD.setNewProgram(std::move(New));727}728return Broken;729}730731/// debugMiscompilation - This method is used when the passes selected are not732/// crashing, but the generated output is semantically different from the733/// input.734///735Error BugDriver::debugMiscompilation() {736// Make sure something was miscompiled...737if (!BugpointIsInterrupted) {738Expected<bool> Result =739ReduceMiscompilingPasses(*this).reduceList(PassesToRun);740if (Error E = Result.takeError())741return E;742if (!*Result)743return make_error<StringError>(744"*** Optimized program matches reference output! No problem"745" detected...\nbugpoint can't help you with your problem!\n",746inconvertibleErrorCode());747}748749outs() << "\n*** Found miscompiling pass"750<< (getPassesToRun().size() == 1 ? "" : "es") << ": "751<< getPassesString(getPassesToRun()) << '\n';752EmitProgressBitcode(*Program, "passinput");753754Expected<std::vector<Function *>> MiscompiledFunctions =755DebugAMiscompilation(*this, TestOptimizer);756if (Error E = MiscompiledFunctions.takeError())757return E;758759// Output a bunch of bitcode files for the user...760outs() << "Outputting reduced bitcode files which expose the problem:\n";761ValueToValueMapTy VMap;762Module *ToNotOptimize = CloneModule(getProgram(), VMap).release();763Module *ToOptimize =764SplitFunctionsOutOfModule(ToNotOptimize, *MiscompiledFunctions, VMap)765.release();766767outs() << " Non-optimized portion: ";768EmitProgressBitcode(*ToNotOptimize, "tonotoptimize", true);769delete ToNotOptimize; // Delete hacked module.770771outs() << " Portion that is input to optimizer: ";772EmitProgressBitcode(*ToOptimize, "tooptimize");773delete ToOptimize; // Delete hacked module.774775return Error::success();776}777778/// Get the specified modules ready for code generator testing.779///780static std::unique_ptr<Module>781CleanupAndPrepareModules(BugDriver &BD, std::unique_ptr<Module> Test,782Module *Safe) {783// Clean up the modules, removing extra cruft that we don't need anymore...784Test = BD.performFinalCleanups(std::move(Test));785786// If we are executing the JIT, we have several nasty issues to take care of.787if (!BD.isExecutingJIT())788return Test;789790// First, if the main function is in the Safe module, we must add a stub to791// the Test module to call into it. Thus, we create a new function `main'792// which just calls the old one.793if (Function *oldMain = Safe->getFunction("main"))794if (!oldMain->isDeclaration()) {795// Rename it796oldMain->setName("llvm_bugpoint_old_main");797// Create a NEW `main' function with same type in the test module.798Function *newMain =799Function::Create(oldMain->getFunctionType(),800GlobalValue::ExternalLinkage, "main", Test.get());801// Create an `oldmain' prototype in the test module, which will802// corresponds to the real main function in the same module.803Function *oldMainProto = Function::Create(oldMain->getFunctionType(),804GlobalValue::ExternalLinkage,805oldMain->getName(), Test.get());806// Set up and remember the argument list for the main function.807std::vector<Value *> args;808for (Function::arg_iterator I = newMain->arg_begin(),809E = newMain->arg_end(),810OI = oldMain->arg_begin();811I != E; ++I, ++OI) {812I->setName(OI->getName()); // Copy argument names from oldMain813args.push_back(&*I);814}815816// Call the old main function and return its result817BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain);818CallInst *call = CallInst::Create(oldMainProto, args, "", BB);819820// If the type of old function wasn't void, return value of call821ReturnInst::Create(Safe->getContext(), call, BB);822}823824// The second nasty issue we must deal with in the JIT is that the Safe825// module cannot directly reference any functions defined in the test826// module. Instead, we use a JIT API call to dynamically resolve the827// symbol.828829// Add the resolver to the Safe module.830// Prototype: void *getPointerToNamedFunction(const char* Name)831FunctionCallee resolverFunc = Safe->getOrInsertFunction(832"getPointerToNamedFunction", PointerType::getUnqual(Safe->getContext()),833PointerType::getUnqual(Safe->getContext()));834835// Use the function we just added to get addresses of functions we need.836for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {837if (F->isDeclaration() && !F->use_empty() &&838&*F != resolverFunc.getCallee() &&839!F->isIntrinsic() /* ignore intrinsics */) {840Function *TestFn = Test->getFunction(F->getName());841842// Don't forward functions which are external in the test module too.843if (TestFn && !TestFn->isDeclaration()) {844// 1. Add a string constant with its name to the global file845Constant *InitArray =846ConstantDataArray::getString(F->getContext(), F->getName());847GlobalVariable *funcName = new GlobalVariable(848*Safe, InitArray->getType(), true /*isConstant*/,849GlobalValue::InternalLinkage, InitArray, F->getName() + "_name");850851// 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an852// sbyte* so it matches the signature of the resolver function.853854// GetElementPtr *funcName, ulong 0, ulong 0855std::vector<Constant *> GEPargs(8562, Constant::getNullValue(Type::getInt32Ty(F->getContext())));857Value *GEP = ConstantExpr::getGetElementPtr(InitArray->getType(),858funcName, GEPargs);859std::vector<Value *> ResolverArgs;860ResolverArgs.push_back(GEP);861862// Rewrite uses of F in global initializers, etc. to uses of a wrapper863// function that dynamically resolves the calls to F via our JIT API864if (!F->use_empty()) {865// Create a new global to hold the cached function pointer.866Constant *NullPtr = ConstantPointerNull::get(F->getType());867GlobalVariable *Cache = new GlobalVariable(868*F->getParent(), F->getType(), false,869GlobalValue::InternalLinkage, NullPtr, F->getName() + ".fpcache");870871// Construct a new stub function that will re-route calls to F872FunctionType *FuncTy = F->getFunctionType();873Function *FuncWrapper =874Function::Create(FuncTy, GlobalValue::InternalLinkage,875F->getName() + "_wrapper", F->getParent());876BasicBlock *EntryBB =877BasicBlock::Create(F->getContext(), "entry", FuncWrapper);878BasicBlock *DoCallBB =879BasicBlock::Create(F->getContext(), "usecache", FuncWrapper);880BasicBlock *LookupBB =881BasicBlock::Create(F->getContext(), "lookupfp", FuncWrapper);882883// Check to see if we already looked up the value.884Value *CachedVal =885new LoadInst(F->getType(), Cache, "fpcache", EntryBB);886Value *IsNull = new ICmpInst(EntryBB, ICmpInst::ICMP_EQ, CachedVal,887NullPtr, "isNull");888BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB);889890// Resolve the call to function F via the JIT API:891//892// call resolver(GetElementPtr...)893CallInst *Resolver = CallInst::Create(resolverFunc, ResolverArgs,894"resolver", LookupBB);895896// Cast the result from the resolver to correctly-typed function.897CastInst *CastedResolver = new BitCastInst(898Resolver, PointerType::getUnqual(F->getFunctionType()),899"resolverCast", LookupBB);900901// Save the value in our cache.902new StoreInst(CastedResolver, Cache, LookupBB);903BranchInst::Create(DoCallBB, LookupBB);904905PHINode *FuncPtr =906PHINode::Create(NullPtr->getType(), 2, "fp", DoCallBB);907FuncPtr->addIncoming(CastedResolver, LookupBB);908FuncPtr->addIncoming(CachedVal, EntryBB);909910// Save the argument list.911std::vector<Value *> Args;912for (Argument &A : FuncWrapper->args())913Args.push_back(&A);914915// Pass on the arguments to the real function, return its result916if (F->getReturnType()->isVoidTy()) {917CallInst::Create(FuncTy, FuncPtr, Args, "", DoCallBB);918ReturnInst::Create(F->getContext(), DoCallBB);919} else {920CallInst *Call =921CallInst::Create(FuncTy, FuncPtr, Args, "retval", DoCallBB);922ReturnInst::Create(F->getContext(), Call, DoCallBB);923}924925// Use the wrapper function instead of the old function926F->replaceAllUsesWith(FuncWrapper);927}928}929}930}931932if (verifyModule(*Test) || verifyModule(*Safe)) {933errs() << "Bugpoint has a bug, which corrupted a module!!\n";934abort();935}936937return Test;938}939940/// This is the predicate function used to check to see if the "Test" portion of941/// the program is miscompiled by the code generator under test. If so, return942/// true. In any case, both module arguments are deleted.943///944static Expected<bool> TestCodeGenerator(BugDriver &BD,945std::unique_ptr<Module> Test,946std::unique_ptr<Module> Safe) {947Test = CleanupAndPrepareModules(BD, std::move(Test), Safe.get());948949SmallString<128> TestModuleBC;950int TestModuleFD;951std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc",952TestModuleFD, TestModuleBC);953if (EC) {954errs() << BD.getToolName()955<< "Error making unique filename: " << EC.message() << "\n";956exit(1);957}958if (BD.writeProgramToFile(std::string(TestModuleBC), TestModuleFD, *Test)) {959errs() << "Error writing bitcode to `" << TestModuleBC.str()960<< "'\nExiting.";961exit(1);962}963964FileRemover TestModuleBCRemover(TestModuleBC.str(), !SaveTemps);965966// Make the shared library967SmallString<128> SafeModuleBC;968int SafeModuleFD;969EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD,970SafeModuleBC);971if (EC) {972errs() << BD.getToolName()973<< "Error making unique filename: " << EC.message() << "\n";974exit(1);975}976977if (BD.writeProgramToFile(std::string(SafeModuleBC), SafeModuleFD, *Safe)) {978errs() << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting.";979exit(1);980}981982FileRemover SafeModuleBCRemover(SafeModuleBC.str(), !SaveTemps);983984Expected<std::string> SharedObject =985BD.compileSharedObject(std::string(SafeModuleBC));986if (Error E = SharedObject.takeError())987return std::move(E);988989FileRemover SharedObjectRemover(*SharedObject, !SaveTemps);990991// Run the code generator on the `Test' code, loading the shared library.992// The function returns whether or not the new output differs from reference.993Expected<bool> Result = BD.diffProgram(994BD.getProgram(), std::string(TestModuleBC), *SharedObject, false);995if (Error E = Result.takeError())996return std::move(E);997998if (*Result)999errs() << ": still failing!\n";1000else1001errs() << ": didn't fail.\n";10021003return Result;1004}10051006/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.1007///1008Error BugDriver::debugCodeGenerator() {1009if ((void *)SafeInterpreter == (void *)Interpreter) {1010Expected<std::string> Result =1011executeProgramSafely(*Program, "bugpoint.safe.out");1012if (Result) {1013outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "1014<< "the reference diff. This may be due to a\n front-end "1015<< "bug or a bug in the original program, but this can also "1016<< "happen if bugpoint isn't running the program with the "1017<< "right flags or input.\n I left the result of executing "1018<< "the program with the \"safe\" backend in this file for "1019<< "you: '" << *Result << "'.\n";1020}1021return Error::success();1022}10231024DisambiguateGlobalSymbols(*Program);10251026Expected<std::vector<Function *>> Funcs =1027DebugAMiscompilation(*this, TestCodeGenerator);1028if (Error E = Funcs.takeError())1029return E;10301031// Split the module into the two halves of the program we want.1032ValueToValueMapTy VMap;1033std::unique_ptr<Module> ToNotCodeGen = CloneModule(getProgram(), VMap);1034std::unique_ptr<Module> ToCodeGen =1035SplitFunctionsOutOfModule(ToNotCodeGen.get(), *Funcs, VMap);10361037// Condition the modules1038ToCodeGen =1039CleanupAndPrepareModules(*this, std::move(ToCodeGen), ToNotCodeGen.get());10401041SmallString<128> TestModuleBC;1042int TestModuleFD;1043std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc",1044TestModuleFD, TestModuleBC);1045if (EC) {1046errs() << getToolName() << "Error making unique filename: " << EC.message()1047<< "\n";1048exit(1);1049}10501051if (writeProgramToFile(std::string(TestModuleBC), TestModuleFD, *ToCodeGen)) {1052errs() << "Error writing bitcode to `" << TestModuleBC << "'\nExiting.";1053exit(1);1054}10551056// Make the shared library1057SmallString<128> SafeModuleBC;1058int SafeModuleFD;1059EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD,1060SafeModuleBC);1061if (EC) {1062errs() << getToolName() << "Error making unique filename: " << EC.message()1063<< "\n";1064exit(1);1065}10661067if (writeProgramToFile(std::string(SafeModuleBC), SafeModuleFD,1068*ToNotCodeGen)) {1069errs() << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting.";1070exit(1);1071}1072Expected<std::string> SharedObject =1073compileSharedObject(std::string(SafeModuleBC));1074if (Error E = SharedObject.takeError())1075return E;10761077outs() << "You can reproduce the problem with the command line: \n";1078if (isExecutingJIT()) {1079outs() << " lli -load " << *SharedObject << " " << TestModuleBC;1080} else {1081outs() << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";1082outs() << " cc " << *SharedObject << " " << TestModuleBC.str() << ".s -o "1083<< TestModuleBC << ".exe\n";1084outs() << " ./" << TestModuleBC << ".exe";1085}1086for (unsigned i = 0, e = InputArgv.size(); i != e; ++i)1087outs() << " " << InputArgv[i];1088outs() << '\n';1089outs() << "The shared object was created with:\n llc -march=c "1090<< SafeModuleBC.str() << " -o temporary.c\n"1091<< " cc -xc temporary.c -O2 -o " << *SharedObject;1092if (TargetTriple.getArch() == Triple::sparc)1093outs() << " -G"; // Compile a shared library, `-G' for Sparc1094else1095outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others10961097outs() << " -fno-strict-aliasing\n";10981099return Error::success();1100}110111021103