13
13
// ===----------------------------------------------------------------------===//
14
14
15
15
#include " llvm/IR/GlobalValue.h"
16
+ #include " llvm/ProfileData/PGOCtxProfReader.h"
16
17
#include " llvm/ProfileData/PGOCtxProfWriter.h"
17
18
#include " llvm/Support/CommandLine.h"
18
19
#include " llvm/Support/Error.h"
23
24
using namespace llvm ;
24
25
25
26
static cl::SubCommand FromYAML (" fromYAML" , " Convert from yaml" );
27
+ static cl::SubCommand ToYAML (" toYAML" , " Convert to yaml" );
26
28
27
29
static cl::opt<std::string> InputFilename (
28
30
" input" , cl::value_desc(" input" ), cl::init(" -" ),
@@ -35,15 +37,16 @@ static cl::opt<std::string> InputFilename(
35
37
" 'Contexts', optional. An array containing arrays of contexts. The "
36
38
" context array at a position 'i' is the set of callees at that "
37
39
" callsite index. Use an empty array to indicate no callees." ),
38
- cl::sub(FromYAML));
40
+ cl::sub(FromYAML), cl::sub(ToYAML) );
39
41
40
42
static cl::opt<std::string> OutputFilename (" output" , cl::value_desc(" output" ),
41
43
cl::init(" -" ),
42
44
cl::desc(" Output file" ),
43
- cl::sub(FromYAML));
45
+ cl::sub(FromYAML), cl::sub(ToYAML) );
44
46
47
+ namespace {
45
48
// Save the bitstream profile from the JSON representation.
46
- Error convertFromYAML () {
49
+ Error convertFromYaml () {
47
50
auto BufOrError =
48
51
MemoryBuffer::getFileOrSTDIN (InputFilename, /* IsText=*/ true );
49
52
if (!BufOrError)
@@ -61,19 +64,45 @@ Error convertFromYAML() {
61
64
return llvm::createCtxProfFromYAML (BufOrError.get ()->getBuffer (), Out);
62
65
}
63
66
67
+ Error convertToYaml () {
68
+ auto BufOrError = MemoryBuffer::getFileOrSTDIN (InputFilename);
69
+ if (!BufOrError)
70
+ return createFileError (InputFilename, BufOrError.getError ());
71
+
72
+ std::error_code EC;
73
+ raw_fd_ostream Out (OutputFilename, EC);
74
+ if (EC)
75
+ return createStringError (EC, " failed to open output" );
76
+ PGOCtxProfileReader Reader (BufOrError.get ()->getBuffer ());
77
+ auto Prof = Reader.loadContexts ();
78
+ if (!Prof)
79
+ return Prof.takeError ();
80
+ llvm::convertCtxProfToYaml (Out, *Prof);
81
+ Out << " \n " ;
82
+ return Error::success ();
83
+ }
84
+ } // namespace
85
+
64
86
int main (int argc, const char **argv) {
65
87
cl::ParseCommandLineOptions (argc, argv, " LLVM Contextual Profile Utils\n " );
66
88
ExitOnError ExitOnErr (" llvm-ctxprof-util: " );
67
- if (FromYAML) {
68
- if (auto E = convertFromYAML () ) {
89
+ auto HandleErr = [&](Error E) -> int {
90
+ if (E ) {
69
91
handleAllErrors (std::move (E), [&](const ErrorInfoBase &E) {
70
92
E.log (errs ());
71
93
errs () << " \n " ;
72
94
});
73
95
return 1 ;
74
96
}
75
97
return 0 ;
76
- }
98
+ };
99
+
100
+ if (FromYAML)
101
+ return HandleErr (convertFromYaml ());
102
+
103
+ if (ToYAML)
104
+ return HandleErr (convertToYaml ());
105
+
77
106
cl::PrintHelpMessage ();
78
107
return 1 ;
79
108
}
0 commit comments