From 83e074a0c652a668c8a5d572f8c77b58c8383ff0 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 5 Oct 2021 09:32:35 -0400 Subject: [PATCH] [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 --- .../mlir/Dialect/SPIRV/IR/SPIRVAvailability.td | 2 ++ mlir/include/mlir/Dialect/SPIRV/IR/SPIRVBase.td | 4 ++++ mlir/include/mlir/Dialect/SPIRV/IR/SPIRVOps.h | 8 +++---- mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp | 6 ----- mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp | 28 +++++++++++++++++++--- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVAvailability.td b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVAvailability.td index 4e2e943..bfb03da 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 539de6a..990af80 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 : 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 : MinVersionBase< class MaxVersion : 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 : MaxVersionBase< } class Extension extensions> : Availability { + let cppNamespace = "::mlir::spirv"; let interfaceName = "QueryExtensionInterface"; let interfaceDescription = [{ Querying interface for required SPIR-V extensions. @@ -189,6 +192,7 @@ class Extension extensions> : Availability { } class Capability 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 c4f5fd1..e3d4e56 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 f711c18..9a26622 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 10c00fe..4053bc9 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 @@ public: // 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 @@ public: // 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 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"; -- 2.7.4