From 118b49a09b9759d8d2e4171fdd2ad2784a6e4148 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Sat, 17 Dec 2022 23:07:45 +0300 Subject: [PATCH] [NFCI][llvm-exegesis] `BenchmarkRunner::runConfiguration()`: extract `assembleSnippet()` helper --- llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp | 45 +++++++++++++----------- llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h | 5 +++ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp index 8f25e21..db7d49f 100644 --- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp +++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp @@ -135,6 +135,21 @@ private: }; } // namespace +Expected> BenchmarkRunner::assembleSnippet( + const BenchmarkCode &BC, const SnippetRepetitor &Repetitor, + unsigned MinInstructions, unsigned LoopBodySize) const { + const std::vector &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 BenchmarkRunner::runConfiguration( const BenchmarkCode &BC, unsigned NumRepetitions, unsigned LoopBodySize, const SnippetRepetitor &Repetitor, bool DumpObjectToDisk) const { @@ -156,38 +171,26 @@ Expected 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 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; @@ -195,7 +198,7 @@ Expected BenchmarkRunner::runConfiguration( outs() << "Check generated assembly with: /usr/bin/objdump -d " << *ObjectFilePath << "\n"; } - ObjectFile = getObjectFromBuffer(OS.str()); + ObjectFile = getObjectFromBuffer(*Snippet); } if (BenchmarkSkipMeasurements) { diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h index f963ff0..dd935c3 100644 --- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h +++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h @@ -83,6 +83,11 @@ private: virtual Expected> runMeasurements(const FunctionExecutor &Executor) const = 0; + Expected> assembleSnippet(const BenchmarkCode &BC, + const SnippetRepetitor &Repetitor, + unsigned MinInstructions, + unsigned LoopBodySize) const; + Expected writeObjectFile(StringRef Buffer) const; const std::unique_ptr Scratch; -- 2.7.4