Skip to content

Commit c195981

Browse files
authored
[CGData][ThinLTO][NFC] Prep for two-codegen rounds (#90934)
This is NFC for #90933. - Create a lambda function, `RunBackends`, to group the backend operations into a single function. - Explicitly pass the `CodeGenOnly` argument to thinBackend, instead of depending on a configuration value. Depends on #90304. This is a patch for https://discourse.llvm.org/t/rfc-enhanced-machine-outliner-part-2-thinlto-nolto/78753.
1 parent a72248c commit c195981

File tree

4 files changed

+51
-42
lines changed

4 files changed

+51
-42
lines changed

clang/lib/CodeGen/BackendUtil.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1321,10 +1321,10 @@ static void runThinLTOBackend(
13211321
Conf.CGFileType = getCodeGenFileType(Action);
13221322
break;
13231323
}
1324-
if (Error E =
1325-
thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1326-
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1327-
/* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
1324+
if (Error E = thinBackend(
1325+
Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1326+
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1327+
/* ModuleMap */ nullptr, Conf.CodeGenOnly, CGOpts.CmdArgs)) {
13281328
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
13291329
errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
13301330
});

llvm/include/llvm/LTO/LTOBackend.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ Error backend(const Config &C, AddStreamFn AddStream,
5050
/// already been mapped to memory and the corresponding BitcodeModule objects
5151
/// are saved in the ModuleMap. If \p ModuleMap is nullptr, module files will
5252
/// be mapped to memory on demand and at any given time during importing, only
53-
/// one source module will be kept open at the most.
53+
/// one source module will be kept open at the most. If \p CodeGenOnly is true,
54+
/// the backend will skip optimization and only perform code generation.
5455
Error thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream,
5556
Module &M, const ModuleSummaryIndex &CombinedIndex,
5657
const FunctionImporter::ImportMapTy &ImportList,
5758
const GVSummaryMapTy &DefinedGlobals,
5859
MapVector<StringRef, BitcodeModule> *ModuleMap,
60+
bool CodeGenOnly,
5961
const std::vector<uint8_t> &CmdArgs = std::vector<uint8_t>());
6062

6163
Error finalizeOptimizationRemarks(

llvm/lib/LTO/LTO.cpp

+40-35
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,8 @@ class InProcessThinBackend : public ThinBackendProc {
14731473
return MOrErr.takeError();
14741474

14751475
return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
1476-
ImportList, DefinedGlobals, &ModuleMap);
1476+
ImportList, DefinedGlobals, &ModuleMap,
1477+
Conf.CodeGenOnly);
14771478
};
14781479

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

18401841
TimeTraceScopeExit.release();
18411842

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

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

1859-
if (BackendProc->getThreadCount() == 1) {
1860-
// Process the modules in the order they were provided on the command-line.
1861-
// It is important for this codepath to be used for WriteIndexesThinBackend,
1862-
// to ensure the emitted LinkedObjectsFile lists ThinLTO objects in the same
1863-
// order as the inputs, which otherwise would 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` (out
1871-
// 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 BackendProc->wait();
1882+
std::unique_ptr<ThinBackendProc> BackendProc =
1883+
ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
1884+
AddStream, Cache);
1885+
return RunBackends(BackendProc.get());
18811886
}
18821887

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

llvm/lib/LTO/LTOBackend.cpp

+4-2
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. This value may differ from Conf.CodeGenOnly.
590592
codegen(Conf, TM.get(), AddStream, Task, Mod, CombinedIndex);
591593
return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
592594
}

0 commit comments

Comments
 (0)