blob: 24c476376c144ec92003dc587eebb6158ba37ccc [file] [log] [blame]
Peter Collingbourne1398a322016-12-16 00:26:30 +00001//===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Peter Collingbourne1398a322016-12-16 00:26:30 +00006//
7//===----------------------------------------------------------------------===//
Peter Collingbourne1398a322016-12-16 00:26:30 +00008
Tim Shen6b4114182017-06-01 01:02:12 +00009#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
Peter Collingbourne002c2d52017-02-14 03:42:38 +000010#include "llvm/Analysis/BasicAliasAnalysis.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000011#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Teresa Johnson94624ac2017-05-10 18:52:16 +000012#include "llvm/Analysis/ProfileSummaryInfo.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000013#include "llvm/Analysis/TypeMetadataUtils.h"
14#include "llvm/Bitcode/BitcodeWriter.h"
15#include "llvm/IR/Constants.h"
Peter Collingbourne28ffd322017-02-08 20:44:00 +000016#include "llvm/IR/DebugInfo.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000017#include "llvm/IR/Intrinsics.h"
18#include "llvm/IR/Module.h"
19#include "llvm/IR/PassManager.h"
Vlad Tsyrklevich230b2562018-04-20 01:36:48 +000020#include "llvm/Object/ModuleSymbolTable.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000021#include "llvm/Pass.h"
22#include "llvm/Support/ScopedPrinter.h"
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +000023#include "llvm/Support/raw_ostream.h"
24#include "llvm/Transforms/IPO.h"
Peter Collingbourne002c2d52017-02-14 03:42:38 +000025#include "llvm/Transforms/IPO/FunctionAttrs.h"
George Rimard3704f62018-02-08 07:23:24 +000026#include "llvm/Transforms/IPO/FunctionImport.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000027#include "llvm/Transforms/Utils/Cloning.h"
Evgeniy Stepanov964f4662017-04-27 20:27:27 +000028#include "llvm/Transforms/Utils/ModuleUtils.h"
Peter Collingbourne1398a322016-12-16 00:26:30 +000029using namespace llvm;
30
31namespace {
32
Peter Collingbourne1398a322016-12-16 00:26:30 +000033// Promote each local-linkage entity defined by ExportM and used by ImportM by
34// changing visibility and appending the given ModuleId.
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +000035void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId,
36 SetVector<GlobalValue *> &PromoteExtra) {
Bob Haarman4075ccc2017-04-12 01:43:07 +000037 DenseMap<const Comdat *, Comdat *> RenamedComdats;
Peter Collingbourne6b193962017-03-30 23:43:08 +000038 for (auto &ExportGV : ExportM.global_values()) {
Peter Collingbourne1398a322016-12-16 00:26:30 +000039 if (!ExportGV.hasLocalLinkage())
Peter Collingbourne6b193962017-03-30 23:43:08 +000040 continue;
Peter Collingbourne1398a322016-12-16 00:26:30 +000041
Bob Haarman4075ccc2017-04-12 01:43:07 +000042 auto Name = ExportGV.getName();
Peter Collingbourne1f034222017-11-30 23:05:52 +000043 GlobalValue *ImportGV = nullptr;
44 if (!PromoteExtra.count(&ExportGV)) {
45 ImportGV = ImportM.getNamedValue(Name);
46 if (!ImportGV)
47 continue;
48 ImportGV->removeDeadConstantUsers();
49 if (ImportGV->use_empty()) {
50 ImportGV->eraseFromParent();
51 continue;
52 }
53 }
Peter Collingbourne1398a322016-12-16 00:26:30 +000054
Bob Haarman4075ccc2017-04-12 01:43:07 +000055 std::string NewName = (Name + ModuleId).str();
56
57 if (const auto *C = ExportGV.getComdat())
58 if (C->getName() == Name)
59 RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName));
Peter Collingbourne1398a322016-12-16 00:26:30 +000060
61 ExportGV.setName(NewName);
62 ExportGV.setLinkage(GlobalValue::ExternalLinkage);
63 ExportGV.setVisibility(GlobalValue::HiddenVisibility);
64
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +000065 if (ImportGV) {
66 ImportGV->setName(NewName);
67 ImportGV->setVisibility(GlobalValue::HiddenVisibility);
68 }
Peter Collingbourne6b193962017-03-30 23:43:08 +000069 }
Bob Haarman4075ccc2017-04-12 01:43:07 +000070
71 if (!RenamedComdats.empty())
72 for (auto &GO : ExportM.global_objects())
73 if (auto *C = GO.getComdat()) {
74 auto Replacement = RenamedComdats.find(C);
75 if (Replacement != RenamedComdats.end())
76 GO.setComdat(Replacement->second);
77 }
Peter Collingbourne1398a322016-12-16 00:26:30 +000078}
79
80// Promote all internal (i.e. distinct) type ids used by the module by replacing
81// them with external type ids formed using the module id.
82//
83// Note that this needs to be done before we clone the module because each clone
84// will receive its own set of distinct metadata nodes.
85void promoteTypeIds(Module &M, StringRef ModuleId) {
86 DenseMap<Metadata *, Metadata *> LocalToGlobal;
87 auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) {
88 Metadata *MD =
89 cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata();
90
91 if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) {
92 Metadata *&GlobalMD = LocalToGlobal[MD];
93 if (!GlobalMD) {
Benjamin Kramer3a13ed62017-12-28 16:58:54 +000094 std::string NewName = (Twine(LocalToGlobal.size()) + ModuleId).str();
Peter Collingbourne1398a322016-12-16 00:26:30 +000095 GlobalMD = MDString::get(M.getContext(), NewName);
96 }
97
98 CI->setArgOperand(ArgNo,
99 MetadataAsValue::get(M.getContext(), GlobalMD));
100 }
101 };
102
103 if (Function *TypeTestFunc =
104 M.getFunction(Intrinsic::getName(Intrinsic::type_test))) {
105 for (const Use &U : TypeTestFunc->uses()) {
106 auto CI = cast<CallInst>(U.getUser());
107 ExternalizeTypeId(CI, 1);
108 }
109 }
110
111 if (Function *TypeCheckedLoadFunc =
112 M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) {
113 for (const Use &U : TypeCheckedLoadFunc->uses()) {
114 auto CI = cast<CallInst>(U.getUser());
115 ExternalizeTypeId(CI, 2);
116 }
117 }
118
119 for (GlobalObject &GO : M.global_objects()) {
120 SmallVector<MDNode *, 1> MDs;
121 GO.getMetadata(LLVMContext::MD_type, MDs);
122
123 GO.eraseMetadata(LLVMContext::MD_type);
124 for (auto MD : MDs) {
125 auto I = LocalToGlobal.find(MD->getOperand(1));
126 if (I == LocalToGlobal.end()) {
127 GO.addMetadata(LLVMContext::MD_type, *MD);
128 continue;
129 }
130 GO.addMetadata(
131 LLVMContext::MD_type,
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000132 *MDNode::get(M.getContext(), {MD->getOperand(0), I->second}));
Peter Collingbourne1398a322016-12-16 00:26:30 +0000133 }
134 }
135}
136
137// Drop unused globals, and drop type information from function declarations.
138// FIXME: If we made functions typeless then there would be no need to do this.
139void simplifyExternals(Module &M) {
140 FunctionType *EmptyFT =
141 FunctionType::get(Type::getVoidTy(M.getContext()), false);
142
143 for (auto I = M.begin(), E = M.end(); I != E;) {
144 Function &F = *I++;
145 if (F.isDeclaration() && F.use_empty()) {
146 F.eraseFromParent();
147 continue;
148 }
149
Peter Collingbourne93fdaca2017-07-19 17:54:29 +0000150 if (!F.isDeclaration() || F.getFunctionType() == EmptyFT ||
151 // Changing the type of an intrinsic may invalidate the IR.
152 F.getName().startswith("llvm."))
Peter Collingbourne1398a322016-12-16 00:26:30 +0000153 continue;
154
155 Function *NewF =
Dylan McKayf920da02018-12-18 09:52:52 +0000156 Function::Create(EmptyFT, GlobalValue::ExternalLinkage,
157 F.getAddressSpace(), "", &M);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000158 NewF->setVisibility(F.getVisibility());
159 NewF->takeName(&F);
160 F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType()));
161 F.eraseFromParent();
162 }
163
164 for (auto I = M.global_begin(), E = M.global_end(); I != E;) {
165 GlobalVariable &GV = *I++;
166 if (GV.isDeclaration() && GV.use_empty()) {
167 GV.eraseFromParent();
168 continue;
169 }
170 }
171}
172
George Rimard3704f62018-02-08 07:23:24 +0000173static void
174filterModule(Module *M,
175 function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) {
176 std::vector<GlobalValue *> V;
177 for (GlobalValue &GV : M->global_values())
178 if (!ShouldKeepDefinition(&GV))
179 V.push_back(&GV);
George Rimar54565242018-02-07 08:46:36 +0000180
George Rimard3704f62018-02-08 07:23:24 +0000181 for (GlobalValue *GV : V)
182 if (!convertToDeclaration(*GV))
183 GV->eraseFromParent();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000184}
185
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000186void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) {
187 if (auto *F = dyn_cast<Function>(C))
188 return Fn(F);
Peter Collingbourne3baa72a2017-03-02 23:10:17 +0000189 if (isa<GlobalValue>(C))
190 return;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000191 for (Value *Op : C->operands())
192 forEachVirtualFunction(cast<Constant>(Op), Fn);
193}
194
Peter Collingbourne1398a322016-12-16 00:26:30 +0000195// If it's possible to split M into regular and thin LTO parts, do so and write
196// a multi-module bitcode file with the two parts to OS. Otherwise, write only a
197// regular LTO bitcode file to OS.
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000198void splitAndWriteThinLTOBitcode(
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000199 raw_ostream &OS, raw_ostream *ThinLinkOS,
200 function_ref<AAResults &(Function &)> AARGetter, Module &M) {
Evgeniy Stepanov964f4662017-04-27 20:27:27 +0000201 std::string ModuleId = getUniqueModuleId(&M);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000202 if (ModuleId.empty()) {
Vlad Tsyrklevich6867ab72018-06-01 15:20:47 +0000203 // We couldn't generate a module ID for this module, write it out as a
204 // regular LTO module with an index for summary-based dead stripping.
205 ProfileSummaryInfo PSI(M);
206 M.addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
207 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
208 WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, &Index);
209
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000210 if (ThinLinkOS)
211 // We don't have a ThinLTO part, but still write the module to the
212 // ThinLinkOS if requested so that the expected output file is produced.
Vlad Tsyrklevich6867ab72018-06-01 15:20:47 +0000213 WriteBitcodeToFile(M, *ThinLinkOS, /*ShouldPreserveUseListOrder=*/false,
214 &Index);
215
Peter Collingbourne1398a322016-12-16 00:26:30 +0000216 return;
217 }
218
219 promoteTypeIds(M, ModuleId);
220
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000221 // Returns whether a global has attached type metadata. Such globals may
222 // participate in CFI or whole-program devirtualization, so they need to
223 // appear in the merged module instead of the thin LTO module.
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000224 auto HasTypeMetadata = [](const GlobalObject *GO) {
225 return GO->hasMetadata(LLVMContext::MD_type);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000226 };
227
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000228 // Collect the set of virtual functions that are eligible for virtual constant
229 // propagation. Each eligible function must not access memory, must return
230 // an integer of width <=64 bits, must take at least one argument, must not
231 // use its first argument (assumed to be "this") and all arguments other than
232 // the first one must be of <=64 bit integer type.
233 //
234 // Note that we test whether this copy of the function is readnone, rather
235 // than testing function attributes, which must hold for any copy of the
236 // function, even a less optimized version substituted at link time. This is
237 // sound because the virtual constant propagation optimizations effectively
238 // inline all implementations of the virtual function into each call site,
239 // rather than using function attributes to perform local optimization.
George Burgess IVaa09a822018-08-27 22:10:59 +0000240 DenseSet<const Function *> EligibleVirtualFns;
Bob Haarman4075ccc2017-04-12 01:43:07 +0000241 // If any member of a comdat lives in MergedM, put all members of that
242 // comdat in MergedM to keep the comdat together.
243 DenseSet<const Comdat *> MergedMComdats;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000244 for (GlobalVariable &GV : M.globals())
Bob Haarman4075ccc2017-04-12 01:43:07 +0000245 if (HasTypeMetadata(&GV)) {
246 if (const auto *C = GV.getComdat())
247 MergedMComdats.insert(C);
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000248 forEachVirtualFunction(GV.getInitializer(), [&](Function *F) {
249 auto *RT = dyn_cast<IntegerType>(F->getReturnType());
250 if (!RT || RT->getBitWidth() > 64 || F->arg_empty() ||
251 !F->arg_begin()->use_empty())
252 return;
253 for (auto &Arg : make_range(std::next(F->arg_begin()), F->arg_end())) {
254 auto *ArgT = dyn_cast<IntegerType>(Arg.getType());
255 if (!ArgT || ArgT->getBitWidth() > 64)
256 return;
257 }
Chandler Carruth01f0c8a2017-07-11 05:39:20 +0000258 if (!F->isDeclaration() &&
259 computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone)
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000260 EligibleVirtualFns.insert(F);
261 });
Bob Haarman4075ccc2017-04-12 01:43:07 +0000262 }
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000263
Peter Collingbourne1398a322016-12-16 00:26:30 +0000264 ValueToValueMapTy VMap;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000265 std::unique_ptr<Module> MergedM(
Rafael Espindola71867532018-02-14 19:50:40 +0000266 CloneModule(M, VMap, [&](const GlobalValue *GV) -> bool {
Bob Haarman4075ccc2017-04-12 01:43:07 +0000267 if (const auto *C = GV->getComdat())
268 if (MergedMComdats.count(C))
269 return true;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000270 if (auto *F = dyn_cast<Function>(GV))
271 return EligibleVirtualFns.count(F);
272 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
273 return HasTypeMetadata(GVar);
274 return false;
275 }));
Peter Collingbourne28ffd322017-02-08 20:44:00 +0000276 StripDebugInfo(*MergedM);
Peter Collingbourne29c6f482018-02-06 03:29:18 +0000277 MergedM->setModuleInlineAsm("");
Peter Collingbourne1398a322016-12-16 00:26:30 +0000278
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000279 for (Function &F : *MergedM)
280 if (!F.isDeclaration()) {
281 // Reset the linkage of all functions eligible for virtual constant
282 // propagation. The canonical definitions live in the thin LTO module so
283 // that they can be imported.
284 F.setLinkage(GlobalValue::AvailableExternallyLinkage);
285 F.setComdat(nullptr);
286 }
287
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000288 SetVector<GlobalValue *> CfiFunctions;
289 for (auto &F : M)
290 if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F))
291 CfiFunctions.insert(&F);
292
Bob Haarman4075ccc2017-04-12 01:43:07 +0000293 // Remove all globals with type metadata, globals with comdats that live in
294 // MergedM, and aliases pointing to such globals from the thin LTO module.
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000295 filterModule(&M, [&](const GlobalValue *GV) {
296 if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject()))
Bob Haarman4075ccc2017-04-12 01:43:07 +0000297 if (HasTypeMetadata(GVar))
298 return false;
299 if (const auto *C = GV->getComdat())
300 if (MergedMComdats.count(C))
301 return false;
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000302 return true;
303 });
Peter Collingbourne1398a322016-12-16 00:26:30 +0000304
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000305 promoteInternals(*MergedM, M, ModuleId, CfiFunctions);
306 promoteInternals(M, *MergedM, ModuleId, CfiFunctions);
307
Vlad Tsyrklevich230b2562018-04-20 01:36:48 +0000308 auto &Ctx = MergedM->getContext();
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000309 SmallVector<MDNode *, 8> CfiFunctionMDs;
310 for (auto V : CfiFunctions) {
311 Function &F = *cast<Function>(V);
312 SmallVector<MDNode *, 2> Types;
313 F.getMetadata(LLVMContext::MD_type, Types);
314
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000315 SmallVector<Metadata *, 4> Elts;
316 Elts.push_back(MDString::get(Ctx, F.getName()));
317 CfiFunctionLinkage Linkage;
318 if (!F.isDeclarationForLinker())
319 Linkage = CFL_Definition;
320 else if (F.isWeakForLinker())
321 Linkage = CFL_WeakDeclaration;
322 else
323 Linkage = CFL_Declaration;
324 Elts.push_back(ConstantAsMetadata::get(
325 llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage)));
326 for (auto Type : Types)
327 Elts.push_back(Type);
328 CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts));
329 }
330
331 if(!CfiFunctionMDs.empty()) {
332 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions");
333 for (auto MD : CfiFunctionMDs)
334 NMD->addOperand(MD);
335 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000336
Vlad Tsyrklevichcdec22e2018-01-10 00:00:51 +0000337 SmallVector<MDNode *, 8> FunctionAliases;
338 for (auto &A : M.aliases()) {
339 if (!isa<Function>(A.getAliasee()))
340 continue;
341
342 auto *F = cast<Function>(A.getAliasee());
Vlad Tsyrklevichcdec22e2018-01-10 00:00:51 +0000343
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000344 Metadata *Elts[] = {
345 MDString::get(Ctx, A.getName()),
346 MDString::get(Ctx, F->getName()),
347 ConstantAsMetadata::get(
348 ConstantInt::get(Type::getInt8Ty(Ctx), A.getVisibility())),
349 ConstantAsMetadata::get(
350 ConstantInt::get(Type::getInt8Ty(Ctx), A.isWeakForLinker())),
351 };
Vlad Tsyrklevichcdec22e2018-01-10 00:00:51 +0000352
353 FunctionAliases.push_back(MDTuple::get(Ctx, Elts));
354 }
355
356 if (!FunctionAliases.empty()) {
357 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("aliases");
358 for (auto MD : FunctionAliases)
359 NMD->addOperand(MD);
360 }
361
Vlad Tsyrklevich230b2562018-04-20 01:36:48 +0000362 SmallVector<MDNode *, 8> Symvers;
363 ModuleSymbolTable::CollectAsmSymvers(M, [&](StringRef Name, StringRef Alias) {
364 Function *F = M.getFunction(Name);
365 if (!F || F->use_empty())
366 return;
367
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000368 Symvers.push_back(MDTuple::get(
369 Ctx, {MDString::get(Ctx, Name), MDString::get(Ctx, Alias)}));
Vlad Tsyrklevich230b2562018-04-20 01:36:48 +0000370 });
371
372 if (!Symvers.empty()) {
373 NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("symvers");
374 for (auto MD : Symvers)
375 NMD->addOperand(MD);
376 }
377
Peter Collingbourne1398a322016-12-16 00:26:30 +0000378 simplifyExternals(*MergedM);
379
Peter Collingbourne1398a322016-12-16 00:26:30 +0000380 // FIXME: Try to re-use BSI and PFI from the original module here.
Teresa Johnson94624ac2017-05-10 18:52:16 +0000381 ProfileSummaryInfo PSI(M);
382 ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI);
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000383
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000384 // Mark the merged module as requiring full LTO. We still want an index for
385 // it though, so that it can participate in summary-based dead stripping.
386 MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
387 ModuleSummaryIndex MergedMIndex =
388 buildModuleSummaryIndex(*MergedM, nullptr, &PSI);
389
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000390 SmallVector<char, 0> Buffer;
391
392 BitcodeWriter W(Buffer);
393 // Save the module hash produced for the full bitcode, which will
394 // be used in the backends, and use that in the minimized bitcode
395 // produced for the full link.
396 ModuleHash ModHash = {{0}};
Rafael Espindola6a86e252018-02-14 19:11:32 +0000397 W.writeModule(M, /*ShouldPreserveUseListOrder=*/false, &Index,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000398 /*GenerateHash=*/true, &ModHash);
Rafael Espindola6a86e252018-02-14 19:11:32 +0000399 W.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false, &MergedMIndex);
Peter Collingbourne92648c22017-06-27 23:50:11 +0000400 W.writeSymtab();
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000401 W.writeStrtab();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000402 OS << Buffer;
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000403
Haojie Wang1dec57d2017-07-21 17:25:20 +0000404 // If a minimized bitcode module was requested for the thin link, only
405 // the information that is needed by thin link will be written in the
406 // given OS (the merged module will be written as usual).
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000407 if (ThinLinkOS) {
408 Buffer.clear();
409 BitcodeWriter W2(Buffer);
410 StripDebugInfo(M);
Rafael Espindola6a86e252018-02-14 19:11:32 +0000411 W2.writeThinLinkBitcode(M, Index, ModHash);
412 W2.writeModule(*MergedM, /*ShouldPreserveUseListOrder=*/false,
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000413 &MergedMIndex);
Peter Collingbourne92648c22017-06-27 23:50:11 +0000414 W2.writeSymtab();
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000415 W2.writeStrtab();
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000416 *ThinLinkOS << Buffer;
417 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000418}
419
Teresa Johnsona7004362019-07-02 19:38:02 +0000420// Check if the LTO Unit splitting has been enabled.
421bool enableSplitLTOUnit(Module &M) {
Teresa Johnson290a8392019-01-11 18:31:57 +0000422 bool EnableSplitLTOUnit = false;
423 if (auto *MD = mdconst::extract_or_null<ConstantInt>(
424 M.getModuleFlag("EnableSplitLTOUnit")))
425 EnableSplitLTOUnit = MD->getZExtValue();
Teresa Johnsona7004362019-07-02 19:38:02 +0000426 return EnableSplitLTOUnit;
427}
Teresa Johnson290a8392019-01-11 18:31:57 +0000428
Teresa Johnsona7004362019-07-02 19:38:02 +0000429// Returns whether this module needs to be split because it uses type metadata.
430bool hasTypeMetadata(Module &M) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000431 for (auto &GO : M.global_objects()) {
Benjamin Kramer0deb9a92018-05-31 13:29:58 +0000432 if (GO.hasMetadata(LLVMContext::MD_type))
Peter Collingbourne1398a322016-12-16 00:26:30 +0000433 return true;
434 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000435 return false;
436}
437
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000438void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS,
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000439 function_ref<AAResults &(Function &)> AARGetter,
440 Module &M, const ModuleSummaryIndex *Index) {
Teresa Johnsona7004362019-07-02 19:38:02 +0000441 std::unique_ptr<ModuleSummaryIndex> NewIndex = nullptr;
442 // See if this module has any type metadata. If so, we try to split it
443 // or at least promote type ids to enable WPD.
444 if (hasTypeMetadata(M)) {
445 if (enableSplitLTOUnit(M))
446 return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M);
447 // Promote type ids as needed for index-based WPD.
448 std::string ModuleId = getUniqueModuleId(&M);
449 if (!ModuleId.empty()) {
450 promoteTypeIds(M, ModuleId);
451 // Need to rebuild the index so that it contains type metadata
452 // for the newly promoted type ids.
453 // FIXME: Probably should not bother building the index at all
454 // in the caller of writeThinLTOBitcode (which does so via the
455 // ModuleSummaryIndexAnalysis pass), since we have to rebuild it
456 // anyway whenever there is type metadata (here or in
457 // splitAndWriteThinLTOBitcode). Just always build it once via the
458 // buildModuleSummaryIndex when Module(s) are ready.
459 ProfileSummaryInfo PSI(M);
460 NewIndex = llvm::make_unique<ModuleSummaryIndex>(
461 buildModuleSummaryIndex(M, nullptr, &PSI));
462 Index = NewIndex.get();
463 }
464 }
Peter Collingbourne1398a322016-12-16 00:26:30 +0000465
Teresa Johnsona7004362019-07-02 19:38:02 +0000466 // Write it out as an unsplit ThinLTO module.
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000467
468 // Save the module hash produced for the full bitcode, which will
469 // be used in the backends, and use that in the minimized bitcode
470 // produced for the full link.
471 ModuleHash ModHash = {{0}};
Rafael Espindola6a86e252018-02-14 19:11:32 +0000472 WriteBitcodeToFile(M, OS, /*ShouldPreserveUseListOrder=*/false, Index,
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000473 /*GenerateHash=*/true, &ModHash);
Haojie Wang1dec57d2017-07-21 17:25:20 +0000474 // If a minimized bitcode module was requested for the thin link, only
475 // the information that is needed by thin link will be written in the
476 // given OS.
477 if (ThinLinkOS && Index)
Rafael Espindola6a86e252018-02-14 19:11:32 +0000478 WriteThinLinkBitcodeToFile(M, *ThinLinkOS, *Index, ModHash);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000479}
480
481class WriteThinLTOBitcode : public ModulePass {
482 raw_ostream &OS; // raw_ostream to print on
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000483 // The output stream on which to emit a minimized module for use
484 // just in the thin link, if requested.
485 raw_ostream *ThinLinkOS;
Peter Collingbourne1398a322016-12-16 00:26:30 +0000486
487public:
488 static char ID; // Pass identification, replacement for typeid
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000489 WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000490 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
491 }
492
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000493 explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS)
494 : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) {
Peter Collingbourne1398a322016-12-16 00:26:30 +0000495 initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry());
496 }
497
498 StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; }
499
500 bool runOnModule(Module &M) override {
501 const ModuleSummaryIndex *Index =
502 &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex());
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000503 writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000504 return true;
505 }
506 void getAnalysisUsage(AnalysisUsage &AU) const override {
507 AU.setPreservesAll();
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000508 AU.addRequired<AssumptionCacheTracker>();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000509 AU.addRequired<ModuleSummaryIndexWrapperPass>();
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000510 AU.addRequired<TargetLibraryInfoWrapperPass>();
Peter Collingbourne1398a322016-12-16 00:26:30 +0000511 }
512};
513} // anonymous namespace
514
515char WriteThinLTOBitcode::ID = 0;
516INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode",
517 "Write ThinLTO Bitcode", false, true)
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000518INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Peter Collingbourne1398a322016-12-16 00:26:30 +0000519INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
Peter Collingbourne002c2d52017-02-14 03:42:38 +0000520INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Peter Collingbourne1398a322016-12-16 00:26:30 +0000521INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode",
522 "Write ThinLTO Bitcode", false, true)
523
Teresa Johnson0c6a4ff2017-03-23 19:47:39 +0000524ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str,
525 raw_ostream *ThinLinkOS) {
526 return new WriteThinLTOBitcode(Str, ThinLinkOS);
Peter Collingbourne1398a322016-12-16 00:26:30 +0000527}
Tim Shen6b4114182017-06-01 01:02:12 +0000528
529PreservedAnalyses
530llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
531 FunctionAnalysisManager &FAM =
532 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
533 writeThinLTOBitcode(OS, ThinLinkOS,
534 [&FAM](Function &F) -> AAResults & {
535 return FAM.getResult<AAManager>(F);
536 },
537 M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
538 return PreservedAnalyses::all();
539}