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/Filesystem.h"
17 #include "lld/Common/Strings.h"
18 #include "lld/Common/TargetOptionsCommandFlags.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Bitcode/BitcodeWriter.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
25 #include "llvm/LTO/Config.h"
26 #include "llvm/LTO/LTO.h"
27 #include "llvm/Object/SymbolicFile.h"
28 #include "llvm/Support/Caching.h"
29 #include "llvm/Support/CodeGen.h"
30 #include "llvm/Support/Error.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/raw_ostream.h"
38 #include <system_error>
42 using namespace llvm::object
;
44 using namespace lld::coff
;
46 std::string
BitcodeCompiler::getThinLTOOutputFile(StringRef path
) {
47 return lto::getThinLTOOutputFile(path
, ctx
.config
.thinLTOPrefixReplaceOld
,
48 ctx
.config
.thinLTOPrefixReplaceNew
);
51 lto::Config
BitcodeCompiler::createConfig() {
53 c
.Options
= initTargetOptionsFromCodeGenFlags();
54 c
.Options
.EmitAddrsig
= true;
55 for (StringRef C
: ctx
.config
.mllvmOpts
)
56 c
.MllvmArgs
.emplace_back(C
.str());
58 // Always emit a section per function/datum with LTO. LLVM LTO should get most
59 // of the benefit of linker GC, but there are still opportunities for ICF.
60 c
.Options
.FunctionSections
= true;
61 c
.Options
.DataSections
= true;
63 // Use static reloc model on 32-bit x86 because it usually results in more
64 // compact code, and because there are also known code generation bugs when
65 // using the PIC model (see PR34306).
66 if (ctx
.config
.machine
== COFF::IMAGE_FILE_MACHINE_I386
)
67 c
.RelocModel
= Reloc::Static
;
69 c
.RelocModel
= Reloc::PIC_
;
71 c
.DisableVerify
= false;
73 c
.DisableVerify
= true;
75 c
.DiagHandler
= diagnosticHandler
;
76 c
.DwoDir
= ctx
.config
.dwoDir
.str();
77 c
.OptLevel
= ctx
.config
.ltoo
;
79 c
.MAttrs
= getMAttrs();
80 std::optional
<CodeGenOptLevel
> optLevelOrNone
= CodeGenOpt::getLevel(
81 ctx
.config
.ltoCgo
.value_or(args::getCGOptLevel(ctx
.config
.ltoo
)));
82 assert(optLevelOrNone
&& "Invalid optimization level!");
83 c
.CGOptLevel
= *optLevelOrNone
;
84 c
.AlwaysEmitRegularLTOObj
= !ctx
.config
.ltoObjPath
.empty();
85 c
.DebugPassManager
= ctx
.config
.ltoDebugPassManager
;
86 c
.CSIRProfile
= std::string(ctx
.config
.ltoCSProfileFile
);
87 c
.RunCSIRInstr
= ctx
.config
.ltoCSProfileGenerate
;
88 c
.PGOWarnMismatch
= ctx
.config
.ltoPGOWarnMismatch
;
89 c
.TimeTraceEnabled
= ctx
.config
.timeTraceEnabled
;
90 c
.TimeTraceGranularity
= ctx
.config
.timeTraceGranularity
;
92 if (ctx
.config
.emit
== EmitKind::LLVM
) {
93 c
.PostInternalizeModuleHook
= [this](size_t task
, const Module
&m
) {
94 if (std::unique_ptr
<raw_fd_ostream
> os
=
95 openLTOOutputFile(ctx
.config
.outputFile
))
96 WriteBitcodeToFile(m
, *os
, false);
99 } else if (ctx
.config
.emit
== EmitKind::ASM
) {
100 c
.CGFileType
= CodeGenFileType::AssemblyFile
;
101 c
.Options
.MCOptions
.AsmVerbose
= true;
104 if (ctx
.config
.saveTemps
)
105 checkError(c
.addSaveTemps(std::string(ctx
.config
.outputFile
) + ".",
106 /*UseInputModulePath*/ true));
110 BitcodeCompiler::BitcodeCompiler(COFFLinkerContext
&c
) : ctx(c
) {
111 // Initialize indexFile.
112 if (!ctx
.config
.thinLTOIndexOnlyArg
.empty())
113 indexFile
= openFile(ctx
.config
.thinLTOIndexOnlyArg
);
115 // Initialize ltoObj.
116 lto::ThinBackend backend
;
117 if (ctx
.config
.thinLTOIndexOnly
) {
118 auto OnIndexWrite
= [&](StringRef S
) { thinIndices
.erase(S
); };
119 backend
= lto::createWriteIndexesThinBackend(
120 std::string(ctx
.config
.thinLTOPrefixReplaceOld
),
121 std::string(ctx
.config
.thinLTOPrefixReplaceNew
),
122 std::string(ctx
.config
.thinLTOPrefixReplaceNativeObject
),
123 ctx
.config
.thinLTOEmitImportsFiles
, indexFile
.get(), OnIndexWrite
);
125 backend
= lto::createInProcessThinBackend(
126 llvm::heavyweight_hardware_concurrency(ctx
.config
.thinLTOJobs
));
129 ltoObj
= std::make_unique
<lto::LTO
>(createConfig(), backend
,
130 ctx
.config
.ltoPartitions
);
133 BitcodeCompiler::~BitcodeCompiler() = default;
135 static void undefine(Symbol
*s
) { replaceSymbol
<Undefined
>(s
, s
->getName()); }
137 void BitcodeCompiler::add(BitcodeFile
&f
) {
138 lto::InputFile
&obj
= *f
.obj
;
140 std::vector
<Symbol
*> symBodies
= f
.getSymbols();
141 std::vector
<lto::SymbolResolution
> resols(symBodies
.size());
143 if (ctx
.config
.thinLTOIndexOnly
)
144 thinIndices
.insert(obj
.getName());
146 // Provide a resolution to the LTO API for each symbol.
147 for (const lto::InputFile::Symbol
&objSym
: obj
.symbols()) {
148 Symbol
*sym
= symBodies
[symNum
];
149 lto::SymbolResolution
&r
= resols
[symNum
];
152 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
153 // reports two symbols for module ASM defined. Without this check, lld
154 // flags an undefined in IR with a definition in ASM as prevailing.
155 // Once IRObjectFile is fixed to report only one symbol this hack can
157 r
.Prevailing
= !objSym
.isUndefined() && sym
->getFile() == &f
;
158 r
.VisibleToRegularObj
= sym
->isUsedInRegularObj
;
162 // We tell LTO to not apply interprocedural optimization for wrapped
163 // (with -wrap) symbols because otherwise LTO would inline them while
164 // their values are still not final.
165 r
.LinkerRedefined
= !sym
->canInline
;
167 checkError(ltoObj
->add(std::move(f
.obj
), resols
));
170 // Merge all the bitcode files we have seen, codegen the result
171 // and return the resulting objects.
172 std::vector
<InputFile
*> BitcodeCompiler::compile() {
173 unsigned maxTasks
= ltoObj
->getMaxTasks();
174 buf
.resize(maxTasks
);
175 files
.resize(maxTasks
);
176 file_names
.resize(maxTasks
);
178 // The /lldltocache option specifies the path to a directory in which to cache
179 // native object files for ThinLTO incremental builds. If a path was
180 // specified, configure LTO to use it as the cache directory.
182 if (!ctx
.config
.ltoCache
.empty())
183 cache
= check(localCache("ThinLTO", "Thin", ctx
.config
.ltoCache
,
184 [&](size_t task
, const Twine
&moduleName
,
185 std::unique_ptr
<MemoryBuffer
> mb
) {
186 files
[task
] = std::move(mb
);
187 file_names
[task
] = moduleName
.str();
190 checkError(ltoObj
->run(
191 [&](size_t task
, const Twine
&moduleName
) {
192 buf
[task
].first
= moduleName
.str();
193 return std::make_unique
<CachedFileStream
>(
194 std::make_unique
<raw_svector_ostream
>(buf
[task
].second
));
198 // Emit empty index files for non-indexed files
199 for (StringRef s
: thinIndices
) {
200 std::string path
= getThinLTOOutputFile(s
);
201 openFile(path
+ ".thinlto.bc");
202 if (ctx
.config
.thinLTOEmitImportsFiles
)
203 openFile(path
+ ".imports");
206 // ThinLTO with index only option is required to generate only the index
207 // files. After that, we exit from linker and ThinLTO backend runs in a
208 // distributed environment.
209 if (ctx
.config
.thinLTOIndexOnly
) {
210 if (!ctx
.config
.ltoObjPath
.empty())
211 saveBuffer(buf
[0].second
, ctx
.config
.ltoObjPath
);
217 if (!ctx
.config
.ltoCache
.empty())
218 pruneCache(ctx
.config
.ltoCache
, ctx
.config
.ltoCachePolicy
, files
);
220 std::vector
<InputFile
*> ret
;
221 bool emitASM
= ctx
.config
.emit
== EmitKind::ASM
;
222 const char *Ext
= emitASM
? ".s" : ".obj";
223 for (unsigned i
= 0; i
!= maxTasks
; ++i
) {
224 StringRef bitcodeFilePath
;
225 // Get the native object contents either from the cache or from memory. Do
226 // not use the cached MemoryBuffer directly, or the PDB will not be
230 objBuf
= files
[i
]->getBuffer();
231 bitcodeFilePath
= file_names
[i
];
233 objBuf
= buf
[i
].second
;
234 bitcodeFilePath
= buf
[i
].first
;
239 // If the input bitcode file is path/to/a.obj, then the corresponding lto
240 // object file name will look something like: path/to/main.exe.lto.a.obj.
241 StringRef ltoObjName
;
242 if (bitcodeFilePath
== "ld-temp.o") {
244 saver().save(Twine(ctx
.config
.outputFile
) + ".lto" +
245 (i
== 0 ? Twine("") : Twine('.') + Twine(i
)) + Ext
);
247 StringRef directory
= sys::path::parent_path(bitcodeFilePath
);
248 StringRef baseName
= sys::path::stem(bitcodeFilePath
);
249 StringRef outputFileBaseName
= sys::path::filename(ctx
.config
.outputFile
);
250 SmallString
<64> path
;
251 sys::path::append(path
, directory
,
252 outputFileBaseName
+ ".lto." + baseName
+ Ext
);
253 sys::path::remove_dots(path
, true);
254 ltoObjName
= saver().save(path
.str());
256 if (ctx
.config
.saveTemps
|| emitASM
)
257 saveBuffer(buf
[i
].second
, ltoObjName
);
259 ret
.push_back(make
<ObjFile
>(ctx
, MemoryBufferRef(objBuf
, ltoObjName
)));