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