aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/ProfileData
diff options
context:
space:
mode:
Diffstat (limited to 'unittests/ProfileData')
-rw-r--r--unittests/ProfileData/CMakeLists.txt10
-rw-r--r--unittests/ProfileData/CoverageMappingTest.cpp262
-rw-r--r--unittests/ProfileData/InstrProfTest.cpp98
-rw-r--r--unittests/ProfileData/Makefile15
4 files changed, 385 insertions, 0 deletions
diff --git a/unittests/ProfileData/CMakeLists.txt b/unittests/ProfileData/CMakeLists.txt
new file mode 100644
index 0000000..79137c9
--- /dev/null
+++ b/unittests/ProfileData/CMakeLists.txt
@@ -0,0 +1,10 @@
+set(LLVM_LINK_COMPONENTS
+ Core
+ ProfileData
+ Support
+ )
+
+add_llvm_unittest(ProfileDataTests
+ CoverageMappingTest.cpp
+ InstrProfTest.cpp
+ )
diff --git a/unittests/ProfileData/CoverageMappingTest.cpp b/unittests/ProfileData/CoverageMappingTest.cpp
new file mode 100644
index 0000000..9fceacb
--- /dev/null
+++ b/unittests/ProfileData/CoverageMappingTest.cpp
@@ -0,0 +1,262 @@
+//===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ProfileData/CoverageMapping.h"
+#include "llvm/ProfileData/CoverageMappingReader.h"
+#include "llvm/ProfileData/CoverageMappingWriter.h"
+#include "llvm/ProfileData/InstrProfReader.h"
+#include "llvm/ProfileData/InstrProfWriter.h"
+#include "llvm/Support/raw_ostream.h"
+#include "gtest/gtest.h"
+
+#include <sstream>
+
+using namespace llvm;
+using namespace coverage;
+
+static ::testing::AssertionResult NoError(std::error_code EC) {
+ if (!EC)
+ return ::testing::AssertionSuccess();
+ return ::testing::AssertionFailure() << "error " << EC.value()
+ << ": " << EC.message();
+}
+
+namespace llvm {
+namespace coverage {
+void PrintTo(const Counter &C, ::std::ostream *os) {
+ if (C.isZero())
+ *os << "Zero";
+ else if (C.isExpression())
+ *os << "Expression " << C.getExpressionID();
+ else
+ *os << "Counter " << C.getCounterID();
+}
+
+void PrintTo(const CoverageSegment &S, ::std::ostream *os) {
+ *os << "CoverageSegment(" << S.Line << ", " << S.Col << ", ";
+ if (S.HasCount)
+ *os << S.Count << ", ";
+ *os << (S.IsRegionEntry ? "true" : "false") << ")";
+}
+}
+}
+
+namespace {
+
+struct OneFunctionCoverageReader : CoverageMappingReader {
+ StringRef Name;
+ uint64_t Hash;
+ std::vector<StringRef> Filenames;
+ ArrayRef<CounterMappingRegion> Regions;
+ bool Done;
+
+ OneFunctionCoverageReader(StringRef Name, uint64_t Hash,
+ ArrayRef<StringRef> Filenames,
+ ArrayRef<CounterMappingRegion> Regions)
+ : Name(Name), Hash(Hash), Filenames(Filenames), Regions(Regions),
+ Done(false) {}
+
+ std::error_code readNextRecord(CoverageMappingRecord &Record) override {
+ if (Done)
+ return instrprof_error::eof;
+ Done = true;
+
+ Record.FunctionName = Name;
+ Record.FunctionHash = Hash;
+ Record.Filenames = Filenames;
+ Record.Expressions = {};
+ Record.MappingRegions = Regions;
+ return instrprof_error::success;
+ }
+};
+
+struct CoverageMappingTest : ::testing::Test {
+ StringMap<unsigned> Files;
+ unsigned NextFile;
+ std::vector<CounterMappingRegion> InputCMRs;
+
+ std::vector<StringRef> OutputFiles;
+ std::vector<CounterExpression> OutputExpressions;
+ std::vector<CounterMappingRegion> OutputCMRs;
+
+ InstrProfWriter ProfileWriter;
+ std::unique_ptr<IndexedInstrProfReader> ProfileReader;
+
+ std::unique_ptr<CoverageMapping> LoadedCoverage;
+
+ void SetUp() override {
+ NextFile = 0;
+ }
+
+ unsigned getFile(StringRef Name) {
+ auto R = Files.find(Name);
+ if (R != Files.end())
+ return R->second;
+ Files[Name] = NextFile;
+ return NextFile++;
+ }
+
+ void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
+ unsigned CE) {
+ InputCMRs.push_back(
+ CounterMappingRegion::makeRegion(C, getFile(File), LS, CS, LE, CE));
+ }
+
+ void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
+ unsigned CS, unsigned LE, unsigned CE) {
+ InputCMRs.push_back(CounterMappingRegion::makeExpansion(
+ getFile(File), getFile(ExpandedFile), LS, CS, LE, CE));
+ }
+
+ std::string writeCoverageRegions() {
+ SmallVector<unsigned, 8> FileIDs;
+ for (const auto &E : Files)
+ FileIDs.push_back(E.getValue());
+ std::string Coverage;
+ llvm::raw_string_ostream OS(Coverage);
+ CoverageMappingWriter(FileIDs, None, InputCMRs).write(OS);
+ return OS.str();
+ }
+
+ void readCoverageRegions(std::string Coverage) {
+ SmallVector<StringRef, 8> Filenames;
+ for (const auto &E : Files)
+ Filenames.push_back(E.getKey());
+ RawCoverageMappingReader Reader(Coverage, Filenames, OutputFiles,
+ OutputExpressions, OutputCMRs);
+ ASSERT_TRUE(NoError(Reader.read()));
+ }
+
+ void readProfCounts() {
+ auto Profile = ProfileWriter.writeBuffer();
+ auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
+ ASSERT_TRUE(NoError(ReaderOrErr.getError()));
+ ProfileReader = std::move(ReaderOrErr.get());
+ }
+
+ void loadCoverageMapping(StringRef FuncName, uint64_t Hash) {
+ std::string Regions = writeCoverageRegions();
+ readCoverageRegions(Regions);
+
+ SmallVector<StringRef, 8> Filenames;
+ for (const auto &E : Files)
+ Filenames.push_back(E.getKey());
+ OneFunctionCoverageReader CovReader(FuncName, Hash, Filenames, OutputCMRs);
+ auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader);
+ ASSERT_TRUE(NoError(CoverageOrErr.getError()));
+ LoadedCoverage = std::move(CoverageOrErr.get());
+ }
+};
+
+TEST_F(CoverageMappingTest, basic_write_read) {
+ addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
+ addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
+ addCMR(Counter::getZero(), "foo", 3, 1, 3, 4);
+ addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
+ addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
+ std::string Coverage = writeCoverageRegions();
+ readCoverageRegions(Coverage);
+
+ size_t N = makeArrayRef(InputCMRs).size();
+ ASSERT_EQ(N, OutputCMRs.size());
+ for (size_t I = 0; I < N; ++I) {
+ ASSERT_EQ(InputCMRs[I].Count, OutputCMRs[I].Count);
+ ASSERT_EQ(InputCMRs[I].FileID, OutputCMRs[I].FileID);
+ ASSERT_EQ(InputCMRs[I].startLoc(), OutputCMRs[I].startLoc());
+ ASSERT_EQ(InputCMRs[I].endLoc(), OutputCMRs[I].endLoc());
+ ASSERT_EQ(InputCMRs[I].Kind, OutputCMRs[I].Kind);
+ }
+}
+
+TEST_F(CoverageMappingTest, expansion_gets_first_counter) {
+ addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
+ // This starts earlier in "foo", so the expansion should get its counter.
+ addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
+ addExpansionCMR("bar", "foo", 3, 3, 3, 3);
+ std::string Coverage = writeCoverageRegions();
+ readCoverageRegions(Coverage);
+
+ ASSERT_EQ(CounterMappingRegion::ExpansionRegion, OutputCMRs[2].Kind);
+ ASSERT_EQ(Counter::getCounter(2), OutputCMRs[2].Count);
+ ASSERT_EQ(3U, OutputCMRs[2].LineStart);
+}
+
+TEST_F(CoverageMappingTest, basic_coverage_iteration) {
+ ProfileWriter.addFunctionCounts("func", 0x1234, {30, 20, 10, 0});
+ readProfCounts();
+
+ addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
+ addCMR(Counter::getCounter(1), "file1", 1, 1, 4, 7);
+ addCMR(Counter::getCounter(2), "file1", 5, 8, 9, 1);
+ addCMR(Counter::getCounter(3), "file1", 10, 10, 11, 11);
+ loadCoverageMapping("func", 0x1234);
+
+ CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
+ std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
+ ASSERT_EQ(7U, Segments.size());
+ ASSERT_EQ(CoverageSegment(1, 1, 20, true), Segments[0]);
+ ASSERT_EQ(CoverageSegment(4, 7, 30, false), Segments[1]);
+ ASSERT_EQ(CoverageSegment(5, 8, 10, true), Segments[2]);
+ ASSERT_EQ(CoverageSegment(9, 1, 30, false), Segments[3]);
+ ASSERT_EQ(CoverageSegment(9, 9, false), Segments[4]);
+ ASSERT_EQ(CoverageSegment(10, 10, 0, true), Segments[5]);
+ ASSERT_EQ(CoverageSegment(11, 11, false), Segments[6]);
+}
+
+TEST_F(CoverageMappingTest, uncovered_function) {
+ readProfCounts();
+
+ addCMR(Counter::getZero(), "file1", 1, 2, 3, 4);
+ loadCoverageMapping("func", 0x1234);
+
+ CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
+ std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
+ ASSERT_EQ(2U, Segments.size());
+ ASSERT_EQ(CoverageSegment(1, 2, 0, true), Segments[0]);
+ ASSERT_EQ(CoverageSegment(3, 4, false), Segments[1]);
+}
+
+TEST_F(CoverageMappingTest, combine_regions) {
+ ProfileWriter.addFunctionCounts("func", 0x1234, {10, 20, 30});
+ readProfCounts();
+
+ addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
+ addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
+ addCMR(Counter::getCounter(2), "file1", 3, 3, 4, 4);
+ loadCoverageMapping("func", 0x1234);
+
+ CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
+ std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
+ ASSERT_EQ(4U, Segments.size());
+ ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
+ ASSERT_EQ(CoverageSegment(3, 3, 50, true), Segments[1]);
+ ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
+ ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
+}
+
+TEST_F(CoverageMappingTest, dont_combine_expansions) {
+ ProfileWriter.addFunctionCounts("func", 0x1234, {10, 20});
+ readProfCounts();
+
+ addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
+ addCMR(Counter::getCounter(1), "file1", 3, 3, 4, 4);
+ addCMR(Counter::getCounter(1), "include1", 6, 6, 7, 7);
+ addExpansionCMR("file1", "include1", 3, 3, 4, 4);
+ loadCoverageMapping("func", 0x1234);
+
+ CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
+ std::vector<CoverageSegment> Segments(Data.begin(), Data.end());
+ ASSERT_EQ(4U, Segments.size());
+ ASSERT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
+ ASSERT_EQ(CoverageSegment(3, 3, 20, true), Segments[1]);
+ ASSERT_EQ(CoverageSegment(4, 4, 10, false), Segments[2]);
+ ASSERT_EQ(CoverageSegment(9, 9, false), Segments[3]);
+}
+
+} // end anonymous namespace
diff --git a/unittests/ProfileData/InstrProfTest.cpp b/unittests/ProfileData/InstrProfTest.cpp
new file mode 100644
index 0000000..26ea0e4
--- /dev/null
+++ b/unittests/ProfileData/InstrProfTest.cpp
@@ -0,0 +1,98 @@
+//===- unittest/ProfileData/InstrProfTest.cpp -------------------------------=//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ProfileData/InstrProfReader.h"
+#include "llvm/ProfileData/InstrProfWriter.h"
+#include "gtest/gtest.h"
+
+#include <cstdarg>
+
+using namespace llvm;
+
+static ::testing::AssertionResult NoError(std::error_code EC) {
+ if (!EC)
+ return ::testing::AssertionSuccess();
+ return ::testing::AssertionFailure() << "error " << EC.value()
+ << ": " << EC.message();
+}
+
+static ::testing::AssertionResult ErrorEquals(std::error_code Expected,
+ std::error_code Found) {
+ if (Expected == Found)
+ return ::testing::AssertionSuccess();
+ return ::testing::AssertionFailure() << "error " << Found.value()
+ << ": " << Found.message();
+}
+
+namespace {
+
+struct InstrProfTest : ::testing::Test {
+ InstrProfWriter Writer;
+ std::unique_ptr<IndexedInstrProfReader> Reader;
+
+ void readProfile(std::unique_ptr<MemoryBuffer> Profile) {
+ auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
+ ASSERT_TRUE(NoError(ReaderOrErr.getError()));
+ Reader = std::move(ReaderOrErr.get());
+ }
+};
+
+TEST_F(InstrProfTest, write_and_read_empty_profile) {
+ auto Profile = Writer.writeBuffer();
+ readProfile(std::move(Profile));
+ ASSERT_TRUE(Reader->begin() == Reader->end());
+}
+
+TEST_F(InstrProfTest, write_and_read_one_function) {
+ Writer.addFunctionCounts("foo", 0x1234, {1, 2, 3, 4});
+ auto Profile = Writer.writeBuffer();
+ readProfile(std::move(Profile));
+
+ auto I = Reader->begin(), E = Reader->end();
+ ASSERT_TRUE(I != E);
+ ASSERT_EQ(StringRef("foo"), I->Name);
+ ASSERT_EQ(0x1234U, I->Hash);
+ ASSERT_EQ(4U, I->Counts.size());
+ ASSERT_EQ(1U, I->Counts[0]);
+ ASSERT_EQ(2U, I->Counts[1]);
+ ASSERT_EQ(3U, I->Counts[2]);
+ ASSERT_EQ(4U, I->Counts[3]);
+ ASSERT_TRUE(++I == E);
+}
+
+TEST_F(InstrProfTest, get_function_counts) {
+ Writer.addFunctionCounts("foo", 0x1234, {1, 2});
+ auto Profile = Writer.writeBuffer();
+ readProfile(std::move(Profile));
+
+ std::vector<uint64_t> Counts;
+ ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
+ ASSERT_EQ(2U, Counts.size());
+ ASSERT_EQ(1U, Counts[0]);
+ ASSERT_EQ(2U, Counts[1]);
+
+ std::error_code EC;
+ EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
+ ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
+
+ EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
+ ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
+}
+
+TEST_F(InstrProfTest, get_max_function_count) {
+ Writer.addFunctionCounts("foo", 0x1234, {1ULL << 31, 2});
+ Writer.addFunctionCounts("bar", 0, {1ULL << 63});
+ Writer.addFunctionCounts("baz", 0x5678, {0, 0, 0, 0});
+ auto Profile = Writer.writeBuffer();
+ readProfile(std::move(Profile));
+
+ ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
+}
+
+} // end anonymous namespace
diff --git a/unittests/ProfileData/Makefile b/unittests/ProfileData/Makefile
new file mode 100644
index 0000000..d017c15
--- /dev/null
+++ b/unittests/ProfileData/Makefile
@@ -0,0 +1,15 @@
+##===- unittests/ProfileData/Makefile ----------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+TESTNAME = ProfileData
+LINK_COMPONENTS := ProfileData Core Support
+
+include $(LEVEL)/Makefile.config
+include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest