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
;
51 c
.PreCodeGenPassesHook
= [](legacy::PassManager
&pm
) {
52 pm
.add(createObjCARCContractPass());
55 c
.AlwaysEmitRegularLTOObj
= !config
->ltoObjPath
.empty();
57 c
.TimeTraceEnabled
= config
->timeTraceEnabled
;
58 c
.TimeTraceGranularity
= config
->timeTraceGranularity
;
59 c
.DebugPassManager
= config
->ltoDebugPassManager
;
60 c
.CSIRProfile
= std::string(config
->csProfilePath
);
61 c
.RunCSIRInstr
= config
->csProfileGenerate
;
62 c
.PGOWarnMismatch
= config
->pgoWarnMismatch
;
63 c
.OptLevel
= config
->ltoo
;
64 c
.CGOptLevel
= config
->ltoCgo
;
65 if (config
->saveTemps
)
66 checkError(c
.addSaveTemps(config
->outputFile
.str() + ".",
67 /*UseInputModulePath=*/true));
71 // If `originalPath` exists, hardlinks `path` to `originalPath`. If that fails,
72 // or `originalPath` is not set, saves `buffer` to `path`.
73 static void saveOrHardlinkBuffer(StringRef buffer
, const Twine
&path
,
74 std::optional
<StringRef
> originalPath
) {
76 auto err
= fs::create_hard_link(*originalPath
, path
);
80 saveBuffer(buffer
, path
);
83 BitcodeCompiler::BitcodeCompiler() {
84 // Initialize indexFile.
85 if (!config
->thinLTOIndexOnlyArg
.empty())
86 indexFile
= openFile(config
->thinLTOIndexOnlyArg
);
89 lto::ThinBackend backend
;
90 auto onIndexWrite
= [&](StringRef S
) { thinIndices
.erase(S
); };
91 if (config
->thinLTOIndexOnly
) {
92 backend
= lto::createWriteIndexesThinBackend(
93 std::string(config
->thinLTOPrefixReplaceOld
),
94 std::string(config
->thinLTOPrefixReplaceNew
),
95 std::string(config
->thinLTOPrefixReplaceNativeObject
),
96 config
->thinLTOEmitImportsFiles
, indexFile
.get(), onIndexWrite
);
98 backend
= lto::createInProcessThinBackend(
99 llvm::heavyweight_hardware_concurrency(config
->thinLTOJobs
),
100 onIndexWrite
, config
->thinLTOEmitIndexFiles
,
101 config
->thinLTOEmitImportsFiles
);
104 ltoObj
= std::make_unique
<lto::LTO
>(createConfig(), backend
);
107 void BitcodeCompiler::add(BitcodeFile
&f
) {
108 lto::InputFile
&obj
= *f
.obj
;
110 if (config
->thinLTOEmitIndexFiles
)
111 thinIndices
.insert(obj
.getName());
113 ArrayRef
<lto::InputFile::Symbol
> objSyms
= obj
.symbols();
114 std::vector
<lto::SymbolResolution
> resols
;
115 resols
.reserve(objSyms
.size());
117 // Provide a resolution to the LTO API for each symbol.
119 config
->outputType
!= MH_EXECUTE
|| config
->exportDynamic
;
120 auto symIt
= f
.symbols
.begin();
121 for (const lto::InputFile::Symbol
&objSym
: objSyms
) {
122 resols
.emplace_back();
123 lto::SymbolResolution
&r
= resols
.back();
124 Symbol
*sym
= *symIt
++;
126 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
127 // reports two symbols for module ASM defined. Without this check, lld
128 // flags an undefined in IR with a definition in ASM as prevailing.
129 // Once IRObjectFile is fixed to report only one symbol this hack can
131 r
.Prevailing
= !objSym
.isUndefined() && sym
->getFile() == &f
;
133 if (const auto *defined
= dyn_cast
<Defined
>(sym
)) {
135 defined
->isExternal() && !defined
->privateExtern
&& exportDynamic
;
136 r
.FinalDefinitionInLinkageUnit
=
137 !defined
->isExternalWeakDef() && !defined
->interposable
;
138 } else if (const auto *common
= dyn_cast
<CommonSymbol
>(sym
)) {
139 r
.ExportDynamic
= !common
->privateExtern
&& exportDynamic
;
140 r
.FinalDefinitionInLinkageUnit
= true;
143 r
.VisibleToRegularObj
=
144 sym
->isUsedInRegularObj
|| (r
.Prevailing
&& r
.ExportDynamic
);
146 // Un-define the symbol so that we don't get duplicate symbol errors when we
147 // load the ObjFile emitted by LTO compilation.
149 replaceSymbol
<Undefined
>(sym
, sym
->getName(), sym
->getFile(),
150 RefState::Strong
, /*wasBitcodeSymbol=*/true);
152 // TODO: set the other resolution configs properly
154 checkError(ltoObj
->add(std::move(f
.obj
), resols
));
158 // If LazyObjFile has not been added to link, emit empty index files.
159 // This is needed because this is what GNU gold plugin does and we have a
160 // distributed build system that depends on that behavior.
161 static void thinLTOCreateEmptyIndexFiles() {
162 DenseSet
<StringRef
> linkedBitCodeFiles
;
163 for (InputFile
*file
: inputFiles
)
164 if (auto *f
= dyn_cast
<BitcodeFile
>(file
))
166 linkedBitCodeFiles
.insert(f
->getName());
168 for (InputFile
*file
: inputFiles
) {
169 if (auto *f
= dyn_cast
<BitcodeFile
>(file
)) {
172 if (linkedBitCodeFiles
.contains(f
->getName()))
175 replaceThinLTOSuffix(getThinLTOOutputFile(f
->obj
->getName()));
176 std::unique_ptr
<raw_fd_ostream
> os
= openFile(path
+ ".thinlto.bc");
180 ModuleSummaryIndex
m(/*HaveGVs=*/false);
181 m
.setSkipModuleByDistributedBackend();
182 writeIndexToFile(m
, *os
);
183 if (config
->thinLTOEmitImportsFiles
)
184 openFile(path
+ ".imports");
189 // Merge all the bitcode files we have seen, codegen the result
190 // and return the resulting ObjectFile(s).
191 std::vector
<ObjFile
*> BitcodeCompiler::compile() {
192 unsigned maxTasks
= ltoObj
->getMaxTasks();
193 buf
.resize(maxTasks
);
194 files
.resize(maxTasks
);
196 // The -cache_path_lto option specifies the path to a directory in which
197 // to cache native object files for ThinLTO incremental builds. If a path was
198 // specified, configure LTO to use it as the cache directory.
200 if (!config
->thinLTOCacheDir
.empty())
201 cache
= check(localCache("ThinLTO", "Thin", config
->thinLTOCacheDir
,
202 [&](size_t task
, const Twine
&moduleName
,
203 std::unique_ptr
<MemoryBuffer
> mb
) {
204 files
[task
] = std::move(mb
);
208 checkError(ltoObj
->run(
209 [&](size_t task
, const Twine
&moduleName
) {
210 return std::make_unique
<CachedFileStream
>(
211 std::make_unique
<raw_svector_ostream
>(buf
[task
]));
215 // Emit empty index files for non-indexed files
216 for (StringRef s
: thinIndices
) {
217 std::string path
= getThinLTOOutputFile(s
);
218 openFile(path
+ ".thinlto.bc");
219 if (config
->thinLTOEmitImportsFiles
)
220 openFile(path
+ ".imports");
223 if (config
->thinLTOEmitIndexFiles
)
224 thinLTOCreateEmptyIndexFiles();
226 // In ThinLTO mode, Clang passes a temporary directory in -object_path_lto,
227 // while the argument is a single file in FullLTO mode.
228 bool objPathIsDir
= true;
229 if (!config
->ltoObjPath
.empty()) {
230 if (std::error_code ec
= fs::create_directories(config
->ltoObjPath
))
231 fatal("cannot create LTO object path " + config
->ltoObjPath
+ ": " +
234 if (!fs::is_directory(config
->ltoObjPath
)) {
235 objPathIsDir
= false;
237 count_if(buf
, [](const SmallString
<0> &b
) { return !b
.empty(); });
239 fatal("-object_path_lto must specify a directory when using ThinLTO");
243 auto outputFilePath
= [objPathIsDir
](int i
) {
244 SmallString
<261> filePath("/tmp/lto.tmp");
245 if (!config
->ltoObjPath
.empty()) {
246 filePath
= config
->ltoObjPath
;
248 path::append(filePath
, Twine(i
) + "." +
249 getArchitectureName(config
->arch()) +
255 // ThinLTO with index only option is required to generate only the index
256 // files. After that, we exit from linker and ThinLTO backend runs in a
257 // distributed environment.
258 if (config
->thinLTOIndexOnly
) {
259 if (!config
->ltoObjPath
.empty())
260 saveBuffer(buf
[0], outputFilePath(0));
266 if (!config
->thinLTOCacheDir
.empty())
267 pruneCache(config
->thinLTOCacheDir
, config
->thinLTOCachePolicy
, files
);
269 std::vector
<ObjFile
*> ret
;
270 for (unsigned i
= 0; i
< maxTasks
; ++i
) {
271 // Get the native object contents either from the cache or from memory. Do
272 // not use the cached MemoryBuffer directly to ensure dsymutil does not
273 // race with the cache pruner.
275 std::optional
<StringRef
> cachePath
;
277 objBuf
= files
[i
]->getBuffer();
278 cachePath
= files
[i
]->getBufferIdentifier();
285 // FIXME: should `saveTemps` and `ltoObjPath` use the same file name?
286 if (config
->saveTemps
)
288 config
->outputFile
+ ((i
== 0) ? "" : Twine(i
)) + ".lto.o");
290 auto filePath
= outputFilePath(i
);
291 uint32_t modTime
= 0;
292 if (!config
->ltoObjPath
.empty()) {
293 saveOrHardlinkBuffer(objBuf
, filePath
, cachePath
);
294 modTime
= getModTime(filePath
);
296 ret
.push_back(make
<ObjFile
>(
297 MemoryBufferRef(objBuf
, saver().save(filePath
.str())), modTime
,
298 /*archiveName=*/"", /*lazy=*/false,
299 /*forceHidden=*/false, /*compatArch=*/true, /*builtFromBitcode=*/true));