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 //===----------------------------------------------------------------------===//
10 #include "COFFLinkerContext.h"
12 #include "InputFiles.h"
14 #include "lld/Common/Args.h"
15 #include "lld/Common/CommonLinkerContext.h"
16 #include "lld/Common/Strings.h"
17 #include "lld/Common/TargetOptionsCommandFlags.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Bitcode/BitcodeWriter.h"
23 #include "llvm/IR/DiagnosticPrinter.h"
24 #include "llvm/LTO/Config.h"
25 #include "llvm/LTO/LTO.h"
26 #include "llvm/Object/SymbolicFile.h"
27 #include "llvm/Support/Caching.h"
28 #include "llvm/Support/CodeGen.h"
29 #include "llvm/Support/Error.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/raw_ostream.h"
37 #include <system_error>
41 using namespace llvm::object
;
43 using namespace lld::coff
;
45 // Creates an empty file to and returns a raw_fd_ostream to write to it.
46 static std::unique_ptr
<raw_fd_ostream
> openFile(StringRef file
) {
49 std::make_unique
<raw_fd_ostream
>(file
, ec
, sys::fs::OpenFlags::OF_None
);
51 error("cannot open " + file
+ ": " + ec
.message());
57 std::string
BitcodeCompiler::getThinLTOOutputFile(StringRef path
) {
58 return lto::getThinLTOOutputFile(path
, ctx
.config
.thinLTOPrefixReplaceOld
,
59 ctx
.config
.thinLTOPrefixReplaceNew
);
62 lto::Config
BitcodeCompiler::createConfig() {
64 c
.Options
= initTargetOptionsFromCodeGenFlags();
65 c
.Options
.EmitAddrsig
= true;
66 for (StringRef C
: ctx
.config
.mllvmOpts
)
67 c
.MllvmArgs
.emplace_back(C
.str());
69 // Always emit a section per function/datum with LTO. LLVM LTO should get most
70 // of the benefit of linker GC, but there are still opportunities for ICF.
71 c
.Options
.FunctionSections
= true;
72 c
.Options
.DataSections
= true;
74 // Use static reloc model on 32-bit x86 because it usually results in more
75 // compact code, and because there are also known code generation bugs when
76 // using the PIC model (see PR34306).
77 if (ctx
.config
.machine
== COFF::IMAGE_FILE_MACHINE_I386
)
78 c
.RelocModel
= Reloc::Static
;
80 c
.RelocModel
= Reloc::PIC_
;
82 c
.DisableVerify
= false;
84 c
.DisableVerify
= true;
86 c
.DiagHandler
= diagnosticHandler
;
87 c
.DwoDir
= ctx
.config
.dwoDir
.str();
88 c
.OptLevel
= ctx
.config
.ltoo
;
90 c
.MAttrs
= getMAttrs();
91 std::optional
<CodeGenOpt::Level
> optLevelOrNone
= CodeGenOpt::getLevel(
92 ctx
.config
.ltoCgo
.value_or(args::getCGOptLevel(ctx
.config
.ltoo
)));
93 assert(optLevelOrNone
&& "Invalid optimization level!");
94 c
.CGOptLevel
= *optLevelOrNone
;
95 c
.AlwaysEmitRegularLTOObj
= !ctx
.config
.ltoObjPath
.empty();
96 c
.DebugPassManager
= ctx
.config
.ltoDebugPassManager
;
97 c
.CSIRProfile
= std::string(ctx
.config
.ltoCSProfileFile
);
98 c
.RunCSIRInstr
= ctx
.config
.ltoCSProfileGenerate
;
99 c
.PGOWarnMismatch
= ctx
.config
.ltoPGOWarnMismatch
;
101 if (ctx
.config
.saveTemps
)
102 checkError(c
.addSaveTemps(std::string(ctx
.config
.outputFile
) + ".",
103 /*UseInputModulePath*/ true));
107 BitcodeCompiler::BitcodeCompiler(COFFLinkerContext
&c
) : ctx(c
) {
108 // Initialize indexFile.
109 if (!ctx
.config
.thinLTOIndexOnlyArg
.empty())
110 indexFile
= openFile(ctx
.config
.thinLTOIndexOnlyArg
);
112 // Initialize ltoObj.
113 lto::ThinBackend backend
;
114 if (ctx
.config
.thinLTOIndexOnly
) {
115 auto OnIndexWrite
= [&](StringRef S
) { thinIndices
.erase(S
); };
116 backend
= lto::createWriteIndexesThinBackend(
117 std::string(ctx
.config
.thinLTOPrefixReplaceOld
),
118 std::string(ctx
.config
.thinLTOPrefixReplaceNew
),
119 std::string(ctx
.config
.thinLTOPrefixReplaceNativeObject
),
120 ctx
.config
.thinLTOEmitImportsFiles
, indexFile
.get(), OnIndexWrite
);
122 backend
= lto::createInProcessThinBackend(
123 llvm::heavyweight_hardware_concurrency(ctx
.config
.thinLTOJobs
));
126 ltoObj
= std::make_unique
<lto::LTO
>(createConfig(), backend
,
127 ctx
.config
.ltoPartitions
);
130 BitcodeCompiler::~BitcodeCompiler() = default;
132 static void undefine(Symbol
*s
) { replaceSymbol
<Undefined
>(s
, s
->getName()); }
134 void BitcodeCompiler::add(BitcodeFile
&f
) {
135 lto::InputFile
&obj
= *f
.obj
;
137 std::vector
<Symbol
*> symBodies
= f
.getSymbols();
138 std::vector
<lto::SymbolResolution
> resols(symBodies
.size());
140 if (ctx
.config
.thinLTOIndexOnly
)
141 thinIndices
.insert(obj
.getName());
143 // Provide a resolution to the LTO API for each symbol.
144 for (const lto::InputFile::Symbol
&objSym
: obj
.symbols()) {
145 Symbol
*sym
= symBodies
[symNum
];
146 lto::SymbolResolution
&r
= resols
[symNum
];
149 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
150 // reports two symbols for module ASM defined. Without this check, lld
151 // flags an undefined in IR with a definition in ASM as prevailing.
152 // Once IRObjectFile is fixed to report only one symbol this hack can
154 r
.Prevailing
= !objSym
.isUndefined() && sym
->getFile() == &f
;
155 r
.VisibleToRegularObj
= sym
->isUsedInRegularObj
;
159 // We tell LTO to not apply interprocedural optimization for wrapped
160 // (with -wrap) symbols because otherwise LTO would inline them while
161 // their values are still not final.
162 r
.LinkerRedefined
= !sym
->canInline
;
164 checkError(ltoObj
->add(std::move(f
.obj
), resols
));
167 // Merge all the bitcode files we have seen, codegen the result
168 // and return the resulting objects.
169 std::vector
<InputFile
*> BitcodeCompiler::compile() {
170 unsigned maxTasks
= ltoObj
->getMaxTasks();
171 buf
.resize(maxTasks
);
172 files
.resize(maxTasks
);
173 file_names
.resize(maxTasks
);
175 // The /lldltocache option specifies the path to a directory in which to cache
176 // native object files for ThinLTO incremental builds. If a path was
177 // specified, configure LTO to use it as the cache directory.
179 if (!ctx
.config
.ltoCache
.empty())
180 cache
= check(localCache("ThinLTO", "Thin", ctx
.config
.ltoCache
,
181 [&](size_t task
, const Twine
&moduleName
,
182 std::unique_ptr
<MemoryBuffer
> mb
) {
183 files
[task
] = std::move(mb
);
184 file_names
[task
] = moduleName
.str();
187 checkError(ltoObj
->run(
188 [&](size_t task
, const Twine
&moduleName
) {
189 buf
[task
].first
= moduleName
.str();
190 return std::make_unique
<CachedFileStream
>(
191 std::make_unique
<raw_svector_ostream
>(buf
[task
].second
));
195 // Emit empty index files for non-indexed files
196 for (StringRef s
: thinIndices
) {
197 std::string path
= getThinLTOOutputFile(s
);
198 openFile(path
+ ".thinlto.bc");
199 if (ctx
.config
.thinLTOEmitImportsFiles
)
200 openFile(path
+ ".imports");
203 // ThinLTO with index only option is required to generate only the index
204 // files. After that, we exit from linker and ThinLTO backend runs in a
205 // distributed environment.
206 if (ctx
.config
.thinLTOIndexOnly
) {
207 if (!ctx
.config
.ltoObjPath
.empty())
208 saveBuffer(buf
[0].second
, ctx
.config
.ltoObjPath
);
214 if (!ctx
.config
.ltoCache
.empty())
215 pruneCache(ctx
.config
.ltoCache
, ctx
.config
.ltoCachePolicy
, files
);
217 std::vector
<InputFile
*> ret
;
218 for (unsigned i
= 0; i
!= maxTasks
; ++i
) {
219 StringRef bitcodeFilePath
;
220 // Get the native object contents either from the cache or from memory. Do
221 // not use the cached MemoryBuffer directly, or the PDB will not be
225 objBuf
= files
[i
]->getBuffer();
226 bitcodeFilePath
= file_names
[i
];
228 objBuf
= buf
[i
].second
;
229 bitcodeFilePath
= buf
[i
].first
;
234 // If the input bitcode file is path/to/a.obj, then the corresponding lto
235 // object file name will look something like: path/to/main.exe.lto.a.obj.
236 StringRef ltoObjName
;
237 if (bitcodeFilePath
== "ld-temp.o") {
239 saver().save(Twine(ctx
.config
.outputFile
) + ".lto" +
240 (i
== 0 ? Twine("") : Twine('.') + Twine(i
)) + ".obj");
242 StringRef directory
= sys::path::parent_path(bitcodeFilePath
);
243 StringRef baseName
= sys::path::filename(bitcodeFilePath
);
244 StringRef outputFileBaseName
= sys::path::filename(ctx
.config
.outputFile
);
245 SmallString
<64> path
;
246 sys::path::append(path
, directory
,
247 outputFileBaseName
+ ".lto." + baseName
);
248 sys::path::remove_dots(path
, true);
249 ltoObjName
= saver().save(path
.str());
251 if (ctx
.config
.saveTemps
)
252 saveBuffer(buf
[i
].second
, ltoObjName
);
253 ret
.push_back(make
<ObjFile
>(ctx
, MemoryBufferRef(objBuf
, ltoObjName
)));