SampleProfWriter.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===- SampleProfWriter.h - Write LLVM sample profile data ----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains definitions needed for writing sample profiles.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
  14. #define LLVM_PROFILEDATA_SAMPLEPROFWRITER_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/ProfileData/SampleProf.h"
  19. #include "llvm/Support/ErrorOr.h"
  20. #include "llvm/Support/FileSystem.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. namespace llvm {
  23. namespace sampleprof {
  24. enum SampleProfileFormat { SPF_None = 0, SPF_Text, SPF_Binary, SPF_GCC };
  25. /// \brief Sample-based profile writer. Base class.
  26. class SampleProfileWriter {
  27. public:
  28. SampleProfileWriter(StringRef Filename, std::error_code &EC,
  29. sys::fs::OpenFlags Flags)
  30. : OS(Filename, EC, Flags) {}
  31. virtual ~SampleProfileWriter() {}
  32. /// \brief Write sample profiles in \p S for function \p FName.
  33. ///
  34. /// \returns true if the file was updated successfully. False, otherwise.
  35. virtual bool write(StringRef FName, const FunctionSamples &S) = 0;
  36. /// \brief Write sample profiles in \p S for function \p F.
  37. bool write(const Function &F, const FunctionSamples &S) {
  38. return write(F.getName(), S);
  39. }
  40. /// \brief Write all the sample profiles for all the functions in \p M.
  41. ///
  42. /// \returns true if the file was updated successfully. False, otherwise.
  43. bool write(const Module &M, StringMap<FunctionSamples> &P) {
  44. for (const auto &F : M) {
  45. StringRef Name = F.getName();
  46. if (!write(Name, P[Name]))
  47. return false;
  48. }
  49. return true;
  50. }
  51. /// \brief Write all the sample profiles in the given map of samples.
  52. ///
  53. /// \returns true if the file was updated successfully. False, otherwise.
  54. bool write(StringMap<FunctionSamples> &ProfileMap) {
  55. for (auto &I : ProfileMap) {
  56. StringRef FName = I.first();
  57. FunctionSamples &Profile = I.second;
  58. if (!write(FName, Profile))
  59. return false;
  60. }
  61. return true;
  62. }
  63. /// \brief Profile writer factory. Create a new writer based on the value of
  64. /// \p Format.
  65. static ErrorOr<std::unique_ptr<SampleProfileWriter>>
  66. create(StringRef Filename, SampleProfileFormat Format);
  67. protected:
  68. /// \brief Output stream where to emit the profile to.
  69. raw_fd_ostream OS;
  70. };
  71. /// \brief Sample-based profile writer (text format).
  72. class SampleProfileWriterText : public SampleProfileWriter {
  73. public:
  74. SampleProfileWriterText(StringRef F, std::error_code &EC)
  75. : SampleProfileWriter(F, EC, sys::fs::F_Text) {}
  76. bool write(StringRef FName, const FunctionSamples &S) override;
  77. bool write(const Module &M, StringMap<FunctionSamples> &P) {
  78. return SampleProfileWriter::write(M, P);
  79. }
  80. };
  81. /// \brief Sample-based profile writer (binary format).
  82. class SampleProfileWriterBinary : public SampleProfileWriter {
  83. public:
  84. SampleProfileWriterBinary(StringRef F, std::error_code &EC);
  85. bool write(StringRef F, const FunctionSamples &S) override;
  86. bool write(const Module &M, StringMap<FunctionSamples> &P) {
  87. return SampleProfileWriter::write(M, P);
  88. }
  89. };
  90. } // End namespace sampleprof
  91. } // End namespace llvm
  92. #endif // LLVM_PROFILEDATA_SAMPLEPROFWRITER_H