-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
[Exegesis] Add supports to serialize/deserialize object files into benchmarks #121993
Open
mshockwave
wants to merge
4
commits into
llvm:main
Choose a base branch
from
mshockwave:patch/exegesis/serialize-benchmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f69c8ab
[Exegesis] Add supports to serialize/deserialize benchmarks
mshockwave 14913ca
fixup! Turn serialize-obj-file.test into a generic test
mshockwave 619a4c3
fixup! Address review comments
mshockwave c039376
fixup! Use a shorter flag name
mshockwave 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 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 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 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,41 @@ | ||
# RUN: llvm-exegesis -mtriple=riscv64 -mcpu=sifive-p470 --opcode-name=SH3ADD --serialize-benchmarks --benchmark-phase=assemble-measured-code \ | ||
# RUN: --mode=latency --benchmarks-file=%t.yaml | ||
# RUN: FileCheck --input-file=%t.yaml %s --check-prefixes=CHECK,SERIALIZE | ||
# RUN: llvm-exegesis -mtriple=riscv64 -mcpu=sifive-p470 --run-measurement=%t.yaml --mode=latency --benchmark-phase=dry-run-measurement --use-dummy-perf-counters \ | ||
# RUN: --dump-object-to-disk=%t.o | FileCheck %s --check-prefixes=CHECK,DESERIALIZE | ||
# RUN: llvm-objdump -d %t.o | FileCheck %s --check-prefix=OBJDUMP | ||
|
||
# We should not serialie benchmarks by default. | ||
# RUN: llvm-exegesis -mtriple=riscv64 -mcpu=sifive-p470 --opcode-name=SH3ADD --benchmark-phase=assemble-measured-code --mode=latency | \ | ||
# RUN: FileCheck %s --check-prefix=NO-SERIALIZE | ||
|
||
# We currently don't support serialization for repetition modes that require more than one snippets. | ||
# RUN: not llvm-exegesis -mtriple=riscv64 -mcpu=sifive-p470 --opcode-name=SH3ADD --mode=latency --serialize-benchmarks --benchmark-phase=assemble-measured-code \ | ||
# RUN: --repetition-mode=min 2>&1 | FileCheck %s --check-prefix=NOT-SUPPORTED | ||
# RUN: not llvm-exegesis -mtriple=riscv64 -mcpu=sifive-p470 --opcode-name=SH3ADD --mode=latency --serialize-benchmarks --benchmark-phase=assemble-measured-code \ | ||
# RUN: --repetition-mode=middle-half-loop 2>&1 | FileCheck %s --check-prefix=NOT-SUPPORTED | ||
# RUN: not llvm-exegesis -mtriple=riscv64 -mcpu=sifive-p470 --opcode-name=SH3ADD --mode=latency --serialize-benchmarks --benchmark-phase=assemble-measured-code \ | ||
# RUN: --repetition-mode=middle-half-duplicate 2>&1 | FileCheck %s --check-prefix=NOT-SUPPORTED | ||
|
||
# REQUIRES: riscv-registered-target && native-registered-exegesis-target | ||
# REQUIRES: zlib || zstd | ||
|
||
# A round-trip test for serialize/deserialize benchmarks. | ||
|
||
# CHECK: mode: latency | ||
# CHECK: instructions: | ||
# CHECK-NEXT: - 'SH3ADD X{{.*}} X{{.*}} X{{.*}}' | ||
# CHECK: cpu_name: sifive-p470 | ||
# CHECK-NEXT: llvm_triple: riscv64 | ||
# CHECK-NEXT: min_instructions: 10000 | ||
# CHECK-NEXT: measurements: [] | ||
# SERIALIZE: error: actual measurements skipped. | ||
# DESERIALIZE: error: '' | ||
# CHECK: info: Repeating a single explicitly serial instruction | ||
|
||
# OBJDUMP: sh3add | ||
|
||
# Negative tests. | ||
|
||
# NOT-SUPPORTED: -serialize-benchmarks currently only supports -repetition-mode of loop and duplicate. | ||
# NO-SERIALIZE-NOT: object_file: |
This file contains 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 |
---|---|---|
|
@@ -15,10 +15,13 @@ | |
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/ADT/bit.h" | ||
#include "llvm/ObjectYAML/YAML.h" | ||
#include "llvm/Support/Base64.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/Errc.h" | ||
#include "llvm/Support/FileOutputBuffer.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include "llvm/Support/Format.h" | ||
#include "llvm/Support/Timer.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
|
||
static constexpr const char kIntegerPrefix[] = "i_0x"; | ||
|
@@ -27,6 +30,13 @@ static constexpr const char kInvalidOperand[] = "INVALID"; | |
|
||
namespace llvm { | ||
|
||
static cl::opt<compression::Format> ForceObjectFileCompressionFormat( | ||
"force-serialized-obj-compress-format", cl::Hidden, | ||
cl::desc( | ||
"Force to use this compression format for serialized object files."), | ||
cl::values(clEnumValN(compression::Format::Zstd, "zstd", "Using Zstandard"), | ||
clEnumValN(compression::Format::Zlib, "zlib", "Using LibZ"))); | ||
|
||
namespace { | ||
|
||
// A mutable struct holding an LLVMState that can be passed through the | ||
|
@@ -288,6 +298,33 @@ template <> struct MappingContextTraits<exegesis::BenchmarkKey, YamlContext> { | |
} | ||
}; | ||
|
||
template <> struct MappingTraits<exegesis::Benchmark::ObjectFile> { | ||
struct NormalizedBase64Binary { | ||
std::string Base64Str; | ||
|
||
NormalizedBase64Binary(IO &) {} | ||
NormalizedBase64Binary(IO &, const std::vector<uint8_t> &Data) | ||
: Base64Str(llvm::encodeBase64(Data)) {} | ||
|
||
std::vector<uint8_t> denormalize(IO &) { | ||
std::vector<char> Buffer; | ||
if (Error E = llvm::decodeBase64(Base64Str, Buffer)) | ||
report_fatal_error(std::move(E)); | ||
|
||
StringRef Data(Buffer.data(), Buffer.size()); | ||
return std::vector<uint8_t>(Data.bytes_begin(), Data.bytes_end()); | ||
} | ||
}; | ||
|
||
static void mapping(IO &Io, exegesis::Benchmark::ObjectFile &Obj) { | ||
Io.mapRequired("compression", Obj.CompressionFormat); | ||
Io.mapRequired("original_size", Obj.UncompressedSize); | ||
MappingNormalization<NormalizedBase64Binary, std::vector<uint8_t>> | ||
ObjFileString(Io, Obj.CompressedBytes); | ||
Io.mapRequired("compressed_bytes", ObjFileString->Base64Str); | ||
} | ||
}; | ||
|
||
template <> struct MappingContextTraits<exegesis::Benchmark, YamlContext> { | ||
struct NormalizedBinary { | ||
NormalizedBinary(IO &io) {} | ||
|
@@ -325,9 +362,11 @@ template <> struct MappingContextTraits<exegesis::Benchmark, YamlContext> { | |
Io.mapRequired("error", Obj.Error); | ||
Io.mapOptional("info", Obj.Info); | ||
// AssembledSnippet | ||
MappingNormalization<NormalizedBinary, std::vector<uint8_t>> BinaryString( | ||
MappingNormalization<NormalizedBinary, std::vector<uint8_t>> SnippetString( | ||
Io, Obj.AssembledSnippet); | ||
Io.mapOptional("assembled_snippet", BinaryString->Binary); | ||
Io.mapOptional("assembled_snippet", SnippetString->Binary); | ||
// ObjectFile | ||
Io.mapOptional("object_file", Obj.ObjFile); | ||
} | ||
}; | ||
|
||
|
@@ -364,6 +403,52 @@ Benchmark::readTriplesAndCpusFromYamls(MemoryBufferRef Buffer) { | |
return Result; | ||
} | ||
|
||
Error Benchmark::setObjectFile(StringRef RawBytes) { | ||
SmallVector<uint8_t> CompressedBytes; | ||
llvm::compression::Format CompressionFormat; | ||
|
||
auto isFormatAvailable = [](llvm::compression::Format F) -> bool { | ||
switch (F) { | ||
case compression::Format::Zstd: | ||
return compression::zstd::isAvailable(); | ||
case compression::Format::Zlib: | ||
return compression::zlib::isAvailable(); | ||
} | ||
}; | ||
if (ForceObjectFileCompressionFormat.getNumOccurrences() > 0) { | ||
CompressionFormat = ForceObjectFileCompressionFormat; | ||
if (!isFormatAvailable(CompressionFormat)) | ||
return make_error<StringError>( | ||
"The designated compression format is not available.", | ||
inconvertibleErrorCode()); | ||
} else if (isFormatAvailable(compression::Format::Zstd)) { | ||
// Try newer compression algorithm first. | ||
CompressionFormat = compression::Format::Zstd; | ||
} else if (isFormatAvailable(compression::Format::Zlib)) { | ||
CompressionFormat = compression::Format::Zlib; | ||
} else { | ||
return make_error<StringError>( | ||
"None of the compression methods is available.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is -> are |
||
inconvertibleErrorCode()); | ||
} | ||
|
||
switch (CompressionFormat) { | ||
case compression::Format::Zstd: | ||
compression::zstd::compress({RawBytes.bytes_begin(), RawBytes.bytes_end()}, | ||
CompressedBytes); | ||
break; | ||
case compression::Format::Zlib: | ||
compression::zlib::compress({RawBytes.bytes_begin(), RawBytes.bytes_end()}, | ||
CompressedBytes); | ||
break; | ||
} | ||
|
||
ObjFile = {CompressionFormat, | ||
RawBytes.size(), | ||
{CompressedBytes.begin(), CompressedBytes.end()}}; | ||
return Error::success(); | ||
} | ||
|
||
Expected<Benchmark> Benchmark::readYaml(const LLVMState &State, | ||
MemoryBufferRef Buffer) { | ||
yaml::Input Yin(Buffer); | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/ADT/Twine.h" | ||
#include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/CrashRecoveryContext.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/FileSystem.h" | ||
|
@@ -51,6 +52,14 @@ | |
#endif // __linux__ | ||
|
||
namespace llvm { | ||
|
||
static cl::opt<bool> | ||
SerializeBenchmarks("serialize-benchmarks", | ||
cl::desc("Generate fully-serialized benchmarks " | ||
"that can later be deserialized and " | ||
"resuming the measurement."), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resuming -> resume |
||
cl::init(false)); | ||
|
||
namespace exegesis { | ||
|
||
BenchmarkRunner::BenchmarkRunner(const LLVMState &State, Benchmark::ModeE Mode, | ||
|
@@ -619,6 +628,7 @@ Expected<SmallString<0>> BenchmarkRunner::assembleSnippet( | |
Expected<BenchmarkRunner::RunnableConfiguration> | ||
BenchmarkRunner::getRunnableConfiguration( | ||
const BenchmarkCode &BC, unsigned MinInstructions, unsigned LoopBodySize, | ||
Benchmark::RepetitionModeE RepetitionMode, | ||
const SnippetRepetitor &Repetitor) const { | ||
RunnableConfiguration RC; | ||
|
||
|
@@ -663,12 +673,57 @@ BenchmarkRunner::getRunnableConfiguration( | |
LoopBodySize, GenerateMemoryInstructions); | ||
if (Error E = Snippet.takeError()) | ||
return std::move(E); | ||
|
||
// Generate fully-serialized benchmarks. | ||
if (SerializeBenchmarks) { | ||
if (RepetitionMode != Benchmark::Loop && | ||
RepetitionMode != Benchmark::Duplicate) | ||
return make_error<Failure>( | ||
"-serialize-benchmarks currently only supports -repetition-mode " | ||
"of loop and duplicate."); | ||
|
||
if (Error E = BenchmarkResult.setObjectFile(*Snippet)) | ||
return std::move(E); | ||
} | ||
|
||
RC.ObjectFile = getObjectFromBuffer(*Snippet); | ||
} | ||
|
||
return std::move(RC); | ||
} | ||
|
||
Expected<BenchmarkRunner::RunnableConfiguration> | ||
BenchmarkRunner::getRunnableConfiguration(Benchmark &&B) const { | ||
assert(B.ObjFile.has_value() && B.ObjFile->isValid() && | ||
"No serialized obejct file is attached?"); | ||
const Benchmark::ObjectFile &ObjFile = *B.ObjFile; | ||
SmallVector<uint8_t> DecompressedObjFile; | ||
switch (ObjFile.CompressionFormat) { | ||
case compression::Format::Zstd: | ||
if (!compression::zstd::isAvailable()) | ||
return make_error<StringError>("zstd is not available for decompression.", | ||
inconvertibleErrorCode()); | ||
if (Error E = compression::zstd::decompress(ObjFile.CompressedBytes, | ||
DecompressedObjFile, | ||
ObjFile.UncompressedSize)) | ||
return std::move(E); | ||
break; | ||
case compression::Format::Zlib: | ||
if (!compression::zlib::isAvailable()) | ||
return make_error<StringError>("zlib is not available for decompression.", | ||
inconvertibleErrorCode()); | ||
if (Error E = compression::zlib::decompress(ObjFile.CompressedBytes, | ||
DecompressedObjFile, | ||
ObjFile.UncompressedSize)) | ||
return std::move(E); | ||
break; | ||
} | ||
|
||
StringRef Buffer(reinterpret_cast<const char *>(DecompressedObjFile.begin()), | ||
DecompressedObjFile.size()); | ||
return RunnableConfiguration{std::move(B), getObjectFromBuffer(Buffer)}; | ||
} | ||
|
||
Expected<std::unique_ptr<BenchmarkRunner::FunctionExecutor>> | ||
BenchmarkRunner::createFunctionExecutor( | ||
object::OwningBinary<object::ObjectFile> ObjectFile, | ||
|
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
serialize*