Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions llvm/test/tools/llvm-ctxprof-util/Inputs/valid.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
- Guid: 1000
Counters: [1, 2, 3]
Callsites: - []
-
- Guid: 2000
Counters: [4, 5]
- Guid: 18446744073709551613
Counters: [6, 7, 8]
-
- Guid: 3000
Counters: [40, 50]
- Guid: 18446744073709551612
Counters: [5, 9, 10]

- Guid: 1000
Counters: [ 1, 2, 3 ]
Callsites:
- [ ]
- - Guid: 2000
Counters: [ 4, 5 ]
- Guid: 18446744073709551613
Counters: [ 6, 7, 8 ]
- - Guid: 3000
Counters: [ 40, 50 ]
- Guid: 18446744073709551612
Counters: [ 5, 9, 10 ]
4 changes: 3 additions & 1 deletion llvm/test/tools/llvm-ctxprof-util/llvm-ctxprof-util.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
; RUN: llvm-ctxprof-util fromYAML --input %S/Inputs/empty.yaml -output %t/empty.bitstream
; RUN: llvm-bcanalyzer --dump %t/empty.bitstream | FileCheck %s --check-prefix=EMPTY

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

; For the valid case, check against a reference output.
; Note that uint64_t are printed as signed values by llvm-bcanalyzer:
Expand Down
41 changes: 35 additions & 6 deletions llvm/tools/llvm-ctxprof-util/llvm-ctxprof-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/IR/GlobalValue.h"
#include "llvm/ProfileData/PGOCtxProfReader.h"
#include "llvm/ProfileData/PGOCtxProfWriter.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
Expand All @@ -23,6 +24,7 @@
using namespace llvm;

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

static cl::opt<std::string> InputFilename(
"input", cl::value_desc("input"), cl::init("-"),
Expand All @@ -35,15 +37,16 @@ static cl::opt<std::string> InputFilename(
"'Contexts', optional. An array containing arrays of contexts. The "
"context array at a position 'i' is the set of callees at that "
"callsite index. Use an empty array to indicate no callees."),
cl::sub(FromYAML));
cl::sub(FromYAML), cl::sub(ToYAML));

static cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
cl::init("-"),
cl::desc("Output file"),
cl::sub(FromYAML));
cl::sub(FromYAML), cl::sub(ToYAML));

namespace {
// Save the bitstream profile from the JSON representation.
Error convertFromYAML() {
Error convertFromYaml() {
auto BufOrError =
MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true);
if (!BufOrError)
Expand All @@ -61,19 +64,45 @@ Error convertFromYAML() {
return llvm::createCtxProfFromYAML(BufOrError.get()->getBuffer(), Out);
}

Error convertToYaml() {
auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilename);
if (!BufOrError)
return createFileError(InputFilename, BufOrError.getError());

std::error_code EC;
raw_fd_ostream Out(OutputFilename, EC);
if (EC)
return createStringError(EC, "failed to open output");
PGOCtxProfileReader Reader(BufOrError.get()->getBuffer());
auto Prof = Reader.loadContexts();
if (!Prof)
return Prof.takeError();
llvm::convertCtxProfToYaml(Out, *Prof);
Out << "\n";
return Error::success();
}
} // namespace

int main(int argc, const char **argv) {
cl::ParseCommandLineOptions(argc, argv, "LLVM Contextual Profile Utils\n");
ExitOnError ExitOnErr("llvm-ctxprof-util: ");
if (FromYAML) {
if (auto E = convertFromYAML()) {
auto HandleErr = [&](Error E) -> int {
if (E) {
handleAllErrors(std::move(E), [&](const ErrorInfoBase &E) {
E.log(errs());
errs() << "\n";
});
return 1;
}
return 0;
}
};

if (FromYAML)
return HandleErr(convertFromYaml());

if (ToYAML)
return HandleErr(convertToYaml());

cl::PrintHelpMessage();
return 1;
}
Loading