[llvm] 118b49a - [NFCI][llvm-exegesis] `BenchmarkRunner::runConfiguration()`: extract `assembleSnippet()` helper

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 17 12:15:15 PST 2022


Author: Roman Lebedev
Date: 2022-12-17T23:14:53+03:00
New Revision: 118b49a09b9759d8d2e4171fdd2ad2784a6e4148

URL: https://github.com/llvm/llvm-project/commit/118b49a09b9759d8d2e4171fdd2ad2784a6e4148
DIFF: https://github.com/llvm/llvm-project/commit/118b49a09b9759d8d2e4171fdd2ad2784a6e4148.diff

LOG: [NFCI][llvm-exegesis] `BenchmarkRunner::runConfiguration()`: extract `assembleSnippet()` helper

Added: 
    

Modified: 
    llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
    llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
index 8f25e21a0d59..db7d49f6c248 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
@@ -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 {
@@ -156,38 +171,26 @@ 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;
@@ -195,7 +198,7 @@ Expected<InstructionBenchmark> 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 f963ff0f6a86..dd935c32b5ae 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.h
@@ -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;


        


More information about the llvm-commits mailing list