Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 1 | //===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass prepares a module containing type metadata for ThinLTO by splitting |
| 11 | // it into regular and thin LTO parts if possible, and writing both parts to |
| 12 | // a multi-module bitcode file. Modules that do not contain type metadata are |
| 13 | // written unmodified as a single module. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
Teresa Johnson | 94624ac | 2017-05-10 18:52:16 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TypeMetadataUtils.h" |
| 21 | #include "llvm/Bitcode/BitcodeWriter.h" |
| 22 | #include "llvm/IR/Constants.h" |
Peter Collingbourne | 28ffd32 | 2017-02-08 20:44:00 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DebugInfo.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Intrinsics.h" |
| 25 | #include "llvm/IR/Module.h" |
| 26 | #include "llvm/IR/PassManager.h" |
| 27 | #include "llvm/Pass.h" |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ScopedPrinter.h" |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | #include "llvm/Transforms/IPO.h" |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/IPO/FunctionAttrs.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/Cloning.h" |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
| 37 | namespace { |
| 38 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 39 | // Promote each local-linkage entity defined by ExportM and used by ImportM by |
| 40 | // changing visibility and appending the given ModuleId. |
| 41 | void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId) { |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 42 | DenseMap<const Comdat *, Comdat *> RenamedComdats; |
Peter Collingbourne | 6b19396 | 2017-03-30 23:43:08 +0000 | [diff] [blame] | 43 | for (auto &ExportGV : ExportM.global_values()) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 44 | if (!ExportGV.hasLocalLinkage()) |
Peter Collingbourne | 6b19396 | 2017-03-30 23:43:08 +0000 | [diff] [blame] | 45 | continue; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 46 | |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 47 | auto Name = ExportGV.getName(); |
| 48 | GlobalValue *ImportGV = ImportM.getNamedValue(Name); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 49 | if (!ImportGV || ImportGV->use_empty()) |
Peter Collingbourne | 6b19396 | 2017-03-30 23:43:08 +0000 | [diff] [blame] | 50 | continue; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 51 | |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 52 | std::string NewName = (Name + ModuleId).str(); |
| 53 | |
| 54 | if (const auto *C = ExportGV.getComdat()) |
| 55 | if (C->getName() == Name) |
| 56 | RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName)); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 57 | |
| 58 | ExportGV.setName(NewName); |
| 59 | ExportGV.setLinkage(GlobalValue::ExternalLinkage); |
| 60 | ExportGV.setVisibility(GlobalValue::HiddenVisibility); |
| 61 | |
| 62 | ImportGV->setName(NewName); |
| 63 | ImportGV->setVisibility(GlobalValue::HiddenVisibility); |
Peter Collingbourne | 6b19396 | 2017-03-30 23:43:08 +0000 | [diff] [blame] | 64 | } |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 65 | |
| 66 | if (!RenamedComdats.empty()) |
| 67 | for (auto &GO : ExportM.global_objects()) |
| 68 | if (auto *C = GO.getComdat()) { |
| 69 | auto Replacement = RenamedComdats.find(C); |
| 70 | if (Replacement != RenamedComdats.end()) |
| 71 | GO.setComdat(Replacement->second); |
| 72 | } |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Promote all internal (i.e. distinct) type ids used by the module by replacing |
| 76 | // them with external type ids formed using the module id. |
| 77 | // |
| 78 | // Note that this needs to be done before we clone the module because each clone |
| 79 | // will receive its own set of distinct metadata nodes. |
| 80 | void promoteTypeIds(Module &M, StringRef ModuleId) { |
| 81 | DenseMap<Metadata *, Metadata *> LocalToGlobal; |
| 82 | auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) { |
| 83 | Metadata *MD = |
| 84 | cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata(); |
| 85 | |
| 86 | if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) { |
| 87 | Metadata *&GlobalMD = LocalToGlobal[MD]; |
| 88 | if (!GlobalMD) { |
| 89 | std::string NewName = |
| 90 | (to_string(LocalToGlobal.size()) + ModuleId).str(); |
| 91 | GlobalMD = MDString::get(M.getContext(), NewName); |
| 92 | } |
| 93 | |
| 94 | CI->setArgOperand(ArgNo, |
| 95 | MetadataAsValue::get(M.getContext(), GlobalMD)); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | if (Function *TypeTestFunc = |
| 100 | M.getFunction(Intrinsic::getName(Intrinsic::type_test))) { |
| 101 | for (const Use &U : TypeTestFunc->uses()) { |
| 102 | auto CI = cast<CallInst>(U.getUser()); |
| 103 | ExternalizeTypeId(CI, 1); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (Function *TypeCheckedLoadFunc = |
| 108 | M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) { |
| 109 | for (const Use &U : TypeCheckedLoadFunc->uses()) { |
| 110 | auto CI = cast<CallInst>(U.getUser()); |
| 111 | ExternalizeTypeId(CI, 2); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | for (GlobalObject &GO : M.global_objects()) { |
| 116 | SmallVector<MDNode *, 1> MDs; |
| 117 | GO.getMetadata(LLVMContext::MD_type, MDs); |
| 118 | |
| 119 | GO.eraseMetadata(LLVMContext::MD_type); |
| 120 | for (auto MD : MDs) { |
| 121 | auto I = LocalToGlobal.find(MD->getOperand(1)); |
| 122 | if (I == LocalToGlobal.end()) { |
| 123 | GO.addMetadata(LLVMContext::MD_type, *MD); |
| 124 | continue; |
| 125 | } |
| 126 | GO.addMetadata( |
| 127 | LLVMContext::MD_type, |
| 128 | *MDNode::get(M.getContext(), |
| 129 | ArrayRef<Metadata *>{MD->getOperand(0), I->second})); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Drop unused globals, and drop type information from function declarations. |
| 135 | // FIXME: If we made functions typeless then there would be no need to do this. |
| 136 | void simplifyExternals(Module &M) { |
| 137 | FunctionType *EmptyFT = |
| 138 | FunctionType::get(Type::getVoidTy(M.getContext()), false); |
| 139 | |
| 140 | for (auto I = M.begin(), E = M.end(); I != E;) { |
| 141 | Function &F = *I++; |
| 142 | if (F.isDeclaration() && F.use_empty()) { |
| 143 | F.eraseFromParent(); |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | if (!F.isDeclaration() || F.getFunctionType() == EmptyFT) |
| 148 | continue; |
| 149 | |
| 150 | Function *NewF = |
| 151 | Function::Create(EmptyFT, GlobalValue::ExternalLinkage, "", &M); |
| 152 | NewF->setVisibility(F.getVisibility()); |
| 153 | NewF->takeName(&F); |
| 154 | F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType())); |
| 155 | F.eraseFromParent(); |
| 156 | } |
| 157 | |
| 158 | for (auto I = M.global_begin(), E = M.global_end(); I != E;) { |
| 159 | GlobalVariable &GV = *I++; |
| 160 | if (GV.isDeclaration() && GV.use_empty()) { |
| 161 | GV.eraseFromParent(); |
| 162 | continue; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void filterModule( |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 168 | Module *M, function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) { |
Bob Haarman | 6de8134 | 2017-04-05 00:42:07 +0000 | [diff] [blame] | 169 | for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 170 | I != E;) { |
| 171 | GlobalAlias *GA = &*I++; |
| 172 | if (ShouldKeepDefinition(GA)) |
| 173 | continue; |
| 174 | |
| 175 | GlobalObject *GO; |
| 176 | if (GA->getValueType()->isFunctionTy()) |
| 177 | GO = Function::Create(cast<FunctionType>(GA->getValueType()), |
| 178 | GlobalValue::ExternalLinkage, "", M); |
| 179 | else |
| 180 | GO = new GlobalVariable( |
| 181 | *M, GA->getValueType(), false, GlobalValue::ExternalLinkage, |
Serge Guelton | f4dc59b | 2017-05-11 08:53:00 +0000 | [diff] [blame^] | 182 | nullptr, "", nullptr, |
Bob Haarman | 6de8134 | 2017-04-05 00:42:07 +0000 | [diff] [blame] | 183 | GA->getThreadLocalMode(), GA->getType()->getAddressSpace()); |
| 184 | GO->takeName(GA); |
| 185 | GA->replaceAllUsesWith(GO); |
| 186 | GA->eraseFromParent(); |
| 187 | } |
| 188 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 189 | for (Function &F : *M) { |
| 190 | if (ShouldKeepDefinition(&F)) |
| 191 | continue; |
| 192 | |
| 193 | F.deleteBody(); |
Peter Collingbourne | 20a0093 | 2017-01-18 20:03:02 +0000 | [diff] [blame] | 194 | F.setComdat(nullptr); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 195 | F.clearMetadata(); |
| 196 | } |
| 197 | |
| 198 | for (GlobalVariable &GV : M->globals()) { |
| 199 | if (ShouldKeepDefinition(&GV)) |
| 200 | continue; |
| 201 | |
| 202 | GV.setInitializer(nullptr); |
| 203 | GV.setLinkage(GlobalValue::ExternalLinkage); |
Peter Collingbourne | 20a0093 | 2017-01-18 20:03:02 +0000 | [diff] [blame] | 204 | GV.setComdat(nullptr); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 205 | GV.clearMetadata(); |
| 206 | } |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 209 | void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) { |
| 210 | if (auto *F = dyn_cast<Function>(C)) |
| 211 | return Fn(F); |
Peter Collingbourne | 3baa72a | 2017-03-02 23:10:17 +0000 | [diff] [blame] | 212 | if (isa<GlobalValue>(C)) |
| 213 | return; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 214 | for (Value *Op : C->operands()) |
| 215 | forEachVirtualFunction(cast<Constant>(Op), Fn); |
| 216 | } |
| 217 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 218 | // If it's possible to split M into regular and thin LTO parts, do so and write |
| 219 | // a multi-module bitcode file with the two parts to OS. Otherwise, write only a |
| 220 | // regular LTO bitcode file to OS. |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 221 | void splitAndWriteThinLTOBitcode( |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 222 | raw_ostream &OS, raw_ostream *ThinLinkOS, |
| 223 | function_ref<AAResults &(Function &)> AARGetter, Module &M) { |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 224 | std::string ModuleId = getUniqueModuleId(&M); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 225 | if (ModuleId.empty()) { |
| 226 | // We couldn't generate a module ID for this module, just write it out as a |
| 227 | // regular LTO module. |
| 228 | WriteBitcodeToFile(&M, OS); |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 229 | if (ThinLinkOS) |
| 230 | // We don't have a ThinLTO part, but still write the module to the |
| 231 | // ThinLinkOS if requested so that the expected output file is produced. |
| 232 | WriteBitcodeToFile(&M, *ThinLinkOS); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | |
| 236 | promoteTypeIds(M, ModuleId); |
| 237 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 238 | // Returns whether a global has attached type metadata. Such globals may |
| 239 | // participate in CFI or whole-program devirtualization, so they need to |
| 240 | // appear in the merged module instead of the thin LTO module. |
| 241 | auto HasTypeMetadata = [&](const GlobalObject *GO) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 242 | SmallVector<MDNode *, 1> MDs; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 243 | GO->getMetadata(LLVMContext::MD_type, MDs); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 244 | return !MDs.empty(); |
| 245 | }; |
| 246 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 247 | // Collect the set of virtual functions that are eligible for virtual constant |
| 248 | // propagation. Each eligible function must not access memory, must return |
| 249 | // an integer of width <=64 bits, must take at least one argument, must not |
| 250 | // use its first argument (assumed to be "this") and all arguments other than |
| 251 | // the first one must be of <=64 bit integer type. |
| 252 | // |
| 253 | // Note that we test whether this copy of the function is readnone, rather |
| 254 | // than testing function attributes, which must hold for any copy of the |
| 255 | // function, even a less optimized version substituted at link time. This is |
| 256 | // sound because the virtual constant propagation optimizations effectively |
| 257 | // inline all implementations of the virtual function into each call site, |
| 258 | // rather than using function attributes to perform local optimization. |
| 259 | std::set<const Function *> EligibleVirtualFns; |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 260 | // If any member of a comdat lives in MergedM, put all members of that |
| 261 | // comdat in MergedM to keep the comdat together. |
| 262 | DenseSet<const Comdat *> MergedMComdats; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 263 | for (GlobalVariable &GV : M.globals()) |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 264 | if (HasTypeMetadata(&GV)) { |
| 265 | if (const auto *C = GV.getComdat()) |
| 266 | MergedMComdats.insert(C); |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 267 | forEachVirtualFunction(GV.getInitializer(), [&](Function *F) { |
| 268 | auto *RT = dyn_cast<IntegerType>(F->getReturnType()); |
| 269 | if (!RT || RT->getBitWidth() > 64 || F->arg_empty() || |
| 270 | !F->arg_begin()->use_empty()) |
| 271 | return; |
| 272 | for (auto &Arg : make_range(std::next(F->arg_begin()), F->arg_end())) { |
| 273 | auto *ArgT = dyn_cast<IntegerType>(Arg.getType()); |
| 274 | if (!ArgT || ArgT->getBitWidth() > 64) |
| 275 | return; |
| 276 | } |
| 277 | if (computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone) |
| 278 | EligibleVirtualFns.insert(F); |
| 279 | }); |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 280 | } |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 281 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 282 | ValueToValueMapTy VMap; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 283 | std::unique_ptr<Module> MergedM( |
| 284 | CloneModule(&M, VMap, [&](const GlobalValue *GV) -> bool { |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 285 | if (const auto *C = GV->getComdat()) |
| 286 | if (MergedMComdats.count(C)) |
| 287 | return true; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 288 | if (auto *F = dyn_cast<Function>(GV)) |
| 289 | return EligibleVirtualFns.count(F); |
| 290 | if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject())) |
| 291 | return HasTypeMetadata(GVar); |
| 292 | return false; |
| 293 | })); |
Peter Collingbourne | 28ffd32 | 2017-02-08 20:44:00 +0000 | [diff] [blame] | 294 | StripDebugInfo(*MergedM); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 295 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 296 | for (Function &F : *MergedM) |
| 297 | if (!F.isDeclaration()) { |
| 298 | // Reset the linkage of all functions eligible for virtual constant |
| 299 | // propagation. The canonical definitions live in the thin LTO module so |
| 300 | // that they can be imported. |
| 301 | F.setLinkage(GlobalValue::AvailableExternallyLinkage); |
| 302 | F.setComdat(nullptr); |
| 303 | } |
| 304 | |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 305 | // Remove all globals with type metadata, globals with comdats that live in |
| 306 | // MergedM, and aliases pointing to such globals from the thin LTO module. |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 307 | filterModule(&M, [&](const GlobalValue *GV) { |
| 308 | if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject())) |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 309 | if (HasTypeMetadata(GVar)) |
| 310 | return false; |
| 311 | if (const auto *C = GV->getComdat()) |
| 312 | if (MergedMComdats.count(C)) |
| 313 | return false; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 314 | return true; |
| 315 | }); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 316 | |
| 317 | promoteInternals(*MergedM, M, ModuleId); |
| 318 | promoteInternals(M, *MergedM, ModuleId); |
| 319 | |
| 320 | simplifyExternals(*MergedM); |
| 321 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 322 | |
| 323 | // FIXME: Try to re-use BSI and PFI from the original module here. |
Teresa Johnson | 94624ac | 2017-05-10 18:52:16 +0000 | [diff] [blame] | 324 | ProfileSummaryInfo PSI(M); |
| 325 | ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI); |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 326 | |
| 327 | SmallVector<char, 0> Buffer; |
| 328 | |
| 329 | BitcodeWriter W(Buffer); |
| 330 | // Save the module hash produced for the full bitcode, which will |
| 331 | // be used in the backends, and use that in the minimized bitcode |
| 332 | // produced for the full link. |
| 333 | ModuleHash ModHash = {{0}}; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 334 | W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index, |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 335 | /*GenerateHash=*/true, &ModHash); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 336 | W.writeModule(MergedM.get()); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 337 | W.writeStrtab(); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 338 | OS << Buffer; |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 339 | |
| 340 | // If a minimized bitcode module was requested for the thin link, |
| 341 | // strip the debug info (the merged module was already stripped above) |
| 342 | // and write it to the given OS. |
| 343 | if (ThinLinkOS) { |
| 344 | Buffer.clear(); |
| 345 | BitcodeWriter W2(Buffer); |
| 346 | StripDebugInfo(M); |
| 347 | W2.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index, |
| 348 | /*GenerateHash=*/false, &ModHash); |
| 349 | W2.writeModule(MergedM.get()); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 350 | W2.writeStrtab(); |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 351 | *ThinLinkOS << Buffer; |
| 352 | } |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Returns whether this module needs to be split because it uses type metadata. |
| 356 | bool requiresSplit(Module &M) { |
| 357 | SmallVector<MDNode *, 1> MDs; |
| 358 | for (auto &GO : M.global_objects()) { |
| 359 | GO.getMetadata(LLVMContext::MD_type, MDs); |
| 360 | if (!MDs.empty()) |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | return false; |
| 365 | } |
| 366 | |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 367 | void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS, |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 368 | function_ref<AAResults &(Function &)> AARGetter, |
| 369 | Module &M, const ModuleSummaryIndex *Index) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 370 | // See if this module has any type metadata. If so, we need to split it. |
| 371 | if (requiresSplit(M)) |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 372 | return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 373 | |
| 374 | // Otherwise we can just write it out as a regular module. |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 375 | |
| 376 | // Save the module hash produced for the full bitcode, which will |
| 377 | // be used in the backends, and use that in the minimized bitcode |
| 378 | // produced for the full link. |
| 379 | ModuleHash ModHash = {{0}}; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 380 | WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false, Index, |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 381 | /*GenerateHash=*/true, &ModHash); |
| 382 | // If a minimized bitcode module was requested for the thin link, |
| 383 | // strip the debug info and write it to the given OS. |
| 384 | if (ThinLinkOS) { |
| 385 | StripDebugInfo(M); |
| 386 | WriteBitcodeToFile(&M, *ThinLinkOS, /*ShouldPreserveUseListOrder=*/false, |
| 387 | Index, |
| 388 | /*GenerateHash=*/false, &ModHash); |
| 389 | } |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | class WriteThinLTOBitcode : public ModulePass { |
| 393 | raw_ostream &OS; // raw_ostream to print on |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 394 | // The output stream on which to emit a minimized module for use |
| 395 | // just in the thin link, if requested. |
| 396 | raw_ostream *ThinLinkOS; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 397 | |
| 398 | public: |
| 399 | static char ID; // Pass identification, replacement for typeid |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 400 | WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 401 | initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry()); |
| 402 | } |
| 403 | |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 404 | explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS) |
| 405 | : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 406 | initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry()); |
| 407 | } |
| 408 | |
| 409 | StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; } |
| 410 | |
| 411 | bool runOnModule(Module &M) override { |
| 412 | const ModuleSummaryIndex *Index = |
| 413 | &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex()); |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 414 | writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 415 | return true; |
| 416 | } |
| 417 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 418 | AU.setPreservesAll(); |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 419 | AU.addRequired<AssumptionCacheTracker>(); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 420 | AU.addRequired<ModuleSummaryIndexWrapperPass>(); |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 421 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 422 | } |
| 423 | }; |
| 424 | } // anonymous namespace |
| 425 | |
| 426 | char WriteThinLTOBitcode::ID = 0; |
| 427 | INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode", |
| 428 | "Write ThinLTO Bitcode", false, true) |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 429 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 430 | INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 431 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 432 | INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode", |
| 433 | "Write ThinLTO Bitcode", false, true) |
| 434 | |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 435 | ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str, |
| 436 | raw_ostream *ThinLinkOS) { |
| 437 | return new WriteThinLTOBitcode(Str, ThinLinkOS); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 438 | } |