Skip to content

Commit c70f246

Browse files
authored
[ctxprof] add toYAML conversion to llvm-ctxprof-utils (#123131)
Also modified test file to match "toYaml" formatting.
1 parent b15845c commit c70f246

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
- Guid: 1000
2-
Counters: [1, 2, 3]
3-
Callsites: - []
4-
-
5-
- Guid: 2000
6-
Counters: [4, 5]
7-
- Guid: 18446744073709551613
8-
Counters: [6, 7, 8]
9-
-
10-
- Guid: 3000
11-
Counters: [40, 50]
12-
- Guid: 18446744073709551612
13-
Counters: [5, 9, 10]
1+
2+
- Guid: 1000
3+
Counters: [ 1, 2, 3 ]
4+
Callsites:
5+
- [ ]
6+
- - Guid: 2000
7+
Counters: [ 4, 5 ]
8+
- Guid: 18446744073709551613
9+
Counters: [ 6, 7, 8 ]
10+
- - Guid: 3000
11+
Counters: [ 40, 50 ]
12+
- Guid: 18446744073709551612
13+
Counters: [ 5, 9, 10 ]

llvm/test/tools/llvm-ctxprof-util/llvm-ctxprof-util.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
; RUN: llvm-ctxprof-util fromYAML --input %S/Inputs/empty.yaml -output %t/empty.bitstream
55
; RUN: llvm-bcanalyzer --dump %t/empty.bitstream | FileCheck %s --check-prefix=EMPTY
66

7-
; RUN: llvm-ctxprof-util fromYAML --input %S/Inputs/valid.yaml -output %t/valid.bitstream
7+
; RUN: llvm-ctxprof-util fromYAML -input %S/Inputs/valid.yaml -output %t/valid.bitstream
8+
; RUN: llvm-ctxprof-util toYAML -input %t/valid.bitstream -output %t/valid2.yaml
9+
; RUN: diff %t/valid2.yaml %S/Inputs/valid.yaml
810

911
; For the valid case, check against a reference output.
1012
; Note that uint64_t are printed as signed values by llvm-bcanalyzer:

llvm/tools/llvm-ctxprof-util/llvm-ctxprof-util.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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"
@@ -23,6 +24,7 @@
2324
using namespace llvm;
2425

2526
static cl::SubCommand FromYAML("fromYAML", "Convert from yaml");
27+
static cl::SubCommand ToYAML("toYAML", "Convert to yaml");
2628

2729
static 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

4042
static 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+
6486
int 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

Comments
 (0)