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 //===----------------------------------------------------------------------===//
12 #include "InputFiles.h"
16 #include "lld/Common/Args.h"
17 #include "lld/Common/CommonLinkerContext.h"
18 #include "lld/Common/Filesystem.h"
19 #include "lld/Common/Strings.h"
20 #include "lld/Common/TargetOptionsCommandFlags.h"
21 #include "llvm/Bitcode/BitcodeWriter.h"
22 #include "llvm/LTO/Config.h"
23 #include "llvm/LTO/LTO.h"
24 #include "llvm/Support/Caching.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Transforms/ObjCARC.h"
31 using namespace lld::macho
;
33 using namespace llvm::MachO
;
34 using namespace llvm::sys
;
36 static std::string
getThinLTOOutputFile(StringRef modulePath
) {
37 return lto::getThinLTOOutputFile(modulePath
, config
->thinLTOPrefixReplaceOld
,
38 config
->thinLTOPrefixReplaceNew
);
41 static lto::Config
createConfig() {
43 c
.Options
= initTargetOptionsFromCodeGenFlags();
44 c
.Options
.EmitAddrsig
= config
->icfLevel
== ICFLevel::safe
;
45 for (StringRef C
: config
->mllvmOpts
)
46 c
.MllvmArgs
.emplace_back(C
.str());
47 c
.CodeModel
= getCodeModelFromCMModel();
49 c
.MAttrs
= getMAttrs();
50 c
.DiagHandler
= diagnosticHandler
;
52 c
.AlwaysEmitRegularLTOObj
= !config
->ltoObjPath
.empty();
54 c
.TimeTraceEnabled
= config
->timeTraceEnabled
;
55 c
.TimeTraceGranularity
= config
->timeTraceGranularity
;
56 c
.DebugPassManager
= config
->ltoDebugPassManager
;
57 c
.CSIRProfile
= std::string(config
->csProfilePath
);
58 c
.RunCSIRInstr
= config
->csProfileGenerate
;
59 c
.PGOWarnMismatch
= config
->pgoWarnMismatch
;
60 c
.OptLevel
= config
->ltoo
;
61 c
.CGOptLevel
= config
->ltoCgo
;
62 if (config
->saveTemps
)
63 checkError(c
.addSaveTemps(config
->outputFile
.str() + ".",
64 /*UseInputModulePath=*/true));
68 // If `originalPath` exists, hardlinks `path` to `originalPath`. If that fails,
69 // or `originalPath` is not set, saves `buffer` to `path`.
70 static void saveOrHardlinkBuffer(StringRef buffer
, const Twine
&path
,
71 std::optional
<StringRef
> originalPath
) {
73 auto err
= fs::create_hard_link(*originalPath
, path
);
77 saveBuffer(buffer
, path
);
80 BitcodeCompiler::BitcodeCompiler() {
81 // Initialize indexFile.
82 if (!config
->thinLTOIndexOnlyArg
.empty())
83 indexFile
= openFile(config
->thinLTOIndexOnlyArg
);
86 lto::ThinBackend backend
;
87 auto onIndexWrite
= [&](StringRef S
) { thinIndices
.erase(S
); };
88 if (config
->thinLTOIndexOnly
) {
89 backend
= lto::createWriteIndexesThinBackend(
90 llvm::hardware_concurrency(config
->thinLTOJobs
),
91 std::string(config
->thinLTOPrefixReplaceOld
),
92 std::string(config
->thinLTOPrefixReplaceNew
),
93 std::string(config
->thinLTOPrefixReplaceNativeObject
),
94 config
->thinLTOEmitImportsFiles
, indexFile
.get(), onIndexWrite
);
96 backend
= lto::createInProcessThinBackend(
97 llvm::heavyweight_hardware_concurrency(config
->thinLTOJobs
),
98 onIndexWrite
, config
->thinLTOEmitIndexFiles
,
99 config
->thinLTOEmitImportsFiles
);
102 ltoObj
= std::make_unique
<lto::LTO
>(createConfig(), backend
);
105 void BitcodeCompiler::add(BitcodeFile
&f
) {
106 lto::InputFile
&obj
= *f
.obj
;
108 if (config
->thinLTOEmitIndexFiles
)
109 thinIndices
.insert(obj
.getName());
111 ArrayRef
<lto::InputFile::Symbol
> objSyms
= obj
.symbols();
112 std::vector
<lto::SymbolResolution
> resols
;
113 resols
.reserve(objSyms
.size());
115 // Provide a resolution to the LTO API for each symbol.
117 config
->outputType
!= MH_EXECUTE
|| config
->exportDynamic
;
118 auto symIt
= f
.symbols
.begin();
119 for (const lto::InputFile::Symbol
&objSym
: objSyms
) {
120 resols
.emplace_back();
121 lto::SymbolResolution
&r
= resols
.back();
122 Symbol
*sym
= *symIt
++;
124 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
125 // reports two symbols for module ASM defined. Without this check, lld
126 // flags an undefined in IR with a definition in ASM as prevailing.
127 // Once IRObjectFile is fixed to report only one symbol this hack can
129 r
.Prevailing
= !objSym
.isUndefined() && sym
->getFile() == &f
;
131 if (const auto *defined
= dyn_cast
<Defined
>(sym
)) {
133 defined
->isExternal() && !defined
->privateExtern
&& exportDynamic
;
134 r
.FinalDefinitionInLinkageUnit
=
135 !defined
->isExternalWeakDef() && !defined
->interposable
;
136 } else if (const auto *common
= dyn_cast
<CommonSymbol
>(sym
)) {
137 r
.ExportDynamic
= !common
->privateExtern
&& exportDynamic
;
138 r
.FinalDefinitionInLinkageUnit
= true;
141 r
.VisibleToRegularObj
=
142 sym
->isUsedInRegularObj
|| (r
.Prevailing
&& r
.ExportDynamic
);
144 // Un-define the symbol so that we don't get duplicate symbol errors when we
145 // load the ObjFile emitted by LTO compilation.
147 replaceSymbol
<Undefined
>(sym
, sym
->getName(), sym
->getFile(),
148 RefState::Strong
, /*wasBitcodeSymbol=*/true);
150 // TODO: set the other resolution configs properly
152 checkError(ltoObj
->add(std::move(f
.obj
), resols
));
156 // If LazyObjFile has not been added to link, emit empty index files.
157 // This is needed because this is what GNU gold plugin does and we have a
158 // distributed build system that depends on that behavior.
159 static void thinLTOCreateEmptyIndexFiles() {
160 DenseSet
<StringRef
> linkedBitCodeFiles
;
161 for (InputFile
*file
: inputFiles
)
162 if (auto *f
= dyn_cast
<BitcodeFile
>(file
))
164 linkedBitCodeFiles
.insert(f
->getName());
166 for (InputFile
*file
: inputFiles
) {
167 if (auto *f
= dyn_cast
<BitcodeFile
>(file
)) {
170 if (linkedBitCodeFiles
.contains(f
->getName()))
173 replaceThinLTOSuffix(getThinLTOOutputFile(f
->obj
->getName()));
174 std::unique_ptr
<raw_fd_ostream
> os
= openFile(path
+ ".thinlto.bc");
178 ModuleSummaryIndex
m(/*HaveGVs=*/false);
179 m
.setSkipModuleByDistributedBackend();
180 writeIndexToFile(m
, *os
);
181 if (config
->thinLTOEmitImportsFiles
)
182 openFile(path
+ ".imports");
187 // Merge all the bitcode files we have seen, codegen the result
188 // and return the resulting ObjectFile(s).
189 std::vector
<ObjFile
*> BitcodeCompiler::compile() {
190 unsigned maxTasks
= ltoObj
->getMaxTasks();
191 buf
.resize(maxTasks
);
192 files
.resize(maxTasks
);
194 // The -cache_path_lto option specifies the path to a directory in which
195 // to cache native object files for ThinLTO incremental builds. If a path was
196 // specified, configure LTO to use it as the cache directory.
198 if (!config
->thinLTOCacheDir
.empty())
199 cache
= check(localCache("ThinLTO", "Thin", config
->thinLTOCacheDir
,
200 [&](size_t task
, const Twine
&moduleName
,
201 std::unique_ptr
<MemoryBuffer
> mb
) {
202 files
[task
] = std::move(mb
);
206 checkError(ltoObj
->run(
207 [&](size_t task
, const Twine
&moduleName
) {
208 return std::make_unique
<CachedFileStream
>(
209 std::make_unique
<raw_svector_ostream
>(buf
[task
]));
213 // Emit empty index files for non-indexed files
214 for (StringRef s
: thinIndices
) {
215 std::string path
= getThinLTOOutputFile(s
);
216 openFile(path
+ ".thinlto.bc");
217 if (config
->thinLTOEmitImportsFiles
)
218 openFile(path
+ ".imports");
221 if (config
->thinLTOEmitIndexFiles
)
222 thinLTOCreateEmptyIndexFiles();
224 // In ThinLTO mode, Clang passes a temporary directory in -object_path_lto,
225 // while the argument is a single file in FullLTO mode.
226 bool objPathIsDir
= true;
227 if (!config
->ltoObjPath
.empty()) {
228 if (std::error_code ec
= fs::create_directories(config
->ltoObjPath
))
229 fatal("cannot create LTO object path " + config
->ltoObjPath
+ ": " +
232 if (!fs::is_directory(config
->ltoObjPath
)) {
233 objPathIsDir
= false;
235 count_if(buf
, [](const SmallString
<0> &b
) { return !b
.empty(); });
237 fatal("-object_path_lto must specify a directory when using ThinLTO");
241 auto outputFilePath
= [objPathIsDir
](int i
) {
242 SmallString
<261> filePath("/tmp/lto.tmp");
243 if (!config
->ltoObjPath
.empty()) {
244 filePath
= config
->ltoObjPath
;
246 path::append(filePath
, Twine(i
) + "." +
247 getArchitectureName(config
->arch()) +
253 // ThinLTO with index only option is required to generate only the index
254 // files. After that, we exit from linker and ThinLTO backend runs in a
255 // distributed environment.
256 if (config
->thinLTOIndexOnly
) {
257 if (!config
->ltoObjPath
.empty())
258 saveBuffer(buf
[0], outputFilePath(0));
264 if (!config
->thinLTOCacheDir
.empty())
265 pruneCache(config
->thinLTOCacheDir
, config
->thinLTOCachePolicy
, files
);
267 std::vector
<ObjFile
*> ret
;
268 for (unsigned i
= 0; i
< maxTasks
; ++i
) {
269 // Get the native object contents either from the cache or from memory. Do
270 // not use the cached MemoryBuffer directly to ensure dsymutil does not
271 // race with the cache pruner.
273 std::optional
<StringRef
> cachePath
;
275 objBuf
= files
[i
]->getBuffer();
276 cachePath
= files
[i
]->getBufferIdentifier();
283 // FIXME: should `saveTemps` and `ltoObjPath` use the same file name?
284 if (config
->saveTemps
)
286 config
->outputFile
+ ((i
== 0) ? "" : Twine(i
)) + ".lto.o");
288 auto filePath
= outputFilePath(i
);
289 uint32_t modTime
= 0;
290 if (!config
->ltoObjPath
.empty()) {
291 saveOrHardlinkBuffer(objBuf
, filePath
, cachePath
);
292 modTime
= getModTime(filePath
);
294 ret
.push_back(make
<ObjFile
>(
295 MemoryBufferRef(objBuf
, saver().save(filePath
.str())), modTime
,
296 /*archiveName=*/"", /*lazy=*/false,
297 /*forceHidden=*/false, /*compatArch=*/true, /*builtFromBitcode=*/true));