[mlir] Make `OperationFingerPrint` class public
authorIvan Butygin <ivan.butygin@gmail.com>
Mon, 31 Oct 2022 13:16:03 +0000 (14:16 +0100)
committerIvan Butygin <ivan.butygin@gmail.com>
Wed, 2 Nov 2022 10:00:26 +0000 (11:00 +0100)
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

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

index 3ce7ff3..24732de 100644 (file)
@@ -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 {
index 33828a9..d46f1b4 100644 (file)
@@ -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();
+}
index c20d9b1..ee52bf8 100644 (file)
 #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
 //===----------------------------------------------------------------------===//