Skip to content

Commit

Permalink
[NFCI][llvm-exegesis] BenchmarkRunner::runConfiguration(): extract …
Browse files Browse the repository at this point in the history
…`assembleSnippet()` helper
  • Loading branch information
LebedevRI committed Dec 17, 2022
1 parent 41dd767 commit 118b49a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
45 changes: 24 additions & 21 deletions llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
Expand Up @@ -135,6 +135,21 @@ class FunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
};
} // namespace

Expected<SmallString<0>> BenchmarkRunner::assembleSnippet(
const BenchmarkCode &BC, const SnippetRepetitor &Repetitor,
unsigned MinInstructions, unsigned LoopBodySize) const {
const std::vector<MCInst> &Instructions = BC.Key.Instructions;
SmallString<0> Buffer;
raw_svector_ostream OS(Buffer);
if (Error E = assembleToStream(
State.getExegesisTarget(), State.createTargetMachine(), BC.LiveIns,
BC.Key.RegisterInitialValues,
Repetitor.Repeat(Instructions, MinInstructions, LoopBodySize), OS)) {
return std::move(E);
}
return Buffer;
}

Expected<InstructionBenchmark> BenchmarkRunner::runConfiguration(
const BenchmarkCode &BC, unsigned NumRepetitions, unsigned LoopBodySize,
const SnippetRepetitor &Repetitor, bool DumpObjectToDisk) const {
Expand All @@ -156,46 +171,34 @@ Expected<InstructionBenchmark> BenchmarkRunner::runConfiguration(
const int MinInstructionsForSnippet = 4 * Instructions.size();
const int LoopBodySizeForSnippet = 2 * Instructions.size();
{
SmallString<0> Buffer;
raw_svector_ostream OS(Buffer);
if (Error E = assembleToStream(
State.getExegesisTarget(), State.createTargetMachine(), BC.LiveIns,
BC.Key.RegisterInitialValues,
Repetitor.Repeat(Instructions, MinInstructionsForSnippet,
LoopBodySizeForSnippet),
OS)) {
auto Snippet = assembleSnippet(BC, Repetitor, MinInstructionsForSnippet,
LoopBodySizeForSnippet);
if (Error E = Snippet.takeError())
return std::move(E);
}
const ExecutableFunction EF(State.createTargetMachine(),
getObjectFromBuffer(OS.str()));
getObjectFromBuffer(*Snippet));
const auto FnBytes = EF.getFunctionBytes();
llvm::append_range(InstrBenchmark.AssembledSnippet, FnBytes);
}

// Assemble NumRepetitions instructions repetitions of the snippet for
// measurements.
const auto Filler = Repetitor.Repeat(
Instructions, InstrBenchmark.NumRepetitions, LoopBodySize);

object::OwningBinary<object::ObjectFile> ObjectFile;
{
SmallString<0> Buffer;
raw_svector_ostream OS(Buffer);
if (Error E = assembleToStream(State.getExegesisTarget(),
State.createTargetMachine(), BC.LiveIns,
BC.Key.RegisterInitialValues, Filler, OS)) {
auto Snippet = assembleSnippet(BC, Repetitor, InstrBenchmark.NumRepetitions,
LoopBodySize);
if (Error E = Snippet.takeError())
return std::move(E);
}
if (DumpObjectToDisk) {
auto ObjectFilePath = writeObjectFile(Buffer);
auto ObjectFilePath = writeObjectFile(*Snippet);
if (Error E = ObjectFilePath.takeError()) {
InstrBenchmark.Error = toString(std::move(E));
return InstrBenchmark;
}
outs() << "Check generated assembly with: /usr/bin/objdump -d "
<< *ObjectFilePath << "\n";
}
ObjectFile = getObjectFromBuffer(OS.str());
ObjectFile = getObjectFromBuffer(*Snippet);
}

if (BenchmarkSkipMeasurements) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h
Expand Up @@ -83,6 +83,11 @@ class BenchmarkRunner {
virtual Expected<std::vector<BenchmarkMeasure>>
runMeasurements(const FunctionExecutor &Executor) const = 0;

Expected<SmallString<0>> assembleSnippet(const BenchmarkCode &BC,
const SnippetRepetitor &Repetitor,
unsigned MinInstructions,
unsigned LoopBodySize) const;

Expected<std::string> writeObjectFile(StringRef Buffer) const;

const std::unique_ptr<ScratchSpace> Scratch;
Expand Down

0 comments on commit 118b49a

Please sign in to comment.