[Mlir-commits] [mlir] decac2d - [mlir][EnumGen] Generate utility symbolizeEnum and stringifyEnum methods during EnumGen

River Riddle llvmlistbot at llvm.org
Sun Apr 12 19:08:58 PDT 2020


Author: River Riddle
Date: 2020-04-12T19:08:08-07:00
New Revision: decac2d9ea9a5e7f2cce4326db198bf8f8980a78

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

LOG: [mlir][EnumGen] Generate utility symbolizeEnum and stringifyEnum methods during EnumGen

Summary:
This revision adds generation of two utility methods during EnumGen:
```
llvm::Optional<EnumType> symbolizeEnum<EnumType>(llvm::StringRef)
<stringifyResult> stringifyEnum(EnumType);
```

This provides a generic interface for stringifying/symbolizing any enum that can be used in a template environment.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/SPIRV/SPIRVOps.h
    mlir/lib/Dialect/SPIRV/SPIRVDialect.cpp
    mlir/lib/Dialect/SPIRV/SPIRVOps.cpp
    mlir/tools/mlir-tblgen/EnumsGen.cpp
    mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/SPIRV/SPIRVOps.h b/mlir/include/mlir/Dialect/SPIRV/SPIRVOps.h
index 9bb0d52095d4..cfe10e87223a 100644
--- a/mlir/include/mlir/Dialect/SPIRV/SPIRVOps.h
+++ b/mlir/include/mlir/Dialect/SPIRV/SPIRVOps.h
@@ -39,9 +39,6 @@ class VerCapExtAttr;
 // `EnumClass`.
 // template <typename EnumClass> StringRef attributeName();
 //
-// Get the function that can be used to symbolize an enum value.
-// template <typename EnumClass>
-// Optional<EnumClass> (*)(StringRef) symbolizeEnum();
 #include "mlir/Dialect/SPIRV/SPIRVOpUtils.inc"
 
 } // end namespace spirv

diff  --git a/mlir/lib/Dialect/SPIRV/SPIRVDialect.cpp b/mlir/lib/Dialect/SPIRV/SPIRVDialect.cpp
index 92ca31de6213..9478abb690af 100644
--- a/mlir/lib/Dialect/SPIRV/SPIRVDialect.cpp
+++ b/mlir/lib/Dialect/SPIRV/SPIRVDialect.cpp
@@ -311,7 +311,7 @@ static Optional<ValTy> parseAndVerify(SPIRVDialect const &dialect,
     return llvm::None;
   }
 
-  auto val = spirv::symbolizeEnum<ValTy>()(enumSpec);
+  auto val = spirv::symbolizeEnum<ValTy>(enumSpec);
   if (!val)
     parser.emitError(enumLoc, "unknown attribute: '") << enumSpec << "'";
   return val;

diff  --git a/mlir/lib/Dialect/SPIRV/SPIRVOps.cpp b/mlir/lib/Dialect/SPIRV/SPIRVOps.cpp
index f0a7c2af6d2d..9a1bb68dd1a6 100644
--- a/mlir/lib/Dialect/SPIRV/SPIRVOps.cpp
+++ b/mlir/lib/Dialect/SPIRV/SPIRVOps.cpp
@@ -116,7 +116,7 @@ parseEnumStrAttr(EnumClass &value, OpAsmParser &parser,
            << attrName << " attribute specified as string";
   }
   auto attrOptional =
