Skip to content

Commit 0434173

Browse files
committed
Pass through to LTO
1 parent b52feaa commit 0434173

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
@@ -762,6 +762,10 @@ Error LTO::add(std::unique_ptr<InputFile> Input,
762762
return Error::success();
763763
}
764764

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

@@ -1498,18 +1503,20 @@ class CGThinBackend : public ThinBackendProc {
14981503
class InProcessThinBackend : public CGThinBackend {
14991504
protected:
15001505
FileCache Cache;
1506+
const SmallVector<const char*> &BitcodeLibFuncs;
15011507

15021508
public:
15031509
InProcessThinBackend(
15041510
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
15051511
ThreadPoolStrategy ThinLTOParallelism,
15061512
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
15071513
AddStreamFn AddStream, FileCache Cache, lto::IndexWriteCallback OnWrite,
1508-
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles)
1514+
bool ShouldEmitIndexFiles, bool ShouldEmitImportsFiles,
1515+
const SmallVector<const char *> &BitcodeLibFuncs)
15091516
: CGThinBackend(Conf, CombinedIndex, ModuleToDefinedGVSummaries,
15101517
AddStream, OnWrite, ShouldEmitIndexFiles,
15111518
ShouldEmitImportsFiles, ThinLTOParallelism),
1512-
Cache(std::move(Cache)) {}
1519+
Cache(std::move(Cache)), BitcodeLibFuncs(BitcodeLibFuncs) {}
15131520

15141521
virtual Error runThinLTOBackendThread(
15151522
AddStreamFn AddStream, FileCache Cache, unsigned Task, BitcodeModule BM,
@@ -1530,7 +1537,7 @@ class InProcessThinBackend : public CGThinBackend {
15301537

15311538
return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
15321539
ImportList, DefinedGlobals, &ModuleMap,
1533-
Conf.CodeGenOnly);
1540+
Conf.CodeGenOnly, BitcodeLibFuncs);
15341541
};
15351542
if (ShouldEmitIndexFiles) {
15361543
if (auto E = emitFiles(ImportList, ModuleID, ModuleID.str()))
@@ -1615,13 +1622,14 @@ class FirstRoundThinBackend : public InProcessThinBackend {
16151622
const Config &Conf, ModuleSummaryIndex &CombinedIndex,
16161623
ThreadPoolStrategy ThinLTOParallelism,
16171624
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1618-
AddStreamFn CGAddStream, FileCache CGCache, AddStreamFn IRAddStream,
1625+
AddStreamFn CGAddStream, FileCache CGCache,
1626+
const SmallVector<const char *> &BitcodeLibFuncs, AddStreamFn IRAddStream,
16191627
FileCache IRCache)
16201628
: InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
16211629
ModuleToDefinedGVSummaries, std::move(CGAddStream),
16221630
std::move(CGCache), /*OnWrite=*/nullptr,
16231631
/*ShouldEmitIndexFiles=*/false,
1624-
/*ShouldEmitImportsFiles=*/false),
1632+
/*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
16251633
IRAddStream(std::move(IRAddStream)), IRCache(std::move(IRCache)) {}
16261634

16271635
Error runThinLTOBackendThread(
@@ -1644,7 +1652,7 @@ class FirstRoundThinBackend : public InProcessThinBackend {
16441652

16451653
return thinBackend(Conf, Task, CGAddStream, **MOrErr, CombinedIndex,
16461654
ImportList, DefinedGlobals, &ModuleMap,
1647-
Conf.CodeGenOnly, IRAddStream);
1655+
Conf.CodeGenOnly, BitcodeLibFuncs, IRAddStream);
16481656
};
16491657
// Like InProcessThinBackend, we produce index files as needed for
16501658
// FirstRoundThinBackend. However, these files are not generated for
@@ -1711,14 +1719,15 @@ class SecondRoundThinBackend : public InProcessThinBackend {
17111719
ThreadPoolStrategy ThinLTOParallelism,
17121720
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
17131721
AddStreamFn AddStream, FileCache Cache,
1722+
const SmallVector<const char *> &BitcodeLibFuncs,
17141723
std::unique_ptr<SmallVector<StringRef>> IRFiles,
17151724
stable_hash CombinedCGDataHash)
17161725
: InProcessThinBackend(Conf, CombinedIndex, ThinLTOParallelism,
17171726
ModuleToDefinedGVSummaries, std::move(AddStream),
17181727
std::move(Cache),
17191728
/*OnWrite=*/nullptr,
17201729
/*ShouldEmitIndexFiles=*/false,
1721-
/*ShouldEmitImportsFiles=*/false),
1730+
/*ShouldEmitImportsFiles=*/false, BitcodeLibFuncs),
17221731
IRFiles(std::move(IRFiles)), CombinedCGDataHash(CombinedCGDataHash) {}
17231732

