diff --git a/llvm/include/llvm/LTO/LTO.h b/llvm/include/llvm/LTO/LTO.h --- a/llvm/include/llvm/LTO/LTO.h +++ b/llvm/include/llvm/LTO/LTO.h @@ -191,8 +191,13 @@ /// destructor. class NativeObjectStream { public: - NativeObjectStream(std::unique_ptr OS) : OS(std::move(OS)) {} - std::unique_ptr OS; + NativeObjectStream(std::unique_ptr OS) + : OS_(std::move(OS)) { + this->OS = &*this->OS_; + } + NativeObjectStream(raw_pwrite_stream *OS) : OS(OS) {} + std::unique_ptr OS_; + raw_pwrite_stream *OS; virtual ~NativeObjectStream() = default; }; diff --git a/llvm/lib/LTO/Caching.cpp b/llvm/lib/LTO/Caching.cpp --- a/llvm/lib/LTO/Caching.cpp +++ b/llvm/lib/LTO/Caching.cpp @@ -85,7 +85,8 @@ ~CacheStream() { // Make sure the stream is closed before committing it. - OS.reset(); + OS_.reset(); + OS = nullptr; // Open the file first to avoid racing with a cache pruner. ErrorOr> MBOrErr = diff --git a/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp --- a/llvm/lib/LTO/LTOCodeGenerator.cpp +++ b/llvm/lib/LTO/LTOCodeGenerator.cpp @@ -37,6 +37,7 @@ #include "llvm/IR/Verifier.h" #include "llvm/InitializePasses.h" #include "llvm/LTO/LTO.h" +#include "llvm/LTO/LTOBackend.h" #include "llvm/LTO/legacy/LTOModule.h" #include "llvm/LTO/legacy/UpdateCompilerUsed.h" #include "llvm/Linker/Linker.h" @@ -123,43 +124,10 @@ TheLinker(new Linker(*MergedModule)) { Context.setDiscardValueNames(LTODiscardValueNames); Context.enableDebugTypeODRUniquing(); - initializeLTOPasses(); } LTOCodeGenerator::~LTOCodeGenerator() {} -// Initialize LTO passes. Please keep this function in sync with -// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO -// passes are initialized. -void LTOCodeGenerator::initializeLTOPasses() { - PassRegistry &R = *PassRegistry::getPassRegistry(); - - initializeInternalizeLegacyPassPass(R); - initializeIPSCCPLegacyPassPass(R); - initializeGlobalOptLegacyPassPass(R); - initializeConstantMergeLegacyPassPass(R); - initializeDAHPass(R); - initializeInstructionCombiningPassPass(R); - initializeSimpleInlinerPass(R); - initializePruneEHPass(R); - initializeGlobalDCELegacyPassPass(R); - initializeOpenMPOptLegacyPassPass(R); - initializeArgPromotionPass(R); - initializeJumpThreadingPass(R); - initializeSROALegacyPassPass(R); - initializeAttributorLegacyPassPass(R); - initializeAttributorCGSCCLegacyPassPass(R); - initializePostOrderFunctionAttrsLegacyPassPass(R); - initializeReversePostOrderFunctionAttrsLegacyPassPass(R); - initializeGlobalsAAWrapperPassPass(R); - initializeLegacyLICMPassPass(R); - initializeMergedLoadStoreMotionLegacyPassPass(R); - initializeGVNLegacyPassPass(R); - initializeMemCpyOptLegacyPassPass(R); - initializeDCELegacyPassPass(R); - initializeCFGSimplifyPassPass(R); -} - void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) { const std::vector &undefs = Mod->getAsmUndefinedRefs(); for (int i = 0, e = undefs.size(); i != e; ++i) @@ -283,7 +251,24 @@ // generate object file ToolOutputFile objFile(Filename, FD); - bool genResult = compileOptimized(&objFile.os()); + lto::Config Conf; + Conf.CodeGenOnly = true; + Conf.OptLevel = OptLevel; + Conf.CGOptLevel = CGOptLevel; + Conf.Freestanding = Freestanding; + + ModuleSummaryIndex CombinedIndex(false); + auto AddStream = + [&](size_t Task) -> std::unique_ptr { + auto X = std::make_unique(&objFile.os()); + return X; + }; + + bool genResult = true; + + if (Error Err = + backend(Conf, AddStream, 1, std::move(MergedModule), CombinedIndex)) + genResult = false; objFile.os().close(); if (objFile.os().has_error()) { emitError((Twine("could not write object file: ") + Filename + ": " + @@ -574,34 +559,37 @@ // Write LTOPostLink flag for passes that require all the modules. MergedModule->addModuleFlag(Module::Error, "LTOPostLink", 1); - // Instantiate the pass manager to organize the passes. - legacy::PassManager passes; - // Add an appropriate DataLayout instance for this module... MergedModule->setDataLayout(TargetMach->createDataLayout()); - passes.add( - createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis())); + // TODO + // PMB.DisableGVNLoadPRE = DisableGVNLoadPRE; - Triple TargetTriple(TargetMach->getTargetTriple()); - PassManagerBuilder PMB; - PMB.DisableGVNLoadPRE = DisableGVNLoadPRE; - PMB.LoopVectorize = !DisableVectorization; - PMB.SLPVectorize = !DisableVectorization; - if (!DisableInline) - PMB.Inliner = createFunctionInliningPass(); - PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple); - if (Freestanding) - PMB.LibraryInfo->disableAllFunctions(); - PMB.OptLevel = OptLevel; - PMB.VerifyInput = !DisableVerify; - PMB.VerifyOutput = !DisableVerify; + // TODO: Disable inlining + // if (!DisableInline) + // PMB.Inliner = createFunctionInliningPass(); - PMB.populateLTOPassManager(passes); + lto::Config Conf; + Conf.StatsFile = LTOStatsFile; + Conf.OptLevel = OptLevel; + Conf.Freestanding = Freestanding; + Conf.PTO.LoopVectorization = OptLevel > 1 && !DisableVectorization; + Conf.PTO.SLPVectorization = OptLevel > 1 && !DisableVectorization; + Conf.DisableVerify = DisableVerify; - // Run our queue of passes all at once now, efficiently. - passes.run(*MergedModule); + ModuleSummaryIndex CombinedIndex(false); + TargetMach = createTargetMachine(); + if (!opt(Conf, TargetMach.get(), 0, *MergedModule, /*IsThinLTO=*/false, + /*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr, + /*CmdArgs*/ std::vector())) { + return false; + } + + if (StatsFile) { + PrintStatisticsJSON(StatsFile->os()); + } + // TODO: report error return true; } diff --git a/llvm/test/LTO/X86/disable-verify.ll b/llvm/test/LTO/X86/disable-verify.ll --- a/llvm/test/LTO/X86/disable-verify.ll +++ b/llvm/test/LTO/X86/disable-verify.ll @@ -7,7 +7,7 @@ ; -disable-verify should disable verification from the optimization pipeline. ; CHECK: Pass Arguments: -; CHECK-NOT: -verify +; CHECK-NOT: -verify {{.*}} -verify ; VERIFY: Pass Arguments: {{.*}} -verify {{.*}} -verify diff --git a/llvm/test/tools/lto/print-stats.ll b/llvm/test/tools/lto/print-stats.ll --- a/llvm/test/tools/lto/print-stats.ll +++ b/llvm/test/tools/lto/print-stats.ll @@ -5,5 +5,12 @@ target triple = "x86_64-apple-macosx10.8.0" +define i32 @test(i32 %a) { + %r = add i32 %a, 1 + %r.1 = add i32 1, %a + %r.2 = add i32 %r, %r.1 + ret i32 %r.2 +} + ; STATS: Statistics Collected ; NO_STATS-NOT: Statistics Collected