Skip to content

Commit ea69e03

Browse files
committed
[ThinLTO][NFC] Prep for two-codegen rounds
1 parent 84682d4 commit ea69e03

File tree

4 files changed

+48
-42
lines changed

4 files changed

+48
-42
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,10 +1299,10 @@ static void runThinLTOBackend(
12991299
Conf.CGFileType = getCodeGenFileType(Action);
13001300
break;
13011301
}
1302-
if (Error E =
1303-
thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1304-
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1305-
/* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
1302+
if (Error E = thinBackend(
1303+
Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1304+
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1305+
/* ModuleMap */ nullptr, Conf.CodeGenOnly, CGOpts.CmdArgs)) {
13061306
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
13071307
errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
13081308
});

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: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ class InProcessThinBackend : public ThinBackendProc {
14411441
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
14421442
}
14431443

1444-
Error runThinLTOBackendThread(
1444+
virtual Error runThinLTOBackendThread(
14451445
AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
14461446
ModuleSummaryIndex &CombinedIndex,
14471447
const FunctionImporter::ImportMapTy &ImportList,
@@ -1456,7 +1456,8 @@ class InProcessThinBackend : public ThinBackendProc {
14561456
return MOrErr.takeError();
14571457

14581458
return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1459-
ImportList, DefinedGlobals, &ModuleMap);
1459+
ImportList, DefinedGlobals, &ModuleMap,
1460+
Conf.CodeGenOnly);
14601461
};
14611462

14621463
auto ModuleID = BM.getModuleIdentifier();
@@ -1827,45 +1828,49 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
18271828

18281829
TimeTraceScopeExit.release();
18291830

1830-
std::unique_ptr<ThinBackendProc> BackendProc =
1831-
ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1832-
AddStream, Cache);
1833-
18341831
auto &ModuleMap =
18351832
ThinLTO.ModulesToCompile ? *ThinLTO.ModulesToCompile : ThinLTO.ModuleMap;
18361833

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

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

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

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
555555
const FunctionImporter::ImportMapTy &ImportList,
556556
const GVSummaryMapTy &DefinedGlobals,
557557
MapVector<StringRef, BitcodeModule> *ModuleMap,
558-
const std::vector<uint8_t> &CmdArgs) {
558+
bool CodeGenOnly, const std::vector<uint8_t> &CmdArgs) {
559559
Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
560560
if (!TOrErr)
561561
return TOrErr.takeError();
@@ -576,7 +576,7 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
576576
Mod.setPartialSampleProfileRatio(CombinedIndex);
577577

578578
LLVM_DEBUG(dbgs() << "Running ThinLTO\n");
579-
if (Conf.CodeGenOnly) {
579+
if (CodeGenOnly) {
580580
codegen(Conf, TM.get(), AddStream, Task, Mod, CombinedIndex);
581581
return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
582582
}

0 commit comments

Comments
 (0)