13
13
14
14
#include " llvm/ADT/StringRef.h"
15
15
#include " llvm/ProfileData/InstrProfReader.h"
16
+ #include " llvm/ProfileData/InstrProfWriter.h"
16
17
#include " llvm/Support/CommandLine.h"
17
18
#include " llvm/Support/ManagedStatic.h"
18
19
#include " llvm/Support/MemoryBuffer.h"
@@ -31,10 +32,8 @@ static void exitWithError(const Twine &Message, StringRef Whence = "") {
31
32
}
32
33
33
34
int merge_main (int argc, const char *argv[]) {
34
- cl::opt<std::string> Filename1 (cl::Positional, cl::Required,
35
- cl::desc (" file1" ));
36
- cl::opt<std::string> Filename2 (cl::Positional, cl::Required,
37
- cl::desc (" file2" ));
35
+ cl::list<std::string> Inputs (cl::Positional, cl::Required, cl::OneOrMore,
36
+ cl::desc (" <filenames...>" ));
38
37
39
38
cl::opt<std::string> OutputFilename (" output" , cl::value_desc (" output" ),
40
39
cl::init (" -" ),
@@ -44,12 +43,6 @@ int merge_main(int argc, const char *argv[]) {
44
43
45
44
cl::ParseCommandLineOptions (argc, argv, " LLVM profile data merger\n " );
46
45
47
- std::unique_ptr<InstrProfReader> Reader1, Reader2;
48
- if (error_code ec = InstrProfReader::create (Filename1, Reader1))
49
- exitWithError (ec.message (), Filename1);
50
- if (error_code ec = InstrProfReader::create (Filename2, Reader2))
51
- exitWithError (ec.message (), Filename2);
52
-
53
46
if (OutputFilename.empty ())
54
47
OutputFilename = " -" ;
55
48
@@ -58,32 +51,19 @@ int merge_main(int argc, const char *argv[]) {
58
51
if (!ErrorInfo.empty ())
59
52
exitWithError (ErrorInfo, OutputFilename);
60
53
61
- for (InstrProfIterator I1 = Reader1->begin (), E1 = Reader1->end (),
62
- I2 = Reader2->begin (), E2 = Reader2->end ();
63
- I1 != E1 && I2 != E2 ; ++I1, ++I2) {
64
- if (I1->Name != I2->Name )
65
- exitWithError (" Function name mismatch, " + I1->Name + " != " + I2->Name );
66
- if (I1->Hash != I2->Hash )
67
- exitWithError (" Function hash mismatch for " + I1->Name );
68
- if (I1->Counts .size () != I2->Counts .size ())
69
- exitWithError (" Function count mismatch for " + I1->Name );
70
-
71
- Output << I1->Name << " \n " << I1->Hash << " \n " << I1->Counts .size () << " \n " ;
72
-
73
- for (size_t II = 0 , EE = I1->Counts .size (); II < EE; ++II) {
74
- uint64_t Sum = I1->Counts [II] + I2->Counts [II];
75
- if (Sum < I1->Counts [II])
76
- exitWithError (" Counter overflow for " + I1->Name );
77
- Output << Sum << " \n " ;
78
- }
79
- Output << " \n " ;
54
+ InstrProfWriter Writer;
55
+ for (const auto &Filename : Inputs) {
56
+ std::unique_ptr<InstrProfReader> Reader;
57
+ if (error_code ec = InstrProfReader::create (Filename, Reader))
58
+ exitWithError (ec.message (), Filename);
59
+
60
+ for (const auto &I : *Reader)
61
+ if (error_code EC = Writer.addFunctionCounts (I.Name , I.Hash , I.Counts ))
62
+ errs () << Filename << " : " << I.Name << " : " << EC.message () << " \n " ;
63
+ if (Reader->hasError ())
64
+ exitWithError (Reader->getError ().message (), Filename);
80
65
}
81
- if (Reader1->hasError ())
82
- exitWithError (Reader1->getError ().message (), Filename1);
83
- if (Reader2->hasError ())
84
- exitWithError (Reader2->getError ().message (), Filename2);
85
- if (!Reader1->isEOF () || !Reader2->isEOF ())
86
- exitWithError (" Number of instrumented functions differ." );
66
+ Writer.write (Output);
87
67
88
68
return 0 ;
89
69
}
0 commit comments