Skip to content

Commit 9017229

Browse files
authored
[llvm-exegesis]Allow clients to do their own snippet running error ha… (#74711)
…ndling. Returns an error *and* a benchmark rather than an error *or* a benchmark. This allows users to have custom error handling while still being able to inspect the benchmark. Apart from this small API change, this is an NFC. This is an alternative to #74211.
1 parent 1d6a678 commit 9017229

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ BenchmarkRunner::createFunctionExecutor(
572572
llvm_unreachable("ExecutionMode is outside expected range");
573573
}
574574

575-
Expected<Benchmark> BenchmarkRunner::runConfiguration(
575+
std::pair<Error, Benchmark> BenchmarkRunner::runConfiguration(
576576
RunnableConfiguration &&RC,
577577
const std::optional<StringRef> &DumpFile) const {
578578
Benchmark &InstrBenchmark = RC.InstrBenchmark;
@@ -583,29 +583,25 @@ Expected<Benchmark> BenchmarkRunner::runConfiguration(
583583
auto ObjectFilePath =
584584
writeObjectFile(ObjectFile.getBinary()->getData(), *DumpFile);
585585
if (Error E = ObjectFilePath.takeError()) {
586-
InstrBenchmark.Error = toString(std::move(E));
587-
return std::move(InstrBenchmark);
586+
return {std::move(E), std::move(InstrBenchmark)};
588587
}
589588
outs() << "Check generated assembly with: /usr/bin/objdump -d "
590589
<< *ObjectFilePath << "\n";
591590
}
592591

593592
if (BenchmarkPhaseSelector < BenchmarkPhaseSelectorE::Measure) {
594593
InstrBenchmark.Error = "actual measurements skipped.";
595-
return std::move(InstrBenchmark);
594+
return {Error::success(), std::move(InstrBenchmark)};
596595
}
597596

598597
Expected<std::unique_ptr<BenchmarkRunner::FunctionExecutor>> Executor =
599598
createFunctionExecutor(std::move(ObjectFile), RC.InstrBenchmark.Key);
600599
if (!Executor)
601-
return Executor.takeError();
600+
return {Executor.takeError(), std::move(InstrBenchmark)};
602601
auto NewMeasurements = runMeasurements(**Executor);
603602

604603
if (Error E = NewMeasurements.takeError()) {
605-
if (!E.isA<SnippetCrash>())
606-
return std::move(E);
607-
InstrBenchmark.Error = toString(std::move(E));
608-
return std::move(InstrBenchmark);
604+
return {std::move(E), std::move(InstrBenchmark)};
609605
}
610606
assert(InstrBenchmark.NumRepetitions > 0 && "invalid NumRepetitions");
611607
for (BenchmarkMeasure &BM : *NewMeasurements) {
@@ -618,7 +614,7 @@ Expected<Benchmark> BenchmarkRunner::runConfiguration(
618614
}
619615
InstrBenchmark.Measurements = std::move(*NewMeasurements);
620616

621-
return std::move(InstrBenchmark);
617+
return {Error::success(), std::move(InstrBenchmark)};
622618
}
623619

624620
Expected<std::string>

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class BenchmarkRunner {
6565
unsigned NumRepetitions, unsigned LoopUnrollFactor,
6666
const SnippetRepetitor &Repetitor) const;
6767

68-
Expected<Benchmark>
68+
std::pair<Error, Benchmark>
6969
runConfiguration(RunnableConfiguration &&RC,
7070
const std::optional<StringRef> &DumpFile) const;
7171

llvm/tools/llvm-exegesis/llvm-exegesis.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,18 @@ static void runBenchmarkConfigurations(
410410
std::optional<StringRef> DumpFile;
411411
if (DumpObjectToDisk.getNumOccurrences())
412412
DumpFile = DumpObjectToDisk;
413-
AllResults.emplace_back(
414-
ExitOnErr(Runner.runConfiguration(std::move(RC), DumpFile)));
413+
auto [Err, InstrBenchmark] =
414+
Runner.runConfiguration(std::move(RC), DumpFile);
415+
if (Err) {
416+
// Errors from executing the snippets are fine.
417+
// All other errors are a framework issue and should fail.
418+
if (!Err.isA<SnippetCrash>()) {
419+
llvm::errs() << "llvm-exegesis error: " << toString(std::move(Err));
420+
exit(1);
421+
}
422+
InstrBenchmark.Error = toString(std::move(Err));
423+
}
424+
AllResults.push_back(std::move(InstrBenchmark));
415425
}
416426
Benchmark &Result = AllResults.front();
417427

0 commit comments

Comments
 (0)