[ctxprof] add toYAML conversion to llvm-ctxprof-utils (#123131)

Also modified test file to match "toYaml" formatting.
This commit is contained in:
Mircea Trofin 2025-01-15 16:53:43 -08:00 committed by GitHub
parent b15845c005
commit c70f246251
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 20 deletions

View File

@ -1,13 +1,13 @@
- Guid: 1000
Counters: [ 1, 2, 3 ]
Callsites: - []
-
- Guid: 2000
Callsites:
- [ ]
- - Guid: 2000
Counters: [ 4, 5 ]
- Guid: 18446744073709551613
Counters: [ 6, 7, 8 ]
-
- Guid: 3000
- - Guid: 3000
Counters: [ 40, 50 ]
- Guid: 18446744073709551612
Counters: [ 5, 9, 10 ]

View File

@ -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:

View File

@ -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"
@ -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("-"),
@ -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)
@ -61,11 +64,30 @@ 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";
@ -73,7 +95,14 @@ int main(int argc, const char **argv) {
return 1;
}
return 0;
}
};
if (FromYAML)
return HandleErr(convertFromYaml());
if (ToYAML)
return HandleErr(convertToYaml());
cl::PrintHelpMessage();
return 1;
}