diff --git a/llvm/tools/bugpoint/BugDriver.h b/llvm/tools/bugpoint/BugDriver.h --- a/llvm/tools/bugpoint/BugDriver.h +++ b/llvm/tools/bugpoint/BugDriver.h @@ -33,6 +33,7 @@ class AbstractInterpreter; class Instruction; class LLVMContext; +class TargetMachine; class CC; @@ -47,6 +48,7 @@ const char *ToolName; // argv[0] of bugpoint std::string ReferenceOutputFile; // Name of `good' output file std::unique_ptr Program; // The raw program, linked together + const TargetMachine *TM; std::vector PassesToRun; AbstractInterpreter *Interpreter; // How to run the program AbstractInterpreter *SafeInterpreter; // To generate reference output, etc. @@ -125,6 +127,7 @@ bool isExecutingJIT(); Module &getProgram() const { return *Program; } + TargetMachine *getTargetMachine(); /// Set the current module to the specified module, returning the old one. std::unique_ptr swapProgramIn(std::unique_ptr M); @@ -301,6 +304,8 @@ SplitFunctionsOutOfModule(Module *M, const std::vector &F, ValueToValueMapTy &VMap); +void setTargetTriple(StringRef TheTriple); + } // End llvm namespace #endif diff --git a/llvm/tools/bugpoint/BugDriver.cpp b/llvm/tools/bugpoint/BugDriver.cpp --- a/llvm/tools/bugpoint/BugDriver.cpp +++ b/llvm/tools/bugpoint/BugDriver.cpp @@ -18,17 +18,42 @@ #include "llvm/IR/Verifier.h" #include "llvm/IRReader/IRReader.h" #include "llvm/Linker/Linker.h" +#include "llvm/MC/TargetRegistry.h" #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/Host.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetMachine.h" #include using namespace llvm; namespace llvm { Triple TargetTriple; +std::unique_ptr TheTargetMachine; +void setTargetTriple(StringRef TheTriple) { + TargetTriple = Triple(TheTriple); + + // Construct a plausible TM, for simplifyCFG needs + std::string Error; + const Target *TheTarget = + TargetRegistry::lookupTarget(TheTriple.str(), Error); + if (!TheTarget) { + errs() << "warning: bugpoint could not allocate Target: " << Error << "\n"; + return; + } + StringRef CPUStr(""); + StringRef FeaturesStr(""); + Optional RM; + Optional CM; + CodeGenOpt::Level OLvl = CodeGenOpt::Default; + TargetOptions Options; + TheTargetMachine.reset(TheTarget->createTargetMachine( + TheTriple, CPUStr, FeaturesStr, Options, RM, CM, OLvl)); + if (!TheTargetMachine) + errs() << "warning: bugpoint could not allocate TargetMachine\n"; +} } DiscardTemp::~DiscardTemp() { @@ -54,6 +79,8 @@ "(for miscompilation detection)")); } +TargetMachine *BugDriver::getTargetMachine() { return TheTargetMachine.get(); } + /// If we reduce or update the program somehow, call this method to update /// bugdriver with it. This deletes the old module and sets the specified one /// as the current program. @@ -111,7 +138,7 @@ if (TheTriple.getTriple().empty()) TheTriple.setTriple(sys::getDefaultTargetTriple()); - TargetTriple.setTriple(TheTriple.getTriple()); + setTargetTriple(TheTriple.getTriple()); } Result->setTargetTriple(TargetTriple.getTriple()); // override the triple diff --git a/llvm/tools/bugpoint/CrashDebugger.cpp b/llvm/tools/bugpoint/CrashDebugger.cpp --- a/llvm/tools/bugpoint/CrashDebugger.cpp +++ b/llvm/tools/bugpoint/CrashDebugger.cpp @@ -29,6 +29,7 @@ #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileUtilities.h" +#include "llvm/Target/TargetMachine.h" #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Cloning.h" @@ -654,11 +655,9 @@ class ReduceSimplifyCFG : public ListReducer { BugDriver &BD; BugTester TestFn; - TargetTransformInfo TTI; public: - ReduceSimplifyCFG(BugDriver &bd, BugTester testFn) - : BD(bd), TestFn(testFn), TTI(bd.getProgram().getDataLayout()) {} + ReduceSimplifyCFG(BugDriver &bd, BugTester testFn) : BD(bd), TestFn(testFn) {} Expected doTest(std::vector &Prefix, std::vector &Kept) override { @@ -677,6 +676,7 @@ // Clone the program to try hacking it apart... ValueToValueMapTy VMap; std::unique_ptr M = CloneModule(BD.getProgram(), VMap); + TargetMachine *TM = BD.getTargetMachine(); // Convert list to set for fast lookup... SmallPtrSet Blocks; @@ -701,7 +701,9 @@ std::string(BB->getName())); // Loop over and delete any hack up any blocks that are not listed... - for (auto &F : *M) + for (auto &F : *M) { + TargetTransformInfo TTI = TM ? TM->getTargetTransformInfo(F) + : TargetTransformInfo(M->getDataLayout()); // Loop over all of the basic blocks and remove them if they are unneeded. for (Function::iterator BBIt = F.begin(); BBIt != F.end();) { if (!Blocks.count(&*BBIt)) { @@ -710,6 +712,7 @@ } simplifyCFG(&*BBIt++, TTI); } + } // Verify we didn't break anything std::vector Passes; Passes.push_back("verify"); diff --git a/llvm/tools/bugpoint/ExecutionDriver.cpp b/llvm/tools/bugpoint/ExecutionDriver.cpp --- a/llvm/tools/bugpoint/ExecutionDriver.cpp +++ b/llvm/tools/bugpoint/ExecutionDriver.cpp @@ -13,6 +13,7 @@ #include "BugDriver.h" #include "ToolRunner.h" +#include "llvm/IR/Module.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FileUtilities.h" @@ -331,9 +332,10 @@ if (!SharedObj.empty()) SharedObjs.push_back(SharedObj); - Expected RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, - OutputFile, AdditionalLinkerArgs, - SharedObjs, Timeout, MemoryLimit); + Expected RetVal = AI->ExecuteProgram( + BitcodeFile, InputArgv, InputFile, OutputFile, + Triple(Program.getTargetTriple()), AdditionalLinkerArgs, SharedObjs, + Timeout, MemoryLimit); if (Error E = RetVal.takeError()) return std::move(E); @@ -376,6 +378,7 @@ BugDriver::compileSharedObject(const std::string &BitcodeFile) { assert(Interpreter && "Interpreter should have been created already!"); std::string OutputFile; + Triple TargetTriple(Program->getTargetTriple()); // Using the known-good backend. Expected FT = @@ -385,7 +388,7 @@ std::string SharedObjectFile; if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile, - AdditionalLinkerArgs)) + TargetTriple, AdditionalLinkerArgs)) return std::move(E); // Remove the intermediate C file diff --git a/llvm/tools/bugpoint/Miscompilation.cpp b/llvm/tools/bugpoint/Miscompilation.cpp --- a/llvm/tools/bugpoint/Miscompilation.cpp +++ b/llvm/tools/bugpoint/Miscompilation.cpp @@ -1093,7 +1093,7 @@ outs() << "The shared object was created with:\n llc -march=c " << SafeModuleBC.str() << " -o temporary.c\n" << " cc -xc temporary.c -O2 -o " << *SharedObject; - if (TargetTriple.getArch() == Triple::sparc) + if (Triple(getProgram().getTargetTriple()).getArch() == Triple::sparc) outs() << " -G"; // Compile a shared library, `-G' for Sparc else outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others diff --git a/llvm/tools/bugpoint/ToolRunner.h b/llvm/tools/bugpoint/ToolRunner.h --- a/llvm/tools/bugpoint/ToolRunner.h +++ b/llvm/tools/bugpoint/ToolRunner.h @@ -27,7 +27,6 @@ namespace llvm { extern cl::opt SaveTemps; -extern Triple TargetTriple; class LLC; @@ -62,7 +61,7 @@ Expected ExecuteProgram( const std::string &ProgramFile, const std::vector &Args, FileType fileType, const std::string &InputFile, - const std::string &OutputFile, + const std::string &OutputFile, const Triple &TargetTriple, const std::vector &CCArgs = std::vector(), unsigned Timeout = 0, unsigned MemoryLimit = 0); @@ -70,7 +69,7 @@ /// file or a .s file) into a shared object. /// Error MakeSharedObject(const std::string &InputFile, FileType fileType, - std::string &OutputFile, + std::string &OutputFile, const Triple &TargetTriple, const std::vector &ArgsForCC); }; @@ -135,6 +134,7 @@ virtual Expected ExecuteProgram( const std::string &Bitcode, const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, + const Triple &TargetTriple, const std::vector &CCArgs = std::vector(), const std::vector &SharedLibs = std::vector(), unsigned Timeout = 0, unsigned MemoryLimit = 0) = 0; @@ -169,6 +169,7 @@ Expected ExecuteProgram( const std::string &Bitcode, const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, + const Triple &TargetTriple, const std::vector &CCArgs = std::vector(), const std::vector &SharedLibs = std::vector(), unsigned Timeout = 0, unsigned MemoryLimit = 0) override; diff --git a/llvm/tools/bugpoint/ToolRunner.cpp b/llvm/tools/bugpoint/ToolRunner.cpp --- a/llvm/tools/bugpoint/ToolRunner.cpp +++ b/llvm/tools/bugpoint/ToolRunner.cpp @@ -156,7 +156,7 @@ Expected ExecuteProgram( const std::string &Bitcode, const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, - const std::vector &CCArgs, + const Triple &TargetTriple, const std::vector &CCArgs, const std::vector &SharedLibs = std::vector(), unsigned Timeout = 0, unsigned MemoryLimit = 0) override; }; @@ -166,6 +166,7 @@ const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, + const Triple &TargetTriple, const std::vector &CCArgs, const std::vector &SharedLibs, unsigned Timeout, unsigned MemoryLimit) { @@ -252,6 +253,7 @@ Expected ExecuteProgram( const std::string &Bitcode, const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, + const Triple &TargetTriple, const std::vector &CCArgs = std::vector(), const std::vector &SharedLibs = std::vector(), unsigned Timeout = 0, unsigned MemoryLimit = 0) override { @@ -301,7 +303,7 @@ Expected ExecuteProgram( const std::string &Bitcode, const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, - const std::vector &CCArgs, + const Triple &TargetTriple, const std::vector &CCArgs, const std::vector &SharedLibs = std::vector(), unsigned Timeout = 0, unsigned MemoryLimit = 0) override; }; @@ -310,7 +312,7 @@ Expected CustomExecutor::ExecuteProgram( const std::string &Bitcode, const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, - const std::vector &CCArgs, + const Triple &TargetTriple, const std::vector &CCArgs, const std::vector &SharedLibs, unsigned Timeout, unsigned MemoryLimit) { @@ -483,6 +485,7 @@ const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, + const Triple &TargetTriple, const std::vector &ArgsForCC, const std::vector &SharedLibs, unsigned Timeout, unsigned MemoryLimit) { @@ -499,7 +502,8 @@ // Assuming LLC worked, compile the result with CC and run it. return cc->ExecuteProgram(OutputAsmFile, Args, *FileKind, InputFile, - OutputFile, CCArgs, Timeout, MemoryLimit); + OutputFile, TargetTriple, CCArgs, Timeout, + MemoryLimit); } /// createLLC - Try to find the LLC executable @@ -544,6 +548,7 @@ Expected ExecuteProgram( const std::string &Bitcode, const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, + const Triple &TargetTriple, const std::vector &CCArgs = std::vector(), const std::vector &SharedLibs = std::vector(), unsigned Timeout = 0, unsigned MemoryLimit = 0) override; @@ -554,6 +559,7 @@ const std::vector &Args, const std::string &InputFile, const std::string &OutputFile, + const Triple &TargetTriple, const std::vector &CCArgs, const std::vector &SharedLibs, unsigned Timeout, unsigned MemoryLimit) { @@ -624,6 +630,7 @@ FileType fileType, const std::string &InputFile, const std::string &OutputFile, + const Triple &TargetTriple, const std::vector &ArgsForCC, unsigned Timeout, unsigned MemoryLimit) { std::vector CCArgs; @@ -763,7 +770,7 @@ } Error CC::MakeSharedObject(const std::string &InputFile, FileType fileType, - std::string &OutputFile, + std::string &OutputFile, const Triple &TargetTriple, const std::vector &ArgsForCC) { SmallString<128> UniqueFilename; std::error_code EC = sys::fs::createUniqueFile( diff --git a/llvm/tools/bugpoint/bugpoint.cpp b/llvm/tools/bugpoint/bugpoint.cpp --- a/llvm/tools/bugpoint/bugpoint.cpp +++ b/llvm/tools/bugpoint/bugpoint.cpp @@ -173,12 +173,12 @@ sys::SetInterruptFunction(BugpointInterruptFunction); #endif - LLVMContext Context; // If we have an override, set it and then track the triple we want Modules // to use. if (!OverrideTriple.empty()) { - TargetTriple.setTriple(Triple::normalize(OverrideTriple)); - outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n"; + std::string TargetTriple = Triple::normalize(OverrideTriple); + setTargetTriple(TargetTriple); + outs() << "Override triple set to '" << TargetTriple << "'\n"; } if (MemoryLimit < 0) { @@ -196,6 +196,7 @@ #endif } + LLVMContext Context; BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind, Context); if (D.addSources(InputFilenames))