[Mlir-commits] [mlir] 032743e - [mlir] Add support for the attribute verifiers in the dialect declaration generator.

River Riddle llvmlistbot at llvm.org
Mon Mar 16 18:35:03 PDT 2020


Author: River Riddle
Date: 2020-03-16T18:32:23-07:00
New Revision: 032743e18f2bd9b44fd46061f3c9eeb9cdd0061c

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

LOG: [mlir] Add support for the attribute verifiers in the dialect declaration generator.

Summary: This adds bitfields that map to the dialect attribute verifier hooks. This also moves over the Test dialect to have its declaration generated.

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

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/GPU/GPUOps.td
    mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
    mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td
    mlir/include/mlir/IR/OpBase.td
    mlir/include/mlir/TableGen/Dialect.h
    mlir/lib/TableGen/Dialect.cpp
    mlir/test/lib/TestDialect/CMakeLists.txt
    mlir/test/lib/TestDialect/TestDialect.cpp
    mlir/test/lib/TestDialect/TestDialect.h
    mlir/test/lib/TestDialect/TestOps.td
    mlir/tools/mlir-tblgen/DialectGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/GPU/GPUOps.td b/mlir/include/mlir/Dialect/GPU/GPUOps.td
index 1ff0a8b57f5d..659c10142e81 100644
--- a/mlir/include/mlir/Dialect/GPU/GPUOps.td
+++ b/mlir/include/mlir/Dialect/GPU/GPUOps.td
@@ -28,6 +28,8 @@ def IntLikeOrLLVMInt : TypeConstraint<
 
 def GPU_Dialect : Dialect {
   let name = "gpu";
+  let hasOperationAttrVerify = 1;
+
   let extraClassDeclaration = [{
     /// Get the name of the attribute used to annotate the modules that contain
     /// kernel modules.
@@ -57,9 +59,6 @@ def GPU_Dialect : Dialect {
     /// Returns the numeric value used to identify the private memory address
     /// space.
     static unsigned getPrivateAddressSpace() { return 5; }
-
-    LogicalResult verifyOperationAttribute(Operation *op,
-                                           NamedAttribute attr) override;
   }];
 }
 

diff  --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
index b2d1e57c0f11..20ed573ab8bd 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
@@ -19,17 +19,12 @@ include "mlir/IR/OpBase.td"
 def LLVM_Dialect : Dialect {
   let name = "llvm";
   let cppNamespace = "LLVM";
+  let hasRegionArgAttrVerify = 1;
   let extraClassDeclaration = [{
     ~LLVMDialect();
     llvm::LLVMContext &getLLVMContext();
     llvm::Module &getLLVMModule();
 
-    /// Verify a region argument attribute registered to this dialect.
-    /// Returns failure if the verification failed, success otherwise.
-    LogicalResult verifyRegionArgAttribute(Operation *op, unsigned regionIdx,
-                                           unsigned argIdx,
-                                           NamedAttribute argAttr) override;
-
   private:
     friend LLVMType;
 

diff  --git a/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td b/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td
index 4dc1886fae2d..26d8f1401c32 100644
--- a/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td
+++ b/mlir/include/mlir/Dialect/SPIRV/SPIRVBase.td
@@ -47,6 +47,10 @@ def SPIRV_Dialect : Dialect {
 
   let cppNamespace = "spirv";
   let hasConstantMaterializer = 1;
+  let hasOperationAttrVerify = 1;
+  let hasRegionArgAttrVerify = 1;
+  let hasRegionResultAttrVerify = 1;
+
   let extraClassDeclaration = [{
     //===------------------------------------------------------------------===//
     // Type
@@ -65,23 +69,6 @@ def SPIRV_Dialect : Dialect {
     /// Returns the attribute name to use when specifying decorations on results
     /// of operations.
     static std::string getAttributeName(Decoration decoration);
-
-    /// Provides a hook for verifying SPIR-V dialect attributes attached to the
-    /// given op.
-    LogicalResult verifyOperationAttribute(Operation *op,
-                                           NamedAttribute attribute) override;
-
-    /// Provides a hook for verifying SPIR-V dialect attributes attached to the
-    /// given op's region argument.
-    LogicalResult verifyRegionArgAttribute(Operation *op, unsigned regionIndex,
-                                           unsigned argIndex,
-                                           NamedAttribute attribute) override;
-
-    /// Provides a hook for verifying SPIR-V dialect attributes attached to the
-    /// given op's region result.
-    LogicalResult verifyRegionResultAttribute(
-        Operation *op, unsigned regionIndex, unsigned resultIndex,
-        NamedAttribute attribute) override;
   }];
 }
 

diff  --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td
index 159a3c5eae54..d76c9d5476ef 100644
--- a/mlir/include/mlir/IR/OpBase.td
+++ b/mlir/include/mlir/IR/OpBase.td
@@ -260,6 +260,16 @@ class Dialect {
 
   // If this dialect overrides the hook for materializing constants.
   bit hasConstantMaterializer = 0;
+
+  // If this dialect overrides the hook for verifying operation attributes.
+  bit hasOperationAttrVerify = 0;
+
+  // If this dialect overrides the hook for verifying region argument
+  // attributes.
+  bit hasRegionArgAttrVerify = 0;
+
+  // If this dialect overrides the hook for verifying region result attributes.
+  bit hasRegionResultAttrVerify = 0;
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/include/mlir/TableGen/Dialect.h b/mlir/include/mlir/TableGen/Dialect.h
index 799b244783c4..5e85806f377f 100644
--- a/mlir/include/mlir/TableGen/Dialect.h
+++ b/mlir/include/mlir/TableGen/Dialect.h
@@ -49,6 +49,15 @@ class Dialect {
   // Returns if this dialect has a constant materializer or not.
   bool hasConstantMaterializer() const;
 
+  /// Returns if this dialect has an operation attribute verifier.
+  bool hasOperationAttrVerify() const;
+
+  /// Returns if this dialect has a region argument attribute verifier.
+  bool hasRegionArgAttrVerify() const;
+
+  /// Returns if this dialect has a region result attribute verifier.
+  bool hasRegionResultAttrVerify() const;
+
   // Returns whether two dialects are equal by checking the equality of the
   // underlying record.
   bool operator==(const Dialect &other) const;

diff  --git a/mlir/lib/TableGen/Dialect.cpp b/mlir/lib/TableGen/Dialect.cpp
index 7e757eaeae4f..db68ed43593f 100644
--- a/mlir/lib/TableGen/Dialect.cpp
+++ b/mlir/lib/TableGen/Dialect.cpp
@@ -58,6 +58,18 @@ bool tblgen::Dialect::hasConstantMaterializer() const {
   return def->getValueAsBit("hasConstantMaterializer");
 }
 
+bool tblgen::Dialect::hasOperationAttrVerify() const {
+  return def->getValueAsBit("hasOperationAttrVerify");
+}
+
+bool tblgen::Dialect::hasRegionArgAttrVerify() const {
+  return def->getValueAsBit("hasRegionArgAttrVerify");
+}
+
+bool tblgen::Dialect::hasRegionResultAttrVerify() const {
+  return def->getValueAsBit("hasRegionResultAttrVerify");
+}
+
 bool Dialect::operator==(const Dialect &other) const {
   return def == other.def;
 }

diff  --git a/mlir/test/lib/TestDialect/CMakeLists.txt b/mlir/test/lib/TestDialect/CMakeLists.txt
index 6424d5ab23e1..d625a5819096 100644
--- a/mlir/test/lib/TestDialect/CMakeLists.txt
+++ b/mlir/test/lib/TestDialect/CMakeLists.txt
@@ -6,6 +6,7 @@ set(LLVM_OPTIONAL_SOURCES
 set(LLVM_TARGET_DEFINITIONS TestOps.td)
 mlir_tablegen(TestOps.h.inc -gen-op-decls)
 mlir_tablegen(TestOps.cpp.inc -gen-op-defs)
+mlir_tablegen(TestOpsDialect.h.inc -gen-dialect-decls)
 mlir_tablegen(TestOpEnums.h.inc -gen-enum-decls)
 mlir_tablegen(TestOpEnums.cpp.inc -gen-enum-defs)
 mlir_tablegen(TestPatterns.inc -gen-rewriters)

diff  --git a/mlir/test/lib/TestDialect/TestDialect.cpp b/mlir/test/lib/TestDialect/TestDialect.cpp
index 94b5017978a0..da15486ef117 100644
--- a/mlir/test/lib/TestDialect/TestDialect.cpp
+++ b/mlir/test/lib/TestDialect/TestDialect.cpp
@@ -128,7 +128,7 @@ struct TestInlinerInterface : public DialectInlinerInterface {
 //===----------------------------------------------------------------------===//
 
 TestDialect::TestDialect(MLIRContext *context)
-    : Dialect(getDialectName(), context) {
+    : Dialect(getDialectNamespace(), context) {
   addOperations<
 #define GET_OP_LIST
 #include "TestOps.cpp.inc"

diff  --git a/mlir/test/lib/TestDialect/TestDialect.h b/mlir/test/lib/TestDialect/TestDialect.h
index f751918bb1b3..9b4dfee2daaa 100644
--- a/mlir/test/lib/TestDialect/TestDialect.h
+++ b/mlir/test/lib/TestDialect/TestDialect.h
@@ -29,23 +29,7 @@
 
 namespace mlir {
 
-class TestDialect : public Dialect {
-public:
-  /// Create the dialect in the given `context`.
-  TestDialect(MLIRContext *context);
-
-  /// Get the canonical string name of the dialect.
-  static StringRef getDialectName() { return "test"; }
-
-  LogicalResult verifyOperationAttribute(Operation *op,
-                                         NamedAttribute namedAttr) override;
-  LogicalResult verifyRegionArgAttribute(Operation *op, unsigned regionIndex,
-                                         unsigned argIndex,
-                                         NamedAttribute namedAttr) override;
-  LogicalResult verifyRegionResultAttribute(Operation *op, unsigned regionIndex,
-                                            unsigned resultIndex,
-                                            NamedAttribute namedAttr) override;
-};
+#include "TestOpsDialect.h.inc"
 
 #define GET_OP_CLASSES
 #include "TestOps.h.inc"

diff  --git a/mlir/test/lib/TestDialect/TestOps.td b/mlir/test/lib/TestDialect/TestOps.td
index d9710ba03961..cf0ec63fe6c5 100644
--- a/mlir/test/lib/TestDialect/TestOps.td
+++ b/mlir/test/lib/TestDialect/TestOps.td
@@ -17,13 +17,16 @@ include "mlir/Interfaces/ControlFlowInterfaces.td"
 include "mlir/Interfaces/InferTypeOpInterface.td"
 include "mlir/Interfaces/SideEffects.td"
 
-def TEST_Dialect : Dialect {
+def Test_Dialect : Dialect {
   let name = "test";
   let cppNamespace = "";
+  let hasOperationAttrVerify = 1;
+  let hasRegionArgAttrVerify = 1;
+  let hasRegionResultAttrVerify = 1;
 }
 
 class TEST_Op<string mnemonic, list<OpTrait> traits = []> :
-    Op<TEST_Dialect, mnemonic, traits>;
+    Op<Test_Dialect, mnemonic, traits>;
 
 //===----------------------------------------------------------------------===//
 // Test Types

diff  --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index 695ce3de9b93..a662f6273b9d 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -101,6 +101,32 @@ static const char *const constantMaterializerDecl = R"(
                                          ::mlir::Location loc) override;
 )";
 
+/// The code block for the operation attribute verifier hook.
+static const char *const opAttrVerifierDecl = R"(
+    /// Provides a hook for verifying dialect attributes attached to the given
+    /// op.
+    ::mlir::LogicalResult verifyOperationAttribute(
+        ::mlir::Operation *op, ::mlir::NamedAttribute attribute) override;
+)";
+
+/// The code block for the region argument attribute verifier hook.
+static const char *const regionArgAttrVerifierDecl = R"(
+    /// Provides a hook for verifying dialect attributes attached to the given
+    /// op's region argument.
+    ::mlir::LogicalResult verifyRegionArgAttribute(
+        ::mlir::Operation *op, unsigned regionIndex, unsigned argIndex,
+        ::mlir::NamedAttribute attribute) override;
+)";
+
+/// The code block for the region result attribute verifier hook.
+static const char *const regionResultAttrVerifierDecl = R"(
+    /// Provides a hook for verifying dialect attributes attached to the given
+    /// op's region result.
+    ::mlir::LogicalResult verifyRegionResultAttribute(
+        ::mlir::Operation *op, unsigned regionIndex, unsigned resultIndex,
+        ::mlir::NamedAttribute attribute) override;
+)";
+
 /// Generate the declaration for the given dialect class.
 static void emitDialectDecl(Dialect &dialect,
                             iterator_range<DialectFilterIterator> dialectAttrs,
@@ -120,6 +146,12 @@ static void emitDialectDecl(Dialect &dialect,
   // Add the decls for the various features of the dialect.
   if (dialect.hasConstantMaterializer())
     os << constantMaterializerDecl;
+  if (dialect.hasOperationAttrVerify())
+    os << opAttrVerifierDecl;
+  if (dialect.hasRegionArgAttrVerify())
+    os << regionArgAttrVerifierDecl;
+  if (dialect.hasRegionResultAttrVerify())
+    os << regionResultAttrVerifierDecl;
   if (llvm::Optional<StringRef> extraDecl = dialect.getExtraClassDeclaration())
     os << *extraDecl;
 


        


More information about the Mlir-commits mailing list