[Mlir-commits] [mlir] 2e36dad - [mlir] Make `OperationFingerPrint` class public

Ivan Butygin llvmlistbot at llvm.org
Wed Nov 2 03:26:30 PDT 2022


Author: Ivan Butygin
Date: 2022-11-02T11:00:26+01:00
New Revision: 2e36dadbd6eab10641ac53969ae3dee0b0391e25

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

LOG: [mlir] Make `OperationFingerPrint` class public

It can be useful to external users as well for detecting if there were any changes in IR between passes.

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

Added: 
    

Modified: 
    mlir/include/mlir/IR/OperationSupport.h
    mlir/lib/IR/OperationSupport.cpp
    mlir/lib/Pass/IRPrinting.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h
index 3ce7ff37c8252..24732decc856a 100644
--- a/mlir/include/mlir/IR/OperationSupport.h
+++ b/mlir/include/mlir/IR/OperationSupport.h
@@ -894,6 +894,29 @@ struct OperationEquivalence {
 /// Enable Bitmask enums for OperationEquivalence::Flags.
 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
 
+//===----------------------------------------------------------------------===//
+// OperationFingerPrint
+//===----------------------------------------------------------------------===//
+
+/// A unique fingerprint for a specific operation, and all of it's internal
+/// operations.
+class OperationFingerPrint {
+public:
+  OperationFingerPrint(Operation *topOp);
+  OperationFingerPrint(const OperationFingerPrint &) = default;
+  OperationFingerPrint &operator=(const OperationFingerPrint &) = default;
+
+  bool operator==(const OperationFingerPrint &other) const {
+    return hash == other.hash;
+  }
+  bool operator!=(const OperationFingerPrint &other) const {
+    return !(*this == other);
+  }
+
+private:
+  std::array<uint8_t, 20> hash;
+};
+
 } // namespace mlir
 
 namespace llvm {

diff  --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp
index 33828a954c633..d46f1b46bf7b8 100644
--- a/mlir/lib/IR/OperationSupport.cpp
+++ b/mlir/lib/IR/OperationSupport.cpp
@@ -16,6 +16,7 @@
 #include "mlir/IR/BuiltinTypes.h"
 #include "mlir/IR/OpDefinition.h"
 #include "llvm/ADT/BitVector.h"
+#include "llvm/Support/SHA1.h"
 #include <numeric>
 
 using namespace mlir;
@@ -757,3 +758,42 @@ bool OperationEquivalence::isEquivalentTo(
       return false;
   return true;
 }
+
+//===----------------------------------------------------------------------===//
+// OperationFingerPrint
+//===----------------------------------------------------------------------===//
+
+template <typename T>
+static void addDataToHash(llvm::SHA1 &hasher, const T &data) {
+  hasher.update(
+      ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&data), sizeof(T)));
+}
+
+OperationFingerPrint::OperationFingerPrint(Operation *topOp) {
+  llvm::SHA1 hasher;
+
+  // Hash each of the operations based upon their mutable bits:
+  topOp->walk([&](Operation *op) {
+    //   - Operation pointer
+    addDataToHash(hasher, op);
+    //   - Attributes
+    addDataToHash(hasher, op->getAttrDictionary());
+    //   - Blocks in Regions
+    for (Region &region : op->getRegions()) {
+      for (Block &block : region) {
+        addDataToHash(hasher, &block);
+        for (BlockArgument arg : block.getArguments())
+          addDataToHash(hasher, arg);
+      }
+    }
+    //   - Location
+    addDataToHash(hasher, op->getLoc().getAsOpaquePointer());
+    //   - Operands
+    for (Value operand : op->getOperands())
+      addDataToHash(hasher, operand);
+    //   - Successors
+    for (unsigned i = 0, e = op->getNumSuccessors(); i != e; ++i)
+      addDataToHash(hasher, op->getSuccessor(i));
+  });
+  hash = hasher.result();
+}

diff  --git a/mlir/lib/Pass/IRPrinting.cpp b/mlir/lib/Pass/IRPrinting.cpp
index c20d9b1e9135f..ee52bf81847c2 100644
--- a/mlir/lib/Pass/IRPrinting.cpp
+++ b/mlir/lib/Pass/IRPrinting.cpp
@@ -11,66 +11,11 @@
 #include "mlir/Pass/PassManager.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/FormatVariadic.h"
-#include "llvm/Support/SHA1.h"
 
 using namespace mlir;
 using namespace mlir::detail;
 
 namespace {
-//===----------------------------------------------------------------------===//
-// OperationFingerPrint
-//===----------------------------------------------------------------------===//
-
-/// A unique fingerprint for a specific operation, and all of it's internal
-/// operations.
-class OperationFingerPrint {
-public:
-  OperationFingerPrint(Operation *topOp) {
-    llvm::SHA1 hasher;
-
-    // Hash each of the operations based upon their mutable bits:
-    topOp->walk([&](Operation *op) {
-      //   - Operation pointer
-      addDataToHash(hasher, op);
-      //   - Attributes
-      addDataToHash(hasher, op->getAttrDictionary());
-      //   - Blocks in Regions
-      for (Region &region : op->getRegions()) {
-        for (Block &block : region) {
-          addDataToHash(hasher, &block);
-          for (BlockArgument arg : block.getArguments())
-            addDataToHash(hasher, arg);
-        }
-      }
-      //   - Location
-      addDataToHash(hasher, op->getLoc().getAsOpaquePointer());
-      //   - Operands
-      for (Value operand : op->getOperands())
-        addDataToHash(hasher, operand);
-      //   - Successors
-      for (unsigned i = 0, e = op->getNumSuccessors(); i != e; ++i)
-        addDataToHash(hasher, op->getSuccessor(i));
-    });
-    hash = hasher.result();
-  }
-
-  bool operator==(const OperationFingerPrint &other) const {
-    return hash == other.hash;
-  }
-  bool operator!=(const OperationFingerPrint &other) const {
-    return !(*this == other);
-  }
-
-private:
-  template <typename T>
-  void addDataToHash(llvm::SHA1 &hasher, const T &data) {
-    hasher.update(
-        ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&data), sizeof(T)));
-  }
-
-  std::array<uint8_t, 20> hash;
-};
-
 //===----------------------------------------------------------------------===//
 // IRPrinter
 //===----------------------------------------------------------------------===//


        


More information about the Mlir-commits mailing list