summaryrefslogtreecommitdiff
path: root/tools/llvm-profdata
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-02-17 23:22:49 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-02-17 23:22:49 +0000
commitddc5a010a40a7b85a020a072f9f6b4ae132e94bd (patch)
tree147cfb9555bac4adf6f6ef5a9c5cf3a8dcf69b7e /tools/llvm-profdata
parent4959a2d8780bb6a2424a0bb82df45d5150cef228 (diff)
PGO: llvm-profdata: tool for merging profiles
Introducing llvm-profdata, a tool for merging profile data generated by PGO instrumentation in clang. - The name indicates a file extension of <name>.profdata. Eventually profile data output by clang should be changed to that extension. - llvm-profdata merges two profiles. However, the name is more general, since it will likely pick up more tasks (such as summarizing a single profile). - llvm-profdata parses the current text-based format, but will be updated once we settle on a binary format. <rdar://problem/15949645> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201535 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-profdata')
-rw-r--r--tools/llvm-profdata/CMakeLists.txt5
-rw-r--r--tools/llvm-profdata/LLVMBuild.txt22
-rw-r--r--tools/llvm-profdata/Makefile17
-rw-r--r--tools/llvm-profdata/llvm-profdata.cpp178
4 files changed, 222 insertions, 0 deletions
diff --git a/tools/llvm-profdata/CMakeLists.txt b/tools/llvm-profdata/CMakeLists.txt
new file mode 100644
index 00000000000..4b1357d87e1
--- /dev/null
+++ b/tools/llvm-profdata/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(LLVM_LINK_COMPONENTS core support )
+
+add_llvm_tool(llvm-profdata
+ llvm-profdata.cpp
+ )
diff --git a/tools/llvm-profdata/LLVMBuild.txt b/tools/llvm-profdata/LLVMBuild.txt
new file mode 100644
index 00000000000..fc9e4691997
--- /dev/null
+++ b/tools/llvm-profdata/LLVMBuild.txt
@@ -0,0 +1,22 @@
+;===- ./tools/llvm-profdata/LLVMBuild.txt ----------------------*- Conf -*--===;
+;
+; The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+; http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Tool
+name = llvm-profdata
+parent = Tools
+required_libraries = Support
diff --git a/tools/llvm-profdata/Makefile b/tools/llvm-profdata/Makefile
new file mode 100644
index 00000000000..9d7ad527b16
--- /dev/null
+++ b/tools/llvm-profdata/Makefile
@@ -0,0 +1,17 @@
+##===- tools/llvm-profdata/Makefile ------------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL := ../..
+TOOLNAME := llvm-profdata
+LINK_COMPONENTS := core support
+
+# This tool has no plugins, optimize startup time.
+TOOL_NO_EXPORTS := 1
+
+include $(LEVEL)/Makefile.common
diff --git a/tools/llvm-profdata/llvm-profdata.cpp b/tools/llvm-profdata/llvm-profdata.cpp
new file mode 100644
index 00000000000..df10356eeff
--- /dev/null
+++ b/tools/llvm-profdata/llvm-profdata.cpp
@@ -0,0 +1,178 @@
+//===- llvm-profdata.cpp - LLVM profile data tool -------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// llvm-profdata merges .profdata files.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+
+static cl::opt<std::string> Filename1(cl::Positional, cl::Required,
+ cl::desc("file1"));
+static cl::opt<std::string> Filename2(cl::Positional, cl::Required,
+ cl::desc("file2"));
+
+static cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
+ cl::init("-"),
+ cl::desc("Output file"));
+static cl::alias OutputFilenameA("o", cl::desc("Alias for --output"),
+ cl::aliasopt(OutputFilename));
+
+static bool readLine(const char *&Start, const char *End, StringRef &S) {
+ if (Start == End)
+ return false;
+
+ for (const char *I = Start; I != End; ++I) {
+ assert(*I && "unexpected binary data");
+ if (*I == '\n') {
+ S = StringRef(Start, I - Start);
+ Start = I + 1;
+ return true;
+ }
+ }
+
+ S = StringRef(Start, End - Start);
+ Start = End;
+ return true;
+}
+
+static StringRef getWord(const char *&Start, const char *End) {
+ for (const char *I = Start; I != End; ++I)
+ if (*I == ' ') {
+ StringRef S(Start, I - Start);
+ Start = I + 1;
+ return S;
+ }
+ StringRef S(Start, End - Start);
+ Start = End;
+ return S;
+}
+
+static size_t splitWords(const StringRef &Line, std::vector<StringRef> &Words) {
+ const char *Start = Line.data();
+ const char *End = Line.data() + Line.size();
+ Words.clear();
+ while (Start != End)
+ Words.push_back(getWord(Start, End));
+ return Words.size();
+}
+
+static bool getNumber(const StringRef &S, uint64_t &N) {
+ N = 0;
+ for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I)
+ if (*I >= '0' && *I <= '9')
+ N = N * 10 + (*I - '0');
+ else
+ return false;
+
+ return true;
+}
+
+static void exitWithError(const std::string &Message,
+ const std::string &Filename, int64_t Line = -1) {
+ errs() << "error: " << Filename;
+ if (Line >= 0)
+ errs() << ":" << Line;
+ errs() << ": " << Message << "\n";
+ ::exit(1);
+}
+
+//===----------------------------------------------------------------------===//
+int main(int argc, char **argv) {
+ // Print a stack trace if we signal out.
+ sys::PrintStackTraceOnErrorSignal();
+ PrettyStackTraceProgram X(argc, argv);
+ llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
+
+ cl::ParseCommandLineOptions(argc, argv, "LLVM profile data merger\n");
+
+ OwningPtr<MemoryBuffer> File1;
+ OwningPtr<MemoryBuffer> File2;
+ if (error_code ec = MemoryBuffer::getFile(Filename1, File1))
+ exitWithError(ec.message(), Filename1);
+ if (error_code ec = MemoryBuffer::getFile(Filename2, File2))
+ exitWithError(ec.message(), Filename2);
+
+ if (OutputFilename.empty())
+ OutputFilename = "-";
+
+ std::string ErrorInfo;
+ raw_fd_ostream Output(OutputFilename.data(), ErrorInfo);
+ if (!ErrorInfo.empty())
+ exitWithError(ErrorInfo, OutputFilename);
+
+ const char *Start1 = File1->getBufferStart();
+ const char *Start2 = File2->getBufferStart();
+ const char *End1 = File1->getBufferEnd();
+ const char *End2 = File2->getBufferEnd();
+ const char *P1 = Start1;
+ const char *P2 = Start2;
+
+ StringRef Line1, Line2;
+ int64_t Num = 0;
+ while (readLine(P1, End1, Line1)) {
+ ++Num;
+ if (!readLine(P2, End2, Line2))
+ exitWithError("truncated file", Filename2, Num);
+
+ std::vector<StringRef> Words1, Words2;
+ if (splitWords(Line1, Words1) != splitWords(Line2, Words2))
+ exitWithError("data mismatch", Filename2, Num);
+
+ if (Words1.size() > 2)
+ exitWithError("invalid data", Filename1, Num);
+
+ if (Words1.empty()) {
+ Output << "\n";
+ continue;
+ }
+
+ if (Words1.size() == 2) {
+ if (Words1[0] != Words2[0])
+ exitWithError("function name mismatch", Filename2, Num);
+
+ uint64_t N1, N2;
+ if (!getNumber(Words1[1], N1))
+ exitWithError("bad function count", Filename1, Num);
+ if (!getNumber(Words2[1], N2))
+ exitWithError("bad function count", Filename2, Num);
+
+ if (N1 != N2)
+ exitWithError("function count mismatch", Filename2, Num);
+
+ Output << Line1 << "\n";
+ continue;
+ }
+
+ uint64_t N1, N2;
+ if (!getNumber(Words1[0], N1))
+ exitWithError("invalid counter", Filename1, Num);
+ if (!getNumber(Words2[0], N2))
+ exitWithError("invalid counter", Filename2, Num);
+
+ uint64_t Sum = N1 + N2;
+ if (Sum < N1)
+ exitWithError("counter overflow", Filename2, Num);
+
+ Output << N1 + N2 << "\n";
+ }
+ if (readLine(P2, End2, Line2))
+ exitWithError("truncated file", Filename1, Num + 1);
+
+ return 0;
+}