Skip to content

Commit d5448af

Browse files
committed
Pass through to LTO
1 parent 8458d1b commit d5448af

File tree

7 files changed

+57
-30
lines changed

7 files changed

+57
-30
lines changed

lld/ELF/LTO.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ static void thinLTOCreateEmptyIndexFiles(Ctx &ctx) {
312312
// Merge all the bitcode files we have seen, codegen the result
313313
// and return the resulting ObjectFile(s).
314314
SmallVector<std::unique_ptr<InputFile>, 0>
315-
BitcodeCompiler::compile(const SmallVector<const char *> &bitcodeLibfuncs) {
315+
BitcodeCompiler::compile(const SmallVector<const char *> &bitcodeLibFuncs) {
316+
ltoObj->setBitcodeLibFuncs(bitcodeLibFuncs);
317+
316318
unsigned maxTasks = ltoObj->getMaxTasks();
317319
buf.resize(maxTasks);
318320
files.resize(maxTasks);

lld/ELF/LTO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class BitcodeCompiler {
4343

4444
void add(BitcodeFile &f);
4545
SmallVector<std::unique_ptr<InputFile>, 0>
46-
compile(const SmallVector<const char *> &bitcodeLibfuncs);
46+
compile(const SmallVector<const char *> &bitcodeLibFuncs);
4747

4848
private:
4949
Ctx &ctx;

llvm/include/llvm/LTO/LTO.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ class ThinBackendProc {
264264
using ThinBackendFunction = std::function<std::unique_ptr<ThinBackendProc>(
265265
const Config &C, ModuleSummaryIndex &CombinedIndex,
266266
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
267-
AddStreamFn AddStream, FileCache Cache)>;
267+
AddStreamFn AddStream, FileCache Cache,
268+
const SmallVector<const char *> &BitcodeLibFuncs)>;
268269

269270
/// This type defines the behavior following the thin-link phase during ThinLTO.
270271
/// It encapsulates a backend function and a strategy for thread pool
@@ -279,10 +280,11 @@ struct ThinBackend {
279280
std::unique_ptr<ThinBackendProc> operator()(
280281
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
281282
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
282-
AddStreamFn AddStream, FileCache Cache) {
283+
AddStreamFn AddStream, FileCache Cache,
284+
const SmallVector<const char *> &BitcodeLibFuncs) {
283285
assert(isValid() && "Invalid backend function");
284286
return Func(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
285-
std::move(AddStream), std::move(Cache));
287+
std::move(AddStream), std::move(Cache), BitcodeLibFuncs);
286288
}
287289
ThreadPoolStrategy getParallelism() const { return Parallelism; }
288290
bool isValid() const { return static_cast<bool>(Func); }
@@ -400,6 +402,8 @@ class LTO {
400402
LLVM_ABI Error add(std::unique_ptr<InputFile> Obj,
401403
ArrayRef<SymbolResolution> Res);
402404

405+
LLVM_ABI void setBitcodeLibFuncs(const SmallVector<const char *> &BitcodeLibFuncs);
406+
403407
/// Returns an upper bound on the number of tasks that the client may expect.
404408
/// This may only be called after all IR object files have been added. For a
405409
/// full description of tasks see LTOBackend.h.
@@ -599,6 +603,8 @@ class LTO {
599603

600604
// Diagnostic optimization remarks file
601605
LLVMRemarkFileHandle DiagnosticOutputFile;
606+
607+
const SmallVector<const char *> *BitcodeLibFuncs;
602608
};
603609

604610
/// The resolution for a symbol. The linker must provide a SymbolResolution for

llvm/include/llvm/LTO/LTOBackend.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ LLVM_ABI bool opt(const Config &Conf, TargetMachine *TM, unsigned Task,
3939
Module &Mod, bool IsThinLTO,
4040
ModuleSummaryIndex *ExportSummary,
4141
const ModuleSummaryIndex *ImportSummary,
42-
const std::vector<uint8_t> &CmdArgs);
42+
const std::vector<uint8_t> &CmdArgs,
43+
const SmallVector<const char*>& BitcodeLibFuncs);
4344

4445
/// Runs a regular LTO backend. The regular LTO backend can also act as the
4546
/// regular LTO phase of ThinLTO, which may need to access the combined index.
4647
LLVM_ABI Error backend(const Config &C, AddStreamFn AddStream,
4748
unsigned ParallelCodeGenParallelismLevel, Module &M,
48-
ModuleSummaryIndex &CombinedIndex);
49+
ModuleSummaryIndex &CombinedIndex,
50+
const SmallVector<const char *> &BitcodeLibFuncs);
4951

5052
/// Runs a ThinLTO backend.
5153
/// If \p ModuleMap is not nullptr, all the module files to be imported have
@@ -62,6 +64,7 @@ thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream, Module &M,
6264
const FunctionImporter::ImportMapTy &ImportList,
6365
const GVSummaryMapTy &DefinedGlobals,
6466
MapVector<StringRef, BitcodeModule> *ModuleMap, bool CodeGenOnly,
67+
const SmallVector<const char *> &BitcodeLibFuncs,
6568
AddStreamFn IRAddStream = nullptr,
6669
const std::vector<uint8_t> &CmdArgs = std::vector<uint8_t>());
6770

llvm/lib/LTO/LTO.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,10 @@ Error LTO::add(std::unique_ptr<InputFile> Input,
763763
return Error::success();
764764
}
765765

766+
void LTO::setBitcodeLibFuncs(const SmallVector<const char *> &BitcodeLibFuncs) {
767+
this->BitcodeLibFuncs = &BitcodeLibFuncs;
768+
}
769+
766770
Expected<ArrayRef<SymbolResolution>>
767771
LTO::addModule(InputFile &Input, ArrayRef<SymbolResolution> InputRes,
768772
unsigned ModI, ArrayRef<SymbolResolution> Res) {
@@ -1387,7 +1391,8 @@ Error LTO::runRegularLTO(AddStreamFn AddStream) {
13871391
if (!RegularLTO.EmptyCombinedModule || Conf.AlwaysEmitRegularLTOObj) {
13881392
if (Error Err =
13891393
backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
1390-
*RegularLTO.CombinedModule, ThinLTO.CombinedIndex))
1394+
*RegularLTO.CombinedModule, ThinLTO.CombinedIndex,
1395+
*BitcodeLibFuncs))
13911396
return Err;
13921397
}
13931398

@@ -1499,18 +1504,20 @@ class CGThinBackend : public ThinBackendProc {
14991504
class InProcessThinBackend : public CGThinBackend {
15001505
protected:
15011506
FileCache Cache;
1507+
const SmallVector<const char*> &BitcodeLibFuncs;
15021508

15031509
public:
15041510
InProcessThinBackend(
15051511
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
15061512
ThreadPoolStrategy ThinLTOParallelism,
15071513
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
15081514
AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1509-
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
1515+
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
1516+
const SmallVector<const char *> &BitcodeLibFuncs)
15101517
: CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
15111518
AddStream, OnWrite, ShouldEmitIndexFiles,
15121519
ShouldEmitImportsFiles, ThinLTOParallelism),
1513-
Cache(std::move(Cache)) {}
1520+
Cache(std::move(Cache)), BitcodeLibFuncs(BitcodeLibFuncs) {}
15141521

15151522
virtual Error runThinLTOBackendThread(
15161523
AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
@@ -1531,7 +1538,7 @@ class InProcessThinBackend : public CGThinBackend {
15311538

15321539
return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
15331540
ImportList, DefinedGlobals, &ModuleMap,
1534-
Conf.CodeGenOnly);
1541+
Conf.CodeGenOnly, BitcodeLibFuncs);
15351542
};
15361543
if (ShouldEmitIndexFiles) {
15371544
if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
@@ -1616,13 +1623,14 @@ class FirstRoundThinBackend : public InProcessThinBackend {
16161623
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
16171624
ThreadPoolStrategy ThinLTOParallelism,
16181625
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1619-
AddStreamFn CGAddStream, FileCache CGCache, AddStreamFn IRAddStream,
1626+
AddStreamFn CGAddStream, FileCache CGCache,
1627+
const SmallVector<const char *> &BitcodeLibFuncs, AddStreamFn IRAddStream,
16201628
FileCache IRCache)
16211629
: InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
16221630
ModuleToDefinedGVSummaries, std::move(CGAddStream),
16231631
std::move(CGCache), /*OnWrite=*/nullptr,
16241632
/*ShouldEmitIndexFiles=*/false,
1625-
/*ShouldEmitImportsFiles=*/false),
1633+
/*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
16261634
IRAddStream(std::move(IRAddStream)), IRCache(std::move(IRCache)) {}
16271635

16281636
Error runThinLTOBackendThread(
@@ -1645,7 +1653,7 @@ class FirstRoundThinBackend : public InProcessThinBackend {
16451653

16461654
return thinBackend(Conf, Task, CGAddStream, **MOrErr, CombinedIndex,
16471655
ImportList, DefinedGlobals, &ModuleMap,
1648-
Conf.CodeGenOnly, IRAddStream);
1656+
Conf.CodeGenOnly, BitcodeLibFuncs, IRAddStream);
16491657
};
16501658
// Like InProcessThinBackend, we produce index files as needed for
16511659
// FirstRoundThinBackend. However, these files are not generated for
@@ -1712,14 +1720,15 @@ class SecondRoundThinBackend : public InProcessThinBackend {
17121720
ThreadPoolStrategy ThinLTOParallelism,
17131721
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
17141722
AddStreamFn AddStream, FileCache Cache,
1723+
const SmallVector<const char *> &BitcodeLibFuncs,
17151724
std::unique_ptr<SmallVector<StringRef>> IRFiles,
17161725
stable_hash CombinedCGDataHash)
17171726
: InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
17181727
ModuleToDefinedGVSummaries, std::move(AddStream),
17191728
std::move(Cache),
17201729
/*OnWrite=*/nullptr,
17211730
/*ShouldEmitIndexFiles=*/false,
1722-
/*ShouldEmitImportsFiles=*/false),
1731+
/*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
17231732
IRFiles(std::move(IRFiles)), CombinedCGDataHash(CombinedCGDataHash) {}
17241733

17251734
Error runThinLTOBackendThread(
@@ -1740,7 +1749,7 @@ class SecondRoundThinBackend : public InProcessThinBackend {
17401749

17411750
return thinBackend(Conf, Task, AddStream, *LoadedModule, CombinedIndex,
17421751
ImportList, DefinedGlobals, &ModuleMap,
1743-
/*CodeGenOnly=*/true);
1752+
/*CodeGenOnly=*/true, BitcodeLibFuncs);
17441753
};
17451754
if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
17461755
all_of(CombinedIndex.getModuleHash(ModuleID),
@@ -1779,11 +1788,12 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
17791788
auto Func =
17801789
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
17811790
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1782-
AddStreamFn AddStream, FileCache Cache) {
1791+
AddStreamFn AddStream, FileCache Cache,
1792+
const SmallVector<const char *> &BitcodeLibFuncs) {
17831793
return std::make_unique<InProcessThinBackend>(
17841794
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
17851795
AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
1786-
ShouldEmitImportsFiles);
1796+
ShouldEmitImportsFiles, BitcodeLibFuncs);
17871797
};
17881798
return ThinBackend(Func, Parallelism);
17891799
}
@@ -1900,7 +1910,8 @@ ThinBackend lto::createWriteIndexesThinBackend(
19001910
auto Func =
19011911
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
19021912
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1903-
AddStreamFn AddStream, FileCache Cache) {
1913+
AddStreamFn AddStream, FileCache Cache,
1914+
const SmallVector<const char *> &BitcodeLibFuncs) {
19041915
return std::make_unique<WriteIndexesThinBackend>(
19051916
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
19061917
OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
@@ -2118,7 +2129,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
21182129
if (!CodeGenDataThinLTOTwoRounds) {
21192130
std::unique_ptr<ThinBackendProc> BackendProc =
21202131
ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2121-
AddStream, Cache);
2132+
AddStream, Cache, *BitcodeLibFuncs);
21222133
return RunBackends(BackendProc.get());
21232134
}
21242135

