1 //===- LTO.cpp ------------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "InputFiles.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/Analysis/CGSCCPassManager.h"
18 #include "llvm/Analysis/LoopPassManager.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/CodeGen/CommandFlags.h"
23 #include "llvm/CodeGen/ParallelCG.h"
24 #include "llvm/IR/AutoUpgrade.h"
25 #include "llvm/IR/LegacyPassManager.h"
26 #include "llvm/IR/PassManager.h"
27 #include "llvm/IR/Verifier.h"
28 #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
29 #include "llvm/Linker/IRMover.h"
30 #include "llvm/Passes/PassBuilder.h"
31 #include "llvm/Support/StringSaver.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Transforms/IPO.h"
35 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
36 #include "llvm/Transforms/Utils/ModuleUtils.h"
39 using namespace llvm::object;
40 using namespace llvm::ELF;
43 using namespace lld::elf;
45 // This is for use when debugging LTO.
46 static void saveBuffer(StringRef Buffer, const Twine &Path) {
48 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
50 error(EC, "cannot create " + Path);
54 // This is for use when debugging LTO.
55 static void saveBCFile(Module &M, const Twine &Path) {
57 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
59 error(EC, "cannot create " + Path);
60 WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
63 static void runNewCustomLtoPasses(Module &M, TargetMachine &TM) {
68 // Parse a custom AA pipeline if asked to.
69 if (!PB.parseAAPipeline(AA, Config->LtoAAPipeline)) {
70 error("Unable to parse AA pipeline description: " + Config->LtoAAPipeline);
74 LoopAnalysisManager LAM;
75 FunctionAnalysisManager FAM;
76 CGSCCAnalysisManager CGAM;
77 ModuleAnalysisManager MAM;
79 // Register the AA manager first so that our version is the one used.
80 FAM.registerPass([&] { return std::move(AA); });
82 // Register all the basic analyses with the managers.
83 PB.registerModuleAnalyses(MAM);
84 PB.registerCGSCCAnalyses(CGAM);
85 PB.registerFunctionAnalyses(FAM);
86 PB.registerLoopAnalyses(LAM);
87 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
89 ModulePassManager MPM;
90 if (!Config->DisableVerify)
91 MPM.addPass(VerifierPass());
93 // Now, add all the passes we've been requested to.
94 if (!PB.parsePassPipeline(MPM, Config->LtoNewPmPasses)) {
95 error("unable to parse pass pipeline description: " +
96 Config->LtoNewPmPasses);
100 if (!Config->DisableVerify)
101 MPM.addPass(VerifierPass());
105 static void runOldLtoPasses(Module &M, TargetMachine &TM) {
106 // Note that the gold plugin has a similar piece of code, so
107 // it is probably better to move this code to a common place.
108 legacy::PassManager LtoPasses;
109 LtoPasses.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
110 PassManagerBuilder PMB;
111 PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
112 PMB.Inliner = createFunctionInliningPass();
113 PMB.VerifyInput = PMB.VerifyOutput = !Config->DisableVerify;
114 PMB.LoopVectorize = true;
115 PMB.SLPVectorize = true;
116 PMB.OptLevel = Config->LtoO;
117 PMB.populateLTOPassManager(LtoPasses);
121 static void runLTOPasses(Module &M, TargetMachine &TM) {
122 if (!Config->LtoNewPmPasses.empty()) {
123 // The user explicitly asked for a set of passes to be run.
124 // This needs the new PM to work as there's no clean way to
125 // pass a set of passes to run in the legacy PM.
126 runNewCustomLtoPasses(M, TM);
130 // Run the 'default' set of LTO passes. This code still uses
131 // the legacy PM as the new one is not the default.
132 runOldLtoPasses(M, TM);
135 if (Config->SaveTemps)
136 saveBCFile(M, Config->OutputFile + ".lto.opt.bc");
139 static bool shouldInternalize(const SmallPtrSet<GlobalValue *, 8> &Used,
140 Symbol *S, GlobalValue *GV) {
141 if (S->IsUsedInRegularObj || Used.count(GV))
143 return !S->includeInDynsym();
146 BitcodeCompiler::BitcodeCompiler()
147 : Combined(new Module("ld-temp.o", Driver->Context)) {}
149 static void undefine(Symbol *S) {
150 replaceBody<Undefined>(S, S->body()->getName(), STV_DEFAULT, S->body()->Type,
154 static void handleUndefinedAsmRefs(const BasicSymbolRef &Sym, GlobalValue *GV,
155 StringSet<> &AsmUndefinedRefs) {
156 // GV associated => not an assembly symbol, bail out.
160 // This is an undefined reference to a symbol in asm. We put that in
161 // compiler.used, so that we can preserve it from being dropped from
162 // the output, without necessarily preventing its internalization.
163 SmallString<64> Name;
164 raw_svector_ostream OS(Name);
166 AsmUndefinedRefs.insert(Name.str());
169 void BitcodeCompiler::add(BitcodeFile &F) {
170 std::unique_ptr<IRObjectFile> Obj = std::move(F.Obj);
171 std::vector<GlobalValue *> Keep;
172 unsigned BodyIndex = 0;
173 ArrayRef<Symbol *> Syms = F.getSymbols();
175 Module &M = Obj->getModule();
176 if (M.getDataLayoutStr().empty())
177 fatal("invalid bitcode file: " + F.getName() + " has no datalayout");
179 // Discard non-compatible debug infos if necessary.
180 M.materializeMetadata();
183 // If a symbol appears in @llvm.used, the linker is required
184 // to treat the symbol as there is a reference to the symbol
185 // that it cannot see. Therefore, we can't internalize.
186 SmallPtrSet<GlobalValue *, 8> Used;
187 collectUsedGlobalVariables(M, Used, /* CompilerUsed */ false);
189 for (const BasicSymbolRef &Sym : Obj->symbols()) {
190 uint32_t Flags = Sym.getFlags();
191 GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
192 if (GV && GV->hasAppendingLinkage())
194 if (BitcodeFile::shouldSkip(Flags))
196 Symbol *S = Syms[BodyIndex++];
197 if (Flags & BasicSymbolRef::SF_Undefined) {
198 handleUndefinedAsmRefs(Sym, GV, AsmUndefinedRefs);
201 auto *B = dyn_cast<DefinedBitcode>(S->body());
202 if (!B || B->file() != &F)
205 // We collect the set of symbols we want to internalize here
206 // and change the linkage after the IRMover executed, i.e. after
207 // we imported the symbols and satisfied undefined references
208 // to it. We can't just change linkage here because otherwise
209 // the IRMover will just rename the symbol.
210 if (GV && shouldInternalize(Used, S, GV))
211 InternalizedSyms.insert(GV->getName());
213 // At this point we know that either the combined LTO object will provide a
214 // definition of a symbol, or we will internalize it. In either case, we
215 // need to undefine the symbol. In the former case, the real definition
216 // needs to be able to replace the original definition without conflicting.
217 // In the latter case, we need to allow the combined LTO object to provide a
218 // definition with the same name, for example when doing parallel codegen.
222 // Module asm symbol.
225 switch (GV->getLinkage()) {
228 case GlobalValue::LinkOnceAnyLinkage:
229 GV->setLinkage(GlobalValue::WeakAnyLinkage);
231 case GlobalValue::LinkOnceODRLinkage:
232 GV->setLinkage(GlobalValue::WeakODRLinkage);
239 IRMover Mover(*Combined);
240 if (Error E = Mover.move(Obj->takeModule(), Keep,
241 [](GlobalValue &, IRMover::ValueAdder) {})) {
242 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EIB) {
243 fatal("failed to link module " + F.getName() + ": " + EIB.message());
248 static void internalize(GlobalValue &GV) {
249 assert(!GV.hasLocalLinkage() &&
250 "Trying to internalize a symbol with local linkage!");
251 GV.setLinkage(GlobalValue::InternalLinkage);
254 std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::runSplitCodegen(
255 const std::function<std::unique_ptr<TargetMachine>()> &TMFactory) {
256 unsigned NumThreads = Config->LtoJobs;
257 OwningData.resize(NumThreads);
259 std::list<raw_svector_ostream> OSs;
260 std::vector<raw_pwrite_stream *> OSPtrs;
261 for (SmallString<0> &Obj : OwningData) {
262 OSs.emplace_back(Obj);
263 OSPtrs.push_back(&OSs.back());
266 splitCodeGen(std::move(Combined), OSPtrs, {}, TMFactory);
268 std::vector<std::unique_ptr<InputFile>> ObjFiles;
269 for (SmallString<0> &Obj : OwningData)
270 ObjFiles.push_back(createObjectFile(
271 MemoryBufferRef(Obj, "LLD-INTERNAL-combined-lto-object")));
273 // If -save-temps is given, we need to save temporary objects to files.
274 // This is for debugging.
275 if (Config->SaveTemps) {
276 if (NumThreads == 1) {
277 saveBuffer(OwningData[0], Config->OutputFile + ".lto.o");
279 for (unsigned I = 0; I < NumThreads; ++I)
280 saveBuffer(OwningData[I], Config->OutputFile + Twine(I) + ".lto.o");
287 // Merge all the bitcode files we have seen, codegen the result
288 // and return the resulting ObjectFile.
289 std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::compile() {
290 for (const auto &Name : InternalizedSyms) {
291 GlobalValue *GV = Combined->getNamedValue(Name.first());
296 std::string TheTriple = Combined->getTargetTriple();
298 const Target *T = TargetRegistry::lookupTarget(TheTriple, Msg);
300 fatal("target not found: " + Msg);
302 // LLD supports the new relocations.
303 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
304 Options.RelaxELFRelocations = true;
306 auto CreateTargetMachine = [&]() {
307 return std::unique_ptr<TargetMachine>(T->createTargetMachine(
308 TheTriple, "", "", Options, Config->Pic ? Reloc::PIC_ : Reloc::Static));
311 std::unique_ptr<TargetMachine> TM = CreateTargetMachine();
313 // Update llvm.compiler.used so that optimizations won't strip
314 // off AsmUndefinedReferences.
315 updateCompilerUsed(*Combined, *TM, AsmUndefinedRefs);
317 if (Config->SaveTemps)
318 saveBCFile(*Combined, Config->OutputFile + ".lto.bc");
320 runLTOPasses(*Combined, *TM);
324 return runSplitCodegen(CreateTargetMachine);