Skip to content

Commit 9570974

Browse files
committed
[ThinLTO][NFC] Prep for two-codegen rounds
1 parent 14b4356 commit 9570974

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,10 +1286,10 @@ static void runThinLTOBackend(
12861286
Conf.CGFileType = getCodeGenFileType(Action);
12871287
break;
12881288
}
1289-
if (Error E =
1290-
thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1291-
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1292-
/* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
1289+
if (Error E = thinBackend(
1290+
Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1291+
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1292+
/* ModuleMap */ nullptr, Conf.CodeGenOnly, CGOpts.CmdArgs)) {
12931293
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
12941294
errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
12951295
});

llvm/include/llvm/LTO/LTOBackend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Error thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream,
5656
const FunctionImporter::ImportMapTy &ImportList,
5757
const GVSummaryMapTy &DefinedGlobals,
5858
MapVector<StringRef, BitcodeModule> *ModuleMap,
59+
bool CodeGenOnly,
5960
const std::vector<uint8_t> &CmdArgs = std::vector<uint8_t>());
6061

6162
Error finalizeOptimizationRemarks(

llvm/lib/LTO/LTO.cpp

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,8 @@ class InProcessThinBackend : public ThinBackendProc {
14741474
return MOrErr.takeError();
14751475

14761476
return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1477-
ImportList, DefinedGlobals, &ModuleMap);
1477+
ImportList, DefinedGlobals, &ModuleMap,
1478+
Conf.CodeGenOnly);
14781479
};
14791480

14801481
auto ModuleID = BM.getModuleIdentifier();
@@ -1840,45 +1841,49 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
18401841

18411842
TimeTraceScopeExit.release();
18421843

1843-
std::unique_ptr<ThinBackendProc> BackendProc =
1844-
ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1845-
AddStream, Cache);
1846-
18471844
auto &ModuleMap =
18481845
ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
18491846

1850-
auto ProcessOneModule = [&](int I) -> Error {
1851-
auto &Mod = *(ModuleMap.begin() + I);
1852-
// Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
1853-
// combined module and parallel code generation partitions.
1854-
return BackendProc->start(RegularLTO.ParallelCodeGenParallelismLevel + I,
1855-
Mod.second, ImportLists[Mod.first],
1856-
ExportLists[Mod.first], ResolvedODR[Mod.first],
1857-
ThinLTO.ModuleMap);
1847+
auto RunBackends = [&](ThinBackendProc *BackendProcess) -> Error {
1848+
auto ProcessOneModule = [&](int I) -> Error {
1849+
auto &Mod = *(ModuleMap.begin() + I);
1850+
// Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for
1851+
// combined module and parallel code generation partitions.
1852+
return BackendProcess->start(
1853+
RegularLTO.ParallelCodeGenParallelismLevel + I, Mod.second,
1854+
ImportLists[Mod.first], ExportLists[Mod.first],
1855+
ResolvedODR[Mod.first], ThinLTO.ModuleMap);
1856+
};
1857+
1858+
if (BackendProcess->getThreadCount() == 1) {
1859+
// Process the modules in the order they were provided on the
1860+
// command-line. It is important for this codepath to be used for
1861+
// WriteIndexesThinBackend, to ensure the emitted LinkedObjectsFile lists
1862+
// ThinLTO objects in the same order as the inputs, which otherwise would
1863+
// affect the final link order.
1864+
for (int I = 0, E = ModuleMap.size(); I != E; ++I)
1865+
if (Error E = ProcessOneModule(I))
1866+
return E;
1867+
} else {
1868+
// When executing in parallel, process largest bitsize modules first to
1869+
// improve parallelism, and avoid starving the thread pool near the end.
1870+
// This saves about 15 sec on a 36-core machine while link `clang.exe`
1871+
// (out of 100 sec).
1872+
std::vector<BitcodeModule *> ModulesVec;
1873+
ModulesVec.reserve(ModuleMap.size());
1874+
for (auto &Mod : ModuleMap)
1875+
ModulesVec.push_back(&Mod.second);
1876+
for (int I : generateModulesOrdering(ModulesVec))
1877+
if (Error E = ProcessOneModule(I))
1878+
return E;
1879+
}
1880+
return BackendProcess->wait();
18581881
};
18591882

1860-
if (BackendProc->getThreadCount() == 1) {
1861-
// Process the modules in the order they were provided on the command-line.
1862-
// It is important for this codepath to be used for WriteIndexesThinBackend,
1863-
// to ensure the emitted LinkedObjectsFile lists ThinLTO objects in the same
1864-
// order as the inputs, which otherwise would affect the final link order.
1865-
for (int I = 0, E = ModuleMap.size(); I != E; ++I)
1866-
if (Error E = ProcessOneModule(I))
1867-
return E;
1868-
} else {
1869-
// When executing in parallel, process largest bitsize modules first to
1870-
// improve parallelism, and avoid starving the thread pool near the end.
1871-
// This saves about 15 sec on a 36-core machine while link `clang.exe` (out
1872-
// of 100 sec).
1873-
std::vector<BitcodeModule *> ModulesVec;
1874-
ModulesVec.reserve(ModuleMap.size());
1875-
for (auto &Mod : ModuleMap)
1876-
ModulesVec.push_back(&Mod.second);
1877-
for (int I : generateModulesOrdering(ModulesVec))
1878-
if (Error E = ProcessOneModule(I))
1879-
return E;
1880-
}
1881-
return BackendProc->wait();
1883+
std::unique_ptr<ThinBackendProc> BackendProc =
1884+
ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1885+
AddStream, Cache);
1886+
return RunBackends(BackendProc.get());
18821887
}
18831888

18841889
Expected<std::unique_ptr<ToolOutputFile>> lto::setupLLVMOptimizationRemarks(

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
565565
const FunctionImporter::ImportMapTy &ImportList,
566566
const GVSummaryMapTy &DefinedGlobals,
567567
MapVector<StringRef, BitcodeModule> *ModuleMap,
568-
const std::vector<uint8_t> &CmdArgs) {
568+
bool CodeGenOnly, const std::vector<uint8_t> &CmdArgs) {
569569
Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
570570
if (!TOrErr)
571571
return TOrErr.takeError();
@@ -586,7 +586,9 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
586586
Mod.setPartialSampleProfileRatio(CombinedIndex);
587587

588588
LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
589-
if (Conf.CodeGenOnly) {
589+
if (CodeGenOnly) {
590+
// If CodeGenOnly is set, we only perform code generation and skip
591+
// optimization.
590592
codegen(Conf, TM.get(), AddStream, Task, Mod, CombinedIndex);
591593
return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
592594
}

0 commit comments

Comments
 (0)