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/Strings.h"
17 #include "lld/Common/TargetOptionsCommandFlags.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/Bitcode/BitcodeWriter.h"
23 #include "llvm/LTO/Config.h"
24 #include "llvm/LTO/LTO.h"
25 #include "llvm/Support/Caching.h"
26 #include "llvm/Support/CodeGen.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/MemoryBuffer.h"
34 #include <system_error>
38 using namespace llvm::object
;
39 using namespace llvm::ELF
;
41 using namespace lld::elf
;
43 // Creates an empty file to store a list of object files for final
44 // linking of distributed ThinLTO.
45 static std::unique_ptr
<raw_fd_ostream
> openFile(StringRef file
) {
48 std::make_unique
<raw_fd_ostream
>(file
, ec
, sys::fs::OpenFlags::OF_None
);
50 error("cannot open " + file
+ ": " + ec
.message());
56 // The merged bitcode after LTO is large. Try opening a file stream that
57 // supports reading, seeking and writing. Such a file allows BitcodeWriter to
58 // flush buffered data to reduce memory consumption. If this fails, open a file
59 // stream that supports only write.
60 static std::unique_ptr
<raw_fd_ostream
> openLTOOutputFile(StringRef file
) {
62 std::unique_ptr
<raw_fd_ostream
> fs
=
63 std::make_unique
<raw_fd_stream
>(file
, ec
);
66 return openFile(file
);
69 static std::string
getThinLTOOutputFile(StringRef modulePath
) {
70 return lto::getThinLTOOutputFile(modulePath
, config
->thinLTOPrefixReplaceOld
,
71 config
->thinLTOPrefixReplaceNew
);
74 static lto::Config
createConfig() {
77 // LLD supports the new relocations and address-significance tables.
78 c
.Options
= initTargetOptionsFromCodeGenFlags();
79 c
.Options
.EmitAddrsig
= true;
80 for (StringRef C
: config
->mllvmOpts
)
81 c
.MllvmArgs
.emplace_back(C
.str());
83 // Always emit a section per function/datum with LTO.
84 c
.Options
.FunctionSections
= true;
85 c
.Options
.DataSections
= true;
87 // Check if basic block sections must be used.
88 // Allowed values for --lto-basic-block-sections are "all", "labels",
89 // "<file name specifying basic block ids>", or none. This is the equivalent
90 // of -fbasic-block-sections= flag in clang.
91 if (!config
->ltoBasicBlockSections
.empty()) {
92 if (config
->ltoBasicBlockSections
== "all") {
93 c
.Options
.BBSections
= BasicBlockSection::All
;
94 } else if (config
->ltoBasicBlockSections
== "labels") {
95 c
.Options
.BBSections
= BasicBlockSection::Labels
;
96 } else if (config
->ltoBasicBlockSections
== "none") {
97 c
.Options
.BBSections
= BasicBlockSection::None
;
99 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MBOrErr
=
100 MemoryBuffer::getFile(config
->ltoBasicBlockSections
.str());
102 error("cannot open " + config
->ltoBasicBlockSections
+ ":" +
103 MBOrErr
.getError().message());
105 c
.Options
.BBSectionsFuncListBuf
= std::move(*MBOrErr
);
107 c
.Options
.BBSections
= BasicBlockSection::List
;
111 c
.Options
.UniqueBasicBlockSectionNames
=
112 config
->ltoUniqueBasicBlockSectionNames
;
114 if (auto relocModel
= getRelocModelFromCMModel())
115 c
.RelocModel
= *relocModel
;
116 else if (config
->relocatable
)
117 c
.RelocModel
= std::nullopt
;
118 else if (config
->isPic
)
119 c
.RelocModel
= Reloc::PIC_
;
121 c
.RelocModel
= Reloc::Static
;
123 c
.CodeModel
= getCodeModelFromCMModel();
124 c
.DisableVerify
= config
->disableVerify
;
125 c
.DiagHandler
= diagnosticHandler
;
126 c
.OptLevel
= config
->ltoo
;
128 c
.MAttrs
= getMAttrs();
129 c
.CGOptLevel
= config
->ltoCgo
;
131 c
.PTO
.LoopVectorization
= c
.OptLevel
> 1;
132 c
.PTO
.SLPVectorization
= c
.OptLevel
> 1;
134 // Set up a custom pipeline if we've been asked to.
135 c
.OptPipeline
= std::string(config
->ltoNewPmPasses
);
136 c
.AAPipeline
= std::string(config
->ltoAAPipeline
);
138 // Set up optimization remarks if we've been asked to.
139 c
.RemarksFilename
= std::string(config
->optRemarksFilename
);
140 c
.RemarksPasses
= std::string(config
->optRemarksPasses
);
141 c
.RemarksWithHotness
= config
->optRemarksWithHotness
;
142 c
.RemarksHotnessThreshold
= config
->optRemarksHotnessThreshold
;
143 c
.RemarksFormat
= std::string(config
->optRemarksFormat
);
145 // Set up output file to emit statistics.
146 c
.StatsFile
= std::string(config
->optStatsFilename
);
148 c
.SampleProfile
= std::string(config
->ltoSampleProfile
);
149 for (StringRef pluginFn
: config
->passPlugins
)
150 c
.PassPlugins
.push_back(std::string(pluginFn
));
151 c
.DebugPassManager
= config
->ltoDebugPassManager
;
152 c
.DwoDir
= std::string(config
->dwoDir
);
154 c
.HasWholeProgramVisibility
= config
->ltoWholeProgramVisibility
;
155 c
.AlwaysEmitRegularLTOObj
= !config
->ltoObjPath
.empty();
157 for (const llvm::StringRef
&name
: config
->thinLTOModulesToCompile
)
158 c
.ThinLTOModulesToCompile
.emplace_back(name
);
160 c
.TimeTraceEnabled
= config
->timeTraceEnabled
;
161 c
.TimeTraceGranularity
= config
->timeTraceGranularity
;
163 c
.CSIRProfile
= std::string(config
->ltoCSProfileFile
);
164 c
.RunCSIRInstr
= config
->ltoCSProfileGenerate
;
165 c
.PGOWarnMismatch
= config
->ltoPGOWarnMismatch
;
167 if (config
->emitLLVM
) {
168 c
.PostInternalizeModuleHook
= [](size_t task
, const Module
&m
) {
169 if (std::unique_ptr
<raw_fd_ostream
> os
=
170 openLTOOutputFile(config
->outputFile
))
171 WriteBitcodeToFile(m
, *os
, false);
176 if (config
->ltoEmitAsm
) {
177 c
.CGFileType
= CGFT_AssemblyFile
;
178 c
.Options
.MCOptions
.AsmVerbose
= true;
181 if (!config
->saveTempsArgs
.empty())
182 checkError(c
.addSaveTemps(config
->outputFile
.str() + ".",
183 /*UseInputModulePath*/ true,
184 config
->saveTempsArgs
));
188 BitcodeCompiler::BitcodeCompiler() {
189 // Initialize indexFile.
190 if (!config
->thinLTOIndexOnlyArg
.empty())
191 indexFile
= openFile(config
->thinLTOIndexOnlyArg
);
193 // Initialize ltoObj.
194 lto::ThinBackend backend
;
195 auto onIndexWrite
= [&](StringRef s
) { thinIndices
.erase(s
); };
196 if (config
->thinLTOIndexOnly
) {
197 backend
= lto::createWriteIndexesThinBackend(
198 std::string(config
->thinLTOPrefixReplaceOld
),
199 std::string(config
->thinLTOPrefixReplaceNew
),
200 std::string(config
->thinLTOPrefixReplaceNativeObject
),
201 config
->thinLTOEmitImportsFiles
, indexFile
.get(), onIndexWrite
);
203 backend
= lto::createInProcessThinBackend(
204 llvm::heavyweight_hardware_concurrency(config
->thinLTOJobs
),
205 onIndexWrite
, config
->thinLTOEmitIndexFiles
,
206 config
->thinLTOEmitImportsFiles
);
209 ltoObj
= std::make_unique
<lto::LTO
>(createConfig(), backend
,
210 config
->ltoPartitions
);
212 // Initialize usedStartStop.
213 if (ctx
.bitcodeFiles
.empty())
215 for (Symbol
*sym
: symtab
.getSymbols()) {
216 if (sym
->isPlaceholder())
218 StringRef s
= sym
->getName();
219 for (StringRef prefix
: {"__start_", "__stop_"})
220 if (s
.starts_with(prefix
))
221 usedStartStop
.insert(s
.substr(prefix
.size()));
225 BitcodeCompiler::~BitcodeCompiler() = default;
227 void BitcodeCompiler::add(BitcodeFile
&f
) {
228 lto::InputFile
&obj
= *f
.obj
;
229 bool isExec
= !config
->shared
&& !config
->relocatable
;
231 if (config
->thinLTOEmitIndexFiles
)
232 thinIndices
.insert(obj
.getName());
234 ArrayRef
<Symbol
*> syms
= f
.getSymbols();
235 ArrayRef
<lto::InputFile::Symbol
> objSyms
= obj
.symbols();
236 std::vector
<lto::SymbolResolution
> resols(syms
.size());
238 // Provide a resolution to the LTO API for each symbol.
239 for (size_t i
= 0, e
= syms
.size(); i
!= e
; ++i
) {
240 Symbol
*sym
= syms
[i
];
241 const lto::InputFile::Symbol
&objSym
= objSyms
[i
];
242 lto::SymbolResolution
&r
= resols
[i
];
244 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
245 // reports two symbols for module ASM defined. Without this check, lld
246 // flags an undefined in IR with a definition in ASM as prevailing.
247 // Once IRObjectFile is fixed to report only one symbol this hack can
249 r
.Prevailing
= !objSym
.isUndefined() && sym
->file
== &f
;
251 // We ask LTO to preserve following global symbols:
252 // 1) All symbols when doing relocatable link, so that them can be used
253 // for doing final link.
254 // 2) Symbols that are used in regular objects.
255 // 3) C named sections if we have corresponding __start_/__stop_ symbol.
256 // 4) Symbols that are defined in bitcode files and used for dynamic
258 // 5) Symbols that will be referenced after linker wrapping is performed.
259 r
.VisibleToRegularObj
= config
->relocatable
|| sym
->isUsedInRegularObj
||
260 sym
->referencedAfterWrap
||
261 (r
.Prevailing
&& sym
->includeInDynsym()) ||
262 usedStartStop
.count(objSym
.getSectionName());
263 // Identify symbols exported dynamically, and that therefore could be
264 // referenced by a shared library not visible to the linker.
266 sym
->computeBinding() != STB_LOCAL
&&
267 (config
->exportDynamic
|| sym
->exportDynamic
|| sym
->inDynamicList
);
268 const auto *dr
= dyn_cast
<Defined
>(sym
);
269 r
.FinalDefinitionInLinkageUnit
=
270 (isExec
|| sym
->visibility() != STV_DEFAULT
) && dr
&&
271 // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
272 // will be generated by for them, triggering linker errors.
273 // Symbol section is always null for bitcode symbols, hence the check
274 // for isElf(). Skip linker script defined symbols as well: they have
276 !(dr
->section
== nullptr && (!sym
->file
|| sym
->file
->isElf()));
279 Undefined(nullptr, StringRef(), STB_GLOBAL
, STV_DEFAULT
, sym
->type
)
282 // We tell LTO to not apply interprocedural optimization for wrapped
283 // (with --wrap) symbols because otherwise LTO would inline them while
284 // their values are still not final.
285 r
.LinkerRedefined
= sym
->scriptDefined
;
287 checkError(ltoObj
->add(std::move(f
.obj
), resols
));
290 // If LazyObjFile has not been added to link, emit empty index files.
291 // This is needed because this is what GNU gold plugin does and we have a
292 // distributed build system that depends on that behavior.
293 static void thinLTOCreateEmptyIndexFiles() {
294 DenseSet
<StringRef
> linkedBitCodeFiles
;
295 for (BitcodeFile
*f
: ctx
.bitcodeFiles
)
296 linkedBitCodeFiles
.insert(f
->getName());
298 for (BitcodeFile
*f
: ctx
.lazyBitcodeFiles
) {
301 if (linkedBitCodeFiles
.contains(f
->getName()))
304 replaceThinLTOSuffix(getThinLTOOutputFile(f
->obj
->getName()));
305 std::unique_ptr
<raw_fd_ostream
> os
= openFile(path
+ ".thinlto.bc");
309 ModuleSummaryIndex
m(/*HaveGVs*/ false);
310 m
.setSkipModuleByDistributedBackend();
311 writeIndexToFile(m
, *os
);
312 if (config
->thinLTOEmitImportsFiles
)
313 openFile(path
+ ".imports");
317 // Merge all the bitcode files we have seen, codegen the result
318 // and return the resulting ObjectFile(s).
319 std::vector
<InputFile
*> BitcodeCompiler::compile() {
320 unsigned maxTasks
= ltoObj
->getMaxTasks();
321 buf
.resize(maxTasks
);
322 files
.resize(maxTasks
);
324 // The --thinlto-cache-dir option specifies the path to a directory in which
325 // to cache native object files for ThinLTO incremental builds. If a path was
326 // specified, configure LTO to use it as the cache directory.
328 if (!config
->thinLTOCacheDir
.empty())
329 cache
= check(localCache("ThinLTO", "Thin", config
->thinLTOCacheDir
,
330 [&](size_t task
, const Twine
&moduleName
,
331 std::unique_ptr
<MemoryBuffer
> mb
) {
332 files
[task
] = std::move(mb
);
335 if (!ctx
.bitcodeFiles
.empty())
336 checkError(ltoObj
->run(
337 [&](size_t task
, const Twine
&moduleName
) {
338 return std::make_unique
<CachedFileStream
>(
339 std::make_unique
<raw_svector_ostream
>(buf
[task
]));
343 // Emit empty index files for non-indexed files but not in single-module mode.
344 if (config
->thinLTOModulesToCompile
.empty()) {
345 for (StringRef s
: thinIndices
) {
346 std::string path
= getThinLTOOutputFile(s
);
347 openFile(path
+ ".thinlto.bc");
348 if (config
->thinLTOEmitImportsFiles
)
349 openFile(path
+ ".imports");
353 if (config
->thinLTOEmitIndexFiles
)
354 thinLTOCreateEmptyIndexFiles();
356 if (config
->thinLTOIndexOnly
) {
357 if (!config
->ltoObjPath
.empty())
358 saveBuffer(buf
[0], config
->ltoObjPath
);
360 // ThinLTO with index only option is required to generate only the index
361 // files. After that, we exit from linker and ThinLTO backend runs in a
362 // distributed environment.
368 if (!config
->thinLTOCacheDir
.empty())
369 pruneCache(config
->thinLTOCacheDir
, config
->thinLTOCachePolicy
, files
);
371 if (!config
->ltoObjPath
.empty()) {
372 saveBuffer(buf
[0], config
->ltoObjPath
);
373 for (unsigned i
= 1; i
!= maxTasks
; ++i
)
374 saveBuffer(buf
[i
], config
->ltoObjPath
+ Twine(i
));
377 if (config
->saveTempsArgs
.contains("prelink")) {
379 saveBuffer(buf
[0], config
->outputFile
+ ".lto.o");
380 for (unsigned i
= 1; i
!= maxTasks
; ++i
)
381 saveBuffer(buf
[i
], config
->outputFile
+ Twine(i
) + ".lto.o");
384 if (config
->ltoEmitAsm
) {
385 saveBuffer(buf
[0], config
->outputFile
);
386 for (unsigned i
= 1; i
!= maxTasks
; ++i
)
387 saveBuffer(buf
[i
], config
->outputFile
+ Twine(i
));
391 std::vector
<InputFile
*> ret
;
392 for (unsigned i
= 0; i
!= maxTasks
; ++i
)
394 ret
.push_back(createObjFile(MemoryBufferRef(buf
[i
], "lto.tmp")));
396 for (std::unique_ptr
<MemoryBuffer
> &file
: files
)
398 ret
.push_back(createObjFile(*file
));