Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp =================================================================== --- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp +++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp @@ -77,6 +77,18 @@ std::exit(1); } +// Helper function that returns a *TargetMachine, used by OptLLVM +static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr, + StringRef FeaturesStr, + const TargetOptions &Options, + CodeGenOpt::Level OLvl) { + std::string E; + const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, E); + return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, + FeaturesStr, Options, getRelocModel(), + getCodeModel(), OLvl); +} + // Helper function to add optimization passes to the TargetMachine at the // specified optimization level, OptLevel static void AddOptimizationPasses(legacy::PassManagerBase &MPM, @@ -100,17 +112,24 @@ if (!M || verifyModule(*M, &errs())) ErrorAndExit("Could not parse IR"); + Triple ModuleTriple(M->getTargetTriple()); + const TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); + std::unique_ptr TM(GetTargetMachine(ModuleTriple, getCPUStr(), + getFeaturesStr(), Options, OLvl)); setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M); - + legacy::PassManager Passes; - Triple ModuleTriple(M->getTargetTriple()); Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple)); - Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis())); + Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis())); + + auto <M = static_cast(*TM); + Passes.add(LTM.createPassConfig(Passes)); + Passes.add(createVerifierPass()); AddOptimizationPasses(Passes, OLvl, 0); - + // Add a pass that writes the optimized IR to an output stream std::string outString; raw_string_ostream OS(outString);