1c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===- SampleProf.h - Sampling profiling format support ---------*- C++ -*-===// 2c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// 3c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// The LLVM Compiler Infrastructure 4c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// 5c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file is distributed under the University of Illinois Open Source 6c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// License. See LICENSE.TXT for details. 7c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// 8c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===// 9c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// 10c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file contains common definitions used in the reading and writing of 11c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// sample profile data. 12c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// 13c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===// 14c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 15c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#ifndef LLVM_PROFILEDATA_SAMPLEPROF_H 16c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#define LLVM_PROFILEDATA_SAMPLEPROF_H 17c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 18c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/DenseSet.h" 19c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/SmallVector.h" 20c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/StringMap.h" 21c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/StringRef.h" 22c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/Function.h" 23c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/GlobalValue.h" 24c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/Module.h" 25c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Support/Debug.h" 26c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Support/ErrorOr.h" 27c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Support/MathExtras.h" 28c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <algorithm> 29c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <cstdint> 30c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <map> 31c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <string> 32c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <system_error> 33c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <utility> 34c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 35c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace llvm { 36c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 37c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass raw_ostream; 38c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 39c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotconst std::error_category &sampleprof_category(); 40c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 41c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotenum class sampleprof_error { 42c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot success = 0, 43c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot bad_magic, 44c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot unsupported_version, 45c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot too_large, 46c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot truncated, 47c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot malformed, 48c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot unrecognized_format, 49c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot unsupported_writing_format, 50c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot truncated_name_table, 51c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot not_implemented, 52c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot counter_overflow 53c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot}; 54c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 55c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotinline std::error_code make_error_code(sampleprof_error E) { 56c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return std::error_code(static_cast<int>(E), sampleprof_category()); 57c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} 58c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 59c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotinline sampleprof_error MergeResult(sampleprof_error &Accumulator, 60c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error Result) { 61c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // Prefer first error encountered as later errors may be secondary effects of 62c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // the initial problem. 63c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (Accumulator == sampleprof_error::success && 64c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot Result != sampleprof_error::success) 65c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot Accumulator = Result; 66c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return Accumulator; 67c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} 68c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 69c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace llvm 70c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 71c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace std { 72c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 73c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robottemplate <> 74c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotstruct is_error_code_enum<llvm::sampleprof_error> : std::true_type {}; 75c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 76c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace std 77c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 78c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace llvm { 79c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace sampleprof { 80c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 81c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotstatic inline uint64_t SPMagic() { 82c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) | 83c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) | 84c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) | 85c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t('2') << (64 - 56) | uint64_t(0xff); 86c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} 87c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 88c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotstatic inline uint64_t SPVersion() { return 103; } 89c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 90c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Represents the relative location of an instruction. 91c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// 92c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Instruction locations are specified by the line offset from the 93c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// beginning of the function (marked by the line where the function 94c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// header is) and the discriminator value within that line. 95c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// 96c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// The discriminator value is useful to distinguish instructions 97c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// that are on the same line but belong to different basic blocks 98c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// (e.g., the two post-increment instructions in "if (p) x++; else y++;"). 99c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotstruct LineLocation { 100c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {} 101c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 102c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void print(raw_ostream &OS) const; 103c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void dump() const; 104c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 105c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot bool operator<(const LineLocation &O) const { 106c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return LineOffset < O.LineOffset || 107c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot (LineOffset == O.LineOffset && Discriminator < O.Discriminator); 108c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 109c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 110c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint32_t LineOffset; 111c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint32_t Discriminator; 112c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot}; 113c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 114c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotraw_ostream &operator<<(raw_ostream &OS, const LineLocation &Loc); 115c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 116c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Representation of a single sample record. 117c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// 118c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// A sample record is represented by a positive integer value, which 119c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// indicates how frequently was the associated line location executed. 120c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// 121c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Additionally, if the associated location contains a function call, 122c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// the record will hold a list of all the possible called targets. For 123c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// direct calls, this will be the exact function being invoked. For 124c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// indirect calls (function pointers, virtual table dispatch), this 125c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// will be a list of one or more functions. 126c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass SampleRecord { 127c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic: 128c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot using CallTargetMap = StringMap<uint64_t>; 129c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 130c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleRecord() = default; 131c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 132c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Increment the number of samples for this record by \p S. 133c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Optionally scale sample count \p S by \p Weight. 134c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 135c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Sample counts accumulate using saturating arithmetic, to avoid wrapping 136c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// around unsigned integers. 137c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error addSamples(uint64_t S, uint64_t Weight = 1) { 138c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot bool Overflowed; 139c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot NumSamples = SaturatingMultiplyAdd(S, Weight, NumSamples, &Overflowed); 140c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return Overflowed ? sampleprof_error::counter_overflow 141c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot : sampleprof_error::success; 142c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 143c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 144c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Add called function \p F with samples \p S. 145c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Optionally scale sample count \p S by \p Weight. 146c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 147c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Sample counts accumulate using saturating arithmetic, to avoid wrapping 148c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// around unsigned integers. 149c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error addCalledTarget(StringRef F, uint64_t S, 150c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t Weight = 1) { 151c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t &TargetSamples = CallTargets[F]; 152c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot bool Overflowed; 153c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot TargetSamples = 154c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SaturatingMultiplyAdd(S, Weight, TargetSamples, &Overflowed); 155c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return Overflowed ? sampleprof_error::counter_overflow 156c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot : sampleprof_error::success; 157c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 158c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 159c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return true if this sample record contains function calls. 160c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot bool hasCalls() const { return !CallTargets.empty(); } 161c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 162c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t getSamples() const { return NumSamples; } 163c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const CallTargetMap &getCallTargets() const { return CallTargets; } 164c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 165c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Merge the samples in \p Other into this record. 166c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Optionally scale sample counts by \p Weight. 167c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error merge(const SampleRecord &Other, uint64_t Weight = 1) { 168c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error Result = addSamples(Other.getSamples(), Weight); 169c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &I : Other.getCallTargets()) { 170c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot MergeResult(Result, addCalledTarget(I.first(), I.second, Weight)); 171c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 172c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return Result; 173c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 174c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 175c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void print(raw_ostream &OS, unsigned Indent) const; 176c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void dump() const; 177c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 178c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprivate: 179c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t NumSamples = 0; 180c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot CallTargetMap CallTargets; 181c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot}; 182c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 183c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotraw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample); 184c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 185c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass FunctionSamples; 186c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 187c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotusing BodySampleMap = std::map<LineLocation, SampleRecord>; 188c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotusing FunctionSamplesMap = StringMap<FunctionSamples>; 189c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotusing CallsiteSampleMap = std::map<LineLocation, FunctionSamplesMap>; 190c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 191c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Representation of the samples collected for a function. 192c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// 193c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// This data structure contains all the collected samples for the body 194c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// of a function. Each sample corresponds to a LineLocation instance 195c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// within the body of the function. 196c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass FunctionSamples { 197c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic: 198c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot FunctionSamples() = default; 199c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 200c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const; 201c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void dump() const; 202c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 203c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight = 1) { 204c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot bool Overflowed; 205c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot TotalSamples = 206c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SaturatingMultiplyAdd(Num, Weight, TotalSamples, &Overflowed); 207c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return Overflowed ? sampleprof_error::counter_overflow 208c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot : sampleprof_error::success; 209c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 210c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 211c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight = 1) { 212c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot bool Overflowed; 213c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot TotalHeadSamples = 214c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SaturatingMultiplyAdd(Num, Weight, TotalHeadSamples, &Overflowed); 215c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return Overflowed ? sampleprof_error::counter_overflow 216c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot : sampleprof_error::success; 217c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 218c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 219c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, 220c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t Num, uint64_t Weight = 1) { 221c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return BodySamples[LineLocation(LineOffset, Discriminator)].addSamples( 222c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot Num, Weight); 223c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 224c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 225c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error addCalledTargetSamples(uint32_t LineOffset, 226c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint32_t Discriminator, 227c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const std::string &FName, 228c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t Num, uint64_t Weight = 1) { 229c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget( 230c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot FName, Num, Weight); 231c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 232c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 233c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return the number of samples collected at the given location. 234c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Each location is specified by \p LineOffset and \p Discriminator. 235c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// If the location is not found in profile, return error. 236c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset, 237c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint32_t Discriminator) const { 238c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator)); 239c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (ret == BodySamples.end()) 240c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return std::error_code(); 241c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot else 242c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return ret->second.getSamples(); 243c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 244c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 245c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Returns the call target map collected at a given location. 246c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Each location is specified by \p LineOffset and \p Discriminator. 247c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// If the location is not found in profile, return error. 248c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot ErrorOr<SampleRecord::CallTargetMap> 249c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const { 250c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator)); 251c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (ret == BodySamples.end()) 252c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return std::error_code(); 253c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return ret->second.getCallTargets(); 254c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 255c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 256c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return the function samples at the given callsite location. 257c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot FunctionSamplesMap &functionSamplesAt(const LineLocation &Loc) { 258c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return CallsiteSamples[Loc]; 259c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 260c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 261c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Returns the FunctionSamplesMap at the given \p Loc. 262c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const FunctionSamplesMap * 263c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot findFunctionSamplesMapAt(const LineLocation &Loc) const { 264c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot auto iter = CallsiteSamples.find(Loc); 265c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (iter == CallsiteSamples.end()) 266c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return nullptr; 267c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return &iter->second; 268c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 269c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 270c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Returns a pointer to FunctionSamples at the given callsite location \p Loc 271c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// with callee \p CalleeName. If no callsite can be found, relax the 272c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// restriction to return the FunctionSamples at callsite location \p Loc 273c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// with the maximum total sample count. 274c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const FunctionSamples *findFunctionSamplesAt(const LineLocation &Loc, 275c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot StringRef CalleeName) const { 276c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot auto iter = CallsiteSamples.find(Loc); 277c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (iter == CallsiteSamples.end()) 278c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return nullptr; 279c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot auto FS = iter->second.find(CalleeName); 280c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (FS != iter->second.end()) 281c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return &FS->getValue(); 282c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // If we cannot find exact match of the callee name, return the FS with 283c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // the max total count. 284c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t MaxTotalSamples = 0; 285c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const FunctionSamples *R = nullptr; 286c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &NameFS : iter->second) 287c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (NameFS.second.getTotalSamples() >= MaxTotalSamples) { 288c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot MaxTotalSamples = NameFS.second.getTotalSamples(); 289c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot R = &NameFS.second; 290c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 291c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return R; 292c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 293c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 294c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot bool empty() const { return TotalSamples == 0; } 295c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 296c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return the total number of samples collected inside the function. 297c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t getTotalSamples() const { return TotalSamples; } 298c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 299c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return the total number of branch samples that have the function as the 300c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// branch target. This should be equivalent to the sample of the first 301c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// instruction of the symbol. But as we directly get this info for raw 302c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// profile without referring to potentially inaccurate debug info, this 303c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// gives more accurate profile data and is preferred for standalone symbols. 304c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t getHeadSamples() const { return TotalHeadSamples; } 305c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 306c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return the sample count of the first instruction of the function. 307c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// The function can be either a standalone symbol or an inlined function. 308c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t getEntrySamples() const { 309c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // Use either BodySamples or CallsiteSamples which ever has the smaller 310c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // lineno. 311c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (!BodySamples.empty() && 312c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot (CallsiteSamples.empty() || 313c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot BodySamples.begin()->first < CallsiteSamples.begin()->first)) 314c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return BodySamples.begin()->second.getSamples(); 315c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (!CallsiteSamples.empty()) { 316c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t T = 0; 317c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // An indirect callsite may be promoted to several inlined direct calls. 318c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // We need to get the sum of them. 319c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &N_FS : CallsiteSamples.begin()->second) 320c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot T += N_FS.second.getEntrySamples(); 321c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return T; 322c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 323c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return 0; 324c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 325c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 326c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return all the samples collected in the body of the function. 327c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const BodySampleMap &getBodySamples() const { return BodySamples; } 328c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 329c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return all the callsite samples collected in the body of the function. 330c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const CallsiteSampleMap &getCallsiteSamples() const { 331c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return CallsiteSamples; 332c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 333c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 334c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Merge the samples in \p Other into this one. 335c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Optionally scale samples by \p Weight. 336c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight = 1) { 337c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot sampleprof_error Result = sampleprof_error::success; 338c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot Name = Other.getName(); 339c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot MergeResult(Result, addTotalSamples(Other.getTotalSamples(), Weight)); 340c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot MergeResult(Result, addHeadSamples(Other.getHeadSamples(), Weight)); 341c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &I : Other.getBodySamples()) { 342c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const LineLocation &Loc = I.first; 343c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const SampleRecord &Rec = I.second; 344c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot MergeResult(Result, BodySamples[Loc].merge(Rec, Weight)); 345c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 346c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &I : Other.getCallsiteSamples()) { 347c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const LineLocation &Loc = I.first; 348c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot FunctionSamplesMap &FSMap = functionSamplesAt(Loc); 349c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &Rec : I.second) 350c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot MergeResult(Result, FSMap[Rec.first()].merge(Rec.second, Weight)); 351c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 352c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return Result; 353c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 354c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 355c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Recursively traverses all children, if the corresponding function is 356c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// not defined in module \p M, and its total sample is no less than 357c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// \p Threshold, add its corresponding GUID to \p S. Also traverse the 358c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// BodySamples to add hot CallTarget's GUID to \p S. 359c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void findImportedFunctions(DenseSet<GlobalValue::GUID> &S, const Module *M, 360c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t Threshold) const { 361c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (TotalSamples <= Threshold) 362c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return; 363c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot Function *F = M->getFunction(Name); 364c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (!F || !F->getSubprogram()) 365c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot S.insert(Function::getGUID(Name)); 366c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // Import hot CallTargets, which may not be available in IR because full 367c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot // profile annotation cannot be done until backend compilation in ThinLTO. 368c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &BS : BodySamples) 369c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &TS : BS.second.getCallTargets()) 370c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (TS.getValue() > Threshold) { 371c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot Function *Callee = M->getFunction(TS.getKey()); 372c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot if (!Callee || !Callee->getSubprogram()) 373c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot S.insert(Function::getGUID(TS.getKey())); 374c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 375c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (auto CS : CallsiteSamples) 376c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &NameFS : CS.second) 377c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot NameFS.second.findImportedFunctions(S, M, Threshold); 378c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 379c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 380c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Set the name of the function. 381c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void setName(StringRef FunctionName) { Name = FunctionName; } 382c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 383c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Return the function name. 384c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const StringRef &getName() const { return Name; } 385c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 386c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprivate: 387c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Mangled name of the function. 388c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot StringRef Name; 389c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 390c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Total number of samples collected inside this function. 391c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 392c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Samples are cumulative, they include all the samples collected 393c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// inside this function and all its inlined callees. 394c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t TotalSamples = 0; 395c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 396c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Total number of samples collected at the head of the function. 397c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// This is an approximation of the number of calls made to this function 398c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// at runtime. 399c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot uint64_t TotalHeadSamples = 0; 400c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 401c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Map instruction locations to collected samples. 402c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 403c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Each entry in this map contains the number of samples 404c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// collected at the corresponding line offset. All line locations 405c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// are an offset from the start of the function. 406c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot BodySampleMap BodySamples; 407c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 408c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Map call sites to collected samples for the called function. 409c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 410c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Each entry in this map corresponds to all the samples 411c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// collected for the inlined function call at the given 412c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// location. For example, given: 413c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 414c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// void foo() { 415c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 1 bar(); 416c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// ... 417c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 8 baz(); 418c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// } 419c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 420c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// If the bar() and baz() calls were inlined inside foo(), this 421c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// map will contain two entries. One for all the samples collected 422c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// in the call to bar() at line offset 1, the other for all the samples 423c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// collected in the call to baz() at line offset 8. 424c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot CallsiteSampleMap CallsiteSamples; 425c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot}; 426c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 427c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotraw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS); 428c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 429c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// Sort a LocationT->SampleT map by LocationT. 430c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// 431c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// It produces a sorted list of <LocationT, SampleT> records by ascending 432c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// order of LocationT. 433c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robottemplate <class LocationT, class SampleT> class SampleSorter { 434c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic: 435c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot using SamplesWithLoc = std::pair<const LocationT, SampleT>; 436c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot using SamplesWithLocList = SmallVector<const SamplesWithLoc *, 20>; 437c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 438c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleSorter(const std::map<LocationT, SampleT> &Samples) { 439c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot for (const auto &I : Samples) 440c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot V.push_back(&I); 441c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::stable_sort(V.begin(), V.end(), 442c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot [](const SamplesWithLoc *A, const SamplesWithLoc *B) { 443c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return A->first < B->first; 444c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot }); 445c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 446c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 447c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot const SamplesWithLocList &get() const { return V; } 448c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 449c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprivate: 450c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SamplesWithLocList V; 451c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot}; 452c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 453c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace sampleprof 454c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace llvm 455c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 456c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#endif // LLVM_PROFILEDATA_SAMPLEPROF_H 457