[Mlir-commits] [mlir] 83e074a - [mlir] Add an 'cppNamespace' field to availability

Lei Zhang llvmlistbot at llvm.org
Tue Oct 5 06:38:49 PDT 2021


Author: Lei Zhang
Date: 2021-10-05T09:38:09-04:00
New Revision: 83e074a0c652a668c8a5d572f8c77b58c8383ff0

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

LOG: [mlir] Add an 'cppNamespace' field to availability

This allows us to generate interfaces in a namespace,
following other TableGen'erated code.

Reviewed By: rriddle

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/SPIRV/IR/SPIRVAvailability.td
    mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td
    mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h
    mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
    mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVAvailability.td b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVAvailability.td
index 4e2e943bc3d93..bfb03dac33a79 100644
--- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVAvailability.td
+++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVAvailability.td
@@ -19,6 +19,8 @@ include "mlir/IR/OpBase.td"
 class Availability {
   // The following are fields for controlling the generated C++ OpInterface.
 
+  // The namespace for the generated C++ OpInterface subclass.
+  string cppNamespace = ?;
   // The name for the generated C++ OpInterface subclass.
   string interfaceName = ?;
   // The documentation for the generated C++ OpInterface subclass.

diff  --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td
index 539de6a1bcc3e..990af804f270d 100644
--- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td
+++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td
@@ -125,6 +125,7 @@ def SPV_VersionAttr : SPV_I32EnumAttr<"Version", "valid SPIR-V version", [
 
 class MinVersion<I32EnumAttrCase min> : MinVersionBase<
     "QueryMinVersionInterface", SPV_VersionAttr, min> {
+  let cppNamespace = "::mlir::spirv";
   let interfaceDescription = [{
     Querying interface for minimal required SPIR-V version.
 
@@ -136,6 +137,7 @@ class MinVersion<I32EnumAttrCase min> : MinVersionBase<
 
 class MaxVersion<I32EnumAttrCase max> : MaxVersionBase<
     "QueryMaxVersionInterface", SPV_VersionAttr, max> {
+  let cppNamespace = "::mlir::spirv";
   let interfaceDescription = [{
     Querying interface for maximal supported SPIR-V version.
 
@@ -146,6 +148,7 @@ class MaxVersion<I32EnumAttrCase max> : MaxVersionBase<
 }
 
 class Extension<list<StrEnumAttrCase> extensions> : Availability {
+  let cppNamespace = "::mlir::spirv";
   let interfaceName = "QueryExtensionInterface";
   let interfaceDescription = [{
     Querying interface for required SPIR-V extensions.
@@ -189,6 +192,7 @@ class Extension<list<StrEnumAttrCase> extensions> : Availability {
 }
 
 class Capability<list<I32EnumAttrCase> capabilities> : Availability {
+  let cppNamespace = "::mlir::spirv";
   let interfaceName = "QueryCapabilityInterface";
   let interfaceDescription = [{
     Querying interface for required SPIR-V capabilities.

diff  --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h
index c4f5fd1192c4d..e3d4e56c48f5e 100644
--- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h
+++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h
@@ -22,15 +22,15 @@
 #include "mlir/Interfaces/SideEffectInterfaces.h"
 #include "llvm/Support/PointerLikeTypeTraits.h"
 
+// TableGen'erated operation interfaces for querying versions, extensions, and
+// capabilities.
+#include "mlir/Dialect/SPIRV/IR/SPIRVAvailability.h.inc"
+
 namespace mlir {
 class OpBuilder;
 
 namespace spirv {
 class VerCapExtAttr;
-
-// TableGen'erated operation interfaces for querying versions, extensions, and
-// capabilities.
-#include "mlir/Dialect/SPIRV/IR/SPIRVAvailability.h.inc"
 } // namespace spirv
 } // namespace mlir
 

diff  --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
index f711c183f122a..9a2662211c7c6 100644
--- a/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
@@ -3915,14 +3915,9 @@ static LogicalResult verify(spirv::PtrAccessChainOp accessChainOp) {
   return verifyAccessChain(accessChainOp, accessChainOp.indices());
 }
 
-namespace mlir {
-namespace spirv {
-
 // TableGen'erated operation interfaces for querying versions, extensions, and
 // capabilities.
 #include "mlir/Dialect/SPIRV/IR/SPIRVAvailability.cpp.inc"
-} // namespace spirv
-} // namespace mlir
 
 // TablenGen'erated operation definitions.
 #define GET_OP_CLASSES
@@ -3932,6 +3927,5 @@ namespace mlir {
 namespace spirv {
 // TableGen'erated operation availability interface implementations.
 #include "mlir/Dialect/SPIRV/IR/SPIRVOpAvailabilityImpl.inc"
-
 } // namespace spirv
 } // namespace mlir

diff  --git a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp b/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
index 10c00fea9b8be..4053bc90ccc95 100644
--- a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
+++ b/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/TableGen/Attribute.h"
+#include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Operator.h"
@@ -45,6 +46,7 @@ using mlir::tblgen::EnumAttr;
 using mlir::tblgen::EnumAttrCase;
 using mlir::tblgen::NamedAttribute;
 using mlir::tblgen::NamedTypeConstraint;
+using mlir::tblgen::NamespaceEmitter;
 using mlir::tblgen::Operator;
 
 //===----------------------------------------------------------------------===//
@@ -62,6 +64,9 @@ class Availability {
   // instance.
   StringRef getClass() const;
 
+  // Returns the generated C++ interface's class namespace.
+  StringRef getInterfaceClassNamespace() const;
+
   // Returns the generated C++ interface's class name.
   StringRef getInterfaceClassName() const;
 
@@ -91,6 +96,9 @@ class Availability {
   // Returns the concrete availability instance carried in this case.
   StringRef getMergeInstance() const;
 
+  // Returns the underlying LLVM TableGen Record.
+  const llvm::Record *getDef() const { return def; }
+
 private:
   // The TableGen definition of this availability.
   const llvm::Record *def;
@@ -112,6 +120,10 @@ StringRef Availability::getClass() const {
   return parentClass.front()->getName();
 }
 
+StringRef Availability::getInterfaceClassNamespace() const {
+  return def->getValueAsString("cppNamespace");
+}
+
 StringRef Availability::getInterfaceClassName() const {
   return def->getValueAsString("interfaceName");
 }
@@ -168,9 +180,16 @@ std::vector<Availability> getAvailabilities(const Record &def) {
 
 static void emitInterfaceDef(const Availability &availability,
                              raw_ostream &os) {
+
+  os << availability.getQueryFnRetType() << " ";
+
+  StringRef cppNamespace = availability.getInterfaceClassNamespace();
+  cppNamespace.consume_front("::");
+  if (!cppNamespace.empty())
+    os << cppNamespace << "::";
+
   StringRef methodName = availability.getQueryFnName();
-  os << availability.getQueryFnRetType() << " "
-     << availability.getInterfaceClassName() << "::" << methodName << "() {\n"
+  os << availability.getInterfaceClassName() << "::" << methodName << "() {\n"
      << "  return getImpl()->" << methodName << "(getImpl(), getOperation());\n"
      << "}\n";
 }
@@ -237,13 +256,16 @@ static void emitInterfaceDecl(const Availability &availability,
   std::string interfaceTraitsName =
       std::string(formatv("{0}Traits", interfaceName));
 
+  StringRef cppNamespace = availability.getInterfaceClassNamespace();
+  NamespaceEmitter nsEmitter(os, cppNamespace);
+
   // Emit the traits struct containing the concept and model declarations.
   os << "namespace detail {\n"
      << "struct " << interfaceTraitsName << " {\n";
   emitConceptDecl(availability, os);
   os << '\n';
   emitModelDecl(availability, os);
-  os << "};\n} // end namespace detail\n\n";
+  os << "};\n} // namespace detail\n\n";
 
   // Emit the main interface class declaration.
   os << "/*\n" << availability.getInterfaceDescription().trim() << "\n*/\n";


        


More information about the Mlir-commits mailing list