17241733
Error runThinLTOBackendThread(
@@ -1739,7 +1748,7 @@ class SecondRoundThinBackend : public InProcessThinBackend {
17391748

17401749
return thinBackend(Conf, Task, AddStream, *LoadedModule, CombinedIndex,
17411750
ImportList, DefinedGlobals, &ModuleMap,
1742-
/*CodeGenOnly=*/true);
1751+
/*CodeGenOnly=*/true, BitcodeLibFuncs);
17431752
};
17441753
if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
17451754
all_of(CombinedIndex.getModuleHash(ModuleID),
@@ -1778,11 +1787,12 @@ ThinBackend lto::createInProcessThinBackend(ThreadPoolStrategy Parallelism,
17781787
auto Func =
17791788
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
17801789
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1781-
AddStreamFn AddStream, FileCache Cache) {
1790+
AddStreamFn AddStream, FileCache Cache,
1791+
const SmallVector<const char *> &BitcodeLibFuncs) {
17821792
return std::make_unique<InProcessThinBackend>(
17831793
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
17841794
AddStream, Cache, OnWrite, ShouldEmitIndexFiles,
1785-
ShouldEmitImportsFiles);
1795+
ShouldEmitImportsFiles, BitcodeLibFuncs);
17861796
};
17871797
return ThinBackend(Func, Parallelism);
17881798
}
@@ -1899,7 +1909,8 @@ ThinBackend lto::createWriteIndexesThinBackend(
18991909
auto Func =
19001910
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
19011911
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
1902-
AddStreamFn AddStream, FileCache Cache) {
1912+
AddStreamFn AddStream, FileCache Cache,
1913+
const SmallVector<const char *> &BitcodeLibFuncs) {
19031914
return std::make_unique<WriteIndexesThinBackend>(
19041915
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
19051916
OldPrefix, NewPrefix, NativeObjectPrefix, ShouldEmitImportsFiles,
@@ -2117,7 +2128,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
21172128
if (!CodeGenDataThinLTOTwoRounds) {
21182129
std::unique_ptr<ThinBackendProc> BackendProc =
21192130
ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
2120-
AddStream, Cache);
2131+
AddStream, Cache, *BitcodeLibFuncs);
21212132
return RunBackends(BackendProc.get());
21222133
}
21232134

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

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

@@ -2540,7 +2551,8 @@ ThinBackend lto::createOutOfProcessThinBackend(
25402551
auto Func =
25412552
[=](const Config &Conf, ModuleSummaryIndex &CombinedIndex,
25422553
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
2543-
AddStreamFn AddStream, FileCache /*Cache*/) {
2554+
AddStreamFn AddStream, FileCache /*Cache*/,
2555+
const SmallVector<const char *> &BitcodeLibFuncs) {
25442556
return std::make_unique<OutOfProcessThinBackend>(
25452557
Conf, CombinedIndex, Parallelism, ModuleToDefinedGVSummaries,
25462558
AddStream, OnWrite, ShouldEmitIndexFiles, ShouldEmitImportsFiles,

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)