summaryrefslogtreecommitdiff
path: root/lib/ProfileData
diff options
context:
space:
mode:
authorJustin Bogner <mail@justinbogner.com>2014-03-21 17:46:22 +0000
committerJustin Bogner <mail@justinbogner.com>2014-03-21 17:46:22 +0000
commit496d7f66a022e234156ea34fe90bb7db5e6ec03c (patch)
tree90c3c0a7134df8388ba4e4374c2238f9a0e81fc7 /lib/ProfileData
parent469198f9950e1f89a6f0f8d58385c960a3697c66 (diff)
downloadllvm-496d7f66a022e234156ea34fe90bb7db5e6ec03c.tar.gz
llvm-496d7f66a022e234156ea34fe90bb7db5e6ec03c.tar.bz2
llvm-496d7f66a022e234156ea34fe90bb7db5e6ec03c.tar.xz
ProfileData: Introduce InstrProfWriter using the naive text format
This isn't a format we'll want to write out in practice, but moving it to the writer library simplifies llvm-profdata and isolates it from further changes to the format. This also allows us to update the tests to not rely on the text output format. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204489 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ProfileData')
-rw-r--r--lib/ProfileData/CMakeLists.txt1
-rw-r--r--lib/ProfileData/InstrProf.cpp6
-rw-r--r--lib/ProfileData/InstrProfWriter.cpp58
3 files changed, 65 insertions, 0 deletions
diff --git a/lib/ProfileData/CMakeLists.txt b/lib/ProfileData/CMakeLists.txt
index cef3a5336b..5204afcdc4 100644
--- a/lib/ProfileData/CMakeLists.txt
+++ b/lib/ProfileData/CMakeLists.txt
@@ -1,6 +1,7 @@
add_llvm_library(LLVMProfileData
InstrProf.cpp
InstrProfReader.cpp
+ InstrProfWriter.cpp
LINK_LIBS
LLVMSupport
diff --git a/lib/ProfileData/InstrProf.cpp b/lib/ProfileData/InstrProf.cpp
index 329abf84e6..ecabdd75bd 100644
--- a/lib/ProfileData/InstrProf.cpp
+++ b/lib/ProfileData/InstrProf.cpp
@@ -39,6 +39,12 @@ class InstrProfErrorCategoryType : public error_category {
return "Malformed profile data";
case instrprof_error::unknown_function:
return "No profile data available for function";
+ case instrprof_error::hash_mismatch:
+ return "Function hash mismatch";
+ case instrprof_error::count_mismatch:
+ return "Function count mismatch";
+ case instrprof_error::counter_overflow:
+ return "Counter overflow";
}
llvm_unreachable("A value of instrprof_error has no message.");
}
diff --git a/lib/ProfileData/InstrProfWriter.cpp b/lib/ProfileData/InstrProfWriter.cpp
new file mode 100644
index 0000000000..320860a4cb
--- /dev/null
+++ b/lib/ProfileData/InstrProfWriter.cpp
@@ -0,0 +1,58 @@
+//=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains support for writing profiling data for clang's
+// instrumentation based PGO and coverage.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ProfileData/InstrProfWriter.h"
+#include "llvm/Support/Endian.h"
+
+using namespace llvm;
+
+error_code InstrProfWriter::addFunctionCounts(StringRef FunctionName,
+ uint64_t FunctionHash,
+ ArrayRef<uint64_t> Counters) {
+ auto Where = FunctionData.find(FunctionName);
+ if (Where == FunctionData.end()) {
+ // If this is the first time we've seen this function, just add it.
+ FunctionData[FunctionName] = {FunctionHash, Counters};
+ return instrprof_error::success;;
+ }
+
+ auto &Data = Where->getValue();
+ // We can only add to existing functions if they match, so we check the hash
+ // and number of counters.
+ if (Data.Hash != FunctionHash)
+ return instrprof_error::hash_mismatch;
+ if (Data.Counts.size() != Counters.size())
+ return instrprof_error::count_mismatch;
+ // These match, add up the counters.
+ for (size_t I = 0, E = Counters.size(); I < E; ++I) {
+ if (Data.Counts[I] + Counters[I] < Data.Counts[I])
+ return instrprof_error::counter_overflow;
+ Data.Counts[I] += Counters[I];
+ }
+ return instrprof_error::success;
+}
+
+void InstrProfWriter::write(raw_ostream &OS) {
+ // Write out the counts for each function.
+ for (const auto &I : FunctionData) {
+ StringRef Name = I.getKey();
+ uint64_t Hash = I.getValue().Hash;
+ const std::vector<uint64_t> &Counts = I.getValue().Counts;
+
+ OS << Name << "\n" << Hash << "\n" << Counts.size() << "\n";
+ for (uint64_t Count : Counts)
+ OS << Count << "\n";
+ OS << "\n";
+ }
+}