Skip to content

Commit 33fb21b

Browse files
author
Nuri Amari
committed
Address PR Comments #3
1 parent 82a849f commit 33fb21b

File tree

15 files changed

+50
-35
lines changed

15 files changed

+50
-35
lines changed

lld/COFF/LTO.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {
118118
if (ctx.config.thinLTOIndexOnly) {
119119
auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
120120
backend = lto::createWriteIndexesThinBackend(
121+
llvm::hardware_concurrency(ctx.config.thinLTOJobs),
121122
std::string(ctx.config.thinLTOPrefixReplaceOld),
122123
std::string(ctx.config.thinLTOPrefixReplaceNew),
123124
std::string(ctx.config.thinLTOPrefixReplaceNativeObject),

lld/ELF/LTO.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
179179
auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); };
180180
if (ctx.arg.thinLTOIndexOnly) {
181181
backend = lto::createWriteIndexesThinBackend(
182+
llvm::hardware_concurrency(ctx.arg.thinLTOJobs),
182183
std::string(ctx.arg.thinLTOPrefixReplaceOld),
183184
std::string(ctx.arg.thinLTOPrefixReplaceNew),
184185
std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),

lld/MachO/LTO.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ BitcodeCompiler::BitcodeCompiler() {
8787
auto onIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
8888
if (config->thinLTOIndexOnly) {
8989
backend = lto::createWriteIndexesThinBackend(
90+
llvm::hardware_concurrency(config->thinLTOJobs),
9091
std::string(config->thinLTOPrefixReplaceOld),
9192
std::string(config->thinLTOPrefixReplaceNew),
9293
std::string(config->thinLTOPrefixReplaceNativeObject),

lld/test/COFF/thinlto-emit-imports.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
; RUN: not lld-link -entry:main -thinlto-index-only \
3636
; RUN: -thinlto-emit-imports-files %t1.obj %t2.obj %t3.obj \
3737
; RUN: -out:%t4.exe 2>&1 | FileCheck -DMSG=%errc_EACCES %s --check-prefix=ERR
38-
; ERR: cannot open {{.*}}3.obj.imports: [[MSG]]
38+
; ERR: 'cannot open {{.*}}3.obj.imports': [[MSG]]
3939

4040
; Ensure lld doesn't generate import files when thinlto-index-only is not enabled
4141
; RUN: rm -f %t1.obj.imports

lld/test/ELF/lto/thinlto-cant-write-index.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
; RUN: chmod u-w %t2.o.thinlto.bc
1111
; RUN: not ld.lld --plugin-opt=thinlto-index-only -shared %t1.o %t2.o -o /dev/null 2>&1 | FileCheck -DMSG=%errc_EACCES %s
1212
; RUN: chmod u+w %t2.o.thinlto.bc
13-
; CHECK: cannot open {{.*}}2.o.thinlto.bc: [[MSG]]
13+
; CHECK: 'cannot open {{.*}}2.o.thinlto.bc': [[MSG]]
1414

1515
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
1616
target triple = "x86_64-unknown-linux-gnu"

lld/test/ELF/lto/thinlto-emit-imports.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
; RUN: touch %t3.o.imports
1111
; RUN: chmod 400 %t3.o.imports
1212
; RUN: not ld.lld --plugin-opt=thinlto-index-only --plugin-opt=thinlto-emit-imports-files -shared %t1.o %t2.o %t3.o -o /dev/null 2>&1 | FileCheck -DMSG=%errc_EACCES %s --check-prefix=ERR
13-
; ERR: cannot open {{.*}}3.o.imports: [[MSG]]
13+
; ERR: 'cannot open {{.*}}3.o.imports': [[MSG]]
1414

1515
; RUN: rm -f %t1.o.imports %t2.o.imports rm -f %t3.o.imports
1616
; RUN: ld.lld --plugin-opt=thinlto-emit-imports-files -shared %t1.o %t2.o %t3.o -o %t4

lld/test/MachO/thinlto-emit-imports.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
; RUN: chmod 400 %t3.o.imports
3434
; RUN: not %lld --thinlto-index-only --thinlto-emit-imports-files -dylib %t1.o %t2.o %t3.o -o /dev/null 2>&1 \
3535
; RUN: | FileCheck -DMSG=%errc_EACCES %s --check-prefix=ERR
36-
; ERR: cannot open {{.*}}3.o.imports: [[MSG]]
36+
; ERR: 'cannot open {{.*}}3.o.imports': [[MSG]]
3737

3838
; Ensure lld doesn't generate import files when thinlto-index-only is not enabled
3939
; RUN: rm -f %t1.o.imports

llvm/include/llvm/LTO/LTO.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ ThinBackend createInProcessThinBackend(ThreadPoolStrategy Parallelism,
231231
/// the objects with NativeObjectPrefix instead of NewPrefix. OnWrite is
232232
/// callback which receives module identifier and notifies LTO user that index
233233
/// file for the module (and optionally imports file) was created.
234-
ThinBackend createWriteIndexesThinBackend(std::string OldPrefix,
234+
ThinBackend createWriteIndexesThinBackend(ThreadPoolStrategy Parallelism,
235+
std::string OldPrefix,
235236
std::string NewPrefix,
236237
std::string NativeObjectPrefix,
237238
bool ShouldEmitImportsFiles,

llvm/include/llvm/Support/Threading.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ constexpr bool llvm_is_multithreaded() { return LLVM_ENABLE_THREADS; }
188188
return S;
189189
}
190190

191+
/// Like hardware_concurrency() above, but builds a strategy
192+
/// based on the rules described for get_threadpool_strategy().
193+
/// If \p Num is invalid, returns a default strategy where one thread per
194+
/// hardware core is used.
195+
inline ThreadPoolStrategy hardware_concurrency(StringRef Num) {
196+
std::optional<ThreadPoolStrategy> S =
197+
get_threadpool_strategy(Num, hardware_concurrency());
198+
if (S)
199+
return *S;
200+
return hardware_concurrency();
201+
}
202+
191203
/// Returns an optimal thread strategy to execute specified amount of tasks.
192204
/// This strategy should prevent us from creating too many threads if we
193205
/// occasionaly have an unexpectedly small amount of tasks.

llvm/include/llvm/Transforms/IPO/FunctionImport.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ void gatherImportedSummariesForModule(
417417
GVSummaryPtrSet &DecSummaries);
418418

419419
/// Emit into \p OutputFilename the files module \p ModulePath will import from.
420-
std::error_code
421-
EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename,
422-
const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex);
420+
Error EmitImportsFiles(
421+
StringRef ModulePath, StringRef OutputFilename,
422+
const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex);
423423

424424
/// Based on the information recorded in the summaries during global
425425
/// summary-based analysis:

llvm/lib/LTO/LTO.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,6 @@ class lto::ThinBackendProc {
13791379
DefaultThreadPool BackendThreadPool;
13801380
std::optional<Error> Err;
13811381
std::mutex ErrMu;
1382-
std::mutex OnWriteMu;
13831382

13841383
public:
13851384
ThinBackendProc(
@@ -1423,16 +1422,17 @@ class lto::ThinBackendProc {
14231422
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
14241423
sys::fs::OpenFlags::OF_None);
14251424
if (EC)
1426-
return errorCodeToError(EC);
1425+
return createFileError("cannot open " + NewModulePath + ".thinlto.bc",
1426+
EC);
14271427

14281428
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
14291429
&DeclarationSummaries);
14301430

14311431
if (ShouldEmitImportsFiles) {
1432-
EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",
1433-
ModuleToSummariesForIndex);
1434-
if (EC)
1435-
return errorCodeToError(EC);
1432+
Error ImportFilesError = EmitImportsFiles(
1433+
ModulePath, NewModulePath + ".imports", ModuleToSummariesForIndex);
1434+
if (ImportFilesError)
1435+
return ImportFilesError;
14361436
}
14371437
return Error::success();
14381438
}
@@ -1614,13 +1614,13 @@ class WriteIndexesThinBackend : public ThinBackendProc {
16141614
public:
16151615
WriteIndexesThinBackend(
16161616
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
1617+
ThreadPoolStrategy ThinLTOParallelism,
16171618
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
16181619
std::string OldPrefix, std::string NewPrefix,
16191620
std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
16201621
raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
16211622
: ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
1622-
OnWrite, ShouldEmitImportsFiles,
1623-
/* ThinLTOParallelism */ hardware_concurrency()),
1623+
OnWrite, ShouldEmitImportsFiles, ThinLTOParallelism),
16241624
OldPrefix(OldPrefix), NewPrefix(NewPrefix),
16251625
NativeObjectPrefix(NativeObjectPrefix),
16261626
LinkedObjectsFile(LinkedObjectsFile) {}
@@ -1660,14 +1660,11 @@ class WriteIndexesThinBackend : public ThinBackendProc {
16601660
Err = std::move(E);
16611661
return;
16621662
}
1663-
if (OnWrite) {
1664-
// Serialize calls to the on write callback in case it is not thread
1665-
// safe
1666-
std::unique_lock<std::mutex> L(OnWriteMu);
1667-
OnWrite(std::string(ModulePath));
1668-
}
16691663
},
16701664
ModulePath, ImportList, OldPrefix, NewPrefix);
1665+
1666+
if (OnWrite)
1667+
OnWrite(std::string(ModulePath));
16711668
return Error::success();
16721669
}
16731670

@@ -1680,16 +1677,17 @@ class WriteIndexesThinBackend : public ThinBackendProc {
16801677
} // end anonymous namespace
16811678

16821679
ThinBackend lto::createWriteIndexesThinBackend(
1683-
std::string OldPrefix, std::string NewPrefix,
1684-
std::string NativeObjectPrefix, bool ShouldEmitImportsFiles,
1685-
raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
1680+
ThreadPoolStrategy Parallelism, std::string OldPrefix,
1681+
std::string NewPrefix, std::string NativeObjectPrefix,
1682+
bool ShouldEmitImportsFiles, raw_fd_ostream *LinkedObjectsFile,
1683+
IndexWriteCallback OnWrite) {
16861684
return
16871685
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
16881686
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
16891687
AddStreamFn AddStream, FileCache Cache) {
16901688
return std::make_unique<WriteIndexesThinBackend>(
1691-
Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix,
1692-
NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
1689+
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
1690+
OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
16931691
LinkedObjectsFile, OnWrite);
16941692
};
16951693
}

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,8 @@ void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
837837
ModuleIdentifier, ModuleToDefinedGVSummaries,
838838
ImportLists[ModuleIdentifier], ModuleToSummariesForIndex, DecSummaries);
839839