@@ -2141,7 +2152,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
21412152
LLVM_DEBUG(dbgs() << "[TwoRounds] Running the first round of codegen\n");
21422153
auto FirstRoundLTO = std::make_unique<FirstRoundThinBackend>(
21432154
Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2144-
CG.AddStream, CG.Cache, IR.AddStream, IR.Cache);
2155+
CG.AddStream, CG.Cache, *BitcodeLibFuncs, IR.AddStream, IR.Cache);
21452156
if (Error E = RunBackends(FirstRoundLTO.get()))
21462157
return E;
21472158

@@ -2157,7 +2168,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
21572168
LLVM_DEBUG(dbgs() << "[TwoRounds] Running the second round of codegen\n");
21582169
auto SecondRoundLTO = std::make_unique<SecondRoundThinBackend>(
21592170
Conf, ThinLTO.CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
2160-
AddStream, Cache, IR.getResult(), CombinedHash);
2171+
AddStream, Cache, *BitcodeLibFuncs, IR.getResult(), CombinedHash);
21612172
return RunBackends(SecondRoundLTO.get());
21622173
}
21632174

@@ -2635,7 +2646,8 @@ ThinBackend lto::createOutOfProcessThinBackend(
26352646
auto Func =
26362647
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
26372648
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2638-
AddStreamFn AddStream, FileCache Cache) {
2649+
AddStreamFn AddStream, FileCache Cache,
2650+
const SmallVector<const char *> &BitcodeLibFuncs) {
26392651
return std::make_unique<OutOfProcessThinBackend>(
26402652
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
26412653
AddStream, Cache, OnWrite, ShouldEmitIndexFiles,

llvm/lib/LTO/LTOBackend.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ static bool isEmptyModule(const Module &Mod) {
376376
bool lto::opt(const Config &Conf, TargetMachine *TM, unsigned Task, Module &Mod,
377377
bool IsThinLTO, ModuleSummaryIndex *ExportSummary,
378378
const ModuleSummaryIndex *ImportSummary,
379-
const std::vector<uint8_t> &CmdArgs) {
379+
const std::vector<uint8_t> &CmdArgs,
380+
const SmallVector<const char *> &BitcodeLibFuncs) {
380381
llvm::TimeTraceScope timeScope("opt");
381382
if (EmbedBitcode == LTOBitcodeEmbedding::EmbedPostMergePreOptimized) {
382383
// FIXME: the motivation for capturing post-merge bitcode and command line
@@ -564,7 +565,8 @@ Error lto::finalizeOptimizationRemarks(LLVMRemarkFileHandle DiagOutputFile) {
564565

565566
Error lto::backend(const Config &C, AddStreamFn AddStream,
566567
unsigned ParallelCodeGenParallelismLevel, Module &Mod,
567-
ModuleSummaryIndex &CombinedIndex) {
568+
ModuleSummaryIndex &CombinedIndex,
569+
const SmallVector<const char *> &BitcodeLibFuncs) {
568570
llvm::TimeTraceScope timeScope("LTO backend");
569571
Expected<const Target *> TOrErr = initAndLookupTarget(C, Mod);
570572
if (!TOrErr)
@@ -576,7 +578,7 @@ Error lto::backend(const Config &C, AddStreamFn AddStream,
576578
if (!C.CodeGenOnly) {
577579
if (!opt(C, TM.get(), 0, Mod, /*IsThinLTO=*/false,
578580
/*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
579-
/*CmdArgs*/ std::vector<uint8_t>()))
581+
/*CmdArgs*/ std::vector<uint8_t>(), BitcodeLibFuncs))
580582
return Error::success();
581583
}
582584

@@ -616,7 +618,9 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
616618
const FunctionImporter::ImportMapTy &ImportList,
617619
const GVSummaryMapTy &DefinedGlobals,
618620
MapVector<StringRef, BitcodeModule> *ModuleMap,
619-
bool CodeGenOnly, AddStreamFn IRAddStream,
621+
bool CodeGenOnly,
622+
const SmallVector<const char *> &BitcodeLibFuncs,
623+
AddStreamFn IRAddStream,
620624
const std::vector<uint8_t> &CmdArgs) {
621625
llvm::TimeTraceScope timeScope("Thin backend", Mod.getModuleIdentifier());
622626
Expected<const Target *> TOrErr = initAndLookupTarget(Conf, Mod);
@@ -655,7 +659,7 @@ Error lto::thinBackend(const Config &Conf, unsigned Task, AddStreamFn AddStream,
655659
// Perform optimization and code generation for ThinLTO.
656660
if (!opt(Conf, TM, Task, Mod, /*IsThinLTO=*/true,
657661
/*ExportSummary=*/nullptr, /*ImportSummary=*/&CombinedIndex,
658-
CmdArgs))
662+
CmdArgs, BitcodeLibFuncs))
659663
return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
660664

661665
// Save the current module before the first codegen round.

llvm/lib/LTO/LTOCodeGenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ bool LTOCodeGenerator::optimize() {
614614
TargetMach = createTargetMachine();
615615
if (!opt(Config, TargetMach.get(), 0, *MergedModule, /*IsThinLTO=*/false,
616616
/*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
617-
/*CmdArgs*/ std::vector<uint8_t>())) {
617+
/*CmdArgs*/ std::vector<uint8_t>(), {})) {
618618
emitError("LTO middle-end optimizations failed");
619619
return false;
620620
}
@@ -639,7 +639,7 @@ bool LTOCodeGenerator::compileOptimized(AddStreamFn AddStream,
639639

640640
Config.CodeGenOnly = true;
641641
Error Err = backend(Config, AddStream, ParallelismLevel, *MergedModule,
642-
CombinedIndex);
642+
CombinedIndex, {});
643643
assert(!Err && "unexpected code-generation failure");
644644
(void)Err;
645645

0 commit comments

Comments
 (0)