From: Roman Lebedev Date: Sat, 17 Dec 2022 21:28:52 +0000 (+0300) Subject: [NFC][llvm-exegesis] Extract `runBenchmarkConfigurations()` out of `benchmarkMain()` X-Git-Tag: upstream/17.0.6~23366 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=81b35aa69f99d4dec7b836a52eb941075b61a384;p=platform%2Fupstream%2Fllvm.git [NFC][llvm-exegesis] Extract `runBenchmarkConfigurations()` out of `benchmarkMain()` `benchmarkMain()` is already bigger than it should be, let's extract the important chunk before making it even larger. --- diff --git a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp index 02d3b90..160a313 100644 --- a/llvm/tools/llvm-exegesis/llvm-exegesis.cpp +++ b/llvm/tools/llvm-exegesis/llvm-exegesis.cpp @@ -317,6 +317,66 @@ generateSnippets(const LLVMState &State, unsigned Opcode, return Benchmarks; } +static void runBenchmarkConfigurations( + const LLVMState &State, ArrayRef Configurations, + ArrayRef> Repetitors, + const BenchmarkRunner &Runner) { + std::optional FileOstr; + if (BenchmarkFile != "-") { + int ResultFD = 0; + // Create output file or open existing file and truncate it, once. + ExitOnErr(errorCodeToError(openFileForWrite(BenchmarkFile, ResultFD, + sys::fs::CD_CreateAlways, + sys::fs::OF_TextWithCRLF))); + FileOstr.emplace(ResultFD, true /*shouldClose*/); + } + raw_ostream &Ostr = FileOstr ? *FileOstr : outs(); + + std::optional> Meter; + if (BenchmarkMeasurementsPrintProgress) + Meter.emplace(Configurations.size()); + for (const BenchmarkCode &Conf : Configurations) { + ProgressMeter<>::ProgressMeterStep MeterStep(Meter ? &*Meter : nullptr); + SmallVector AllResults; + + for (const std::unique_ptr &Repetitor : + Repetitors) { + auto RC = ExitOnErr(Runner.getRunnableConfiguration( + Conf, NumRepetitions, LoopBodySize, *Repetitor, DumpObjectToDisk)); + AllResults.emplace_back( + ExitOnErr(Runner.runConfiguration(std::move(RC)))); + } + InstructionBenchmark &Result = AllResults.front(); + + if (RepetitionMode == InstructionBenchmark::RepetitionModeE::AggregateMin) { + assert(!Result.Measurements.empty() && + "We're in an 'min' repetition mode, and need to aggregate new " + "result to the existing result."); + for (const InstructionBenchmark &OtherResult : + ArrayRef(AllResults).drop_front()) { + llvm::append_range(Result.AssembledSnippet, + OtherResult.AssembledSnippet); + assert(OtherResult.Measurements.size() == Result.Measurements.size() && + "Expected to have identical number of measurements."); + for (auto I : zip(Result.Measurements, OtherResult.Measurements)) { + BenchmarkMeasure &Measurement = std::get<0>(I); + const BenchmarkMeasure &NewMeasurement = std::get<1>(I); + assert(Measurement.Key == NewMeasurement.Key && + "Expected measurements to be symmetric"); + + Measurement.PerInstructionValue = + std::min(Measurement.PerInstructionValue, + NewMeasurement.PerInstructionValue); + Measurement.PerSnippetValue = std::min( + Measurement.PerSnippetValue, NewMeasurement.PerSnippetValue); + } + } + } + + ExitOnFileError(BenchmarkFile, Result.writeYamlTo(State, Ostr)); + } +} + void benchmarkMain() { if (!BenchmarkSkipMeasurements) { #ifndef HAVE_LIBPFM @@ -401,60 +461,8 @@ void benchmarkMain() { if (BenchmarkFile.empty()) BenchmarkFile = "-"; - std::optional FileOstr; - if (BenchmarkFile != "-") { - int ResultFD = 0; - // Create output file or open existing file and truncate it, once. - ExitOnErr(errorCodeToError(openFileForWrite(BenchmarkFile, ResultFD, - sys::fs::CD_CreateAlways, - sys::fs::OF_TextWithCRLF))); - FileOstr.emplace(ResultFD, true /*shouldClose*/); - } - raw_ostream &Ostr = FileOstr ? *FileOstr : outs(); - - std::optional> Meter; - if (BenchmarkMeasurementsPrintProgress) - Meter.emplace(Configurations.size()); - for (const BenchmarkCode &Conf : Configurations) { - ProgressMeter<>::ProgressMeterStep MeterStep(Meter ? &*Meter : nullptr); - SmallVector AllResults; - - for (const std::unique_ptr &Repetitor : - Repetitors) { - auto RC = ExitOnErr(Runner->getRunnableConfiguration( - Conf, NumRepetitions, LoopBodySize, *Repetitor, DumpObjectToDisk)); - AllResults.emplace_back( - ExitOnErr(Runner->runConfiguration(std::move(RC)))); - } - InstructionBenchmark &Result = AllResults.front(); - - if (RepetitionMode == InstructionBenchmark::RepetitionModeE::AggregateMin) { - assert(!Result.Measurements.empty() && - "We're in an 'min' repetition mode, and need to aggregate new " - "result to the existing result."); - for (const InstructionBenchmark &OtherResult : - ArrayRef(AllResults).drop_front()) { - llvm::append_range(Result.AssembledSnippet, - OtherResult.AssembledSnippet); - assert(OtherResult.Measurements.size() == Result.Measurements.size() && - "Expected to have identical number of measurements."); - for (auto I : zip(Result.Measurements, OtherResult.Measurements)) { - BenchmarkMeasure &Measurement = std::get<0>(I); - const BenchmarkMeasure &NewMeasurement = std::get<1>(I); - assert(Measurement.Key == NewMeasurement.Key && - "Expected measurements to be symmetric"); + runBenchmarkConfigurations(State, Configurations, Repetitors, *Runner); - Measurement.PerInstructionValue = - std::min(Measurement.PerInstructionValue, - NewMeasurement.PerInstructionValue); - Measurement.PerSnippetValue = std::min( - Measurement.PerSnippetValue, NewMeasurement.PerSnippetValue); - } - } - } - - ExitOnFileError(BenchmarkFile, Result.writeYamlTo(State, Ostr)); - } exegesis::pfm::pfmTerminate(); }