Skip to content

Commit 81392d2

Browse files
committed
change GlobalValueSummaryMapTy from std::map to llvm::MapVector
1 parent 8e7385a commit 81392d2

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "llvm/ADT/ArrayRef.h"
1919
#include "llvm/ADT/DenseMap.h"
20+
#include "llvm/ADT/MapVector.h"
2021
#include "llvm/ADT/STLExtras.h"
2122
#include "llvm/ADT/SmallString.h"
2223
#include "llvm/ADT/SmallVector.h"
@@ -177,22 +178,29 @@ struct alignas(8) GlobalValueSummaryInfo {
177178
/// of the map is unknown, resulting in inefficiencies due to repeated
178179
/// insertions and resizing.
179180
using GlobalValueSummaryMapTy =
180-
std::map<GlobalValue::GUID, GlobalValueSummaryInfo>;
181+
llvm::MapVector<GlobalValue::GUID, GlobalValueSummaryInfo>;
181182

182183
/// Struct that holds a reference to a particular GUID in a global value
183184
/// summary.
184185
struct ValueInfo {
185186
enum Flags { HaveGV = 1, ReadOnly = 2, WriteOnly = 4 };
186-
PointerIntPair<const GlobalValueSummaryMapTy::value_type *, 3, int>
187+
PointerIntPair<const GlobalValueSummaryMapTy *, 3, int>
187188
RefAndFlags;
189+
PointerIntPair<const GlobalValueSummaryMapTy::value_type *, 1, bool>
190+
Offset;
188191

189192
ValueInfo() = default;
190-
ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R) {
191-
RefAndFlags.setPointer(R);
193+
ValueInfo(bool HaveGVs, const GlobalValueSummaryMapTy::value_type *R, const GlobalValueSummaryMapTy *Map/* = nullptr*/) {
194+
RefAndFlags.setPointer(Map);
192195
RefAndFlags.setInt(HaveGVs);
196+
Offset.setPointer(R);
197+
if (R != nullptr && Map != nullptr)
198+
Offset.setPointer((const GlobalValueSummaryMapTy::value_type *)((uintptr_t)R - (uintptr_t)Map->begin()));
199+
if (R != nullptr)
200+
Offset.setInt(true);
193201
}
194202

195-
explicit operator bool() const { return getRef(); }
203+
explicit operator bool() const { return Offset.getInt(); }
196204

197205
GlobalValue::GUID getGUID() const { return getRef()->first; }
198206
const GlobalValue *getValue() const {
@@ -243,7 +251,9 @@ struct ValueInfo {
243251
}
244252

245253
const GlobalValueSummaryMapTy::value_type *getRef() const {
246-
return RefAndFlags.getPointer();
254+
if (RefAndFlags.getPointer())
255+
return RefAndFlags.getPointer()->begin() + (intptr_t)Offset.getPointer() / sizeof(GlobalValueSummaryMapTy::value_type);
256+
return Offset.getPointer();
247257
}
248258

249259
/// Returns the most constraining visibility among summaries. The
@@ -286,11 +296,11 @@ inline bool operator<(const ValueInfo &A, const ValueInfo &B) {
286296

287297
template <> struct DenseMapInfo<ValueInfo> {
288298
static inline ValueInfo getEmptyKey() {
289-
return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
299+
return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8, nullptr);
290300
}
291301

292302
static inline ValueInfo getTombstoneKey() {
293-
return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-16);
303+
return ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-16, nullptr);
294304
}
295305

296306
static inline bool isSpecialKey(ValueInfo V) {
@@ -301,7 +311,7 @@ template <> struct DenseMapInfo<ValueInfo> {
301311
// We are not supposed to mix ValueInfo(s) with different HaveGVs flag
302312
// in a same container.
303313
assert(isSpecialKey(L) || isSpecialKey(R) || (L.haveGVs() == R.haveGVs()));
304-
return L.getRef() == R.getRef();
314+
return L.Offset == R.Offset && L.RefAndFlags.getPointer() == R.RefAndFlags.getPointer();
305315
}
306316
static unsigned getHashValue(ValueInfo I) { return hash_value(I.getRef()); }
307317
};
@@ -1397,7 +1407,7 @@ class ModuleSummaryIndex {
13971407
private:
13981408
/// Map from value name to list of summary instances for values of that
13991409
/// name (may be duplicates in the COMDAT case, e.g.).
1400-
GlobalValueSummaryMapTy GlobalValueMap;
1410+
std::unique_ptr<GlobalValueSummaryMapTy> GlobalValueMap = std::make_unique<GlobalValueSummaryMapTy>();
14011411

14021412
/// Holds strings for combined index, mapping to the corresponding module ID.
14031413
ModulePathStringTableTy ModulePathStringTable;
@@ -1501,7 +1511,7 @@ class ModuleSummaryIndex {
15011511

15021512
GlobalValueSummaryMapTy::value_type *
15031513
getOrInsertValuePtr(GlobalValue::GUID GUID) {
1504-
return &*GlobalValueMap.emplace(GUID, GlobalValueSummaryInfo(HaveGVs))
1514+
return &*GlobalValueMap->try_emplace(GUID, GlobalValueSummaryInfo(HaveGVs))
15051515
.first;
15061516
}
15071517

@@ -1534,11 +1544,11 @@ class ModuleSummaryIndex {
15341544
void addBlockCount(uint64_t C) { BlockCount += C; }
15351545
void setBlockCount(uint64_t C) { BlockCount = C; }
15361546

1537-
gvsummary_iterator begin() { return GlobalValueMap.begin(); }
1538-
const_gvsummary_iterator begin() const { return GlobalValueMap.begin(); }
1539-
gvsummary_iterator end() { return GlobalValueMap.end(); }
1540-
const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
1541-
size_t size() const { return GlobalValueMap.size(); }
1547+
gvsummary_iterator begin() { return GlobalValueMap->begin(); }
1548+
const_gvsummary_iterator begin() const { return GlobalValueMap->begin(); }
1549+
gvsummary_iterator end() { return GlobalValueMap->end(); }
1550+
const_gvsummary_iterator end() const { return GlobalValueMap->end(); }
1551+
size_t size() const { return GlobalValueMap->size(); }
15421552

15431553
const std::vector<uint64_t> &stackIds() const { return StackIds; }
15441554

@@ -1610,7 +1620,7 @@ class ModuleSummaryIndex {
16101620
if (!S.second.SummaryList.size() ||
16111621
!isa<FunctionSummary>(S.second.SummaryList.front().get()))
16121622
continue;
1613-
discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent);
1623+
discoverNodes(ValueInfo(HaveGVs, &S, GlobalValueMap.get()), FunctionHasParent);
16141624
}
16151625

16161626
SmallVector<FunctionSummary::EdgeTy, 0> Edges;
@@ -1677,18 +1687,18 @@ class ModuleSummaryIndex {
16771687
/// Return a ValueInfo for the index value_type (convenient when iterating
16781688
/// index).
16791689
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const {
1680-
return ValueInfo(HaveGVs, &R);
1690+
return ValueInfo(HaveGVs, &R, GlobalValueMap.get());
16811691
}
16821692

16831693
/// Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
16841694
ValueInfo getValueInfo(GlobalValue::GUID GUID) const {
1685-
auto I = GlobalValueMap.find(GUID);
1686-
return ValueInfo(HaveGVs, I == GlobalValueMap.end() ? nullptr : &*I);
1695+
auto I = GlobalValueMap->find(GUID);
1696+
return ValueInfo(HaveGVs, I == GlobalValueMap->end() ? nullptr : &*I, GlobalValueMap.get());
16871697
}
16881698

16891699
/// Return a ValueInfo for \p GUID.
16901700
ValueInfo getOrInsertValueInfo(GlobalValue::GUID GUID) {
1691-
return ValueInfo(HaveGVs, getOrInsertValuePtr(GUID));
1701+
return ValueInfo(HaveGVs, getOrInsertValuePtr(GUID), GlobalValueMap.get());
16921702
}
16931703

16941704
// Save a string in the Index. Use before passing Name to
@@ -1701,15 +1711,15 @@ class ModuleSummaryIndex {
17011711
assert(!HaveGVs);
17021712
auto VP = getOrInsertValuePtr(GUID);
17031713
VP->second.U.Name = Name;
1704-
return ValueInfo(HaveGVs, VP);
1714+
return ValueInfo(HaveGVs, VP, GlobalValueMap.get());
17051715
}
17061716

17071717
/// Return a ValueInfo for \p GV and mark it as belonging to GV.
17081718
ValueInfo getOrInsertValueInfo(const GlobalValue *GV) {
17091719
assert(HaveGVs);
17101720
auto VP = getOrInsertValuePtr(GV->getGUID());
17111721
VP->second.U.GV = GV;
1712-
return ValueInfo(HaveGVs, VP);
1722+
return ValueInfo(HaveGVs, VP, GlobalValueMap.get());
17131723
}
17141724

17151725
/// Return the GUID for \p OriginalId in the OidGuidMap.
@@ -2038,7 +2048,7 @@ struct GraphTraits<ModuleSummaryIndex *> : public GraphTraits<ValueInfo> {
20382048
G.SummaryList.push_back(std::move(Root));
20392049
static auto P =
20402050
GlobalValueSummaryMapTy::value_type(GlobalValue::GUID(0), std::move(G));
2041-
return ValueInfo(I->haveGVs(), &P);
2051+
return ValueInfo(I->haveGVs(), &P, nullptr);
20422052
}
20432053
};
20442054
} // end namespace llvm

llvm/include/llvm/IR/ModuleSummaryIndexYAML.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
232232
if (GVSum.Aliasee) {
233233
auto ASum = std::make_unique<AliasSummary>(GVFlags);
234234
V.try_emplace(*GVSum.Aliasee, /*IsAnalysis=*/false);
235-
ValueInfo AliaseeVI(/*IsAnalysis=*/false, &*V.find(*GVSum.Aliasee));
235+
ValueInfo AliaseeVI(/*IsAnalysis=*/false, &*V.find(*GVSum.Aliasee), &V);
236236
// Note: Aliasee cannot be filled until all summaries are loaded.
237237
// This is done in fixAliaseeLinks() which is called in
238238
// MappingTraits<ModuleSummaryIndex>::mapping().
@@ -244,7 +244,7 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
244244
Refs.reserve(GVSum.Refs.size());
245245
for (auto &RefGUID : GVSum.Refs) {
246246
auto It = V.try_emplace(RefGUID, /*IsAnalysis=*/false).first;
247-
Refs.push_back(ValueInfo(/*IsAnalysis=*/false, &*It));
247+
Refs.push_back(ValueInfo(/*IsAnalysis=*/false, &*It, &V));
248248
}
249249
Elem.SummaryList.push_back(std::make_unique<FunctionSummary>(
250250
GVFlags, /*NumInsts=*/0, FunctionSummary::FFlags{}, std::move(Refs),
@@ -324,10 +324,10 @@ template <> struct CustomMappingTraits<TypeIdSummaryMapTy> {
324324

325325
template <> struct MappingTraits<ModuleSummaryIndex> {
326326
static void mapping(IO &io, ModuleSummaryIndex& index) {
327-
io.mapOptional("GlobalValueMap", index.GlobalValueMap);
327+
io.mapOptional("GlobalValueMap", *index.GlobalValueMap);
328328
if (!io.outputting())
329329
CustomMappingTraits<GlobalValueSummaryMapTy>::fixAliaseeLinks(
330-
index.GlobalValueMap);
330+
*index.GlobalValueMap);
331331

332332
if (io.outputting()) {
333333
io.mapOptional("TypeIdMap", index.TypeIdMap);

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9166,7 +9166,7 @@ bool LLParser::parseTypeIdSummary(TypeIdSummary &TIS) {
91669166
}
91679167

91689168
static ValueInfo EmptyVI =
9169-
ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
9169+
ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8, nullptr);
91709170

91719171
/// TypeIdCompatibleVtableEntry
91729172
/// ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ','
@@ -10765,7 +10765,7 @@ bool LLParser::parseGVReference(ValueInfo &VI, unsigned &GVId) {
1076510765
VI = NumberedValueInfos[GVId];
1076610766
} else
1076710767
// We will create a forward reference to the stored location.
10768-
VI = ValueInfo(false, FwdVIRef);
10768+
VI = ValueInfo(false, FwdVIRef, nullptr);
1076910769

1077010770
if (ReadOnly)
1077110771
VI.setReadOnly();

0 commit comments

Comments
 (0)