-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[ctx_prof] test tool: generate ctxprof bistream from json #100379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+299
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5f7ff32
[ctx_prof] test tool: generate ctxprof bistream from json
mtrofin 6400f05
full output
mtrofin b26498e
inlined valid test, reformat, llvm_unreachable
mtrofin fa68caa
Merge branch 'main' into ctx_testing
mtrofin 796ff2d
newline for valid.json
mtrofin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
8 changes: 8 additions & 0 deletions
8
llvm/test/tools/llvm-ctxprof-util/Inputs/invalid-bad-subctx.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[{ | ||
"Guid": 123, | ||
"Counters": [1, 2], | ||
"Callsites": | ||
[ | ||
{"Guid": 1} | ||
] | ||
}] |
5 changes: 5 additions & 0 deletions
5
llvm/test/tools/llvm-ctxprof-util/Inputs/invalid-no-counters.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
{ | ||
"Guid": 1231 | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{}] |
1 change: 1 addition & 0 deletions
1
llvm/test/tools/llvm-ctxprof-util/Inputs/invalid-no-vector.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
[ | ||
{ | ||
"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 | ||
] | ||
} | ||
] |
22 changes: 22 additions & 0 deletions
22
llvm/test/tools/llvm-ctxprof-util/llvm-ctxprof-util-negative.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
; RUN: not llvm-ctxprof-util nofile.json 2>&1 | FileCheck %s --check-prefix=NO_CMD | ||
; RUN: not llvm-ctxprof-util invalidCmd --input nofile.json 2>&1 | FileCheck %s --check-prefix=INVALID_CMD | ||
; RUN: not llvm-ctxprof-util fromJSON nofile.json 2>&1 | FileCheck %s --check-prefix=NO_FLAG | ||
; RUN: not llvm-ctxprof-util fromJSON --input nofile.json 2>&1 | FileCheck %s --check-prefix=NO_FILE | ||
; RUN: not llvm-ctxprof-util fromJSON --input %S/Inputs/bad.json 2>&1 | FileCheck %s --check-prefix=BAD_JSON | ||
; RUN: not llvm-ctxprof-util fromJSON --input %S/Inputs/invalid-no-vector.json 2>&1 | FileCheck %s --check-prefix=NO_VECTOR | ||
; RUN: not llvm-ctxprof-util fromJSON --input %S/Inputs/invalid-no-ctx.json 2>&1 | FileCheck %s --check-prefix=NO_CTX | ||
; RUN: not llvm-ctxprof-util fromJSON --input %S/Inputs/invalid-no-counters.json 2>&1 | FileCheck %s --check-prefix=NO_COUNTERS | ||
; RUN: not llvm-ctxprof-util fromJSON --input %S/Inputs/invalid-bad-subctx.json 2>&1 | FileCheck %s --check-prefix=BAD_SUBCTX | ||
; RUN: rm -rf %t | ||
; RUN: not llvm-ctxprof-util fromJSON --input %S/Inputs/valid.json --output %t/output.bitstream 2>&1 | FileCheck %s --check-prefix=NO_DIR | ||
|
||
; NO_CMD: Unknown subcommand 'nofile.json' | ||
; INVALID_CMD: Unknown subcommand 'invalidCmd' | ||
; NO_FLAG: Unknown command line argument 'nofile.json'. | ||
; NO_FILE: 'nofile.json': No such file or directory | ||
; BAD_JSON: Expected object key | ||
; NO_VECTOR: expected array | ||
; NO_CTX: missing value at (root)[0].Guid | ||
; NO_COUNTERS: missing value at (root)[0].Counters | ||
; BAD_SUBCTX: expected array at (root)[0].Callsites[0] | ||
; NO_DIR: failed to open output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
; RUN: mkdir -p %t | ||
; RUN: llvm-ctxprof-util fromJSON --input %S/Inputs/empty.json -output %t/empty.bitstream | ||
; RUN: llvm-bcanalyzer --dump %t/empty.bitstream | FileCheck %s --check-prefix=EMPTY | ||
|
||
; RUN: llvm-ctxprof-util fromJSON --input %S/Inputs/valid.json -output %t/valid.bitstream | ||
|
||
; For the valid case, check against a reference output. | ||
; Note that uint64_t are printed as signed values by llvm-bcanalyzer: | ||
; * 18446744073709551613 in json is -3 in the output | ||
; * 18446744073709551612 in json is -4 in the output | ||
; Also we have no callee/context at index 0, 2 callsites for index 1, and one for | ||
; index 2. | ||
; RUN: llvm-bcanalyzer --dump %t/valid.bitstream | FileCheck %s --check-prefix=VALID | ||
|
||
; EMPTY: <BLOCKINFO_BLOCK/> | ||
; EMPTY-NEXT: <Metadata NumWords=1 BlockCodeSize=2> | ||
; EMPTY-NEXT: <Version op0=1/> | ||
; EMPTY-NEXT: </Metadata> | ||
|
||
; VALID: <BLOCKINFO_BLOCK/> | ||
; VALID-NEXT: <Metadata NumWords=30 BlockCodeSize=2> | ||
; VALID-NEXT: <Version op0=1/> | ||
; VALID-NEXT: <Context NumWords=20 BlockCodeSize=2> | ||
; VALID-NEXT: <GUID op0=1000/> | ||
; VALID-NEXT: <Counters op0=1 op1=2 op2=3/> | ||
; VALID-NEXT: <Context NumWords=5 BlockCodeSize=2> | ||
; VALID-NEXT: <GUID op0=-3/> | ||
; VALID-NEXT: <CalleeIndex op0=1/> | ||
; VALID-NEXT: <Counters op0=6 op1=7 op2=8/> | ||
; VALID-NEXT: </Context> | ||
; VALID-NEXT: <Context NumWords=3 BlockCodeSize=2> | ||
; VALID-NEXT: <GUID op0=2000/> | ||
; VALID-NEXT: <CalleeIndex op0=1/> | ||
; VALID-NEXT: <Counters op0=4 op1=5/> | ||
; VALID-NEXT: </Context> | ||
; VALID-NEXT: <Context NumWords=3 BlockCodeSize=2> | ||
; VALID-NEXT: <GUID op0=3000/> | ||
; VALID-NEXT: <CalleeIndex op0=2/> | ||
; VALID-NEXT: <Counters op0=40 op1=50/> | ||
; VALID-NEXT: </Context> | ||
; VALID-NEXT: </Context> | ||
; VALID-NEXT: <Context NumWords=4 BlockCodeSize=2> | ||
; VALID-NEXT: <GUID op0=-4/> | ||
; VALID-NEXT: <Counters op0=5 op1=9 op2=10/> | ||
; VALID-NEXT: </Context> | ||
; VALID-NEXT: </Metadata> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
Core | ||
Object | ||
ProfileData | ||
Support | ||
) | ||
|
||
add_llvm_tool(llvm-ctxprof-util | ||
llvm-ctxprof-util.cpp | ||
|
||
DEPENDS | ||
intrinsics_gen | ||
GENERATE_DRIVER | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
//===--- PGOCtxProfJSONReader.h - JSON format ------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// | ||
/// JSON format for the contextual profile for testing. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/IR/GlobalValue.h" | ||
#include "llvm/ProfileData/CtxInstrContextNode.h" | ||
#include "llvm/ProfileData/PGOCtxProfWriter.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
#include "llvm/Support/JSON.h" | ||
#include "llvm/Support/LLVMDriver.h" | ||
#include "llvm/Support/MemoryBuffer.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
|
||
using namespace llvm; | ||
|
||
static cl::SubCommand FromJSON("fromJSON", "Convert from json"); | ||
|
||
static cl::opt<std::string> InputFilename( | ||
"input", cl::value_desc("input"), cl::init("-"), | ||
cl::desc( | ||
"Input file. The format is an array of contexts.\n" | ||
"Each context is a dictionary with the following keys:\n" | ||
"'Guid', mandatory. The value is a 64-bit integer.\n" | ||
"'Counters', mandatory. An array of 32-bit ints. These are the " | ||
"counter values.\n" | ||
"'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(FromJSON)); | ||
|
||
static cl::opt<std::string> OutputFilename("output", cl::value_desc("output"), | ||
cl::init("-"), | ||
cl::desc("Output file"), | ||
cl::sub(FromJSON)); | ||
|
||
namespace { | ||
// A structural representation of the JSON input. | ||
struct DeserializableCtx { | ||
GlobalValue::GUID Guid = 0; | ||
std::vector<uint64_t> Counters; | ||
std::vector<std::vector<DeserializableCtx>> Callsites; | ||
}; | ||
|
||
ctx_profile::ContextNode * | ||
createNode(std::vector<std::unique_ptr<char[]>> &Nodes, | ||
const std::vector<DeserializableCtx> &DCList); | ||
|
||
// Convert a DeserializableCtx into a ContextNode, potentially linking it to | ||
// its sibling (e.g. callee at same callsite) "Next". | ||
ctx_profile::ContextNode * | ||
createNode(std::vector<std::unique_ptr<char[]>> &Nodes, | ||
const DeserializableCtx &DC, | ||
ctx_profile::ContextNode *Next = nullptr) { | ||
auto AllocSize = ctx_profile::ContextNode::getAllocSize(DC.Counters.size(), | ||
DC.Callsites.size()); | ||
auto *Mem = Nodes.emplace_back(std::make_unique<char[]>(AllocSize)).get(); | ||
std::memset(Mem, 0, AllocSize); | ||
auto *Ret = new (Mem) ctx_profile::ContextNode(DC.Guid, DC.Counters.size(), | ||
DC.Callsites.size(), Next); | ||
std::memcpy(Ret->counters(), DC.Counters.data(), | ||
sizeof(uint64_t) * DC.Counters.size()); | ||
for (const auto &[I, DCList] : llvm::enumerate(DC.Callsites)) | ||
Ret->subContexts()[I] = createNode(Nodes, DCList); | ||
return Ret; | ||
} | ||
|
||
// Convert a list of DeserializableCtx into a linked list of ContextNodes. | ||
ctx_profile::ContextNode * | ||
createNode(std::vector<std::unique_ptr<char[]>> &Nodes, | ||
const std::vector<DeserializableCtx> &DCList) { | ||
ctx_profile::ContextNode *List = nullptr; | ||
for (const auto &DC : DCList) | ||
List = createNode(Nodes, DC, List); | ||
return List; | ||
} | ||
} // namespace | ||
|
||
namespace llvm { | ||
namespace json { | ||
// Hook into the JSON deserialization. | ||
bool fromJSON(const Value &E, DeserializableCtx &R, Path P) { | ||
json::ObjectMapper Mapper(E, P); | ||
return Mapper && Mapper.map("Guid", R.Guid) && | ||
Mapper.map("Counters", R.Counters) && | ||
Mapper.mapOptional("Callsites", R.Callsites); | ||
} | ||
} // namespace json | ||
} // namespace llvm | ||
|
||
// Save the bitstream profile from the JSON representation. | ||
Error convertFromJSON() { | ||
auto BufOrError = MemoryBuffer::getFileOrSTDIN(InputFilename); | ||
if (!BufOrError) | ||
return createFileError(InputFilename, BufOrError.getError()); | ||
auto P = json::parse(BufOrError.get()->getBuffer()); | ||
if (!P) | ||
return P.takeError(); | ||
|
||
std::vector<DeserializableCtx> DCList; | ||
json::Path::Root R(""); | ||
if (!fromJSON(*P, DCList, R)) | ||
return R.getError(); | ||
// Nodes provides memory backing for the ContextualNodes. | ||
std::vector<std::unique_ptr<char[]>> Nodes; | ||
std::error_code EC; | ||
raw_fd_stream Out(OutputFilename, EC); | ||
if (EC) | ||
return createStringError(EC, "failed to open output"); | ||
PGOCtxProfileWriter Writer(Out); | ||
for (const auto &DC : DCList) { | ||
auto *TopList = createNode(Nodes, DC); | ||
if (!TopList) | ||
return createStringError( | ||
"Unexpected error converting internal structure to ctx profile"); | ||
Writer.write(*TopList); | ||
} | ||
if (EC) | ||
return createStringError(EC, "failed to write output"); | ||
return Error::success(); | ||
} | ||
|
||
int llvm_ctxprof_util_main(int argc, char **argv, const llvm::ToolContext &) { | ||
cl::ParseCommandLineOptions(argc, argv, "LLVM Contextual Profile Utils\n"); | ||
ExitOnError ExitOnErr("llvm-ctxprof-util: "); | ||
if (FromJSON) { | ||
if (auto E = convertFromJSON()) { | ||
handleAllErrors(std::move(E), [&](const ErrorInfoBase &E) { | ||
E.log(errs()); | ||
errs() << "\n"; | ||
}); | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
llvm_unreachable("Unknown subcommands should have been handled by the " | ||
"command line parser."); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in #89884 (comment) , cl:: is incompatible with GENERATE_DRIVER. Either remove GENERATE_DRIVER, or move this to Opt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack, I'll take care of of it. Thanks.