| 1 | //=-- SampleProf.cpp - Sample profiling format support --------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains common definitions used in the reading and writing of |
| 10 | // sample profile data. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ProfileData/SampleProf.h" |
| 15 | #include "llvm/Config/llvm-config.h" |
| 16 | #include "llvm/IR/DebugInfoMetadata.h" |
| 17 | #include "llvm/IR/PseudoProbe.h" |
| 18 | #include "llvm/ProfileData/SampleProfReader.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/Compiler.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | #include "llvm/Support/LEB128.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <algorithm> |
| 26 | #include <cstdint> |
| 27 | #include <string> |
| 28 | #include <system_error> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | using namespace sampleprof; |
| 32 | |
| 33 | static cl::opt<uint64_t> ProfileSymbolListCutOff( |
| 34 | "profile-symbol-list-cutoff" , cl::Hidden, cl::init(Val: -1), |
| 35 | cl::desc("Cutoff value about how many symbols in profile symbol list " |
| 36 | "will be used. This is very useful for performance debugging" )); |
| 37 | |
| 38 | static cl::opt<bool> GenerateMergedBaseProfiles( |
| 39 | "generate-merged-base-profiles" , |
| 40 | cl::desc("When generating nested context-sensitive profiles, always " |
| 41 | "generate extra base profile for function with all its context " |
| 42 | "profiles merged into it." )); |
| 43 | |
| 44 | namespace llvm { |
| 45 | namespace sampleprof { |
| 46 | bool FunctionSamples::ProfileIsProbeBased = false; |
| 47 | bool FunctionSamples::ProfileIsCS = false; |
| 48 | bool FunctionSamples::ProfileIsPreInlined = false; |
| 49 | bool FunctionSamples::UseMD5 = false; |
| 50 | bool FunctionSamples::HasUniqSuffix = true; |
| 51 | bool FunctionSamples::ProfileIsFS = false; |
| 52 | |
| 53 | std::error_code |
| 54 | serializeTypeMap(const TypeCountMap &Map, |
| 55 | const MapVector<FunctionId, uint32_t> &NameTable, |
| 56 | raw_ostream &OS) { |
| 57 | encodeULEB128(Value: Map.size(), OS); |
| 58 | for (const auto &[TypeName, SampleCount] : Map) { |
| 59 | if (auto NameIndexIter = NameTable.find(Key: TypeName); |
| 60 | NameIndexIter != NameTable.end()) { |
| 61 | encodeULEB128(Value: NameIndexIter->second, OS); |
| 62 | } else { |
| 63 | // If the type is not in the name table, we cannot serialize it. |
| 64 | return sampleprof_error::truncated_name_table; |
| 65 | } |
| 66 | encodeULEB128(Value: SampleCount, OS); |
| 67 | } |
| 68 | return sampleprof_error::success; |
| 69 | } |
| 70 | } // namespace sampleprof |
| 71 | } // namespace llvm |
| 72 | |
| 73 | namespace { |
| 74 | |
| 75 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 76 | // will be removed once this transition is complete. Clients should prefer to |
| 77 | // deal with the Error value directly, rather than converting to error_code. |
| 78 | class SampleProfErrorCategoryType : public std::error_category { |
| 79 | const char *name() const noexcept override { return "llvm.sampleprof" ; } |
| 80 | |
| 81 | std::string message(int IE) const override { |
| 82 | sampleprof_error E = static_cast<sampleprof_error>(IE); |
| 83 | switch (E) { |
| 84 | case sampleprof_error::success: |
| 85 | return "Success" ; |
| 86 | case sampleprof_error::bad_magic: |
| 87 | return "Invalid sample profile data (bad magic)" ; |
| 88 | case sampleprof_error::unsupported_version: |
| 89 | return "Unsupported sample profile format version" ; |
| 90 | case sampleprof_error::too_large: |
| 91 | return "Too much profile data" ; |
| 92 | case sampleprof_error::truncated: |
| 93 | return "Truncated profile data" ; |
| 94 | case sampleprof_error::malformed: |
| 95 | return "Malformed sample profile data" ; |
| 96 | case sampleprof_error::unrecognized_format: |
| 97 | return "Unrecognized sample profile encoding format" ; |
| 98 | case sampleprof_error::unsupported_writing_format: |
| 99 | return "Profile encoding format unsupported for writing operations" ; |
| 100 | case sampleprof_error::truncated_name_table: |
| 101 | return "Truncated function name table" ; |
| 102 | case sampleprof_error::not_implemented: |
| 103 | return "Unimplemented feature" ; |
| 104 | case sampleprof_error::counter_overflow: |
| 105 | return "Counter overflow" ; |
| 106 | case sampleprof_error::ostream_seek_unsupported: |
| 107 | return "Ostream does not support seek" ; |
| 108 | case sampleprof_error::uncompress_failed: |
| 109 | return "Uncompress failure" ; |
| 110 | case sampleprof_error::zlib_unavailable: |
| 111 | return "Zlib is unavailable" ; |
| 112 | case sampleprof_error::hash_mismatch: |
| 113 | return "Function hash mismatch" ; |
| 114 | case sampleprof_error::illegal_line_offset: |
| 115 | return "Illegal line offset in sample profile data" ; |
| 116 | } |
| 117 | llvm_unreachable("A value of sampleprof_error has no message." ); |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | } // end anonymous namespace |
| 122 | |
| 123 | const std::error_category &llvm::sampleprof_category() { |
| 124 | static SampleProfErrorCategoryType ErrorCategory; |
| 125 | return ErrorCategory; |
| 126 | } |
| 127 | |
| 128 | void LineLocation::print(raw_ostream &OS) const { |
| 129 | OS << LineOffset; |
| 130 | if (Discriminator > 0) |
| 131 | OS << "." << Discriminator; |
| 132 | } |
| 133 | |
| 134 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 135 | const LineLocation &Loc) { |
| 136 | Loc.print(OS); |
| 137 | return OS; |
| 138 | } |
| 139 | |
| 140 | /// Merge the samples in \p Other into this record. |
| 141 | /// Optionally scale sample counts by \p Weight. |
| 142 | sampleprof_error SampleRecord::merge(const SampleRecord &Other, |
| 143 | uint64_t Weight) { |
| 144 | sampleprof_error Result; |
| 145 | Result = addSamples(S: Other.getSamples(), Weight); |
| 146 | for (const auto &I : Other.getCallTargets()) { |
| 147 | mergeSampleProfErrors(Accumulator&: Result, Result: addCalledTarget(F: I.first, S: I.second, Weight)); |
| 148 | } |
| 149 | return Result; |
| 150 | } |
| 151 | |
| 152 | std::error_code SampleRecord::serialize( |
| 153 | raw_ostream &OS, const MapVector<FunctionId, uint32_t> &NameTable) const { |
| 154 | encodeULEB128(Value: getSamples(), OS); |
| 155 | encodeULEB128(Value: getCallTargets().size(), OS); |
| 156 | for (const auto &J : getSortedCallTargets()) { |
| 157 | FunctionId Callee = J.first; |
| 158 | uint64_t CalleeSamples = J.second; |
| 159 | if (auto NameIndexIter = NameTable.find(Key: Callee); |
| 160 | NameIndexIter != NameTable.end()) { |
| 161 | encodeULEB128(Value: NameIndexIter->second, OS); |
| 162 | } else { |
| 163 | // If the callee is not in the name table, we cannot serialize it. |
| 164 | return sampleprof_error::truncated_name_table; |
| 165 | } |
| 166 | encodeULEB128(Value: CalleeSamples, OS); |
| 167 | } |
| 168 | return sampleprof_error::success; |
| 169 | } |
| 170 | |
| 171 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 172 | LLVM_DUMP_METHOD void LineLocation::dump() const { print(dbgs()); } |
| 173 | #endif |
| 174 | |
| 175 | void LineLocation::serialize(raw_ostream &OS) const { |
| 176 | encodeULEB128(Value: LineOffset, OS); |
| 177 | encodeULEB128(Value: Discriminator, OS); |
| 178 | } |
| 179 | |
| 180 | /// Print the sample record to the stream \p OS indented by \p Indent. |
| 181 | void SampleRecord::print(raw_ostream &OS, unsigned Indent) const { |
| 182 | OS << NumSamples; |
| 183 | if (hasCalls()) { |
| 184 | OS << ", calls:" ; |
| 185 | for (const auto &I : getSortedCallTargets()) |
| 186 | OS << " " << I.first << ":" << I.second; |
| 187 | } |
| 188 | OS << "\n" ; |
| 189 | } |
| 190 | |
| 191 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 192 | LLVM_DUMP_METHOD void SampleRecord::dump() const { print(dbgs(), 0); } |
| 193 | #endif |
| 194 | |
| 195 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 196 | const SampleRecord &Sample) { |
| 197 | Sample.print(OS, Indent: 0); |
| 198 | return OS; |
| 199 | } |
| 200 | |
| 201 | static void printTypeCountMap(raw_ostream &OS, LineLocation Loc, |
| 202 | const TypeCountMap &TypeCountMap) { |
| 203 | if (TypeCountMap.empty()) { |
| 204 | return; |
| 205 | } |
| 206 | OS << Loc << ": vtables: " ; |
| 207 | for (const auto &[Type, Count] : TypeCountMap) |
| 208 | OS << Type << ":" << Count << " " ; |
| 209 | OS << "\n" ; |
| 210 | } |
| 211 | |
| 212 | /// Print the samples collected for a function on stream \p OS. |
| 213 | void FunctionSamples::print(raw_ostream &OS, unsigned Indent) const { |
| 214 | if (getFunctionHash()) |
| 215 | OS << "CFG checksum " << getFunctionHash() << "\n" ; |
| 216 | |
| 217 | OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() |
| 218 | << " sampled lines\n" ; |
| 219 | |
| 220 | OS.indent(NumSpaces: Indent); |
| 221 | if (!BodySamples.empty()) { |
| 222 | OS << "Samples collected in the function's body {\n" ; |
| 223 | SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples); |
| 224 | for (const auto &SI : SortedBodySamples.get()) { |
| 225 | OS.indent(NumSpaces: Indent + 2); |
| 226 | const auto &Loc = SI->first; |
| 227 | OS << SI->first << ": " << SI->second; |
| 228 | if (const TypeCountMap *TypeCountMap = |
| 229 | this->findCallsiteTypeSamplesAt(Loc)) { |
| 230 | OS.indent(NumSpaces: Indent + 2); |
| 231 | printTypeCountMap(OS, Loc, TypeCountMap: *TypeCountMap); |
| 232 | } |
| 233 | } |
| 234 | OS.indent(NumSpaces: Indent); |
| 235 | OS << "}\n" ; |
| 236 | } else { |
| 237 | OS << "No samples collected in the function's body\n" ; |
| 238 | } |
| 239 | |
| 240 | OS.indent(NumSpaces: Indent); |
| 241 | if (!CallsiteSamples.empty()) { |
| 242 | OS << "Samples collected in inlined callsites {\n" ; |
| 243 | SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples( |
| 244 | CallsiteSamples); |
| 245 | for (const auto *Element : SortedCallsiteSamples.get()) { |
| 246 | // Element is a pointer to a pair of LineLocation and FunctionSamplesMap. |
| 247 | const auto &[Loc, FunctionSampleMap] = *Element; |
| 248 | for (const FunctionSamples &FuncSample : |
| 249 | llvm::make_second_range(c: FunctionSampleMap)) { |
| 250 | OS.indent(NumSpaces: Indent + 2); |
| 251 | OS << Loc << ": inlined callee: " << FuncSample.getFunction() << ": " ; |
| 252 | FuncSample.print(OS, Indent: Indent + 4); |
| 253 | } |
| 254 | auto TypeSamplesIter = VirtualCallsiteTypeCounts.find(x: Loc); |
| 255 | if (TypeSamplesIter != VirtualCallsiteTypeCounts.end()) { |
| 256 | OS.indent(NumSpaces: Indent + 2); |
| 257 | printTypeCountMap(OS, Loc, TypeCountMap: TypeSamplesIter->second); |
| 258 | } |
| 259 | } |
| 260 | OS.indent(NumSpaces: Indent); |
| 261 | OS << "}\n" ; |
| 262 | } else { |
| 263 | OS << "No inlined callsites in this function\n" ; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS, |
| 268 | const FunctionSamples &FS) { |
| 269 | FS.print(OS); |
| 270 | return OS; |
| 271 | } |
| 272 | |
| 273 | void sampleprof::sortFuncProfiles( |
| 274 | const SampleProfileMap &ProfileMap, |
| 275 | std::vector<NameFunctionSamples> &SortedProfiles) { |
| 276 | for (const auto &I : ProfileMap) { |
| 277 | SortedProfiles.push_back(x: std::make_pair(x: I.first, y: &I.second)); |
| 278 | } |
| 279 | llvm::stable_sort(Range&: SortedProfiles, C: [](const NameFunctionSamples &A, |
| 280 | const NameFunctionSamples &B) { |
| 281 | if (A.second->getTotalSamples() == B.second->getTotalSamples()) |
| 282 | return A.second->getContext() < B.second->getContext(); |
| 283 | return A.second->getTotalSamples() > B.second->getTotalSamples(); |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | unsigned FunctionSamples::getOffset(const DILocation *DIL) { |
| 288 | return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) & |
| 289 | 0xffff; |
| 290 | } |
| 291 | |
| 292 | LineLocation FunctionSamples::getCallSiteIdentifier(const DILocation *DIL, |
| 293 | bool ProfileIsFS) { |
| 294 | if (FunctionSamples::ProfileIsProbeBased) { |
| 295 | // In a pseudo-probe based profile, a callsite is simply represented by the |
| 296 | // ID of the probe associated with the call instruction. The probe ID is |
| 297 | // encoded in the Discriminator field of the call instruction's debug |
| 298 | // metadata. |
| 299 | return LineLocation(PseudoProbeDwarfDiscriminator::extractProbeIndex( |
| 300 | Value: DIL->getDiscriminator()), |
| 301 | 0); |
| 302 | } else { |
| 303 | unsigned Discriminator = |
| 304 | ProfileIsFS ? DIL->getDiscriminator() : DIL->getBaseDiscriminator(); |
| 305 | return LineLocation(FunctionSamples::getOffset(DIL), Discriminator); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | const FunctionSamples *FunctionSamples::findFunctionSamples( |
| 310 | const DILocation *DIL, SampleProfileReaderItaniumRemapper *Remapper, |
| 311 | const HashKeyMap<std::unordered_map, FunctionId, FunctionId> |
| 312 | *FuncNameToProfNameMap) const { |
| 313 | assert(DIL); |
| 314 | SmallVector<std::pair<LineLocation, StringRef>, 10> S; |
| 315 | |
| 316 | const DILocation *PrevDIL = DIL; |
| 317 | for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) { |
| 318 | // Use C++ linkage name if possible. |
| 319 | StringRef Name = PrevDIL->getScope()->getSubprogram()->getLinkageName(); |
| 320 | if (Name.empty()) |
| 321 | Name = PrevDIL->getScope()->getSubprogram()->getName(); |
| 322 | S.emplace_back(Args: FunctionSamples::getCallSiteIdentifier( |
| 323 | DIL, ProfileIsFS: FunctionSamples::ProfileIsFS), |
| 324 | Args&: Name); |
| 325 | PrevDIL = DIL; |
| 326 | } |
| 327 | |
| 328 | if (S.size() == 0) |
| 329 | return this; |
| 330 | const FunctionSamples *FS = this; |
| 331 | for (int i = S.size() - 1; i >= 0 && FS != nullptr; i--) { |
| 332 | FS = FS->findFunctionSamplesAt(Loc: S[i].first, CalleeName: S[i].second, Remapper, |
| 333 | FuncNameToProfNameMap); |
| 334 | } |
| 335 | return FS; |
| 336 | } |
| 337 | |
| 338 | void FunctionSamples::findAllNames(DenseSet<FunctionId> &NameSet) const { |
| 339 | NameSet.insert(V: getFunction()); |
| 340 | for (const auto &BS : BodySamples) |
| 341 | NameSet.insert_range(R: llvm::make_first_range(c: BS.second.getCallTargets())); |
| 342 | |
| 343 | for (const auto &CS : CallsiteSamples) { |
| 344 | for (const auto &NameFS : CS.second) { |
| 345 | NameSet.insert(V: NameFS.first); |
| 346 | NameFS.second.findAllNames(NameSet); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | const FunctionSamples *FunctionSamples::findFunctionSamplesAt( |
| 352 | const LineLocation &Loc, StringRef CalleeName, |
| 353 | SampleProfileReaderItaniumRemapper *Remapper, |
| 354 | const HashKeyMap<std::unordered_map, FunctionId, FunctionId> |
| 355 | *FuncNameToProfNameMap) const { |
| 356 | CalleeName = getCanonicalFnName(FnName: CalleeName); |
| 357 | |
| 358 | auto I = CallsiteSamples.find(x: mapIRLocToProfileLoc(IRLoc: Loc)); |
| 359 | if (I == CallsiteSamples.end()) |
| 360 | return nullptr; |
| 361 | auto FS = I->second.find(x: getRepInFormat(Name: CalleeName)); |
| 362 | if (FS != I->second.end()) |
| 363 | return &FS->second; |
| 364 | |
| 365 | if (FuncNameToProfNameMap && !FuncNameToProfNameMap->empty()) { |
| 366 | auto R = FuncNameToProfNameMap->find(Key: FunctionId(CalleeName)); |
| 367 | if (R != FuncNameToProfNameMap->end()) { |
| 368 | CalleeName = R->second.stringRef(); |
| 369 | auto FS = I->second.find(x: getRepInFormat(Name: CalleeName)); |
| 370 | if (FS != I->second.end()) |
| 371 | return &FS->second; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (Remapper) { |
| 376 | if (auto NameInProfile = Remapper->lookUpNameInProfile(FunctionName: CalleeName)) { |
| 377 | auto FS = I->second.find(x: getRepInFormat(Name: *NameInProfile)); |
| 378 | if (FS != I->second.end()) |
| 379 | return &FS->second; |
| 380 | } |
| 381 | } |
| 382 | // If we cannot find exact match of the callee name, return the FS with |
| 383 | // the max total count. Only do this when CalleeName is not provided, |
| 384 | // i.e., only for indirect calls. |
| 385 | if (!CalleeName.empty()) |
| 386 | return nullptr; |
| 387 | uint64_t MaxTotalSamples = 0; |
| 388 | const FunctionSamples *R = nullptr; |
| 389 | for (const auto &NameFS : I->second) |
| 390 | if (NameFS.second.getTotalSamples() >= MaxTotalSamples) { |
| 391 | MaxTotalSamples = NameFS.second.getTotalSamples(); |
| 392 | R = &NameFS.second; |
| 393 | } |
| 394 | return R; |
| 395 | } |
| 396 | |
| 397 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 398 | LLVM_DUMP_METHOD void FunctionSamples::dump() const { print(dbgs(), 0); } |
| 399 | #endif |
| 400 | |
| 401 | std::error_code ProfileSymbolList::read(const uint8_t *Data, |
| 402 | uint64_t ListSize) { |
| 403 | // Scan forward to see how many elements we expect. |
| 404 | reserve(Size: std::min<uint64_t>(a: ProfileSymbolListCutOff, |
| 405 | b: std::count(first: Data, last: Data + ListSize, value: 0))); |
| 406 | |
| 407 | const char *ListStart = reinterpret_cast<const char *>(Data); |
| 408 | uint64_t Size = 0; |
| 409 | uint64_t StrNum = 0; |
| 410 | while (Size < ListSize && StrNum < ProfileSymbolListCutOff) { |
| 411 | StringRef Str(ListStart + Size); |
| 412 | add(Name: Str); |
| 413 | Size += Str.size() + 1; |
| 414 | StrNum++; |
| 415 | } |
| 416 | if (Size != ListSize && StrNum != ProfileSymbolListCutOff) |
| 417 | return sampleprof_error::malformed; |
| 418 | return sampleprof_error::success; |
| 419 | } |
| 420 | |
| 421 | void SampleContextTrimmer::trimAndMergeColdContextProfiles( |
| 422 | uint64_t ColdCountThreshold, bool TrimColdContext, bool MergeColdContext, |
| 423 | uint32_t ColdContextFrameLength, bool TrimBaseProfileOnly) { |
| 424 | if (!TrimColdContext && !MergeColdContext) |
| 425 | return; |
| 426 | |
| 427 | // Nothing to merge if sample threshold is zero |
| 428 | if (ColdCountThreshold == 0) |
| 429 | return; |
| 430 | |
| 431 | // Trimming base profiles only is mainly to honor the preinliner decsion. When |
| 432 | // MergeColdContext is true preinliner decsion is not honored anyway so turn |
| 433 | // off TrimBaseProfileOnly. |
| 434 | if (MergeColdContext) |
| 435 | TrimBaseProfileOnly = false; |
| 436 | |
| 437 | // Filter the cold profiles from ProfileMap and move them into a tmp |
| 438 | // container |
| 439 | std::vector<std::pair<hash_code, const FunctionSamples *>> ColdProfiles; |
| 440 | for (const auto &I : ProfileMap) { |
| 441 | const SampleContext &Context = I.second.getContext(); |
| 442 | const FunctionSamples &FunctionProfile = I.second; |
| 443 | if (FunctionProfile.getTotalSamples() < ColdCountThreshold && |
| 444 | (!TrimBaseProfileOnly || Context.isBaseContext())) |
| 445 | ColdProfiles.emplace_back(args: I.first, args: &I.second); |
| 446 | } |
| 447 | |
| 448 | // Remove the cold profile from ProfileMap and merge them into |
| 449 | // MergedProfileMap by the last K frames of context |
| 450 | SampleProfileMap MergedProfileMap; |
| 451 | for (const auto &I : ColdProfiles) { |
| 452 | if (MergeColdContext) { |
| 453 | auto MergedContext = I.second->getContext().getContextFrames(); |
| 454 | if (ColdContextFrameLength < MergedContext.size()) |
| 455 | MergedContext = MergedContext.take_back(N: ColdContextFrameLength); |
| 456 | // Need to set MergedProfile's context here otherwise it will be lost. |
| 457 | FunctionSamples &MergedProfile = MergedProfileMap.create(Ctx: MergedContext); |
| 458 | MergedProfile.merge(Other: *I.second); |
| 459 | } |
| 460 | ProfileMap.erase(Key: I.first); |
| 461 | } |
| 462 | |
| 463 | // Move the merged profiles into ProfileMap; |
| 464 | for (const auto &I : MergedProfileMap) { |
| 465 | // Filter the cold merged profile |
| 466 | if (TrimColdContext && I.second.getTotalSamples() < ColdCountThreshold && |
| 467 | ProfileMap.find(Ctx: I.second.getContext()) == ProfileMap.end()) |
| 468 | continue; |
| 469 | // Merge the profile if the original profile exists, otherwise just insert |
| 470 | // as a new profile. If inserted as a new profile from MergedProfileMap, it |
| 471 | // already has the right context. |
| 472 | auto Ret = ProfileMap.emplace(Args&: I.second.getContext(), Args: FunctionSamples()); |
| 473 | FunctionSamples &OrigProfile = Ret.first->second; |
| 474 | OrigProfile.merge(Other: I.second); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | std::error_code ProfileSymbolList::write(raw_ostream &OS) { |
| 479 | // Sort the symbols before output. If doing compression. |
| 480 | // It will make the compression much more effective. |
| 481 | std::vector<StringRef> SortedList(Syms.begin(), Syms.end()); |
| 482 | llvm::sort(C&: SortedList); |
| 483 | |
| 484 | std::string OutputString; |
| 485 | for (auto &Sym : SortedList) { |
| 486 | OutputString.append(str: Sym.str()); |
| 487 | OutputString.append(n: 1, c: '\0'); |
| 488 | } |
| 489 | |
| 490 | OS << OutputString; |
| 491 | return sampleprof_error::success; |
| 492 | } |
| 493 | |
| 494 | void ProfileSymbolList::dump(raw_ostream &OS) const { |
| 495 | OS << "======== Dump profile symbol list ========\n" ; |
| 496 | std::vector<StringRef> SortedList(Syms.begin(), Syms.end()); |
| 497 | llvm::sort(C&: SortedList); |
| 498 | |
| 499 | for (auto &Sym : SortedList) |
| 500 | OS << Sym << "\n" ; |
| 501 | } |
| 502 | |
| 503 | ProfileConverter::FrameNode * |
| 504 | ProfileConverter::FrameNode::getOrCreateChildFrame(const LineLocation &CallSite, |
| 505 | FunctionId CalleeName) { |
| 506 | uint64_t Hash = FunctionSamples::getCallSiteHash(Callee: CalleeName, Callsite: CallSite); |
| 507 | auto It = AllChildFrames.find(x: Hash); |
| 508 | if (It != AllChildFrames.end()) { |
| 509 | assert(It->second.FuncName == CalleeName && |
| 510 | "Hash collision for child context node" ); |
| 511 | return &It->second; |
| 512 | } |
| 513 | |
| 514 | AllChildFrames[Hash] = FrameNode(CalleeName, nullptr, CallSite); |
| 515 | return &AllChildFrames[Hash]; |
| 516 | } |
| 517 | |
| 518 | ProfileConverter::ProfileConverter(SampleProfileMap &Profiles) |
| 519 | : ProfileMap(Profiles) { |
| 520 | for (auto &FuncSample : Profiles) { |
| 521 | FunctionSamples *FSamples = &FuncSample.second; |
| 522 | auto *NewNode = getOrCreateContextPath(Context: FSamples->getContext()); |
| 523 | assert(!NewNode->FuncSamples && "New node cannot have sample profile" ); |
| 524 | NewNode->FuncSamples = FSamples; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | ProfileConverter::FrameNode * |
| 529 | ProfileConverter::getOrCreateContextPath(const SampleContext &Context) { |
| 530 | auto Node = &RootFrame; |
| 531 | LineLocation CallSiteLoc(0, 0); |
| 532 | for (auto &Callsite : Context.getContextFrames()) { |
| 533 | Node = Node->getOrCreateChildFrame(CallSite: CallSiteLoc, CalleeName: Callsite.Func); |
| 534 | CallSiteLoc = Callsite.Location; |
| 535 | } |
| 536 | return Node; |
| 537 | } |
| 538 | |
| 539 | void ProfileConverter::convertCSProfiles(ProfileConverter::FrameNode &Node) { |
| 540 | // Process each child profile. Add each child profile to callsite profile map |
| 541 | // of the current node `Node` if `Node` comes with a profile. Otherwise |
| 542 | // promote the child profile to a standalone profile. |
| 543 | auto *NodeProfile = Node.FuncSamples; |
| 544 | for (auto &It : Node.AllChildFrames) { |
| 545 | auto &ChildNode = It.second; |
| 546 | convertCSProfiles(Node&: ChildNode); |
| 547 | auto *ChildProfile = ChildNode.FuncSamples; |
| 548 | if (!ChildProfile) |
| 549 | continue; |
| 550 | SampleContext OrigChildContext = ChildProfile->getContext(); |
| 551 | uint64_t OrigChildContextHash = OrigChildContext.getHashCode(); |
| 552 | // Reset the child context to be contextless. |
| 553 | ChildProfile->getContext().setFunction(OrigChildContext.getFunction()); |
| 554 | if (NodeProfile) { |
| 555 | // Add child profile to the callsite profile map. |
| 556 | auto &SamplesMap = NodeProfile->functionSamplesAt(Loc: ChildNode.CallSiteLoc); |
| 557 | SamplesMap.emplace(args: OrigChildContext.getFunction(), args&: *ChildProfile); |
| 558 | NodeProfile->addTotalSamples(Num: ChildProfile->getTotalSamples()); |
| 559 | // Remove the corresponding body sample for the callsite and update the |
| 560 | // total weight. |
| 561 | auto Count = NodeProfile->removeCalledTargetAndBodySample( |
| 562 | LineOffset: ChildNode.CallSiteLoc.LineOffset, Discriminator: ChildNode.CallSiteLoc.Discriminator, |
| 563 | Func: OrigChildContext.getFunction()); |
| 564 | NodeProfile->removeTotalSamples(Num: Count); |
| 565 | } |
| 566 | |
| 567 | uint64_t NewChildProfileHash = 0; |
| 568 | // Separate child profile to be a standalone profile, if the current parent |
| 569 | // profile doesn't exist. This is a duplicating operation when the child |
| 570 | // profile is already incorporated into the parent which is still useful and |
| 571 | // thus done optionally. It is seen that duplicating context profiles into |
| 572 | // base profiles improves the code quality for thinlto build by allowing a |
| 573 | // profile in the prelink phase for to-be-fully-inlined functions. |
| 574 | if (!NodeProfile) { |
| 575 | ProfileMap[ChildProfile->getContext()].merge(Other: *ChildProfile); |
| 576 | NewChildProfileHash = ChildProfile->getContext().getHashCode(); |
| 577 | } else if (GenerateMergedBaseProfiles) { |
| 578 | ProfileMap[ChildProfile->getContext()].merge(Other: *ChildProfile); |
| 579 | NewChildProfileHash = ChildProfile->getContext().getHashCode(); |
| 580 | auto &SamplesMap = NodeProfile->functionSamplesAt(Loc: ChildNode.CallSiteLoc); |
| 581 | SamplesMap[ChildProfile->getFunction()].getContext().setAttribute( |
| 582 | ContextDuplicatedIntoBase); |
| 583 | } |
| 584 | |
| 585 | // Remove the original child profile. Check if MD5 of new child profile |
| 586 | // collides with old profile, in this case the [] operator already |
| 587 | // overwritten it without the need of erase. |
| 588 | if (NewChildProfileHash != OrigChildContextHash) |
| 589 | ProfileMap.erase(Key: OrigChildContextHash); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void ProfileConverter::convertCSProfiles() { convertCSProfiles(Node&: RootFrame); } |
| 594 | |