InstrProfTest.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===- unittest/ProfileData/InstrProfTest.cpp -------------------------------=//
  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. #include "llvm/ProfileData/InstrProfReader.h"
  10. #include "llvm/ProfileData/InstrProfWriter.h"
  11. #include "gtest/gtest.h"
  12. #include <cstdarg>
  13. using namespace llvm;
  14. static ::testing::AssertionResult NoError(std::error_code EC) {
  15. if (!EC)
  16. return ::testing::AssertionSuccess();
  17. return ::testing::AssertionFailure() << "error " << EC.value()
  18. << ": " << EC.message();
  19. }
  20. static ::testing::AssertionResult ErrorEquals(std::error_code Expected,
  21. std::error_code Found) {
  22. if (Expected == Found)
  23. return ::testing::AssertionSuccess();
  24. return ::testing::AssertionFailure() << "error " << Found.value()
  25. << ": " << Found.message();
  26. }
  27. namespace {
  28. struct InstrProfTest : ::testing::Test {
  29. InstrProfWriter Writer;
  30. std::unique_ptr<IndexedInstrProfReader> Reader;
  31. void readProfile(std::unique_ptr<MemoryBuffer> Profile) {
  32. auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
  33. ASSERT_TRUE(NoError(ReaderOrErr.getError()));
  34. Reader = std::move(ReaderOrErr.get());
  35. }
  36. };
  37. TEST_F(InstrProfTest, write_and_read_empty_profile) {
  38. auto Profile = Writer.writeBuffer();
  39. readProfile(std::move(Profile));
  40. ASSERT_TRUE(Reader->begin() == Reader->end());
  41. }
  42. TEST_F(InstrProfTest, write_and_read_one_function) {
  43. Writer.addFunctionCounts("foo", 0x1234, {1, 2, 3, 4});
  44. auto Profile = Writer.writeBuffer();
  45. readProfile(std::move(Profile));
  46. auto I = Reader->begin(), E = Reader->end();
  47. ASSERT_TRUE(I != E);
  48. ASSERT_EQ(StringRef("foo"), I->Name);
  49. ASSERT_EQ(0x1234U, I->Hash);
  50. ASSERT_EQ(4U, I->Counts.size());
  51. ASSERT_EQ(1U, I->Counts[0]);
  52. ASSERT_EQ(2U, I->Counts[1]);
  53. ASSERT_EQ(3U, I->Counts[2]);
  54. ASSERT_EQ(4U, I->Counts[3]);
  55. ASSERT_TRUE(++I == E);
  56. }
  57. TEST_F(InstrProfTest, get_function_counts) {
  58. Writer.addFunctionCounts("foo", 0x1234, {1, 2});
  59. Writer.addFunctionCounts("foo", 0x1235, {3, 4});
  60. auto Profile = Writer.writeBuffer();
  61. readProfile(std::move(Profile));
  62. std::vector<uint64_t> Counts;
  63. ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
  64. ASSERT_EQ(2U, Counts.size());
  65. ASSERT_EQ(1U, Counts[0]);
  66. ASSERT_EQ(2U, Counts[1]);
  67. ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
  68. ASSERT_EQ(2U, Counts.size());
  69. ASSERT_EQ(3U, Counts[0]);
  70. ASSERT_EQ(4U, Counts[1]);
  71. std::error_code EC;
  72. EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
  73. ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
  74. EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
  75. ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
  76. }
  77. TEST_F(InstrProfTest, get_max_function_count) {
  78. Writer.addFunctionCounts("foo", 0x1234, {1ULL << 31, 2});
  79. Writer.addFunctionCounts("bar", 0, {1ULL << 63});
  80. Writer.addFunctionCounts("baz", 0x5678, {0, 0, 0, 0});
  81. auto Profile = Writer.writeBuffer();
  82. readProfile(std::move(Profile));
  83. ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
  84. }
  85. } // end anonymous namespace