LLVM: lib/ProfileData/DataAccessProf.cpp Source File (original) (raw)

10

11namespace llvm {

13

14

15

16

17static std::pair<StringRef, uint64_t>

20 auto [Iter, Inserted] = Map.try_emplace(Saver.save(Str), Map.size());

21 return *Iter;

22}

23

24

31

32std::optional

34 auto Key = SymbolID;

35 if (std::holds_alternative(SymbolID)) {

36 auto NameOrErr = getCanonicalName(std::get(SymbolID));

37

38 if (!NameOrErr) {

40 std::get(SymbolID).empty() &&

41 "Name canonicalization only fails when stringified string is empty.");

42 return std::nullopt;

43 }

44 Key = *NameOrErr;

45 }

46

47 auto It = Records.find(Key);

48 if (It != Records.end()) {

50 It->second.Locations);

51 }

52

53 return std::nullopt;

54}

55

57 if (std::holds_alternative<uint64_t>(SymID))

58 return KnownColdHashes.contains(std::get<uint64_t>(SymID));

59 return KnownColdSymbols.contains(std::get(SymID));

60}

61

65 const bool IsStringLiteral = std::holds_alternative<uint64_t>(Symbol);

67 if (IsStringLiteral) {

68 RecordID = std::get<uint64_t>(Symbol);

69 Key = RecordID;

70 } else {

71 auto CanonicalName = getCanonicalName(std::get(Symbol));

72 if (!CanonicalName)

73 return CanonicalName.takeError();

74 std::tie(Key, RecordID) =

76 }

77

78 auto [Iter, Inserted] =

79 Records.try_emplace(Key, RecordID, AccessCount, IsStringLiteral);

80 if (!Inserted)

82 "User of DataAccessProfData should "

83 "aggregate count for the same symbol. ",

85

87}

88

93 return E;

94

95 auto &Record = Records.back().second;

96 for (const auto &Location : Locations)

97 Record.Locations.push_back(

98 {saveStringToMap(StrToIndexMap, Saver, Location.FileName).first,

99 Location.Line});

100

102}

103

106 if (std::holds_alternative<uint64_t>(SymbolID)) {

107 KnownColdHashes.insert(std::get<uint64_t>(SymbolID));

109 }

110 auto CanonicalName = getCanonicalName(std::get(SymbolID));

111 if (!CanonicalName)

112 return CanonicalName.takeError();

113 KnownColdSymbols.insert(

114 saveStringToMap(StrToIndexMap, Saver, *CanonicalName).first);

116}

117

121 uint64_t NumColdKnownSymbols =

123 if (Error E = deserializeSymbolsAndFilenames(Ptr, NumSampledSymbols,

124 NumColdKnownSymbols))

125 return E;

126

130 KnownColdHashes.insert(

132

133 return deserializeRecords(Ptr);

134}

135

136Error DataAccessProfData::serializeSymbolsAndFilenames(ProfOStream &OS) const {

138 OS.write(KnownColdSymbols.size());

139

140 std::vectorstd::string Strs;

141 Strs.reserve(StrToIndexMap.size() + KnownColdSymbols.size());

142 for (const auto &Str : StrToIndexMap)

143 Strs.push_back(Str.first.str());

144 for (const auto &Str : KnownColdSymbols)

145 Strs.push_back(Str.str());

146

147 std::string CompressedStrings;

148 if (!Strs.empty())

151 return E;

152 const uint64_t CompressedStringLen = CompressedStrings.length();

153

154 OS.write(CompressedStringLen);

155

156 for (char C : CompressedStrings)

158

159

160 const uint64_t PaddedLength = alignTo(CompressedStringLen, 8);

161 for (uint64_t K = CompressedStringLen; K < PaddedLength; K++)

164}

165

167DataAccessProfData::getEncodedIndex(const SymbolHandleRef SymbolID) const {

168 if (std::holds_alternative<uint64_t>(SymbolID))

169 return std::get<uint64_t>(SymbolID);

170

171 auto Iter = StrToIndexMap.find(std::get(SymbolID));

172 assert(Iter != StrToIndexMap.end() &&

173 "String literals not found in StrToIndexMap");

174 return Iter->second;

175}

176

178 if (Error E = serializeSymbolsAndFilenames(OS))

179 return E;

180 OS.write(KnownColdHashes.size());

181 for (const auto &Hash : KnownColdHashes)

184 for (const auto &[Key, Rec] : Records) {

185 OS.write(getEncodedIndex(Rec.SymbolID));

186 OS.writeByte(Rec.IsStringLiteral);

187 OS.write(Rec.AccessCount);

188 OS.write(Rec.Locations.size());

189 for (const auto &Loc : Rec.Locations) {

190 OS.write(getEncodedIndex(Loc.FileName));

192 }

193 }

