1 //===- LTO.cpp ------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "InputFiles.h"
12 #include "SymbolTable.h"
14 #include "lld/Common/Args.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Filesystem.h"
17 #include "lld/Common/Strings.h"
18 #include "lld/Common/TargetOptionsCommandFlags.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/BinaryFormat/ELF.h"
23 #include "llvm/Bitcode/BitcodeWriter.h"
24 #include "llvm/LTO/Config.h"
25 #include "llvm/LTO/LTO.h"
26 #include "llvm/Support/Caching.h"
27 #include "llvm/Support/CodeGen.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/MemoryBuffer.h"
35 #include <system_error>
39 using namespace llvm::object
;
40 using namespace llvm::ELF
;
42 using namespace lld::elf
;
44 static std::string
getThinLTOOutputFile(StringRef modulePath
) {
45 return lto::getThinLTOOutputFile(modulePath
, config
->thinLTOPrefixReplaceOld
,
46 config
->thinLTOPrefixReplaceNew
);
49 static lto::Config
createConfig() {
52 // LLD supports the new relocations and address-significance tables.
53 c
.Options
= initTargetOptionsFromCodeGenFlags();
54 c
.Options
.EmitAddrsig
= true;
55 for (StringRef C
: config
->mllvmOpts
)
56 c
.MllvmArgs
.emplace_back(C
.str());
58 // Always emit a section per function/datum with LTO.
59 c
.Options
.FunctionSections
= true;
60 c
.Options
.DataSections
= true;
62 // Check if basic block sections must be used.
63 // Allowed values for --lto-basic-block-sections are "all", "labels",
64 // "<file name specifying basic block ids>", or none. This is the equivalent
65 // of -fbasic-block-sections= flag in clang.
66 if (!config
->ltoBasicBlockSections
.empty()) {
67 if (config
->ltoBasicBlockSections
== "all") {
68 c
.Options
.BBSections
= BasicBlockSection::All
;
69 } else if (config
->ltoBasicBlockSections
== "labels") {
70 c
.Options
.BBSections
= BasicBlockSection::Labels
;
71 } else if (config
->ltoBasicBlockSections
== "none") {
72 c
.Options
.BBSections
= BasicBlockSection::None
;
74 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
=
75 MemoryBuffer::getFile(config
->ltoBasicBlockSections
.str());
77 error("cannot open " + config
->ltoBasicBlockSections
+ ":" +
78 MBOrErr
.getError().message());
80 c
.Options
.BBSectionsFuncListBuf
= std::move(*MBOrErr
);
82 c
.Options
.BBSections
= BasicBlockSection::List
;
86 c
.Options
.UniqueBasicBlockSectionNames
=
87 config
->ltoUniqueBasicBlockSectionNames
;
89 if (auto relocModel
= getRelocModelFromCMModel())
90 c
.RelocModel
= *relocModel
;
91 else if (config
->relocatable
)
92 c
.RelocModel
= std::nullopt
;
93 else if (config
->isPic
)
94 c
.RelocModel
= Reloc::PIC_
;
96 c
.RelocModel
= Reloc::Static
;
98 c
.CodeModel
= getCodeModelFromCMModel();
99 c
.DisableVerify
= config
->disableVerify
;
100 c
.DiagHandler
= diagnosticHandler
;
101 c
.OptLevel
= config
->ltoo
;
103 c
.MAttrs
= getMAttrs();
104 c
.CGOptLevel
= config
->ltoCgo
;
106 c
.PTO
.LoopVectorization
= c
.OptLevel
> 1;
107 c
.PTO
.SLPVectorization
= c
.OptLevel
> 1;
109 // Set up a custom pipeline if we've been asked to.
110 c
.OptPipeline
= std::string(config
->ltoNewPmPasses
);
111 c
.AAPipeline
= std::string(config
->ltoAAPipeline
);
113 // Set up optimization remarks if we've been asked to.
114 c
.RemarksFilename
= std::string(config
->optRemarksFilename
);
115 c
.RemarksPasses
= std::string(config
->optRemarksPasses
);
116 c
.RemarksWithHotness
= config
->optRemarksWithHotness
;
117 c
.RemarksHotnessThreshold
= config
->optRemarksHotnessThreshold
;
118 c
.RemarksFormat
= std::string(config
->optRemarksFormat
);
120 // Set up output file to emit statistics.
121 c
.StatsFile
= std::string(config
->optStatsFilename
);
123 c
.SampleProfile
= std::string(config
->ltoSampleProfile
);
124 for (StringRef pluginFn
: config
->passPlugins
)
125 c
.PassPlugins
.push_back(std::string(pluginFn
));
126 c
.DebugPassManager
= config
->ltoDebugPassManager
;
127 c
.DwoDir
= std::string(config
->dwoDir
);
129 c
.HasWholeProgramVisibility
= config
->ltoWholeProgramVisibility
;
130 c
.ValidateAllVtablesHaveTypeInfos
=
131 config
->ltoValidateAllVtablesHaveTypeInfos
;
132 c
.AllVtablesHaveTypeInfos
= ctx
.ltoAllVtablesHaveTypeInfos
;
133 c
.AlwaysEmitRegularLTOObj
= !config
->ltoObjPath
.empty();
135 for (const llvm::StringRef
&name
: config
->thinLTOModulesToCompile
)
136 c
.ThinLTOModulesToCompile
.emplace_back(name
);
138 c
.TimeTraceEnabled
= config
->timeTraceEnabled
;
139 c
.TimeTraceGranularity
= config
->timeTraceGranularity
;
141 c
.CSIRProfile
= std::string(config
->ltoCSProfileFile
);
142 c
.RunCSIRInstr
= config
->ltoCSProfileGenerate
;
143 c
.PGOWarnMismatch
= config
->ltoPGOWarnMismatch
;
145 if (config
->emitLLVM
) {
146 c
.PostInternalizeModuleHook
= [](size_t task
, const Module
&m
) {
147 if (std::unique_ptr
<raw_fd_ostream
> os
=
148 openLTOOutputFile(config
->outputFile
))
149 WriteBitcodeToFile(m
, *os
, false);
154 if (config
->ltoEmitAsm
) {
155 c
.CGFileType
= CodeGenFileType::AssemblyFile
;
156 c
.Options
.MCOptions
.AsmVerbose
= true;
159 if (!config
->saveTempsArgs
.empty())
160 checkError(c
.addSaveTemps(config
->outputFile
.str() + ".",
161 /*UseInputModulePath*/ true,
162 config
->saveTempsArgs
));
166 BitcodeCompiler::BitcodeCompiler() {
167 // Initialize indexFile.
168 if (!config
->thinLTOIndexOnlyArg
.empty())
169 indexFile
= openFile(config
->thinLTOIndexOnlyArg
);
171 // Initialize ltoObj.
172 lto::ThinBackend backend
;
173 auto onIndexWrite
= [&](StringRef s
) { thinIndices
.erase(s
); };
174 if (config
->thinLTOIndexOnly
) {
175 backend
= lto::createWriteIndexesThinBackend(
176 std::string(config
->thinLTOPrefixReplaceOld
),
177 std::string(config
->thinLTOPrefixReplaceNew
),
178 std::string(config
->thinLTOPrefixReplaceNativeObject
),
179 config
->thinLTOEmitImportsFiles
, indexFile
.get(), onIndexWrite
);
181 backend
= lto::createInProcessThinBackend(
182 llvm::heavyweight_hardware_concurrency(config
->thinLTOJobs
),
183 onIndexWrite
, config
->thinLTOEmitIndexFiles
,
184 config
->thinLTOEmitImportsFiles
);
187 constexpr llvm::lto::LTO::LTOKind ltoModes
[3] =
188 {llvm::lto::LTO::LTOKind::LTOK_UnifiedThin
,
189 llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular
,
190 llvm::lto::LTO::LTOKind::LTOK_Default
};
191 ltoObj
= std::make_unique
<lto::LTO
>(
192 createConfig(), backend
, config
->ltoPartitions
,
193 ltoModes
[config
->ltoKind
]);
195 // Initialize usedStartStop.
196 if (ctx
.bitcodeFiles
.empty())
198 for (Symbol
*sym
: symtab
.getSymbols()) {
199 if (sym
->isPlaceholder())
201 StringRef s
= sym
->getName();
202 for (StringRef prefix
: {"__start_", "__stop_"})
203 if (s
.starts_with(prefix
))
204 usedStartStop
.insert(s
.substr(prefix
.size()));
208 BitcodeCompiler::~BitcodeCompiler() = default;
210 void BitcodeCompiler::add(BitcodeFile
&f
) {
211 lto::InputFile
&obj
= *f
.obj
;
212 bool isExec
= !config
->shared
&& !config
->relocatable
;
214 if (config
->thinLTOEmitIndexFiles
)
215 thinIndices
.insert(obj
.getName());
217 ArrayRef
<Symbol
*> syms
= f
.getSymbols();
218 ArrayRef
<lto::InputFile::Symbol
> objSyms
= obj
.symbols();
219 std::vector
<lto::SymbolResolution
> resols(syms
.size());
221 // Provide a resolution to the LTO API for each symbol.
222 for (size_t i
= 0, e
= syms
.size(); i
!= e
; ++i
) {
223 Symbol
*sym
= syms
[i
];
224 const lto::InputFile::Symbol
&objSym
= objSyms
[i
];
225 lto::SymbolResolution
&r
= resols
[i
];
227 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
228 // reports two symbols for module ASM defined. Without this check, lld
229 // flags an undefined in IR with a definition in ASM as prevailing.
230 // Once IRObjectFile is fixed to report only one symbol this hack can
232 r
.Prevailing
= !objSym
.isUndefined() && sym
->file
== &f
;
234 // We ask LTO to preserve following global symbols:
235 // 1) All symbols when doing relocatable link, so that them can be used
236 // for doing final link.
237 // 2) Symbols that are used in regular objects.
238 // 3) C named sections if we have corresponding __start_/__stop_ symbol.
239 // 4) Symbols that are defined in bitcode files and used for dynamic
241 // 5) Symbols that will be referenced after linker wrapping is performed.
242 r
.VisibleToRegularObj
= config
->relocatable
|| sym
->isUsedInRegularObj
||
243 sym
->referencedAfterWrap
||
244 (r
.Prevailing
&& sym
->includeInDynsym()) ||
245 usedStartStop
.count(objSym
.getSectionName());
246 // Identify symbols exported dynamically, and that therefore could be
247 // referenced by a shared library not visible to the linker.
249 sym
->computeBinding() != STB_LOCAL
&&
250 (config
->exportDynamic
|| sym
->exportDynamic
|| sym
->inDynamicList
);
251 const auto *dr
= dyn_cast
<Defined
>(sym
);
252 r
.FinalDefinitionInLinkageUnit
=
253 (isExec
|| sym
->visibility() != STV_DEFAULT
) && dr
&&
254 // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
255 // will be generated by for them, triggering linker errors.
256 // Symbol section is always null for bitcode symbols, hence the check
257 // for isElf(). Skip linker script defined symbols as well: they have
259 !(dr
->section
== nullptr && (!sym
->file
|| sym
->file
->isElf()));
262 Undefined(nullptr, StringRef(), STB_GLOBAL
, STV_DEFAULT
, sym
->type
)
265 // We tell LTO to not apply interprocedural optimization for wrapped
266 // (with --wrap) symbols because otherwise LTO would inline them while
267 // their values are still not final.
268 r
.LinkerRedefined
= sym
->scriptDefined
;
270 checkError(ltoObj
->add(std::move(f
.obj
), resols
));
273 // If LazyObjFile has not been added to link, emit empty index files.
274 // This is needed because this is what GNU gold plugin does and we have a
275 // distributed build system that depends on that behavior.
276 static void thinLTOCreateEmptyIndexFiles() {
277 DenseSet
<StringRef
> linkedBitCodeFiles
;
278 for (BitcodeFile
*f
: ctx
.bitcodeFiles
)
279 linkedBitCodeFiles
.insert(f
->getName());
281 for (BitcodeFile
*f
: ctx
.lazyBitcodeFiles
) {
284 if (linkedBitCodeFiles
.contains(f
->getName()))
287 replaceThinLTOSuffix(getThinLTOOutputFile(f
->obj
->getName()));
288 std::unique_ptr
<raw_fd_ostream
> os
= openFile(path
+ ".thinlto.bc");
292 ModuleSummaryIndex
m(/*HaveGVs*/ false);
293 m
.setSkipModuleByDistributedBackend();
294 writeIndexToFile(m
, *os
);
295 if (config
->thinLTOEmitImportsFiles
)
296 openFile(path
+ ".imports");
300 // Merge all the bitcode files we have seen, codegen the result
301 // and return the resulting ObjectFile(s).
302 std::vector
<InputFile
*> BitcodeCompiler::compile() {
303 unsigned maxTasks
= ltoObj
->getMaxTasks();
304 buf
.resize(maxTasks
);
305 files
.resize(maxTasks
);
307 // The --thinlto-cache-dir option specifies the path to a directory in which
308 // to cache native object files for ThinLTO incremental builds. If a path was
309 // specified, configure LTO to use it as the cache directory.
311 if (!config
->thinLTOCacheDir
.empty())
312 cache
= check(localCache("ThinLTO", "Thin", config
->thinLTOCacheDir
,
313 [&](size_t task
, const Twine
&moduleName
,
314 std::unique_ptr
<MemoryBuffer
> mb
) {
315 files
[task
] = std::move(mb
);
318 if (!ctx
.bitcodeFiles
.empty())
319 checkError(ltoObj
->run(
320 [&](size_t task
, const Twine
&moduleName
) {
321 return std::make_unique
<CachedFileStream
>(
322 std::make_unique
<raw_svector_ostream
>(buf
[task
]));
326 // Emit empty index files for non-indexed files but not in single-module mode.
327 if (config
->thinLTOModulesToCompile
.empty()) {
328 for (StringRef s
: thinIndices
) {
329 std::string path
= getThinLTOOutputFile(s
);
330 openFile(path
+ ".thinlto.bc");
331 if (config
->thinLTOEmitImportsFiles
)
332 openFile(path
+ ".imports");
336 if (config
->thinLTOEmitIndexFiles
)
337 thinLTOCreateEmptyIndexFiles();
339 if (config
->thinLTOIndexOnly
) {
340 if (!config
->ltoObjPath
.empty())
341 saveBuffer(buf
[0], config
->ltoObjPath
);
343 // ThinLTO with index only option is required to generate only the index
344 // files. After that, we exit from linker and ThinLTO backend runs in a
345 // distributed environment.
351 if (!config
->thinLTOCacheDir
.empty())
352 pruneCache(config
->thinLTOCacheDir
, config
->thinLTOCachePolicy
, files
);
354 if (!config
->ltoObjPath
.empty()) {
355 saveBuffer(buf
[0], config
->ltoObjPath
);
356 for (unsigned i
= 1; i
!= maxTasks
; ++i
)
357 saveBuffer(buf
[i
], config
->ltoObjPath
+ Twine(i
));
360 if (config
->saveTempsArgs
.contains("prelink")) {
362 saveBuffer(buf
[0], config
->outputFile
+ ".lto.o");
363 for (unsigned i
= 1; i
!= maxTasks
; ++i
)
364 saveBuffer(buf
[i
], config
->outputFile
+ Twine(i
) + ".lto.o");
367 if (config
->ltoEmitAsm
) {
368 saveBuffer(buf
[0], config
->outputFile
);
369 for (unsigned i
= 1; i
!= maxTasks
; ++i
)
370 saveBuffer(buf
[i
], config
->outputFile
+ Twine(i
));
374 std::vector
<InputFile
*> ret
;
375 for (unsigned i
= 0; i
!= maxTasks
; ++i
)
377 ret
.push_back(createObjFile(MemoryBufferRef(buf
[i
], "lto.tmp")));
379 for (std::unique_ptr
<MemoryBuffer
> &file
: files
)
381 ret
.push_back(createObjFile(*file
));