-      spirv::symbolizeEnum<EnumClass>()(attrVal.cast<StringAttr>().getValue());
+      spirv::symbolizeEnum<EnumClass>(attrVal.cast<StringAttr>().getValue());
   if (!attrOptional) {
     return parser.emitError(loc, "invalid ")
            << attrName << " attribute specification: " << attrVal;
@@ -151,7 +151,7 @@ parseEnumKeywordAttr(EnumClass &value, OpAsmParser &parser,
   auto loc = parser.getCurrentLocation();
   if (parser.parseKeyword(&keyword))
     return failure();
-  if (Optional<EnumClass> attr = spirv::symbolizeEnum<EnumClass>()(keyword)) {
+  if (Optional<EnumClass> attr = spirv::symbolizeEnum<EnumClass>(keyword)) {
     value = attr.getValue();
     return success();
   }

diff  --git a/mlir/tools/mlir-tblgen/EnumsGen.cpp b/mlir/tools/mlir-tblgen/EnumsGen.cpp
index a58ea24a3a74..04d357d762da 100644
--- a/mlir/tools/mlir-tblgen/EnumsGen.cpp
+++ b/mlir/tools/mlir-tblgen/EnumsGen.cpp
@@ -370,6 +370,28 @@ static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
     emitMaxValueFn(enumDef, os);
   }
 
+  // Generate a generic `stringifyEnum` function that forwards to the method
+  // specified by the user.
+  const char *const stringifyEnumStr = R"(
+inline {0} stringifyEnum({1} enumValue) {{
+  return {2}(enumValue);
+}
+)";
+  os << formatv(stringifyEnumStr, symToStrFnRetType, enumName, symToStrFnName);
+
+  // Generate a generic `symbolizeEnum` function that forwards to the method
+  // specified by the user.
+  const char *const symbolizeEnumStr = R"(
+template <typename EnumType>
+llvm::Optional<EnumType> symbolizeEnum(llvm::StringRef);
+
+template <>
+inline llvm::Optional<{0}> symbolizeEnum<{0}>(llvm::StringRef str) {
+  return {1}(str);
+}
+)";
+  os << formatv(symbolizeEnumStr, enumName, strToSymFnName);
+
   for (auto ns : llvm::reverse(namespaces))
     os << "} // namespace " << ns << "\n";
 

diff  --git a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp b/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
index d68d9eeba634..509b55105b21 100644
--- a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
+++ b/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
@@ -1105,13 +1105,6 @@ static void emitEnumGetAttrNameFnDecl(raw_ostream &os) {
                 "attributeName();\n");
 }
 
-static void emitEnumGetSymbolizeFnDecl(raw_ostream &os) {
-  os << "template <typename EnumClass> using SymbolizeFnTy = "
-        "llvm::Optional<EnumClass> (*)(StringRef);\n";
-  os << "template <typename EnumClass> inline constexpr "
-        "SymbolizeFnTy<EnumClass> symbolizeEnum();\n";
-}
-
 static void emitEnumGetAttrNameFnDefn(const EnumAttr &enumAttr,
                                       raw_ostream &os) {
   auto enumName = enumAttr.getEnumClassName();
@@ -1124,17 +1117,6 @@ static void emitEnumGetAttrNameFnDefn(const EnumAttr &enumAttr,
   os << "}\n";
 }
 
-static void emitEnumGetSymbolizeFnDefn(const EnumAttr &enumAttr,
-                                       raw_ostream &os) {
-  auto enumName = enumAttr.getEnumClassName();
-  auto strToSymFnName = enumAttr.getStringToSymbolFnName();
-  os << formatv(
-      "template <> inline SymbolizeFnTy<{0}> symbolizeEnum<{0}>() {{\n",
-      enumName);
-  os << "  return " << strToSymFnName << ";\n";
-  os << "}\n";
-}
-
 static bool emitOpUtils(const RecordKeeper &recordKeeper, raw_ostream &os) {
   llvm::emitSourceFileHeader("SPIR-V Op Utilities", os);
 
@@ -1142,11 +1124,9 @@ static bool emitOpUtils(const RecordKeeper &recordKeeper, raw_ostream &os) {
   os << "#ifndef SPIRV_OP_UTILS_H_\n";
   os << "#define SPIRV_OP_UTILS_H_\n";
   emitEnumGetAttrNameFnDecl(os);
-  emitEnumGetSymbolizeFnDecl(os);
   for (const auto *def : defs) {
     EnumAttr enumAttr(*def);
     emitEnumGetAttrNameFnDefn(enumAttr, os);
-    emitEnumGetSymbolizeFnDefn(enumAttr, os);
   }
   os << "#endif // SPIRV_OP_UTILS_H\n";
   return false;


        


More information about the Mlir-commits mailing list