195}

196

197Error DataAccessProfData::deserializeSymbolsAndFilenames(

198 const unsigned char *&Ptr, const uint64_t NumSampledSymbols,

199 const uint64_t NumColdKnownSymbols) {

202

203

204

207 if (StringCnt < NumSampledSymbols)

209 else

210 KnownColdSymbols.insert(Saver.save(Name));

211 ++StringCnt;

213 };

216 return E;

217

220}

221

222Error DataAccessProfData::deserializeRecords(const unsigned char *&Ptr) {

225

228

229 for (uint64_t I = 0; I < NumRecords; ++I) {

232

233 bool IsStringLiteral =

235

238

240 if (IsStringLiteral)

241 SymbolID = ID;

242 else

243 SymbolID = Strings[ID];

245 return E;

246

247 auto &Record = Records.back().second;

248

251

252 Record.Locations.reserve(NumLocations);

253 for (uint64_t J = 0; J < NumLocations; ++J) {

258 Record.Locations.push_back({Strings[FileNameIndex], Line});

259 }

260 }

262}

263}

264}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

Lightweight error class with error context and mandatory checking.

static ErrorSuccess success()

Create a success value.

Tagged union holding either a T or a Error.

static LLVM_ABI StringRef getCanonicalName(StringRef PGOName)

LLVM_ABI void writeByte(uint8_t V)

LLVM_ABI void write32(uint32_t V)

LLVM_ABI void write(uint64_t V)

StringRef - Represent a constant reference to a string, i.e.

Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.

StringRef save(const char *S)

LLVM_ABI Error addKnownSymbolWithoutSamples(SymbolHandleRef SymbolID)

Add a symbol that's seen in the profiled binary without samples.

Definition DataAccessProf.cpp:104

ArrayRef< StringToIndexMap::value_type > getStrToIndexMapRef() const

The following methods return array reference for various internal data structures.

LLVM_ABI Error serialize(ProfOStream &OS) const

Serialize profile data to the output stream.

Definition DataAccessProf.cpp:177

LLVM_ABI std::optional< DataAccessProfRecord > getProfileRecord(const SymbolHandleRef SymID) const

Returns a profile record for SymbolID, or std::nullopt if there isn't a record.

Definition DataAccessProf.cpp:33

LLVM_ABI Error deserialize(const unsigned char *&Ptr)

Deserialize this class from the given buffer.

Definition DataAccessProf.cpp:118

LLVM_ABI bool isKnownColdSymbol(const SymbolHandleRef SymID) const

Returns true if SymID is seen in profiled binaries and cold.

Definition DataAccessProf.cpp:56

LLVM_ABI Error setDataAccessProfile(SymbolHandleRef SymbolID, uint64_t AccessCount)

Methods to set symbolized data access profile.

Definition DataAccessProf.cpp:62

llvm::MapVector< StringRef, uint64_t > StringToIndexMap

unsigned ID

LLVM IR allows to use arbitrary numbers as calling convention identifiers.

@ C

The default llvm calling convention, compatible with C.

LLVM_ABI bool isAvailable()

static Expected< StringRef > getCanonicalName(StringRef Name)

Definition DataAccessProf.cpp:25

static std::pair< StringRef, uint64_t > saveStringToMap(DataAccessProfData::StringToIndexMap &Map, llvm::UniqueStringSaver &Saver, StringRef Str)

Definition DataAccessProf.cpp:18

std::variant< StringRef, uint64_t > SymbolHandleRef

value_type readNext(const CharT *&memory, endianness endian)

Read a value of a particular endianness from a buffer, and increment the buffer past that value.

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI Error readAndDecodeStrings(StringRef NameStrings, std::function< Error(StringRef)> NameCallback)

NameStrings is a string composed of one or more possibly encoded sub-strings.

auto make_first_range(ContainerTy &&c)

Given a container of pairs, return a range over the first elements.

SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)

Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...

class LLVM_GSL_OWNER SmallVector

Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...

LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key

Error make_error(ArgTs &&... Args)

Make a Error instance representing failure using the given error info type.

uint64_t alignTo(uint64_t Size, Align A)

Returns a multiple of A needed to store Size bytes.

LLVM_ABI Error collectGlobalObjectNameStrings(ArrayRef< std::string > NameStrs, bool doCompression, std::string &Result)

Given a vector of strings (names of global objects like functions or, virtual tables) NameStrs,...

The data access profiles for a symbol.