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