1313// ===----------------------------------------------------------------------===//
1414
1515#include " llvm/IR/GlobalValue.h"
16+ #include " llvm/ProfileData/PGOCtxProfReader.h"
1617#include " llvm/ProfileData/PGOCtxProfWriter.h"
1718#include " llvm/Support/CommandLine.h"
1819#include " llvm/Support/Error.h"
2324using namespace llvm ;
2425
2526static cl::SubCommand FromYAML (" fromYAML" , " Convert from yaml" );
27+ static cl::SubCommand ToYAML (" toYAML" , " Convert to yaml" );
2628
2729static cl::opt<std::string> InputFilename (
2830 " input" , cl::value_desc(" input" ), cl::init(" -" ),
@@ -35,15 +37,16 @@ static cl::opt<std::string> InputFilename(
3537 " 'Contexts', optional. An array containing arrays of contexts. The "
3638 " context array at a position 'i' is the set of callees at that "
3739 " callsite index. Use an empty array to indicate no callees." ),
38- cl::sub(FromYAML));
40+ cl::sub(FromYAML), cl::sub(ToYAML) );
3941
4042static cl::opt<std::string> OutputFilename (" output" , cl::value_desc(" output" ),
4143 cl::init(" -" ),
4244 cl::desc(" Output file" ),
43- cl::sub(FromYAML));
45+ cl::sub(FromYAML), cl::sub(ToYAML) );
4446
47+ namespace {
4548// Save the bitstream profile from the JSON representation.
46- Error convertFromYAML () {
49+ Error convertFromYaml () {
4750 auto BufOrError =
4851 MemoryBuffer::getFileOrSTDIN (InputFilename, /* IsText=*/ true );
4952 if (!BufOrError)
@@ -61,19 +64,45 @@ Error convertFromYAML() {
6164 return llvm::createCtxProfFromYAML (BufOrError.get ()->getBuffer (), Out);
6265}
6366
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+
6486int main (int argc, const char **argv) {
6587 cl::ParseCommandLineOptions (argc, argv, " LLVM Contextual Profile Utils\n " );
6688 ExitOnError ExitOnErr (" llvm-ctxprof-util: " );
67- if (FromYAML) {
68- if (auto E = convertFromYAML () ) {
89+ auto HandleErr = [&](Error E) -> int {
90+ if (E ) {
6991 handleAllErrors (std::move (E), [&](const ErrorInfoBase &E) {
7092 E.log (errs ());
7193 errs () << " \n " ;
7294 });
7395 return 1 ;
7496 }
7597 return 0 ;
76- }
98+ };
99+
100+ if (FromYAML)
101+ return HandleErr (convertFromYaml ());
102+
103+ if (ToYAML)
104+ return HandleErr (convertToYaml ());
105+
77106 cl::PrintHelpMessage ();
78107 return 1 ;
79108}
0 commit comments