[llvm] r343771 - [llvm-exegesis][NFC] Improve parsing of the YAML files

Guillaume Chatelet via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 4 05:33:46 PDT 2018


Author: gchatelet
Date: Thu Oct  4 05:33:46 2018
New Revision: 343771

URL: http://llvm.org/viewvc/llvm-project?rev=343771&view=rev
Log:
[llvm-exegesis][NFC] Improve parsing of the YAML files

Summary: sscanf turns out to be slow for reading floating points.

Reviewers: courbet

Subscribers: tschuett, llvm-commits, RKSimon

Differential Revision: https://reviews.llvm.org/D52866

Modified:
    llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp

Modified: llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp?rev=343771&r1=343770&r2=343771&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp (original)
+++ llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp Thu Oct  4 05:33:46 2018
@@ -10,6 +10,7 @@
 #include "BenchmarkResult.h"
 #include "BenchmarkRunner.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/bit.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ObjectYAML/YAML.h"
 #include "llvm/Support/FileOutputBuffer.h"
@@ -17,8 +18,8 @@
 #include "llvm/Support/Format.h"
 #include "llvm/Support/raw_ostream.h"
 
-static constexpr const char kIntegerFormat[] = "i_0x%" PRId64 "x";
-static constexpr const char kDoubleFormat[] = "f_%la";
+static constexpr const char kIntegerPrefix[] = "i_0x";
+static constexpr const char kDoublePrefix[] = "f_";
 static constexpr const char kInvalidOperand[] = "INVALID";
 
 // A mutable struct holding an LLVMState that can be passed through the
@@ -73,14 +74,37 @@ struct YamlContext {
   }
 
 private:
+  void serializeIntegerOperand(llvm::raw_ostream &OS, int64_t Value) {
+    OS << kIntegerPrefix;
+    OS.write_hex(llvm::bit_cast<uint64_t>(Value));
+  }
+
+  bool tryDeserializeIntegerOperand(llvm::StringRef String, int64_t &Value) {
+    if (!String.consume_front(kIntegerPrefix))
+      return false;
+    return !String.consumeInteger(16, Value);
+  }
+
+  void serializeFPOperand(llvm::raw_ostream &OS, double Value) {
+    OS << kDoublePrefix << llvm::format("%la", Value);
+  }
+
+  bool tryDeserializeFPOperand(llvm::StringRef String, double &Value) {
+    if (!String.consume_front(kDoublePrefix))
+      return false;
+    char *EndPointer = nullptr;
+    Value = strtod(String.begin(), &EndPointer);
+    return EndPointer == String.end();
+  }
+
   void serializeMCOperand(const llvm::MCOperand &MCOperand,
                           llvm::raw_ostream &OS) {
     if (MCOperand.isReg()) {
       OS << getRegName(MCOperand.getReg());
     } else if (MCOperand.isImm()) {
-      OS << llvm::format(kIntegerFormat, MCOperand.getImm());
+      serializeIntegerOperand(OS, MCOperand.getImm());
     } else if (MCOperand.isFPImm()) {
-      OS << llvm::format(kDoubleFormat, MCOperand.getFPImm());
+      serializeFPOperand(OS, MCOperand.getFPImm());
     } else {
       OS << kInvalidOperand;
     }
@@ -90,11 +114,9 @@ private:
     assert(!String.empty());
     int64_t IntValue = 0;
     double DoubleValue = 0;
-    if (String[0] == kIntegerFormat[0] &&
-        sscanf(String.data(), kIntegerFormat, &IntValue) == 1)
+    if (tryDeserializeIntegerOperand(String, IntValue))
       return llvm::MCOperand::createImm(IntValue);
-    if (String[0] == kDoubleFormat[0] &&
-        sscanf(String.data(), kDoubleFormat, &DoubleValue) == 1)
+    if (tryDeserializeFPOperand(String, DoubleValue))
       return llvm::MCOperand::createFPImm(DoubleValue);
     if (unsigned RegNo = getRegNo(String))
       return llvm::MCOperand::createReg(RegNo);




More information about the llvm-commits mailing list