Skip to content

[NFC][llvm-exegesis] Refactor InstrBenchmark to BenchmarkResult #76388

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
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
48 changes: 25 additions & 23 deletions llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,19 +499,20 @@ BenchmarkRunner::getRunnableConfiguration(
const SnippetRepetitor &Repetitor) const {
RunnableConfiguration RC;

Benchmark &InstrBenchmark = RC.InstrBenchmark;
InstrBenchmark.Mode = Mode;
InstrBenchmark.CpuName = std::string(State.getTargetMachine().getTargetCPU());
InstrBenchmark.LLVMTriple =
Benchmark &BenchmarkResult = RC.BenchmarkResult;
BenchmarkResult.Mode = Mode;
BenchmarkResult.CpuName =
std::string(State.getTargetMachine().getTargetCPU());
BenchmarkResult.LLVMTriple =
State.getTargetMachine().getTargetTriple().normalize();
InstrBenchmark.NumRepetitions = NumRepetitions;
InstrBenchmark.Info = BC.Info;
BenchmarkResult.NumRepetitions = NumRepetitions;
BenchmarkResult.Info = BC.Info;

const std::vector<MCInst> &Instructions = BC.Key.Instructions;

bool GenerateMemoryInstructions = ExecutionMode == ExecutionModeE::SubProcess;

InstrBenchmark.Key = BC.Key;
BenchmarkResult.Key = BC.Key;

// Assemble at least kMinInstructionsForSnippet instructions by repeating
// the snippet for debug/analysis. This is so that the user clearly
Expand All @@ -526,16 +527,17 @@ BenchmarkRunner::getRunnableConfiguration(
return std::move(E);

if (auto Err = getBenchmarkFunctionBytes(*Snippet,
InstrBenchmark.AssembledSnippet))
BenchmarkResult.AssembledSnippet))
return std::move(Err);
}

// Assemble NumRepetitions instructions repetitions of the snippet for
// measurements.
if (BenchmarkPhaseSelector >
BenchmarkPhaseSelectorE::PrepareAndAssembleSnippet) {
auto Snippet = assembleSnippet(BC, Repetitor, InstrBenchmark.NumRepetitions,
LoopBodySize, GenerateMemoryInstructions);
auto Snippet =
assembleSnippet(BC, Repetitor, BenchmarkResult.NumRepetitions,
LoopBodySize, GenerateMemoryInstructions);
if (Error E = Snippet.takeError())
return std::move(E);
RC.ObjectFile = getObjectFromBuffer(*Snippet);
Expand Down Expand Up @@ -577,46 +579,46 @@ BenchmarkRunner::createFunctionExecutor(
std::pair<Error, Benchmark> BenchmarkRunner::runConfiguration(
RunnableConfiguration &&RC,
const std::optional<StringRef> &DumpFile) const {
Benchmark &InstrBenchmark = RC.InstrBenchmark;
Benchmark &BenchmarkResult = RC.BenchmarkResult;
object::OwningBinary<object::ObjectFile> &ObjectFile = RC.ObjectFile;

if (DumpFile && BenchmarkPhaseSelector >
BenchmarkPhaseSelectorE::PrepareAndAssembleSnippet) {
auto ObjectFilePath =
writeObjectFile(ObjectFile.getBinary()->getData(), *DumpFile);
if (Error E = ObjectFilePath.takeError()) {
return {std::move(E), std::move(InstrBenchmark)};
return {std::move(E), std::move(BenchmarkResult)};
}
outs() << "Check generated assembly with: /usr/bin/objdump -d "
<< *ObjectFilePath << "\n";
}

if (BenchmarkPhaseSelector < BenchmarkPhaseSelectorE::Measure) {
InstrBenchmark.Error = "actual measurements skipped.";
return {Error::success(), std::move(InstrBenchmark)};
BenchmarkResult.Error = "actual measurements skipped.";
return {Error::success(), std::move(BenchmarkResult)};
}

Expected<std::unique_ptr<BenchmarkRunner::FunctionExecutor>> Executor =
createFunctionExecutor(std::move(ObjectFile), RC.InstrBenchmark.Key);
createFunctionExecutor(std::move(ObjectFile), RC.BenchmarkResult.Key);
if (!Executor)
return {Executor.takeError(), std::move(InstrBenchmark)};
return {Executor.takeError(), std::move(BenchmarkResult)};
auto NewMeasurements = runMeasurements(**Executor);

if (Error E = NewMeasurements.takeError()) {
return {std::move(E), std::move(InstrBenchmark)};
return {std::move(E), std::move(BenchmarkResult)};
}
assert(InstrBenchmark.NumRepetitions > 0 && "invalid NumRepetitions");
assert(BenchmarkResult.NumRepetitions > 0 && "invalid NumRepetitions");
for (BenchmarkMeasure &BM : *NewMeasurements) {
// Scale the measurements by instruction.
BM.PerInstructionValue /= InstrBenchmark.NumRepetitions;
BM.PerInstructionValue /= BenchmarkResult.NumRepetitions;
// Scale the measurements by snippet.
BM.PerSnippetValue *=
static_cast<double>(InstrBenchmark.Key.Instructions.size()) /
InstrBenchmark.NumRepetitions;
static_cast<double>(BenchmarkResult.Key.Instructions.size()) /
BenchmarkResult.NumRepetitions;
}
InstrBenchmark.Measurements = std::move(*NewMeasurements);
BenchmarkResult.Measurements = std::move(*NewMeasurements);

return {Error::success(), std::move(InstrBenchmark)};
return {Error::success(), std::move(BenchmarkResult)};
}

Expected<std::string>
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class BenchmarkRunner {
private:
RunnableConfiguration() = default;

Benchmark InstrBenchmark;
Benchmark BenchmarkResult;
object::OwningBinary<object::ObjectFile> ObjectFile;
};

Expand Down
6 changes: 3 additions & 3 deletions llvm/tools/llvm-exegesis/llvm-exegesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ static void runBenchmarkConfigurations(
std::optional<StringRef> DumpFile;
if (DumpObjectToDisk.getNumOccurrences())
DumpFile = DumpObjectToDisk;
auto [Err, InstrBenchmark] =
auto [Err, BenchmarkResult] =
Runner.runConfiguration(std::move(RC), DumpFile);
if (Err) {
// Errors from executing the snippets are fine.
Expand All @@ -419,9 +419,9 @@ static void runBenchmarkConfigurations(
llvm::errs() << "llvm-exegesis error: " << toString(std::move(Err));
exit(1);
}
InstrBenchmark.Error = toString(std::move(Err));
BenchmarkResult.Error = toString(std::move(Err));
}
AllResults.push_back(std::move(InstrBenchmark));
AllResults.push_back(std::move(BenchmarkResult));
}
Benchmark &Result = AllResults.front();

Expand Down