xref: /llvm-project-15.0.7/lld/ELF/LTO.cpp (revision df04e79f)
1 //===- LTO.cpp ------------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "LTO.h"
11 #include "Config.h"
12 #include "Driver.h"
13 #include "Error.h"
14 #include "InputFiles.h"
15 #include "Symbols.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/Linker/IRMover.h"
29 #include "llvm/Passes/PassBuilder.h"
30 #include "llvm/Support/StringSaver.h"
31 #include "llvm/Support/TargetRegistry.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Transforms/IPO.h"
34 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
35 #include "llvm/Transforms/Utils/ModuleUtils.h"
36 
37 using namespace llvm;
38 using namespace llvm::object;
39 using namespace llvm::ELF;
40 
41 using namespace lld;
42 using namespace lld::elf;
43 
44 // This is for use when debugging LTO.
45 static void saveLtoObjectFile(StringRef Buffer, unsigned I, bool Many) {
46   SmallString<128> Filename = Config->OutputFile;
47   if (Many)
48     Filename += utostr(I);
49   Filename += ".lto.o";
50   std::error_code EC;
51   raw_fd_ostream OS(Filename, EC, sys::fs::OpenFlags::F_None);
52   check(EC);
53   OS << Buffer;
54 }
55 
56 // This is for use when debugging LTO.
57 static void saveBCFile(Module &M, StringRef Suffix) {
58   std::error_code EC;
59   raw_fd_ostream OS(Config->OutputFile.str() + Suffix.str(), EC,
60                     sys::fs::OpenFlags::F_None);
61   check(EC);
62   WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
63 }
64 
65 static void runNewCustomLtoPasses(Module &M, TargetMachine &TM) {
66   PassBuilder PB(&TM);
67 
68   AAManager AA;
69 
70   // Parse a custom AA pipeline if asked to.
71   if (!PB.parseAAPipeline(AA, Config->LtoAAPipeline)) {
72     error("Unable to parse AA pipeline description: " + Config->LtoAAPipeline);
73     return;
74   }
75 
76   LoopAnalysisManager LAM;
77   FunctionAnalysisManager FAM;
78   CGSCCAnalysisManager CGAM;
79   ModuleAnalysisManager MAM;
80 
81   // Register the AA manager first so that our version is the one used.
82   FAM.registerPass([&] { return std::move(AA); });
83 
84   // Register all the basic analyses with the managers.
85   PB.registerModuleAnalyses(MAM);
86   PB.registerCGSCCAnalyses(CGAM);
87   PB.registerFunctionAnalyses(FAM);
88   PB.registerLoopAnalyses(LAM);
89   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
90 
91   ModulePassManager MPM;
92   if (!Config->DisableVerify)
93     MPM.addPass(VerifierPass());
94 
95   // Now, add all the passes we've been requested to.
96   if (!PB.parsePassPipeline(MPM, Config->LtoNewPmPasses)) {
97     error("unable to parse pass pipeline description: " +
98           Config->LtoNewPmPasses);
99     return;
100   }
101 
102   if (!Config->DisableVerify)
103     MPM.addPass(VerifierPass());
104   MPM.run(M, MAM);
105 }
106 
107 static void runOldLtoPasses(Module &M, TargetMachine &TM) {
108   // Note that the gold plugin has a similar piece of code, so
109   // it is probably better to move this code to a common place.
110   legacy::PassManager LtoPasses;
111   LtoPasses.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
112   PassManagerBuilder PMB;
113   PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
114   PMB.Inliner = createFunctionInliningPass();
115   PMB.VerifyInput = PMB.VerifyOutput = !Config->DisableVerify;
116   PMB.LoopVectorize = true;
117   PMB.SLPVectorize = true;
118   PMB.OptLevel = Config->LtoO;
119   PMB.populateLTOPassManager(LtoPasses);
120   LtoPasses.run(M);
121 }
122 
123 static void runLTOPasses(Module &M, TargetMachine &TM) {
124   if (!Config->LtoNewPmPasses.empty()) {
125     // The user explicitly asked for a set of passes to be run.
126     // This needs the new PM to work as there's no clean way to
127     // pass a set of passes to run in the legacy PM.
128     runNewCustomLtoPasses(M, TM);
129     if (HasError)
130       return;
131   } else {
132     // Run the 'default' set of LTO passes. This code still uses
133     // the legacy PM as the new one is not the default.
134     runOldLtoPasses(M, TM);
135   }
136 
137   if (Config->SaveTemps)
138     saveBCFile(M, ".lto.opt.bc");
139 }
140 
141 static bool shouldInternalize(const SmallPtrSet<GlobalValue *, 8> &Used,
142                               Symbol *S, GlobalValue *GV) {
143   if (S->IsUsedInRegularObj)
144     return false;
145 
146   if (Used.count(GV))
147     return false;
148 
149   return !S->includeInDynsym();
150 }
151 
152 BitcodeCompiler::BitcodeCompiler()
153     : Combined(new llvm::Module("ld-temp.o", Driver->Context)),
154       Mover(*Combined) {}
155 
156 static void undefine(Symbol *S) {
157   replaceBody<Undefined>(S, S->body()->getName(), STV_DEFAULT, S->body()->Type);
158 }
159 
160 void BitcodeCompiler::add(BitcodeFile &F) {
161   std::unique_ptr<IRObjectFile> Obj = std::move(F.Obj);
162   std::vector<GlobalValue *> Keep;
163   unsigned BodyIndex = 0;
164   ArrayRef<Symbol *> Syms = F.getSymbols();
165 
166   Module &M = Obj->getModule();
167   if (M.getDataLayoutStr().empty())
168     fatal("invalid bitcode file: " + F.getName() + " has no datalayout");
169 
170   // Discard non-compatible debug infos if necessary.
171   M.materializeMetadata();
172   UpgradeDebugInfo(M);
173 
174   // If a symbol appears in @llvm.used, the linker is required
175   // to treat the symbol as there is a reference to the symbol
176   // that it cannot see. Therefore, we can't internalize.
177   SmallPtrSet<GlobalValue *, 8> Used;
178   collectUsedGlobalVariables(M, Used, /* CompilerUsed */ false);
179 
180   for (const BasicSymbolRef &Sym : Obj->symbols()) {
181     uint32_t Flags = Sym.getFlags();
182     GlobalValue *GV = Obj->getSymbolGV(Sym.getRawDataRefImpl());
183     if (GV && GV->hasAppendingLinkage())
184       Keep.push_back(GV);
185     if (BitcodeFile::shouldSkip(Flags))
186       continue;
187     Symbol *S = Syms[BodyIndex++];
188     if (Flags & BasicSymbolRef::SF_Undefined)
189       continue;
190     auto *B = dyn_cast<DefinedBitcode>(S->body());
191     if (!B || B->File != &F)
192       continue;
193 
194     // We collect the set of symbols we want to internalize here
195     // and change the linkage after the IRMover executed, i.e. after
196     // we imported the symbols and satisfied undefined references
197     // to it. We can't just change linkage here because otherwise
198     // the IRMover will just rename the symbol.
199     if (GV && shouldInternalize(Used, S, GV))
200       InternalizedSyms.insert(GV->getName());
201 
202     // At this point we know that either the combined LTO object will provide a
203     // definition of a symbol, or we will internalize it. In either case, we
204     // need to undefine the symbol. In the former case, the real definition
205     // needs to be able to replace the original definition without conflicting.
206     // In the latter case, we need to allow the combined LTO object to provide a
207     // definition with the same name, for example when doing parallel codegen.
208     undefine(S);
209 
210     if (!GV)
211       // Module asm symbol.
212       continue;
213 
214     switch (GV->getLinkage()) {
215     default:
216       break;
217     case llvm::GlobalValue::LinkOnceAnyLinkage:
218       GV->setLinkage(GlobalValue::WeakAnyLinkage);
219       break;
220     case llvm::GlobalValue::LinkOnceODRLinkage:
221       GV->setLinkage(GlobalValue::WeakODRLinkage);
222       break;
223     }
224 
225     Keep.push_back(GV);
226   }
227 
228   if (Error E = Mover.move(Obj->takeModule(), Keep,
229                            [](GlobalValue &, IRMover::ValueAdder) {})) {
230     handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
231       fatal("failed to link module " + F.getName() + ": " + EIB.message());
232     });
233   }
234 }
235 
236 static void internalize(GlobalValue &GV) {
237   assert(!GV.hasLocalLinkage() &&
238          "Trying to internalize a symbol with local linkage!");
239   GV.setLinkage(GlobalValue::InternalLinkage);
240 }
241 
242 std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::runSplitCodegen(
243     const std::function<std::unique_ptr<TargetMachine>()> &TMFactory) {
244   unsigned NumThreads = Config->LtoJobs;
245   OwningData.resize(NumThreads);
246 
247   std::list<raw_svector_ostream> OSs;
248   std::vector<raw_pwrite_stream *> OSPtrs;
249   for (SmallString<0> &Obj : OwningData) {
250     OSs.emplace_back(Obj);
251     OSPtrs.push_back(&OSs.back());
252   }
253 
254   splitCodeGen(std::move(Combined), OSPtrs, {}, TMFactory);
255 
256   std::vector<std::unique_ptr<InputFile>> ObjFiles;
257   for (SmallString<0> &Obj : OwningData)
258     ObjFiles.push_back(createObjectFile(
259         MemoryBufferRef(Obj, "LLD-INTERNAL-combined-lto-object")));
260 
261   if (Config->SaveTemps)
262     for (unsigned I = 0; I < NumThreads; ++I)
263       saveLtoObjectFile(OwningData[I], I, NumThreads > 1);
264 
265   return ObjFiles;
266 }
267 
268 // Merge all the bitcode files we have seen, codegen the result
269 // and return the resulting ObjectFile.
270 std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::compile() {
271   TheTriple = Combined->getTargetTriple();
272   for (const auto &Name : InternalizedSyms) {
273     GlobalValue *GV = Combined->getNamedValue(Name.first());
274     assert(GV);
275     internalize(*GV);
276   }
277 
278   if (Config->SaveTemps)
279     saveBCFile(*Combined, ".lto.bc");
280 
281   std::string Msg;
282   const Target *T = TargetRegistry::lookupTarget(TheTriple, Msg);
283   if (!T)
284     fatal("target not found: " + Msg);
285   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
286   Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
287 
288   auto CreateTargetMachine = [&]() {
289     return std::unique_ptr<TargetMachine>(
290         T->createTargetMachine(TheTriple, "", "", Options, R));
291   };
292 
293   std::unique_ptr<TargetMachine> TM = CreateTargetMachine();
294   runLTOPasses(*Combined, *TM);
295   if (HasError)
296     return {};
297 
298   return runSplitCodegen(CreateTargetMachine);
299 }
300