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