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