| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifndef LLVM_PROFILEDATA_SAMPLEPROF_H |
| 15 |
#define LLVM_PROFILEDATA_SAMPLEPROF_H |
| 16 |
|
| 17 |
#include "llvm/ADT/DenseSet.h" |
| 18 |
#include "llvm/ADT/SmallVector.h" |
| 19 |
#include "llvm/ADT/StringMap.h" |
| 20 |
#include "llvm/ADT/StringRef.h" |
| 21 |
#include "llvm/ADT/StringSet.h" |
| 22 |
#include "llvm/IR/Function.h" |
| 23 |
#include "llvm/IR/GlobalValue.h" |
| 24 |
#include "llvm/IR/Module.h" |
| 25 |
#include "llvm/Support/Allocator.h" |
| 26 |
#include "llvm/Support/Debug.h" |
| 27 |
#include "llvm/Support/ErrorOr.h" |
| 28 |
#include "llvm/Support/MathExtras.h" |
| 29 |
#include "llvm/Support/raw_ostream.h" |
| 30 |
#include <algorithm> |
| 31 |
#include <cstdint> |
| 32 |
#include <list> |
| 33 |
#include <map> |
| 34 |
#include <set> |
| 35 |
#include <sstream> |
| 36 |
#include <string> |
| 37 |
#include <system_error> |
| 38 |
#include <unordered_map> |
| 39 |
#include <utility> |
| 40 |
|
| 41 |
namespace llvm { |
| 42 |
|
| 43 |
const std::error_category &sampleprof_category(); |
| 44 |
|
| 45 |
enum class sampleprof_error { |
| 46 |
success = 0, |
| 47 |
bad_magic, |
| 48 |
unsupported_version, |
| 49 |
too_large, |
| 50 |
truncated, |
| 51 |
malformed, |
| 52 |
unrecognized_format, |
| 53 |
unsupported_writing_format, |
| 54 |
truncated_name_table, |
| 55 |
not_implemented, |
| 56 |
counter_overflow, |
| 57 |
ostream_seek_unsupported, |
| 58 |
compress_failed, |
| 59 |
uncompress_failed, |
| 60 |
zlib_unavailable, |
| 61 |
hash_mismatch |
| 62 |
}; |
| 63 |
|
| 64 |
inline std::error_code make_error_code(sampleprof_error E) { |
| 65 |
return std::error_code(static_cast<int>(E), sampleprof_category()); |
| 66 |
} |
| 67 |
|
| 68 |
inline sampleprof_error MergeResult(sampleprof_error &Accumulator, |
| 69 |
sampleprof_error Result) { |
| 70 |
|
| 71 |
|
| 72 |
if (Accumulator == sampleprof_error::success && |
| 73 |
Result != sampleprof_error::success) |
| 74 |
Accumulator = Result; |
| 75 |
return Accumulator; |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
namespace std { |
| 81 |
|
| 82 |
template <> |
| 83 |
struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {}; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
namespace llvm { |
| 88 |
namespace sampleprof { |
| 89 |
|
| 90 |
enum SampleProfileFormat { |
| 91 |
SPF_None = 0, |
| 92 |
SPF_Text = 0x1, |
| 93 |
SPF_Compact_Binary = 0x2, |
| 94 |
SPF_GCC = 0x3, |
| 95 |
SPF_Ext_Binary = 0x4, |
| 96 |
SPF_Binary = 0xff |
| 97 |
}; |
| 98 |
|
| 99 |
static inline uint64_t SPMagic(SampleProfileFormat Format = SPF_Binary) { |
| 100 |
return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) | |
| 101 |
uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) | |
| 102 |
uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) | |
| 103 |
uint64_t('2') << (64 - 56) | uint64_t(Format); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static inline StringRef getRepInFormat(StringRef Name, bool UseMD5, |
| 109 |
std::string &GUIDBuf) { |
| 110 |
if (Name.empty() || !UseMD5) |
| 111 |
return Name; |
| 112 |
GUIDBuf = std::to_string(Function::getGUID(Name)); |
| 113 |
return GUIDBuf; |
| 114 |
} |
| 115 |
|
| 116 |
static inline uint64_t SPVersion() { return 103; } |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
enum SecType { |
| 122 |
SecInValid = 0, |
| 123 |
SecProfSummary = 1, |
| 124 |
SecNameTable = 2, |
| 125 |
SecProfileSymbolList = 3, |
| 126 |
SecFuncOffsetTable = 4, |
| 127 |
SecFuncMetadata = 5, |
| 128 |
SecCSNameTable = 6, |
| 129 |
|
| 130 |
SecFuncProfileFirst = 32, |
| 131 |
SecLBRProfile = SecFuncProfileFirst |
| 132 |
}; |
| 133 |
|
| 134 |
static inline std::string getSecName(SecType Type) { |
| 135 |
switch ((int)Type) { |
| 136 |
case SecInValid: |
| 137 |
return "InvalidSection"; |
| 138 |
case SecProfSummary: |
| 139 |
return "ProfileSummarySection"; |
| 140 |
case SecNameTable: |
| 141 |
return "NameTableSection"; |
| 142 |
case SecProfileSymbolList: |
| 143 |
return "ProfileSymbolListSection"; |
| 144 |
case SecFuncOffsetTable: |
| 145 |
return "FuncOffsetTableSection"; |
| 146 |
case SecFuncMetadata: |
| 147 |
return "FunctionMetadata"; |
| 148 |
case SecCSNameTable: |
| 149 |
return "CSNameTableSection"; |
| 150 |
case SecLBRProfile: |
| 151 |
return "LBRProfileSection"; |
| 152 |
default: |
| 153 |
return "UnknownSection"; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
struct SecHdrTableEntry { |
| 160 |
SecType Type; |
| 161 |
uint64_t Flags; |
| 162 |
uint64_t Offset; |
| 163 |
uint64_t Size; |
| 164 |
|
| 165 |
|
| 166 |
uint32_t LayoutIndex; |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
enum class SecCommonFlags : uint32_t { |
| 173 |
SecFlagInValid = 0, |
| 174 |
SecFlagCompress = (1 << 0), |
| 175 |
|
| 176 |
SecFlagFlat = (1 << 1) |
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
enum class SecNameTableFlags : uint32_t { |
| 183 |
SecFlagInValid = 0, |
| 184 |
SecFlagMD5Name = (1 << 0), |
| 185 |
|
| 186 |
|
| 187 |
SecFlagFixedLengthMD5 = (1 << 1), |
| 188 |
|
| 189 |
|
| 190 |
SecFlagUniqSuffix = (1 << 2) |
| 191 |
}; |
| 192 |
enum class SecProfSummaryFlags : uint32_t { |
| 193 |
SecFlagInValid = 0, |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
SecFlagPartial = (1 << 0), |
| 198 |
|
| 199 |
|
| 200 |
SecFlagFullContext = (1 << 1), |
| 201 |
|
| 202 |
|
| 203 |
SecFlagFSDiscriminator = (1 << 2), |
| 204 |
|
| 205 |
|
| 206 |
SecFlagIsCSNested = (1 << 4), |
| 207 |
}; |
| 208 |
|
| 209 |
enum class SecFuncMetadataFlags : uint32_t { |
| 210 |
SecFlagInvalid = 0, |
| 211 |
SecFlagIsProbeBased = (1 << 0), |
| 212 |
SecFlagHasAttribute = (1 << 1), |
| 213 |
}; |
| 214 |
|
| 215 |
enum class SecFuncOffsetFlags : uint32_t { |
| 216 |
SecFlagInvalid = 0, |
| 217 |
|
| 218 |
|
| 219 |
SecFlagOrdered = (1 << 0), |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
template <class SecFlagType> |
| 224 |
static inline void verifySecFlag(SecType Type, SecFlagType Flag) { |
| 225 |
|
| 226 |
if (std::is_same<SecCommonFlags, SecFlagType>()) |
| 227 |
return; |
| 228 |
|
| 229 |
|
| 230 |
bool IsFlagLegal = false; |
| 231 |
switch (Type) { |
| 232 |
case SecNameTable: |
| 233 |
IsFlagLegal = std::is_same<SecNameTableFlags, SecFlagType>(); |
| 234 |
break; |
| 235 |
case SecProfSummary: |
| 236 |
IsFlagLegal = std::is_same<SecProfSummaryFlags, SecFlagType>(); |
| 237 |
break; |
| 238 |
case SecFuncMetadata: |
| 239 |
IsFlagLegal = std::is_same<SecFuncMetadataFlags, SecFlagType>(); |
| 240 |
break; |
| 241 |
default: |
| 242 |
case SecFuncOffsetTable: |
| 243 |
IsFlagLegal = std::is_same<SecFuncOffsetFlags, SecFlagType>(); |
| 244 |
break; |
| 245 |
} |
| 246 |
if (!IsFlagLegal) |
| 247 |
llvm_unreachable("Misuse of a flag in an incompatible section"); |
| 248 |
} |
| 249 |
|
| 250 |
template <class SecFlagType> |
| 251 |
static inline void addSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag) { |
| 252 |
verifySecFlag(Entry.Type, Flag); |
| 253 |
auto FVal = static_cast<uint64_t>(Flag); |
| 254 |
bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>(); |
| 255 |
Entry.Flags |= IsCommon ? FVal : (FVal << 32); |
| 256 |
} |
| 257 |
|
| 258 |
template <class SecFlagType> |
| 259 |
static inline void removeSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag) { |
| 260 |
verifySecFlag(Entry.Type, Flag); |
| 261 |
auto FVal = static_cast<uint64_t>(Flag); |
| 262 |
bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>(); |
| 263 |
Entry.Flags &= ~(IsCommon ? FVal : (FVal << 32)); |
| 264 |
} |
| 265 |
|
| 266 |
template <class SecFlagType> |
| 267 |
static inline bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag) { |
| 268 |
verifySecFlag(Entry.Type, Flag); |
| 269 |
auto FVal = static_cast<uint64_t>(Flag); |
| 270 |
bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>(); |
| 271 |
return Entry.Flags & (IsCommon ? FVal : (FVal << 32)); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
struct LineLocation { |
| 284 |
LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {} |
| 285 |
|
| 286 |
void print(raw_ostream &OS) const; |
| 287 |
void dump() const; |
| 288 |
|
| 289 |
bool operator<(const LineLocation &O) const { |
| 290 |
return LineOffset < O.LineOffset || |
| 291 |
(LineOffset == O.LineOffset && Discriminator < O.Discriminator); |
| 292 |
} |
| 293 |
|
| 294 |
bool operator==(const LineLocation &O) const { |
| 295 |
return LineOffset == O.LineOffset && Discriminator == O.Discriminator; |
| 296 |
} |
| 297 |
|
| 298 |
bool operator!=(const LineLocation &O) const { |
| 299 |
return LineOffset != O.LineOffset || Discriminator != O.Discriminator; |
| 300 |
} |
| 301 |
|
| 302 |
uint32_t LineOffset; |
| 303 |
uint32_t Discriminator; |
| 304 |
}; |
| 305 |
|
| 306 |
raw_ostream &operator<<(raw_ostream &OS, const LineLocation &Loc); |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
class SampleRecord { |
| 319 |
public: |
| 320 |
using CallTarget = std::pair<StringRef, uint64_t>; |
| 321 |
struct CallTargetComparator { |
| 322 |
bool operator()(const CallTarget &LHS, const CallTarget &RHS) const { |
| 323 |
if (LHS.second != RHS.second) |
| 324 |
return LHS.second > RHS.second; |
| 325 |
|
| 326 |
return LHS.first < RHS.first; |
| 327 |
} |
| 328 |
}; |
| 329 |
|
| 330 |
using SortedCallTargetSet = std::set<CallTarget, CallTargetComparator>; |
| 331 |
using CallTargetMap = StringMap<uint64_t>; |
| 332 |
SampleRecord() = default; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
sampleprof_error addSamples(uint64_t S, uint64_t Weight = 1) { |
| 340 |
bool Overflowed; |
| 341 |
NumSamples = SaturatingMultiplyAdd(S, Weight, NumSamples, &Overflowed); |
| 342 |
return Overflowed ? sampleprof_error::counter_overflow |
| 343 |
: sampleprof_error::success; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
sampleprof_error addCalledTarget(StringRef F, uint64_t S, |
| 352 |
uint64_t Weight = 1) { |
| 353 |
uint64_t &TargetSamples = CallTargets[F]; |
| 354 |
bool Overflowed; |
| 355 |
TargetSamples = |
| 356 |
SaturatingMultiplyAdd(S, Weight, TargetSamples, &Overflowed); |
| 357 |
return Overflowed ? sampleprof_error::counter_overflow |
| 358 |
: sampleprof_error::success; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
bool hasCalls() const { return !CallTargets.empty(); } |
| 363 |
|
| 364 |
uint64_t getSamples() const { return NumSamples; } |
| 365 |
const CallTargetMap &getCallTargets() const { return CallTargets; } |
| 366 |
const SortedCallTargetSet getSortedCallTargets() const { |
| 367 |
return SortCallTargets(CallTargets); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
static const SortedCallTargetSet SortCallTargets(const CallTargetMap &Targets) { |
| 372 |
SortedCallTargetSet SortedTargets; |
| 373 |
for (const auto &I : Targets) { |
| 374 |
SortedTargets.emplace(I.first(), I.second); |
| 375 |
} |
| 376 |
return SortedTargets; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
static const CallTargetMap adjustCallTargets(const CallTargetMap &Targets, |
| 381 |
float DistributionFactor) { |
| 382 |
CallTargetMap AdjustedTargets; |
| 383 |
for (const auto &I : Targets) { |
| 384 |
AdjustedTargets[I.first()] = I.second * DistributionFactor; |
| 385 |
} |
| 386 |
return AdjustedTargets; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight = 1); |
| 392 |
void print(raw_ostream &OS, unsigned Indent) const; |
| 393 |
void dump() const; |
| 394 |
|
| 395 |
private: |
| 396 |
uint64_t NumSamples = 0; |
| 397 |
CallTargetMap CallTargets; |
| 398 |
}; |
| 399 |
|
| 400 |
raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample); |
| 401 |
|
| 402 |
|
| 403 |
enum ContextStateMask { |
| 404 |
UnknownContext = 0x0, |
| 405 |
RawContext = 0x1, |
| 406 |
SyntheticContext = 0x2, |
| 407 |
InlinedContext = 0x4, |
| 408 |
MergedContext = 0x8 |
| 409 |
}; |
| 410 |
|
| 411 |
|
| 412 |
enum ContextAttributeMask { |
| 413 |
ContextNone = 0x0, |
| 414 |
ContextWasInlined = 0x1, |
| 415 |
ContextShouldBeInlined = 0x2, |
| 416 |
}; |
| 417 |
|
| 418 |
|
| 419 |
struct SampleContextFrame { |
| 420 |
StringRef FuncName; |
| 421 |
LineLocation Location; |
| 422 |
|
| 423 |
SampleContextFrame() : Location(0, 0) {} |
| 424 |
|
| 425 |
SampleContextFrame(StringRef FuncName, LineLocation Location) |
| 426 |
: FuncName(FuncName), Location(Location) {} |
| 427 |
|
| 428 |
bool operator==(const SampleContextFrame &That) const { |
| 429 |
return Location == That.Location && FuncName == That.FuncName; |
| 430 |
} |
| 431 |
|
| 432 |
bool operator!=(const SampleContextFrame &That) const { |
| 433 |
return !(*this == That); |
| 434 |
} |
| 435 |
|
| 436 |
std::string toString(bool OutputLineLocation) const { |
| 437 |
std::ostringstream OContextStr; |
| 438 |
OContextStr << FuncName.str(); |
| 439 |
if (OutputLineLocation) { |
| 440 |
OContextStr << ":" << Location.LineOffset; |
| 441 |
if (Location.Discriminator) |
| 442 |
OContextStr << "." << Location.Discriminator; |
| 443 |
} |
| 444 |
return OContextStr.str(); |
| 445 |
} |
| 446 |
}; |
| 447 |
|
| 448 |
static inline hash_code hash_value(const SampleContextFrame &arg) { |
| 449 |
return hash_combine(arg.FuncName, arg.Location.LineOffset, |
| 450 |
arg.Location.Discriminator); |
| 451 |
} |
| 452 |
|
| 453 |
using SampleContextFrameVector = SmallVector<SampleContextFrame, 1>; |
| 454 |
using SampleContextFrames = ArrayRef<SampleContextFrame>; |
| 455 |
|
| 456 |
struct SampleContextFrameHash { |
| 457 |
uint64_t operator()(const SampleContextFrameVector &S) const { |
| 458 |
return hash_combine_range(S.begin(), S.end()); |
| 459 |
} |
| 460 |
}; |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
class SampleContext { |
| 473 |
public: |
| 474 |
SampleContext() : State(UnknownContext), Attributes(ContextNone) {} |
| 475 |
|
| 476 |
SampleContext(StringRef Name) |
| 477 |
: Name(Name), State(UnknownContext), Attributes(ContextNone) {} |
| 478 |
|
| 479 |
SampleContext(SampleContextFrames Context, |
| 480 |
ContextStateMask CState = RawContext) |
| 481 |
: Attributes(ContextNone) { |
| 482 |
assert(!Context.empty() && "Context is empty"); |
| 483 |
setContext(Context, CState); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
SampleContext(StringRef ContextStr, |
| 490 |
std::list<SampleContextFrameVector> &CSNameTable, |
| 491 |
ContextStateMask CState = RawContext) |
| 492 |
: Attributes(ContextNone) { |
| 493 |
assert(!ContextStr.empty()); |
| 494 |
|
| 495 |
|
| 496 |
bool HasContext = ContextStr.startswith("["); |
| 497 |
if (!HasContext) { |
| 498 |
State = UnknownContext; |
| 499 |
Name = ContextStr; |
| 500 |
} else { |
| 501 |
CSNameTable.emplace_back(); |
| 502 |
SampleContextFrameVector &Context = CSNameTable.back(); |
| 503 |
createCtxVectorFromStr(ContextStr, Context); |
| 504 |
setContext(Context, CState); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
static void createCtxVectorFromStr(StringRef ContextStr, |
| 511 |
SampleContextFrameVector &Context) { |
| 512 |
|
| 513 |
ContextStr = ContextStr.substr(1, ContextStr.size() - 2); |
| 514 |
StringRef ContextRemain = ContextStr; |
| 515 |
StringRef ChildContext; |
| 516 |
StringRef CalleeName; |
| 517 |
while (!ContextRemain.empty()) { |
| 518 |
auto ContextSplit = ContextRemain.split(" @ "); |
| 519 |
ChildContext = ContextSplit.first; |
| 520 |
ContextRemain = ContextSplit.second; |
| 521 |
LineLocation CallSiteLoc(0, 0); |
| 522 |
decodeContextString(ChildContext, CalleeName, CallSiteLoc); |
| 523 |
Context.emplace_back(CalleeName, CallSiteLoc); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
void promoteOnPath(uint32_t ContextFramesToRemove) { |
| 532 |
assert(ContextFramesToRemove <= FullContext.size() && |
| 533 |
"Cannot remove more than the whole context"); |
| 534 |
FullContext = FullContext.drop_front(ContextFramesToRemove); |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
static void decodeContextString(StringRef ContextStr, StringRef &FName, |
| 540 |
LineLocation &LineLoc) { |
| 541 |
|
| 542 |
auto EntrySplit = ContextStr.split(':'); |
| 543 |
FName = EntrySplit.first; |
| 544 |
|
| 545 |
LineLoc = {0, 0}; |
| 546 |
if (!EntrySplit.second.empty()) { |
| 547 |
|
| 548 |
|
| 549 |
int LineOffset = 0; |
| 550 |
auto LocSplit = EntrySplit.second.split('.'); |
| 551 |
LocSplit.first.getAsInteger(10, LineOffset); |
| 552 |
LineLoc.LineOffset = LineOffset; |
| 553 |
|
| 554 |
|
| 555 |
if (!LocSplit.second.empty()) |
| 556 |
LocSplit.second.getAsInteger(10, LineLoc.Discriminator); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
operator SampleContextFrames() const { return FullContext; } |
| 561 |
bool hasAttribute(ContextAttributeMask A) { return Attributes & (uint32_t)A; } |
| 562 |
void setAttribute(ContextAttributeMask A) { Attributes |= (uint32_t)A; } |
| 563 |
uint32_t getAllAttributes() { return Attributes; } |
| 564 |
void setAllAttributes(uint32_t A) { Attributes = A; } |
| 565 |
bool hasState(ContextStateMask S) { return State & (uint32_t)S; } |
| 566 |
void setState(ContextStateMask S) { State |= (uint32_t)S; } |
| 567 |
void clearState(ContextStateMask S) { State &= (uint32_t)~S; } |
| 568 |
bool hasContext() const { return State != UnknownContext; } |
| 569 |
bool isBaseContext() const { return FullContext.size() == 1; } |
| 570 |
StringRef getName() const { return Name; } |
| 571 |
SampleContextFrames getContextFrames() const { return FullContext; } |
| 572 |
|
| 573 |
static std::string getContextString(SampleContextFrames Context, |
| 574 |
bool IncludeLeafLineLocation = false) { |
| 575 |
std::ostringstream OContextStr; |
| 576 |
for (uint32_t I = 0; I < Context.size(); I++) { |
| 577 |
if (OContextStr.str().size()) { |
| 578 |
OContextStr << " @ "; |
| 579 |
} |
| 580 |
OContextStr << Context[I].toString(I != Context.size() - 1 || |
| 581 |
IncludeLeafLineLocation); |
| 582 |
} |
| 583 |
return OContextStr.str(); |
| 584 |
} |
| 585 |
|
| 586 |
std::string toString() const { |
| 587 |
if (!hasContext()) |
| 588 |
return Name.str(); |
| 589 |
return getContextString(FullContext, false); |
| 590 |
} |
| 591 |
|
| 592 |
uint64_t getHashCode() const { |
| 593 |
return hasContext() ? hash_value(getContextFrames()) |
| 594 |
: hash_value(getName()); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
void setName(StringRef FunctionName) { |
| 599 |
Name = FunctionName; |
| 600 |
FullContext = SampleContextFrames(); |
| 601 |
State = UnknownContext; |
| 602 |
} |
| 603 |
|
| 604 |
void setContext(SampleContextFrames Context, |
| 605 |
ContextStateMask CState = RawContext) { |
| 606 |
assert(CState != UnknownContext); |
| 607 |
FullContext = Context; |
| 608 |
Name = Context.back().FuncName; |
| 609 |
State = CState; |
| 610 |
} |
| 611 |
|
| 612 |
bool operator==(const SampleContext &That) const { |
| 613 |
return State == That.State && Name == That.Name && |
| 614 |
FullContext == That.FullContext; |
| 615 |
} |
| 616 |
|
| 617 |
bool operator!=(const SampleContext &That) const { return !(*this == That); } |
| 618 |
|
| 619 |
bool operator<(const SampleContext &That) const { |
| 620 |
if (State != That.State) |
| 621 |
return State < That.State; |
| 622 |
|
| 623 |
if (!hasContext()) { |
| 624 |
return (Name.compare(That.Name)) == -1; |
| 625 |
} |
| 626 |
|
| 627 |
uint64_t I = 0; |
| 628 |
while (I < std::min(FullContext.size(), That.FullContext.size())) { |
| 629 |
auto &Context1 = FullContext[I]; |
| 630 |
auto &Context2 = That.FullContext[I]; |
| 631 |
auto V = Context1.FuncName.compare(Context2.FuncName); |
| 632 |
if (V) |
| 633 |
return V == -1; |
| 634 |
if (Context1.Location != Context2.Location) |
| 635 |
return Context1.Location < Context2.Location; |
| 636 |
I++; |
| 637 |
} |
| 638 |
|
| 639 |
return FullContext.size() < That.FullContext.size(); |
| 640 |
} |
| 641 |
|
| 642 |
struct Hash { |
| 643 |
uint64_t operator()(const SampleContext &Context) const { |
| 644 |
return Context.getHashCode(); |
| 645 |
} |
| 646 |
}; |
| 647 |
|
| 648 |
bool IsPrefixOf(const SampleContext &That) const { |
| 649 |
auto ThisContext = FullContext; |
| 650 |
auto ThatContext = That.FullContext; |
| 651 |
if (ThatContext.size() < ThisContext.size()) |
| 652 |
return false; |
| 653 |
ThatContext = ThatContext.take_front(ThisContext.size()); |
| 654 |
|
| 655 |
if (ThisContext.back().FuncName != ThatContext.back().FuncName) |
| 656 |
return false; |
| 657 |
|
| 658 |
return ThisContext.drop_back() == ThatContext.drop_back(); |
| 659 |
} |
| 660 |
|
| 661 |
private: |
| 662 |
|
| 663 |
StringRef Name; |
| 664 |
|
| 665 |
SampleContextFrames FullContext; |
| 666 |
|
| 667 |
uint32_t State; |
| 668 |
|
| 669 |
uint32_t Attributes; |
| 670 |
}; |
| 671 |
|
| 672 |
static inline hash_code hash_value(const SampleContext &arg) { |
| 673 |
return arg.hasContext() ? hash_value(arg.getContextFrames()) |
| 674 |
: hash_value(arg.getName()); |
| 675 |
} |
| 676 |
|
| 677 |
class FunctionSamples; |
| 678 |
class SampleProfileReaderItaniumRemapper; |
| 679 |
|
| 680 |
using BodySampleMap = std::map<LineLocation, SampleRecord>; |
| 681 |
|
| 682 |
|
| 683 |
using FunctionSamplesMap = std::map<std::string, FunctionSamples, std::less<>>; |
| 684 |
using CallsiteSampleMap = std::map<LineLocation, FunctionSamplesMap>; |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
class FunctionSamples { |
| 692 |
public: |
| 693 |
FunctionSamples() = default; |
| 694 |
|
| 695 |
void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const; |
| 696 |
void dump() const; |
| 697 |
|
| 698 |
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight = 1) { |
| 699 |
bool Overflowed; |
| 700 |
TotalSamples = |
| 701 |
SaturatingMultiplyAdd(Num, Weight, TotalSamples, &Overflowed); |
| 702 |
return Overflowed ? sampleprof_error::counter_overflow |
| 703 |
: sampleprof_error::success; |
| 704 |
} |
| 705 |
|
| 706 |
void setTotalSamples(uint64_t Num) { TotalSamples = Num; } |
| 707 |
|
| 708 |
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight = 1) { |
| 709 |
bool Overflowed; |
| 710 |
TotalHeadSamples = |
| 711 |
SaturatingMultiplyAdd(Num, Weight, TotalHeadSamples, &Overflowed); |
| 712 |
return Overflowed ? sampleprof_error::counter_overflow |
| 713 |
: sampleprof_error::success; |
| 714 |
} |
| 715 |
|
| 716 |
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, |
| 717 |
uint64_t Num, uint64_t Weight = 1) { |
| 718 |
return BodySamples[LineLocation(LineOffset, Discriminator)].addSamples( |
| 719 |
Num, Weight); |
| 720 |
} |
| 721 |
|
| 722 |
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, |
| 723 |
uint32_t Discriminator, |
| 724 |
StringRef FName, uint64_t Num, |
| 725 |
uint64_t Weight = 1) { |
| 726 |
return BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget( |
| 727 |
FName, Num, Weight); |
| 728 |
} |
| 729 |
|
| 730 |
sampleprof_error addBodySamplesForProbe(uint32_t Index, uint64_t Num, |
| 731 |
uint64_t Weight = 1) { |
| 732 |
SampleRecord S; |
| 733 |
S.addSamples(Num, Weight); |
| 734 |
return BodySamples[LineLocation(Index, 0)].merge(S, Weight); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
void updateTotalSamples() { |
| 739 |
setTotalSamples(0); |
| 740 |
for (const auto &I : BodySamples) |
| 741 |
addTotalSamples(I.second.getSamples()); |
| 742 |
|
| 743 |
for (auto &I : CallsiteSamples) { |
| 744 |
for (auto &CS : I.second) { |
| 745 |
CS.second.updateTotalSamples(); |
| 746 |
addTotalSamples(CS.second.getTotalSamples()); |
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
void SetContextSynthetic() { |
| 753 |
Context.setState(SyntheticContext); |
| 754 |
for (auto &I : CallsiteSamples) { |
| 755 |
for (auto &CS : I.second) { |
| 756 |
CS.second.SetContextSynthetic(); |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset, |
| 765 |
uint32_t Discriminator) const { |
| 766 |
const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator)); |
| 767 |
if (ret == BodySamples.end()) |
| 768 |
return std::error_code(); |
| 769 |
return ret->second.getSamples(); |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
ErrorOr<SampleRecord::CallTargetMap> |
| 776 |
findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const { |
| 777 |
const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator)); |
| 778 |
if (ret == BodySamples.end()) |
| 779 |
return std::error_code(); |
| 780 |
return ret->second.getCallTargets(); |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
ErrorOr<SampleRecord::CallTargetMap> |
| 786 |
findCallTargetMapAt(const LineLocation &CallSite) const { |
| 787 |
const auto &Ret = BodySamples.find(CallSite); |
| 788 |
if (Ret == BodySamples.end()) |
| 789 |
return std::error_code(); |
| 790 |
return Ret->second.getCallTargets(); |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
FunctionSamplesMap &functionSamplesAt(const LineLocation &Loc) { |
| 795 |
return CallsiteSamples[Loc]; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
const FunctionSamplesMap * |
| 800 |
findFunctionSamplesMapAt(const LineLocation &Loc) const { |
| 801 |
auto iter = CallsiteSamples.find(Loc); |
| 802 |
if (iter == CallsiteSamples.end()) |
| 803 |
return nullptr; |
| 804 |
return &iter->second; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
const FunctionSamples * |
| 814 |
findFunctionSamplesAt(const LineLocation &Loc, StringRef CalleeName, |
| 815 |
SampleProfileReaderItaniumRemapper *Remapper) const; |
| 816 |
|
| 817 |
bool empty() const { return TotalSamples == 0; } |
| 818 |
|
| 819 |
|
| 820 |
uint64_t getTotalSamples() const { return TotalSamples; } |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
uint64_t getHeadSamples() const { return TotalHeadSamples; } |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
uint64_t getEntrySamples() const { |
| 832 |
if (FunctionSamples::ProfileIsCSFlat && getHeadSamples()) { |
| 833 |
|
| 834 |
|
| 835 |
return getHeadSamples(); |
| 836 |
} |
| 837 |
uint64_t Count = 0; |
| 838 |
|
| 839 |
|
| 840 |
if (!BodySamples.empty() && |
| 841 |
(CallsiteSamples.empty() || |
| 842 |
BodySamples.begin()->first < CallsiteSamples.begin()->first)) |
| 843 |
Count = BodySamples.begin()->second.getSamples(); |
| 844 |
else if (!CallsiteSamples.empty()) { |
| 845 |
|
| 846 |
|
| 847 |
for (const auto &N_FS : CallsiteSamples.begin()->second) |
| 848 |
Count += N_FS.second.getEntrySamples(); |
| 849 |
} |
| 850 |
|
| 851 |
return Count ? Count : TotalSamples > 0; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
const BodySampleMap &getBodySamples() const { return BodySamples; } |
| 856 |
|
| 857 |
|
| 858 |
const CallsiteSampleMap &getCallsiteSamples() const { |
| 859 |
return CallsiteSamples; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
uint64_t getMaxCountInside() const { |
| 865 |
uint64_t MaxCount = 0; |
| 866 |
for (const auto &L : getBodySamples()) |
| 867 |
MaxCount = std::max(MaxCount, L.second.getSamples()); |
| 868 |
for (const auto &C : getCallsiteSamples()) |
| 869 |
for (const FunctionSamplesMap::value_type &F : C.second) |
| 870 |
MaxCount = std::max(MaxCount, F.second.getMaxCountInside()); |
| 871 |
return MaxCount; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight = 1) { |
| 877 |
sampleprof_error Result = sampleprof_error::success; |
| 878 |
if (!GUIDToFuncNameMap) |
| 879 |
GUIDToFuncNameMap = Other.GUIDToFuncNameMap; |
| 880 |
if (Context.getName().empty()) |
| 881 |
Context = Other.getContext(); |
| 882 |
if (FunctionHash == 0) { |
| 883 |
|
| 884 |
FunctionHash = Other.getFunctionHash(); |
| 885 |
} else if (FunctionHash != Other.getFunctionHash()) { |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
return sampleprof_error::hash_mismatch; |
| 894 |
} |
| 895 |
|
| 896 |
MergeResult(Result, addTotalSamples(Other.getTotalSamples(), Weight)); |
| 897 |
MergeResult(Result, addHeadSamples(Other.getHeadSamples(), Weight)); |
| 898 |
for (const auto &I : Other.getBodySamples()) { |
| 899 |
const LineLocation &Loc = I.first; |
| 900 |
const SampleRecord &Rec = I.second; |
| 901 |
MergeResult(Result, BodySamples[Loc].merge(Rec, Weight)); |
| 902 |
} |
| 903 |
for (const auto &I : Other.getCallsiteSamples()) { |
| 904 |
const LineLocation &Loc = I.first; |
| 905 |
FunctionSamplesMap &FSMap = functionSamplesAt(Loc); |
| 906 |
for (const auto &Rec : I.second) |
| 907 |
MergeResult(Result, FSMap[Rec.first].merge(Rec.second, Weight)); |
| 908 |
} |
| 909 |
return Result; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
void findInlinedFunctions(DenseSet<GlobalValue::GUID> &S, |
| 917 |
const StringMap<Function *> &SymbolMap, |
| 918 |
uint64_t Threshold) const { |
| 919 |
if (TotalSamples <= Threshold) |
| 920 |
return; |
| 921 |
auto isDeclaration = [](const Function *F) { |
| 922 |
return !F || F->isDeclaration(); |
| 923 |
}; |
| 924 |
if (isDeclaration(SymbolMap.lookup(getFuncName()))) { |
| 925 |
|
| 926 |
S.insert(getGUID(getName())); |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
for (const auto &BS : BodySamples) |
| 931 |
for (const auto &TS : BS.second.getCallTargets()) |
| 932 |
if (TS.getValue() > Threshold) { |
| 933 |
const Function *Callee = SymbolMap.lookup(getFuncName(TS.getKey())); |
| 934 |
if (isDeclaration(Callee)) |
| 935 |
S.insert(getGUID(TS.getKey())); |
| 936 |
} |
| 937 |
for (const auto &CS : CallsiteSamples) |
| 938 |
for (const auto &NameFS : CS.second) |
| 939 |
NameFS.second.findInlinedFunctions(S, SymbolMap, Threshold); |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
void setName(StringRef FunctionName) { Context.setName(FunctionName); } |
| 944 |
|
| 945 |
|
| 946 |
StringRef getName() const { return Context.getName(); } |
| 947 |
|
| 948 |
|
| 949 |
StringRef getFuncName() const { return getFuncName(getName()); } |
| 950 |
|
| 951 |
void setFunctionHash(uint64_t Hash) { FunctionHash = Hash; } |
| 952 |
|
| 953 |
uint64_t getFunctionHash() const { return FunctionHash; } |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
static StringRef getCanonicalFnName(const Function &F) { |
| 958 |
auto AttrName = "sample-profile-suffix-elision-policy"; |
| 959 |
auto Attr = F.getFnAttribute(AttrName).getValueAsString(); |
| 960 |
return getCanonicalFnName(F.getName(), Attr); |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
static constexpr const char *LLVMSuffix = ".llvm."; |
| 966 |
static constexpr const char *PartSuffix = ".part."; |
| 967 |
static constexpr const char *UniqSuffix = ".__uniq."; |
| 968 |
|
| 969 |
static StringRef getCanonicalFnName(StringRef FnName, |
| 970 |
StringRef Attr = "selected") { |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
const char *knownSuffixes[] = {LLVMSuffix, PartSuffix, UniqSuffix}; |
| 975 |
if (Attr == "" || Attr == "all") { |
| 976 |
return FnName.split('.').first; |
| 977 |
} else if (Attr == "selected") { |
| 978 |
StringRef Cand(FnName); |
| 979 |
for (const auto &Suf : knownSuffixes) { |
| 980 |
StringRef Suffix(Suf); |
| 981 |
|
| 982 |
|
| 983 |
if (Suffix == UniqSuffix && FunctionSamples::HasUniqSuffix) |
| 984 |
continue; |
| 985 |
auto It = Cand.rfind(Suffix); |
| 986 |
if (It == StringRef::npos) |
| 987 |
continue; |
| 988 |
auto Dit = Cand.rfind('.'); |
| 989 |
if (Dit == It + Suffix.size() - 1) |
| 990 |
Cand = Cand.substr(0, It); |
| 991 |
} |
| 992 |
return Cand; |
| 993 |
} else if (Attr == "none") { |
| 994 |
return FnName; |
| 995 |
} else { |
| 996 |
assert(false && "internal error: unknown suffix elision policy"); |
| 997 |
} |
| 998 |
return FnName; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
StringRef getFuncName(StringRef Name) const { |
| 1009 |
if (!UseMD5) |
| 1010 |
return Name; |
| 1011 |
|
| 1012 |
assert(GUIDToFuncNameMap && "GUIDToFuncNameMap needs to be populated first"); |
| 1013 |
return GUIDToFuncNameMap->lookup(std::stoull(Name.data())); |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
static unsigned getOffset(const DILocation *DIL); |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
static LineLocation getCallSiteIdentifier(const DILocation *DIL, |
| 1025 |
bool ProfileIsFS = false); |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
static uint64_t getCallSiteHash(StringRef CalleeName, |
| 1030 |
const LineLocation &Callsite); |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
const FunctionSamples *findFunctionSamples( |
| 1044 |
const DILocation *DIL, |
| 1045 |
SampleProfileReaderItaniumRemapper *Remapper = nullptr) const; |
| 1046 |
|
| 1047 |
static bool ProfileIsProbeBased; |
| 1048 |
|
| 1049 |
static bool ProfileIsCSFlat; |
| 1050 |
|
| 1051 |
static bool ProfileIsCSNested; |
| 1052 |
|
| 1053 |
SampleContext &getContext() const { return Context; } |
| 1054 |
|
| 1055 |
void setContext(const SampleContext &FContext) { Context = FContext; } |
| 1056 |
|
| 1057 |
static SampleProfileFormat Format; |
| 1058 |
|
| 1059 |
|
| 1060 |
static bool UseMD5; |
| 1061 |
|
| 1062 |
|
| 1063 |
static bool HasUniqSuffix; |
| 1064 |
|
| 1065 |
|
| 1066 |
static bool ProfileIsFS; |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
DenseMap<uint64_t, StringRef> *GUIDToFuncNameMap = nullptr; |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
static uint64_t getGUID(StringRef Name) { |
| 1076 |
return UseMD5 ? std::stoull(Name.data()) : Function::getGUID(Name); |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
void findAllNames(DenseSet<StringRef> &NameSet) const; |
| 1082 |
|
| 1083 |
private: |
| 1084 |
|
| 1085 |
uint64_t FunctionHash = 0; |
| 1086 |
|
| 1087 |
|
| 1088 |
mutable SampleContext Context; |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
uint64_t TotalSamples = 0; |
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
uint64_t TotalHeadSamples = 0; |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
BodySampleMap BodySamples; |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
CallsiteSampleMap CallsiteSamples; |
| 1125 |
}; |
| 1126 |
|
| 1127 |
raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS); |
| 1128 |
|
| 1129 |
using SampleProfileMap = |
| 1130 |
std::unordered_map<SampleContext, FunctionSamples, SampleContext::Hash>; |
| 1131 |
|
| 1132 |
using NameFunctionSamples = std::pair<SampleContext, const FunctionSamples *>; |
| 1133 |
|
| 1134 |
void sortFuncProfiles(const SampleProfileMap &ProfileMap, |
| 1135 |
std::vector<NameFunctionSamples> &SortedProfiles); |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
template <class LocationT, class SampleT> class SampleSorter { |
| 1142 |
public: |
| 1143 |
using SamplesWithLoc = std::pair<const LocationT, SampleT>; |
| 1144 |
using SamplesWithLocList = SmallVector<const SamplesWithLoc *, 20>; |
| 1145 |
|
| 1146 |
SampleSorter(const std::map<LocationT, SampleT> &Samples) { |
| 1147 |
for (const auto &I : Samples) |
| 1148 |
V.push_back(&I); |
| 1149 |
llvm::stable_sort(V, [](const SamplesWithLoc *A, const SamplesWithLoc *B) { |
| 1150 |
return A->first < B->first; |
| 1151 |
}); |
| 1152 |
} |
| 1153 |
|
| 1154 |
const SamplesWithLocList &get() const { return V; } |
| 1155 |
|
| 1156 |
private: |
| 1157 |
SamplesWithLocList V; |
| 1158 |
}; |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
class SampleContextTrimmer { |
| 1164 |
public: |
| 1165 |
SampleContextTrimmer(SampleProfileMap &Profiles) : ProfileMap(Profiles){}; |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
void trimAndMergeColdContextProfiles(uint64_t ColdCountThreshold, |
| 1174 |
bool TrimColdContext, |
| 1175 |
bool MergeColdContext, |
| 1176 |
uint32_t ColdContextFrameLength, |
| 1177 |
bool TrimBaseProfileOnly); |
| 1178 |
|
| 1179 |
void canonicalizeContextProfiles(); |
| 1180 |
|
| 1181 |
private: |
| 1182 |
SampleProfileMap &ProfileMap; |
| 1183 |
}; |
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
class CSProfileConverter { |
| 1188 |
public: |
| 1189 |
CSProfileConverter(SampleProfileMap &Profiles); |
| 1190 |
void convertProfiles(); |
| 1191 |
struct FrameNode { |
| 1192 |
FrameNode(StringRef FName = StringRef(), |
| 1193 |
FunctionSamples *FSamples = nullptr, |
| 1194 |
LineLocation CallLoc = {0, 0}) |
| 1195 |
: FuncName(FName), FuncSamples(FSamples), CallSiteLoc(CallLoc){}; |
| 1196 |
|
| 1197 |
|
| 1198 |
std::map<uint64_t, FrameNode> AllChildFrames; |
| 1199 |
|
| 1200 |
StringRef FuncName; |
| 1201 |
|
| 1202 |
FunctionSamples *FuncSamples; |
| 1203 |
|
| 1204 |
LineLocation CallSiteLoc; |
| 1205 |
|
| 1206 |
FrameNode *getOrCreateChildFrame(const LineLocation &CallSite, |
| 1207 |
StringRef CalleeName); |
| 1208 |
}; |
| 1209 |
|
| 1210 |
private: |
| 1211 |
|
| 1212 |
void convertProfiles(FrameNode &Node); |
| 1213 |
FrameNode *getOrCreateContextPath(const SampleContext &Context); |
| 1214 |
|
| 1215 |
SampleProfileMap &ProfileMap; |
| 1216 |
FrameNode RootFrame; |
| 1217 |
}; |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
class ProfileSymbolList { |
| 1224 |
public: |
| 1225 |
|
| 1226 |
|
| 1227 |
void add(StringRef Name, bool copy = false) { |
| 1228 |
if (!copy) { |
| 1229 |
Syms.insert(Name); |
| 1230 |
return; |
| 1231 |
} |
| 1232 |
Syms.insert(Name.copy(Allocator)); |
| 1233 |
} |
| 1234 |
|
| 1235 |
bool contains(StringRef Name) { return Syms.count(Name); } |
| 1236 |
|
| 1237 |
void merge(const ProfileSymbolList &List) { |
| 1238 |
for (auto Sym : List.Syms) |
| 1239 |
add(Sym, true); |
| 1240 |
} |
| 1241 |
|
| 1242 |
unsigned size() { return Syms.size(); } |
| 1243 |
|
| 1244 |
void setToCompress(bool TC) { ToCompress = TC; } |
| 1245 |
bool toCompress() { return ToCompress; } |
| 1246 |
|
| 1247 |
std::error_code read(const uint8_t *Data, uint64_t ListSize); |
| 1248 |
std::error_code write(raw_ostream &OS); |
| 1249 |
void dump(raw_ostream &OS = dbgs()) const; |
| 1250 |
|
| 1251 |
private: |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
bool ToCompress = false; |
| 1256 |
DenseSet<StringRef> Syms; |
| 1257 |
BumpPtrAllocator Allocator; |
| 1258 |
}; |
| 1259 |
|
| 1260 |
} |
| 1261 |
|
| 1262 |
using namespace sampleprof; |
| 1263 |
|
| 1264 |
template <> struct DenseMapInfo<SampleContext> { |
| 1265 |
static inline SampleContext getEmptyKey() { return SampleContext(); } |
| 1266 |
|
| 1267 |
static inline SampleContext getTombstoneKey() { return SampleContext("@"); } |
| 1268 |
|
| 1269 |
static unsigned getHashValue(const SampleContext &Val) { |
| 1270 |
return Val.getHashCode(); |
| 1271 |
} |
| 1272 |
|
| 1273 |
static bool isEqual(const SampleContext &LHS, const SampleContext &RHS) { |
| 1274 |
return LHS == RHS; |
| 1275 |
} |
| 1276 |
}; |
| 1277 |
} |
| 1278 |
|
| 1279 |
#endif |