[llvm] r204210 - llvm-profdata: Update to use the naive text format with function hash
Justin Bogner
mail at justinbogner.com
Tue Mar 18 19:20:42 PDT 2014
Author: bogner
Date: Tue Mar 18 21:20:42 2014
New Revision: 204210
URL: http://llvm.org/viewvc/llvm-project?rev=204210&view=rev
Log:
llvm-profdata: Update to use the naive text format with function hash
This also uses line_iterator to simplify the parsing logic.
Added:
llvm/trunk/test/tools/llvm-profdata/Inputs/bad-hash.profdata
Removed:
llvm/trunk/test/tools/llvm-profdata/Inputs/bad-function-count.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/three-words-long.profdata
Modified:
llvm/trunk/include/llvm/Support/LineIterator.h
llvm/trunk/test/tools/llvm-profdata/Inputs/bar3-1.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-1.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-2.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-1.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-2.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-1.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-2.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/invalid-count-later.profdata
llvm/trunk/test/tools/llvm-profdata/Inputs/overflow.profdata
llvm/trunk/test/tools/llvm-profdata/errors.test
llvm/trunk/test/tools/llvm-profdata/simple.test
llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp
Modified: llvm/trunk/include/llvm/Support/LineIterator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/LineIterator.h?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/LineIterator.h (original)
+++ llvm/trunk/include/llvm/Support/LineIterator.h Tue Mar 18 21:20:42 2014
@@ -56,6 +56,7 @@ public:
/// \brief Get the current line as a \c StringRef.
StringRef operator*() const { return CurrentLine; }
+ const StringRef *operator->() const { return &CurrentLine; }
friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) {
return LHS.Buffer == RHS.Buffer &&
Removed: llvm/trunk/test/tools/llvm-profdata/Inputs/bad-function-count.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/bad-function-count.profdata?rev=204209&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/bad-function-count.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/bad-function-count.profdata (removed)
@@ -1,2 +0,0 @@
-function_count_not 1count
-1
Added: llvm/trunk/test/tools/llvm-profdata/Inputs/bad-hash.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/bad-hash.profdata?rev=204210&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/bad-hash.profdata (added)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/bad-hash.profdata Tue Mar 18 21:20:42 2014
@@ -0,0 +1,4 @@
+function_count_not
+badhash
+1
+1
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/bar3-1.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/bar3-1.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/bar3-1.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/bar3-1.profdata Tue Mar 18 21:20:42 2014
@@ -1,4 +1,6 @@
-bar 3
+bar
+3
+3
1
2
3
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-1.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-1.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-1.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-1.profdata Tue Mar 18 21:20:42 2014
@@ -1,4 +1,6 @@
-foo 3
+foo
+3
+3
1
2
3
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-2.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-2.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-2.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/foo3-2.profdata Tue Mar 18 21:20:42 2014
@@ -1,4 +1,6 @@
-foo 3
+foo
+3
+3
7
5
3
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-1.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-1.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-1.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-1.profdata Tue Mar 18 21:20:42 2014
@@ -1,9 +1,13 @@
-foo 3
+foo
+3
+3
2
3
5
-bar 3
+bar
+3
+3
7
11
13
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-2.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-2.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-2.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/foo3bar3-2.profdata Tue Mar 18 21:20:42 2014
@@ -1,9 +1,13 @@
-foo 3
+foo
+3
+3
17
19
23
-bar 3
+bar
+3
+3
29
31
37
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-1.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-1.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-1.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-1.profdata Tue Mar 18 21:20:42 2014
@@ -1,4 +1,6 @@
-foo 4
+foo
+4
+4
11
22
33
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-2.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-2.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-2.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/foo4-2.profdata Tue Mar 18 21:20:42 2014
@@ -1,4 +1,6 @@
-foo 4
+foo
+4
+4
7
6
5
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/invalid-count-later.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/invalid-count-later.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/invalid-count-later.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/invalid-count-later.profdata Tue Mar 18 21:20:42 2014
@@ -1,2 +1,4 @@
-invalid_count 1
+invalid_count
+1
+1
1later
Modified: llvm/trunk/test/tools/llvm-profdata/Inputs/overflow.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/overflow.profdata?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/overflow.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/overflow.profdata Tue Mar 18 21:20:42 2014
@@ -1,2 +1,4 @@
-overflow 1
+overflow
+1
+1
9223372036854775808
Removed: llvm/trunk/test/tools/llvm-profdata/Inputs/three-words-long.profdata
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/Inputs/three-words-long.profdata?rev=204209&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/Inputs/three-words-long.profdata (original)
+++ llvm/trunk/test/tools/llvm-profdata/Inputs/three-words-long.profdata (removed)
@@ -1 +0,0 @@
-three words long
Modified: llvm/trunk/test/tools/llvm-profdata/errors.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/errors.test?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/errors.test (original)
+++ llvm/trunk/test/tools/llvm-profdata/errors.test Tue Mar 18 21:20:42 2014
@@ -6,8 +6,8 @@ LENGTH: error: {{.*}}: truncated file
RUN: not llvm-profdata %p/Inputs/foo3-1.profdata %p/Inputs/bar3-1.profdata 2>&1 | FileCheck %s --check-prefix=NAME
NAME: error: {{.*}}: function name mismatch
-RUN: not llvm-profdata %p/Inputs/foo3-1.profdata %p/Inputs/foo4-1.profdata 2>&1 | FileCheck %s --check-prefix=COUNT
-COUNT: error: {{.*}}: function count mismatch
+RUN: not llvm-profdata %p/Inputs/foo3-1.profdata %p/Inputs/foo4-1.profdata 2>&1 | FileCheck %s --check-prefix=HASH
+HASH: error: {{.*}}: function hash mismatch
RUN: not llvm-profdata %p/Inputs/overflow.profdata %p/Inputs/overflow.profdata 2>&1 | FileCheck %s --check-prefix=OVERFLOW
OVERFLOW: error: {{.*}}: counter overflow
@@ -15,8 +15,5 @@ OVERFLOW: error: {{.*}}: counter overflo
RUN: not llvm-profdata %p/Inputs/invalid-count-later.profdata %p/Inputs/invalid-count-later.profdata 2>&1 | FileCheck %s --check-prefix=INVALID-COUNT-LATER
INVALID-COUNT-LATER: error: {{.*}}: invalid counter
-RUN: not llvm-profdata %p/Inputs/bad-function-count.profdata %p/Inputs/bad-function-count.profdata 2>&1 | FileCheck %s --check-prefix=BAD-FUNCTION-COUNT
-BAD-FUNCTION-COUNT: error: {{.*}}: bad function count
-
-RUN: not llvm-profdata %p/Inputs/three-words-long.profdata %p/Inputs/three-words-long.profdata 2>&1 | FileCheck %s --check-prefix=INVALID-DATA
-INVALID-DATA: error: {{.*}}: invalid data
+RUN: not llvm-profdata %p/Inputs/bad-hash.profdata %p/Inputs/bad-hash.profdata 2>&1 | FileCheck %s --check-prefix=BAD-HASH
+BAD-HASH: error: {{.*}}: bad function hash
Modified: llvm/trunk/test/tools/llvm-profdata/simple.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/simple.test?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/simple.test (original)
+++ llvm/trunk/test/tools/llvm-profdata/simple.test Tue Mar 18 21:20:42 2014
@@ -1,13 +1,17 @@
RUN: llvm-profdata %p/Inputs/foo3-1.profdata %p/Inputs/foo3-2.profdata 2>&1 | FileCheck %s --check-prefix=FOO3
RUN: llvm-profdata %p/Inputs/foo3-2.profdata %p/Inputs/foo3-1.profdata 2>&1 | FileCheck %s --check-prefix=FOO3
-FOO3: {{^foo 3$}}
+FOO3: {{^foo$}}
+FOO3-NEXT: {{^3$}}
+FOO3-NEXT: {{^3$}}
FOO3-NEXT: {{^8$}}
FOO3-NEXT: {{^7$}}
FOO3-NEXT: {{^6$}}
RUN: llvm-profdata %p/Inputs/foo4-1.profdata %p/Inputs/foo4-2.profdata 2>&1 | FileCheck %s --check-prefix=FOO4
RUN: llvm-profdata %p/Inputs/foo4-2.profdata %p/Inputs/foo4-1.profdata 2>&1 | FileCheck %s --check-prefix=FOO4
-FOO4: {{^foo 4$}}
+FOO4: {{^foo$}}
+FOO4-NEXT: {{^4$}}
+FOO4-NEXT: {{^4$}}
FOO4-NEXT: {{^18$}}
FOO4-NEXT: {{^28$}}
FOO4-NEXT: {{^38$}}
@@ -15,11 +19,15 @@ FOO4-NEXT: {{^48$}}
RUN: llvm-profdata %p/Inputs/foo3bar3-1.profdata %p/Inputs/foo3bar3-2.profdata 2>&1 | FileCheck %s --check-prefix=FOO3BAR3
RUN: llvm-profdata %p/Inputs/foo3bar3-2.profdata %p/Inputs/foo3bar3-1.profdata 2>&1 | FileCheck %s --check-prefix=FOO3BAR3
-FOO3BAR3: {{^foo 3$}}
+FOO3BAR3: {{^foo$}}
+FOO3BAR3-NEXT: {{^3$}}
+FOO3BAR3-NEXT: {{^3$}}
FOO3BAR3-NEXT: {{^19$}}
FOO3BAR3-NEXT: {{^22$}}
FOO3BAR3-NEXT: {{^28$}}
-FOO3BAR3: {{^bar 3$}}
+FOO3BAR3: {{^bar$}}
+FOO3BAR3-NEXT: {{^3$}}
+FOO3BAR3-NEXT: {{^3$}}
FOO3BAR3-NEXT: {{^36$}}
FOO3BAR3-NEXT: {{^42$}}
FOO3BAR3-NEXT: {{^50$}}
Modified: llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp?rev=204210&r1=204209&r2=204210&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp (original)
+++ llvm/trunk/tools/llvm-profdata/llvm-profdata.cpp Tue Mar 18 21:20:42 2014
@@ -13,6 +13,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/LineIterator.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
@@ -32,56 +33,6 @@ static cl::opt<std::string> OutputFilena
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;
@@ -115,63 +66,64 @@ int main(int argc, char **argv) {
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()) {
+ enum {ReadName, ReadHash, ReadCount, ReadCounters} State = ReadName;
+ uint64_t N1, N2, NumCounters;
+ line_iterator I1(*File1, '#'), I2(*File2, '#');
+ for (; !I1.is_at_end() && !I2.is_at_end(); ++I1, ++I2) {
+ if (I1->empty()) {
+ if (!I2->empty())
+ exitWithError("data mismatch", Filename2, I2.line_number());
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);
-
+ switch (State) {
+ case ReadName:
+ if (*I1 != *I2)
+ exitWithError("function name mismatch", Filename2, I2.line_number());
+ Output << *I1 << "\n";
+ State = ReadHash;
+ break;
+ case ReadHash:
+ if (I1->getAsInteger(10, N1))
+ exitWithError("bad function hash", Filename1, I1.line_number());
+ if (I2->getAsInteger(10, N2))
+ exitWithError("bad function hash", Filename2, I2.line_number());
if (N1 != N2)
- exitWithError("function count mismatch", Filename2, Num);
-
- Output << Line1 << "\n";
- continue;
+ exitWithError("function hash mismatch", Filename2, I2.line_number());
+ Output << N1 << "\n";
+ State = ReadCount;
+ break;
+ case ReadCount:
+ if (I1->getAsInteger(10, N1))
+ exitWithError("bad function count", Filename1, I1.line_number());
+ if (I2->getAsInteger(10, N2))
+ exitWithError("bad function count", Filename2, I2.line_number());
+ if (N1 != N2)
+ exitWithError("function count mismatch", Filename2, I2.line_number());
+ Output << N1 << "\n";
+ NumCounters = N1;
+ State = ReadCounters;
+ break;
+ case ReadCounters:
+ if (I1->getAsInteger(10, N1))
+ exitWithError("invalid counter", Filename1, I1.line_number());
+ if (I2->getAsInteger(10, N2))
+ exitWithError("invalid counter", Filename2, I2.line_number());
+ uint64_t Sum = N1 + N2;
+ if (Sum < N1)
+ exitWithError("counter overflow", Filename2, I2.line_number());
+ Output << N1 + N2 << "\n";
+ if (--NumCounters == 0)
+ State = ReadName;
+ break;
}
-
- 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);
+ if (!I1.is_at_end())
+ exitWithError("truncated file", Filename1, I1.line_number());
+ if (!I2.is_at_end())
+ exitWithError("truncated file", Filename2, I2.line_number());
+ if (State != ReadName)
+ exitWithError("truncated file", Filename1, I1.line_number());
return 0;
}
More information about the llvm-commits
mailing list