llvm.org GIT mirror llvm / 9ab3b37
Store and emit original name in combined index Summary: As discussed in D18298, some local globals can't be renamed/promoted (because they have a section, or because they are referenced from inline assembly). To be able to detect naming collision, we need to keep around the "GUID" using their original name without taking the linkage into account. Reviewers: tejohnson Subscribers: joker.eph, llvm-commits Differential Revision: http://reviews.llvm.org/D19454 From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@267304 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
6 changed file(s) with 116 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
216216 FS_ALIAS = 7,
217217 // COMBINED_ALIAS: [modid, linkage, offset]
218218 FS_COMBINED_ALIAS = 8,
219 // COMBINED_ORIGINAL_NAME: [original_name_hash]
220 FS_COMBINED_ORIGINAL_NAME = 9,
219221 };
220222
221223 enum MetadataCodes {
9595 /// Kind of summary for use in dyn_cast<> et al.
9696 SummaryKind Kind;
9797
98 /// This is the hash of the name of the symbol in the original file. It is
99 /// identical to the GUID for global symbols, but differs for local since the
100 /// GUID includes the module level id in the hash.
101 GlobalValue::GUID OriginalName;
102
98103 /// \brief Path of module IR containing value's definition, used to locate
99104 /// module during importing.
100105 ///
126131
127132 public:
128133 virtual ~GlobalValueSummary() = default;
134
135 /// Returns the hash of the original name, it is identical to the GUID for
136 /// externally visible symbols, but not for local ones.
137 GlobalValue::GUID getOriginalName() { return OriginalName; }
138
139 /// Initialize the original name hash in this summary.
140 void setOriginalName(GlobalValue::GUID Name) { OriginalName = Name; }
129141
130142 /// Which kind of summary subclass this is.
131143 SummaryKind getSummaryKind() const { return Kind; }
486486 // call graph edges read from the function summary from referencing
487487 // callees by their ValueId to using the GUID instead, which is how
488488 // they are recorded in the summary index being built.
489 DenseMap ValueIdToCallGraphGUIDMap;
489 // We save a second GUID which is the same as the first one, but ignoring the
490 // linkage, i.e. for value other than local linkage they are identical.
491 DenseMap>
492 ValueIdToCallGraphGUIDMap;
490493
491494 /// Map to save the association between summary offset in the VST to the
492495 /// GlobalValueInfo object created when parsing it. Used to access the
542545 std::error_code initStream(std::unique_ptr Streamer);
543546 std::error_code initStreamFromBuffer();
544547 std::error_code initLazyStream(std::unique_ptr Streamer);
545 GlobalValue::GUID getGUIDFromValueId(unsigned ValueId);
548 std::pair
549 getGUIDFromValueId(unsigned ValueId);
546550 GlobalValueInfo *getInfoFromSummaryOffset(uint64_t Offset);
547551 };
548552 } // end anonymous namespace
56985702
56995703 void ModuleSummaryIndexBitcodeReader::releaseBuffer() { Buffer.release(); }
57005704
5701 GlobalValue::GUID
5705 std::pair
57025706 ModuleSummaryIndexBitcodeReader::getGUIDFromValueId(unsigned ValueId) {
57035707 auto VGI = ValueIdToCallGraphGUIDMap.find(ValueId);
57045708 assert(VGI != ValueIdToCallGraphGUIDMap.end());
57625766 auto VLI = ValueIdToLinkageMap.find(ValueID);
57635767 assert(VLI != ValueIdToLinkageMap.end() &&
57645768 "No linkage found for VST entry?");
5765 std::string GlobalId = GlobalValue::getGlobalIdentifier(
5766 ValueName, VLI->second, SourceFileName);
5769 auto Linkage = VLI->second;
5770 std::string GlobalId =
5771 GlobalValue::getGlobalIdentifier(ValueName, Linkage, SourceFileName);
57675772 auto ValueGUID = GlobalValue::getGUID(GlobalId);
5773 auto OriginalNameID = ValueGUID;
5774 if (GlobalValue::isLocalLinkage(Linkage))
5775 OriginalNameID = GlobalValue::getGUID(ValueName);
57685776 if (PrintSummaryGUIDs)
5769 dbgs() << "GUID " << ValueGUID << " is " << ValueName << "\n";
5777 dbgs() << "GUID " << ValueGUID << "(" << OriginalNameID << ") is "
5778 << ValueName << "\n";
57705779 TheIndex->addGlobalValueInfo(ValueGUID, std::move(GlobalValInfo));
5771 ValueIdToCallGraphGUIDMap[ValueID] = ValueGUID;
5780 ValueIdToCallGraphGUIDMap[ValueID] =
5781 std::make_pair(ValueGUID, OriginalNameID);
57725782 ValueName.clear();
57735783 break;
57745784 }
57845794 auto VLI = ValueIdToLinkageMap.find(ValueID);
57855795 assert(VLI != ValueIdToLinkageMap.end() &&
57865796 "No linkage found for VST entry?");
5797 auto Linkage = VLI->second;
57875798 std::string FunctionGlobalId = GlobalValue::getGlobalIdentifier(
57885799 ValueName, VLI->second, SourceFileName);
57895800 auto FunctionGUID = GlobalValue::getGUID(FunctionGlobalId);
5801 auto OriginalNameID = FunctionGUID;
5802 if (GlobalValue::isLocalLinkage(Linkage))
5803 OriginalNameID = GlobalValue::getGUID(ValueName);
57905804 if (PrintSummaryGUIDs)
5791 dbgs() << "GUID " << FunctionGUID << " is " << ValueName << "\n";
5805 dbgs() << "GUID " << FunctionGUID << "(" << OriginalNameID << ") is "
5806 << ValueName << "\n";
57925807 TheIndex->addGlobalValueInfo(FunctionGUID, std::move(FuncInfo));
5793 ValueIdToCallGraphGUIDMap[ValueID] = FunctionGUID;
5808 ValueIdToCallGraphGUIDMap[ValueID] =
5809 std::make_pair(FunctionGUID, OriginalNameID);
57945810
57955811 ValueName.clear();
57965812 break;
58045820 llvm::make_unique(GlobalValSummaryOffset);
58055821 SummaryOffsetToInfoMap[GlobalValSummaryOffset] = GlobalValInfo.get();
58065822 TheIndex->addGlobalValueInfo(GlobalValGUID, std::move(GlobalValInfo));
5807 ValueIdToCallGraphGUIDMap[ValueID] = GlobalValGUID;
5823 // The "original name", which is the second value of the pair will be
5824 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
5825 ValueIdToCallGraphGUIDMap[ValueID] =
5826 std::make_pair(GlobalValGUID, GlobalValGUID);
58085827 break;
58095828 }
58105829 case bitc::VST_CODE_COMBINED_ENTRY: {
58115830 // VST_CODE_COMBINED_ENTRY: [valueid, refguid]
58125831 unsigned ValueID = Record[0];
58135832 GlobalValue::GUID RefGUID = Record[1];
5814 ValueIdToCallGraphGUIDMap[ValueID] = RefGUID;
5833 // The "original name", which is the second value of the pair will be
5834 // overriden later by a FS_COMBINED_ORIGINAL_NAME in the combined index.
5835 ValueIdToCallGraphGUIDMap[ValueID] = std::make_pair(RefGUID, RefGUID);
58155836 break;
58165837 }
58175838 }
59755996 return error("Invalid record");
59765997
59775998 SmallVector Record;
5978
5999 // Keep around the last seen summary to be used when we see an optional
6000 // "OriginalName" attachement.
6001 GlobalValueSummary *LastSeenSummary = nullptr;
59796002 bool Combined = false;
59806003 while (1) {
59816004 BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
60406063 "Record size inconsistent with number of references");
60416064 for (unsigned I = 4, E = CallGraphEdgeStartIndex; I != E; ++I) {
60426065 unsigned RefValueId = Record[I];
6043 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId);
6066 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
60446067 FS->addRefEdge(RefGUID);
60456068 }
60466069 bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE);
60496072 unsigned CalleeValueId = Record[I];
60506073 unsigned CallsiteCount = Record[++I];
60516074 uint64_t ProfileCount = HasProfile ? Record[++I] : 0;
6052 GlobalValue::GUID CalleeGUID = getGUIDFromValueId(CalleeValueId);
6075 GlobalValue::GUID CalleeGUID = getGUIDFromValueId(CalleeValueId).first;
60536076 FS->addCallGraphEdge(CalleeGUID,
60546077 CalleeInfo(CallsiteCount, ProfileCount));
60556078 }
6056 GlobalValue::GUID GUID = getGUIDFromValueId(ValueID);
6057 auto *Info = TheIndex->getGlobalValueInfo(GUID);
6079 auto GUID = getGUIDFromValueId(ValueID);
6080 FS->setOriginalName(GUID.second);
6081 auto *Info = TheIndex->getGlobalValueInfo(GUID.first);
60586082 assert(!Info->summary() && "Expected a single summary per VST entry");
60596083 Info->setSummary(std::move(FS));
60606084 break;
60766100 AS->setModulePath(
60776101 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
60786102
6079 GlobalValue::GUID AliaseeGUID = getGUIDFromValueId(AliaseeID);
6103 GlobalValue::GUID AliaseeGUID = getGUIDFromValueId(AliaseeID).first;
60806104 auto *AliaseeInfo = TheIndex->getGlobalValueInfo(AliaseeGUID);
60816105 if (!AliaseeInfo->summary())
60826106 return error("Alias expects aliasee summary to be parsed");
60836107 AS->setAliasee(AliaseeInfo->summary());
60846108
6085 GlobalValue::GUID GUID = getGUIDFromValueId(ValueID);
6086 auto *Info = TheIndex->getGlobalValueInfo(GUID);
6109 auto GUID = getGUIDFromValueId(ValueID);
6110 AS->setOriginalName(GUID.second);
6111 auto *Info = TheIndex->getGlobalValueInfo(GUID.first);
60876112 assert(!Info->summary() && "Expected a single summary per VST entry");
60886113 Info->setSummary(std::move(AS));
60896114 break;
60986123 TheIndex->addModulePath(Buffer->getBufferIdentifier(), 0)->first());
60996124 for (unsigned I = 2, E = Record.size(); I != E; ++I) {
61006125 unsigned RefValueId = Record[I];
6101 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId);
6126 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
61026127 FS->addRefEdge(RefGUID);
61036128 }
6104 GlobalValue::GUID GUID = getGUIDFromValueId(ValueID);
6105 auto *Info = TheIndex->getGlobalValueInfo(GUID);
6129 auto GUID = getGUIDFromValueId(ValueID);
6130 FS->setOriginalName(GUID.second);
6131 auto *Info = TheIndex->getGlobalValueInfo(GUID.first);
61066132 assert(!Info->summary() && "Expected a single summary per VST entry");
61076133 Info->setSummary(std::move(FS));
61086134 break;
61206146 unsigned NumRefs = Record[3];
61216147 std::unique_ptr FS = llvm::make_unique(
61226148 getDecodedLinkage(RawLinkage), InstCount);
6149 LastSeenSummary = FS.get();
61236150 FS->setModulePath(ModuleIdMap[ModuleId]);
61246151 static int RefListStartIndex = 4;
61256152 int CallGraphEdgeStartIndex = RefListStartIndex + NumRefs;
61276154 "Record size inconsistent with number of references");
61286155 for (unsigned I = 4, E = CallGraphEdgeStartIndex; I != E; ++I) {
61296156 unsigned RefValueId = Record[I];
6130 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId);
6157 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
61316158 FS->addRefEdge(RefGUID);
61326159 }
61336160 bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE);
61366163 unsigned CalleeValueId = Record[I];
61376164 unsigned CallsiteCount = Record[++I];
61386165 uint64_t ProfileCount = HasProfile ? Record[++I] : 0;
6139 GlobalValue::GUID CalleeGUID = getGUIDFromValueId(CalleeValueId);
6166 GlobalValue::GUID CalleeGUID = getGUIDFromValueId(CalleeValueId).first;
61406167 FS->addCallGraphEdge(CalleeGUID,
61416168 CalleeInfo(CallsiteCount, ProfileCount));
61426169 }
61556182 uint64_t AliaseeSummaryOffset = Record[2];
61566183 std::unique_ptr AS =
61576184 llvm::make_unique(getDecodedLinkage(RawLinkage));
6185 LastSeenSummary = AS.get();
61586186 AS->setModulePath(ModuleIdMap[ModuleId]);
61596187
61606188 auto *AliaseeInfo = getInfoFromSummaryOffset(AliaseeSummaryOffset);
61746202 uint64_t RawLinkage = Record[1];
61756203 std::unique_ptr FS =
61766204 llvm::make_unique(getDecodedLinkage(RawLinkage));
6205 LastSeenSummary = FS.get();
61776206 FS->setModulePath(ModuleIdMap[ModuleId]);
61786207 for (unsigned I = 2, E = Record.size(); I != E; ++I) {
61796208 unsigned RefValueId = Record[I];
6180 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId);
6209 GlobalValue::GUID RefGUID = getGUIDFromValueId(RefValueId).first;
61816210 FS->addRefEdge(RefGUID);
61826211 }
61836212 auto *Info = getInfoFromSummaryOffset(CurRecordBit);
61856214 Info->setSummary(std::move(FS));
61866215 Combined = true;
61876216 break;
6217 }
6218 // FS_COMBINED_ORIGINAL_NAME: [original_name]
6219 case bitc::FS_COMBINED_ORIGINAL_NAME: {
6220 uint64_t OriginalName = Record[0];
6221 if (!LastSeenSummary)
6222 return error("Name attachment that does not follow a combined record");
6223 LastSeenSummary->setOriginalName(OriginalName);
6224 // Reset the LastSeenSummary
6225 LastSeenSummary = nullptr;
61886226 }
61896227 }
61906228 }
32103210 DenseMap SummaryToOffsetMap;
32113211
32123212 SmallVector NameVals;
3213
3214 // For local linkage, we also emit the original name separately
3215 // immediately after the record.
3216 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
3217 if (!GlobalValue::isLocalLinkage(S.linkage()))
3218 return;
3219 NameVals.push_back(S.getOriginalName());
3220 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals);
3221 NameVals.clear();
3222 };
3223
32133224 for (const auto &FII : Index) {
32143225 for (auto &FI : FII.second) {
32153226 GlobalValueSummary *S = FI->summary();
32403251 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals,
32413252 FSModRefsAbbrev);
32423253 NameVals.clear();
3254 MaybeEmitOriginalName(*S);
32433255 continue;
32443256 }
32453257
32873299 // Emit the finished record.
32883300 Stream.EmitRecord(Code, NameVals, FSAbbrev);
32893301 NameVals.clear();
3302 MaybeEmitOriginalName(*S);
32903303 }
32913304 }
32923305
33063319 // Emit the finished record.
33073320 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev);
33083321 NameVals.clear();
3322 MaybeEmitOriginalName(*AS);
33093323 }
33103324
33113325 Stream.ExitBlock();
0 ; Test to check the callgraph in summary
1 ; RUN: opt -module-summary %s -o %t.o
2 ; RUN: llvm-lto -thinlto-action=thinlink -o %t.index.bc %t.o
3 ; RUN: llvm-bcanalyzer -dump %t.index.bc | FileCheck %s --check-prefix=COMBINED
4
5 ; COMBINED:
6 ; COMBINED-NEXT:
7 ; COMBINED-NEXT:
8 ; COMBINED-NEXT:
9 ; COMBINED-NEXT:
10 ; COMBINED-NEXT:
11 ; COMBINED-NEXT:
12 ; COMBINED-NEXT:
13
14 ; ModuleID = 'thinlto-function-summary-callgraph.ll'
15 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
16 target triple = "x86_64-unknown-linux-gnu"
17
18 @bar = internal global i32 0
19 @fooalias = internal alias void (...), bitcast (void ()* @foo to void (...)*)
20
21 define internal void @foo() {
22 ret void
23 }
307307 STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS)
308308 STRINGIFY_CODE(FS, ALIAS)
309309 STRINGIFY_CODE(FS, COMBINED_ALIAS)
310 STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME)
310311 }
311312 case bitc::METADATA_ATTACHMENT_ID:
312313 switch(CodeID) {