840-
std::error_code EC;
841-
if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,
842-
ModuleToSummariesForIndex)))
840+
if (Error EC = EmitImportsFiles(ModuleIdentifier, OutputName,
841+
ModuleToSummariesForIndex))
843842
report_fatal_error(Twine("Failed to open ") + OutputName +
844843
" to save imports lists\n");
845844
}

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,20 +1553,21 @@ void llvm::gatherImportedSummariesForModule(
15531553
}
15541554

15551555
/// Emit the files \p ModulePath will import from into \p OutputFilename.
1556-
std::error_code llvm::EmitImportsFiles(
1556+
Error llvm::EmitImportsFiles(
15571557
StringRef ModulePath, StringRef OutputFilename,
15581558
const ModuleToSummariesForIndexTy &ModuleToSummariesForIndex) {
15591559
std::error_code EC;
15601560
raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::OF_Text);
15611561
if (EC)
1562-
return EC;
1562+
return createFileError("cannot open " + OutputFilename,
1563+
errorCodeToError(EC));
15631564
for (const auto &ILI : ModuleToSummariesForIndex)
15641565
// The ModuleToSummariesForIndex map includes an entry for the current
15651566
// Module (needed for writing out the index files). We don't want to
15661567
// include it in the imports file, however, so filter it out.
15671568
if (ILI.first != ModulePath)
15681569
ImportsOS << ILI.first << "\n";
1569-
return std::error_code();
1570+
return Error::success();
15701571
}
15711572

15721573
bool llvm::convertToDeclaration(GlobalValue &GV) {

llvm/tools/gold/gold-plugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
899899
std::string OldPrefix, NewPrefix;
900900
getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
901901
Backend = createWriteIndexesThinBackend(
902-
OldPrefix, NewPrefix,
902+
llvm::hardware_concurrency(options::Parallelism) OldPrefix, NewPrefix,
903903
// TODO: Add support for optional native object path in
904904
// thinlto_prefix_replace option to match lld.
905905
/*NativeObjectPrefix=*/"", options::thinlto_emit_imports_files,

llvm/tools/llvm-lto2/llvm-lto2.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ static int run(int argc, char **argv) {
346346

347347
ThinBackend Backend;
348348
if (ThinLTODistributedIndexes)
349-
Backend = createWriteIndexesThinBackend(/*OldPrefix=*/"",
349+
Backend = createWriteIndexesThinBackend(llvm::hardware_concurrency(Threads),
350+
/*OldPrefix=*/"",
350351
/*NewPrefix=*/"",
351352
/*NativeObjectPrefix=*/"",
352353
ThinLTOEmitImports,

0 commit comments

Comments
 (0)