[Mlir-commits] [mlir] ed8bd71 - [MLIR] Allow comparison of opaque properties (#66378)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Sep 17 23:46:35 PDT 2023


Author: Dudeldu
Date: 2023-09-17T23:46:31-07:00
New Revision: ed8bd7176d0507f4d2f711df8eb48bb253fcb667

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

LOG: [MLIR] Allow comparison of opaque properties (#66378)

Add capabilities for comparing opaque properties. This is useful when
dealing with arbitrary operations which can be compare based on their
OperationName. Now you can furthermore compare their properties without
the need to determine their actual type.

Added: 
    

Modified: 
    mlir/include/mlir/IR/ExtensibleDialect.h
    mlir/include/mlir/IR/OperationSupport.h
    mlir/lib/IR/MLIRContext.cpp
    mlir/lib/IR/OperationSupport.cpp
    mlir/unittests/IR/OpPropertiesTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/ExtensibleDialect.h b/mlir/include/mlir/IR/ExtensibleDialect.h
index 37821d3a2a5163f..baff6011ff84651 100644
--- a/mlir/include/mlir/IR/ExtensibleDialect.h
+++ b/mlir/include/mlir/IR/ExtensibleDialect.h
@@ -495,6 +495,7 @@ class DynamicOpDefinition : public OperationName::Impl {
   }
   Attribute getPropertiesAsAttr(Operation *op) final { return {}; }
   void copyProperties(OpaqueProperties lhs, OpaqueProperties rhs) final {}
+  bool compareProperties(OpaqueProperties, OpaqueProperties) final { return false; }
   llvm::hash_code hashProperties(OpaqueProperties prop) final { return {}; }
 
 private:

diff  --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h
index 19ffddc30904897..9e9f89b95e6fd50 100644
--- a/mlir/include/mlir/IR/OperationSupport.h
+++ b/mlir/include/mlir/IR/OperationSupport.h
@@ -141,6 +141,7 @@ class OperationName {
                           function_ref<InFlightDiagnostic &()> getDiag) = 0;
     virtual Attribute getPropertiesAsAttr(Operation *) = 0;
     virtual void copyProperties(OpaqueProperties, OpaqueProperties) = 0;
+    virtual bool compareProperties(OpaqueProperties, OpaqueProperties) = 0;
     virtual llvm::hash_code hashProperties(OpaqueProperties) = 0;
   };
 
@@ -221,6 +222,7 @@ class OperationName {
                           function_ref<InFlightDiagnostic &()> getDiag) final;
     Attribute getPropertiesAsAttr(Operation *) final;
     void copyProperties(OpaqueProperties, OpaqueProperties) final;
+    bool compareProperties(OpaqueProperties, OpaqueProperties) final;
     llvm::hash_code hashProperties(OpaqueProperties) final;
   };
 
@@ -445,6 +447,10 @@ class OperationName {
     return getImpl()->copyProperties(lhs, rhs);
   }
 
+  bool compareOpProperties(OpaqueProperties lhs, OpaqueProperties rhs) const {
+    return getImpl()->compareProperties(lhs, rhs);
+  }
+
   llvm::hash_code hashOpProperties(OpaqueProperties properties) const {
     return getImpl()->hashProperties(properties);
   }
@@ -646,6 +652,13 @@ class RegisteredOperationName : public OperationName {
       }
       return {};
     }
+    bool compareProperties(OpaqueProperties lhs, OpaqueProperties rhs) final {
+      if constexpr (hasProperties) {
+        return *lhs.as<Properties *>() == *rhs.as<Properties *>();
+      } else {
+        return true;
+      }
+    }
     void copyProperties(OpaqueProperties lhs, OpaqueProperties rhs) final {
       *lhs.as<Properties *>() = *rhs.as<Properties *>();
     }

diff  --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp
index 5f1d036d22b918e..ed1029959639587 100644
--- a/mlir/lib/IR/MLIRContext.cpp
+++ b/mlir/lib/IR/MLIRContext.cpp
@@ -864,6 +864,10 @@ void OperationName::UnregisteredOpModel::copyProperties(OpaqueProperties lhs,
                                                         OpaqueProperties rhs) {
   *lhs.as<Attribute *>() = *rhs.as<Attribute *>();
 }
+bool OperationName::UnregisteredOpModel::compareProperties(OpaqueProperties lhs,
+                                                        OpaqueProperties rhs) {
+  return *lhs.as<Attribute *>() == *rhs.as<Attribute *>();
+}
 llvm::hash_code
 OperationName::UnregisteredOpModel::hashProperties(OpaqueProperties prop) {
   return llvm::hash_combine(*prop.as<Attribute *>());

diff  --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp
index 0cb6a1cd191b161..65b123e10ddbc3a 100644
--- a/mlir/lib/IR/OperationSupport.cpp
+++ b/mlir/lib/IR/OperationSupport.cpp
@@ -768,7 +768,8 @@ OperationEquivalence::isRegionEquivalentTo(Region *lhs, Region *rhs,
       lhs->getNumSuccessors() != rhs->getNumSuccessors() ||
       lhs->getNumOperands() != rhs->getNumOperands() ||
       lhs->getNumResults() != rhs->getNumResults() ||
-      lhs->hashProperties() != rhs->hashProperties())
+      !lhs->getName().compareOpProperties(lhs->getPropertiesStorage(),
+                                        rhs->getPropertiesStorage()))
     return false;
   if (!(flags & IgnoreLocations) && lhs->getLoc() != rhs->getLoc())
     return false;

diff  --git a/mlir/unittests/IR/OpPropertiesTest.cpp b/mlir/unittests/IR/OpPropertiesTest.cpp
index 2d272dfb558c81c..e2d0599f1bd9548 100644
--- a/mlir/unittests/IR/OpPropertiesTest.cpp
+++ b/mlir/unittests/IR/OpPropertiesTest.cpp
@@ -28,6 +28,11 @@ struct TestProperties {
   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestProperties)
 };
 
+bool operator==(const TestProperties &lhs, TestProperties &rhs) {
+  return lhs.a == rhs.a && lhs.b == rhs.b && lhs.array == rhs.array &&
+         lhs.label == rhs.label;
+}
+
 /// Convert a DictionaryAttr to a TestProperties struct, optionally emit errors
 /// through the provided diagnostic if any. This is used for example during
 /// parsing with the generic format.


        


More information about the Mlir-commits mailing list