[PATCH] D52866: [llvm-exegesis][NFC] Improve parsing of the YAML files

Guillaume Chatelet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 4 05:35:41 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL343771: [llvm-exegesis][NFC] Improve parsing of the YAML files (authored by gchatelet, committed by ).

Repository:
  rL LLVM

https://reviews.llvm.org/D52866

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


Index: llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp
===================================================================
--- llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp
+++ llvm/trunk/tools/llvm-exegesis/lib/BenchmarkResult.cpp
@@ -10,15 +10,16 @@
 #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"
 #include "llvm/Support/FileSystem.h"
 #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 @@
   }
 
 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 @@
     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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52866.168276.patch
Type: text/x-patch
Size: 2964 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181004/62f049e6/attachment.bin>


More information about the llvm-commits mailing list