1c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===- SampleProfWriter.h - Write LLVM sample profile data ------*- 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 definitions needed for writing sample profiles. 11c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// 12c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===// 13c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 14c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 15c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 16c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/MapVector.h" 17c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/StringMap.h" 18c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/StringRef.h" 19c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/IR/ProfileSummary.h" 20c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ProfileData/SampleProf.h" 21c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Support/ErrorOr.h" 22c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Support/raw_ostream.h" 23c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <algorithm> 24c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <cstdint> 25c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <memory> 26c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <system_error> 27c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 28c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace llvm { 29c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace sampleprof { 30c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 31c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotenum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC }; 32c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 33c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \brief Sample-based profile writer. Base class. 34c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass SampleProfileWriter { 35c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic: 36c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot virtual ~SampleProfileWriter() = default; 37c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 38c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Write sample profiles in \p S. 39c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 40c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// \returns status code of the file update operation. 41c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot virtual std::error_code write(const FunctionSamples &S) = 0; 42c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 43c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Write all the sample profiles in the given map of samples. 44c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 45c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// \returns status code of the file update operation. 46c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::error_code write(const StringMap<FunctionSamples> &ProfileMap); 47c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 48c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot raw_ostream &getOutputStream() { return *OutputStream; } 49c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 50c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Profile writer factory. 51c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 52c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Create a new file writer based on the value of \p Format. 53c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot static ErrorOr<std::unique_ptr<SampleProfileWriter>> 54c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot create(StringRef Filename, SampleProfileFormat Format); 55c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 56c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Create a new stream writer based on the value of \p Format. 57c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// For testing. 58c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot static ErrorOr<std::unique_ptr<SampleProfileWriter>> 59c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot create(std::unique_ptr<raw_ostream> &OS, SampleProfileFormat Format); 60c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 61c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprotected: 62c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleProfileWriter(std::unique_ptr<raw_ostream> &OS) 63c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot : OutputStream(std::move(OS)) {} 64c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 65c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// \brief Write a file header for the profile file. 66c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot virtual std::error_code 67c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot writeHeader(const StringMap<FunctionSamples> &ProfileMap) = 0; 68c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 69c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// \brief Output stream where to emit the profile to. 70c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::unique_ptr<raw_ostream> OutputStream; 71c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 72c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// \brief Profile summary. 73c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::unique_ptr<ProfileSummary> Summary; 74c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 75c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// \brief Compute summary for this profile. 76c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void computeSummary(const StringMap<FunctionSamples> &ProfileMap); 77c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot}; 78c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 79c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \brief Sample-based profile writer (text format). 80c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass SampleProfileWriterText : public SampleProfileWriter { 81c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic: 82c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::error_code write(const FunctionSamples &S) override; 83c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 84c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprotected: 85c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleProfileWriterText(std::unique_ptr<raw_ostream> &OS) 86c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot : SampleProfileWriter(OS), Indent(0) {} 87c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 88c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::error_code 89c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot writeHeader(const StringMap<FunctionSamples> &ProfileMap) override { 90c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot return sampleprof_error::success; 91c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot } 92c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 93c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprivate: 94c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// Indent level to use when writing. 95c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// 96c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot /// This is used when printing inlined callees. 97c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot unsigned Indent; 98c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 99c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot friend ErrorOr<std::unique_ptr<SampleProfileWriter>> 100c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, 101c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleProfileFormat Format); 102c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot}; 103c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 104c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot/// \brief Sample-based profile writer (binary format). 105c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass SampleProfileWriterBinary : public SampleProfileWriter { 106c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic: 107c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::error_code write(const FunctionSamples &S) override; 108c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 109c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprotected: 110c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleProfileWriterBinary(std::unique_ptr<raw_ostream> &OS) 111c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot : SampleProfileWriter(OS) {} 112c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 113c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::error_code 114c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot writeHeader(const StringMap<FunctionSamples> &ProfileMap) override; 115c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::error_code writeSummary(); 116c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::error_code writeNameIdx(StringRef FName); 117c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot std::error_code writeBody(const FunctionSamples &S); 118c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 119c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprivate: 120c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void addName(StringRef FName); 121c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot void addNames(const FunctionSamples &S); 122c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 123c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot MapVector<StringRef, uint32_t> NameTable; 124c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 125c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot friend ErrorOr<std::unique_ptr<SampleProfileWriter>> 126c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS, 127c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot SampleProfileFormat Format); 128c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot}; 129c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 130c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace sampleprof 131c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace llvm 132c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot 133c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H 134