[Mlir-commits] [mlir] a263474 - [mlir][spirv] Remove template functions for getting op's opcode

Lei Zhang llvmlistbot at llvm.org
Thu May 7 06:32:20 PDT 2020


Author: Lei Zhang
Date: 2020-05-07T09:32:03-04:00
New Revision: a2634748cd82762341b87c5b00f374b71f18535a

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

LOG: [mlir][spirv] Remove template functions for getting op's opcode

These template functions are used in the serializer, where we can
actually directly query the opcode from the op's definition and
use that in the auto-generated serialization logic.

This removes a set of templates accounting for 319 lines from
the auto-generated inc file.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h
    mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp
    mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h b/mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h
index 913ba4193b7c..6baafdb9d9e9 100644
--- a/mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h
+++ b/mlir/include/mlir/Dialect/SPIRV/SPIRVBinaryUtils.h
@@ -30,10 +30,6 @@ constexpr uint32_t kMagicNumber = 0x07230203;
 /// The serializer tool ID registered to the Khronos Group
 constexpr uint32_t kGeneratorNumber = 22;
 
-/// Auto-generated getOpcode<*Op>() specializations
-#define GET_SPIRV_SERIALIZATION_UTILS
-#include "mlir/Dialect/SPIRV/SPIRVSerialization.inc"
-
 /// Appends a SPRI-V module header to `header` with the given `version` and
 /// `idBound`.
 void appendModuleHeader(SmallVectorImpl<uint32_t> &header,

diff  --git a/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp b/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp
index cc29bae39c1e..a553778938a9 100644
--- a/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp
+++ b/mlir/lib/Dialect/SPIRV/Serialization/Serializer.cpp
@@ -852,8 +852,7 @@ LogicalResult Serializer::processVariableOp(spirv::VariableOp op) {
     }
     operands.push_back(argID);
   }
-  encodeInstructionInto(functionHeader, spirv::getOpcode<spirv::VariableOp>(),
-                        operands);
+  encodeInstructionInto(functionHeader, spirv::Opcode::OpVariable, operands);
   for (auto attr : op.getAttrs()) {
     if (llvm::any_of(elidedAttrs,
                      [&](StringRef elided) { return attr.first == elided; })) {

diff  --git a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp b/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
index b4ff1797f843..a70fe0455ee4 100644
--- a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
+++ b/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
@@ -480,26 +480,6 @@ static mlir::GenRegistration
 // Serialization AutoGen
 //===----------------------------------------------------------------------===//
 
-// Writes the following function to `os`:
-//   inline uint32_t getOpcode(<op-class-name>) { return <opcode>; }
-static void emitGetOpcodeFunction(const Record *record, Operator const &op,
-                                  raw_ostream &os) {
-  os << formatv("template <> constexpr inline ::mlir::spirv::Opcode "
-                "getOpcode<{0}>() {{\n",
-                op.getQualCppClassName());
-  os << formatv("  return ::mlir::spirv::Opcode::{0};\n",
-                record->getValueAsString("spirvOpName"));
-  os << "}\n";
-}
-
-/// Forward declaration of function to return the SPIR-V opcode corresponding to
-/// an operation. This function will be generated for all SPV_Op instances that
-/// have hasOpcode = 1.
-static void declareOpcodeFn(raw_ostream &os) {
-  os << "template <typename OpClass> inline constexpr ::mlir::spirv::Opcode "
-        "getOpcode();\n";
-}
-
 /// Generates code to serialize attributes of a SPV_Op `op` into `os`. The
 /// generates code extracts the attribute with name `attrName` from
 /// `operandList` of `op`.
@@ -663,8 +643,9 @@ static void emitSerializationFunction(const Record *attrClass,
     // Emit debug info.
     os << formatv("  emitDebugLine(functionBody, {0}.getLoc());\n", opVar);
     os << formatv("  encodeInstructionInto("
-                  "functionBody, spirv::getOpcode<{0}>(), {1});\n",
-                  op.getQualCppClassName(), operands);
+                  "functionBody, spirv::Opcode::{1}, {2});\n",
+                  op.getQualCppClassName(),
+                  record->getValueAsString("spirvOpName"), operands);
   }
 
   // Process decorations.
@@ -1047,11 +1028,10 @@ static bool emitSerializationFns(const RecordKeeper &recordKeeper,
   std::string dSerFnString, dDesFnString, serFnString, deserFnString,
       utilsString;
   raw_string_ostream dSerFn(dSerFnString), dDesFn(dDesFnString),
-      serFn(serFnString), deserFn(deserFnString), utils(utilsString);
-  auto attrClass = recordKeeper.getClass("Attr");
+      serFn(serFnString), deserFn(deserFnString);
+  Record *attrClass = recordKeeper.getClass("Attr");
 
   // Emit the serialization and deserialization functions simultaneously.
-  declareOpcodeFn(utils);
   StringRef opVar("op");
   StringRef opcode("opcode"), words("words");
 
@@ -1067,7 +1047,6 @@ static bool emitSerializationFns(const RecordKeeper &recordKeeper,
       emitSerializationDispatch(op, "  ", opVar, dSerFn);
     }
     if (def->getValueAsBit("hasOpcode")) {
-      emitGetOpcodeFunction(def, op, utils);
       emitDeserializationDispatch(op, def, "  ", words, dDesFn);
     }
   }
@@ -1076,10 +1055,6 @@ static bool emitSerializationFns(const RecordKeeper &recordKeeper,
 
   emitExtendedSetDeserializationDispatch(recordKeeper, dDesFn);
 
-  os << "#ifdef GET_SPIRV_SERIALIZATION_UTILS\n";
-  os << utils.str();
-  os << "#endif // GET_SPIRV_SERIALIZATION_UTILS\n\n";
-
   os << "#ifdef GET_SERIALIZATION_FNS\n\n";
   os << serFn.str();
   os << dSerFn.str();


        


More information about the Mlir-commits mailing list