1 //===- TestOperationEquals.cpp - Passes to test OperationEquivalence ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/IR/BuiltinOps.h"
10 #include "mlir/Pass/Pass.h"
11 
12 using namespace mlir;
13 
14 namespace {
15 /// This pass illustrates the IR def-use chains through printing.
16 struct TestOperationEqualPass
17     : public PassWrapper<TestOperationEqualPass, OperationPass<ModuleOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anon41716f8e0111::TestOperationEqualPass18   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOperationEqualPass)
19 
20   StringRef getArgument() const final { return "test-operations-equality"; }
getDescription__anon41716f8e0111::TestOperationEqualPass21   StringRef getDescription() const final { return "Test operations equality."; }
runOnOperation__anon41716f8e0111::TestOperationEqualPass22   void runOnOperation() override {
23     ModuleOp module = getOperation();
24     // Expects two operations at the top-level:
25     int opCount = module.getBody()->getOperations().size();
26     if (opCount != 2) {
27       module.emitError() << "expected 2 top-level ops in the module, got "
28                          << opCount;
29       return signalPassFailure();
30     }
31     DenseMap<Value, Value> valuesMap;
32     auto mapValue = [&](Value lhs, Value rhs) {
33       auto insertion = valuesMap.insert({lhs, rhs});
34       return success(insertion.first->second == rhs);
35     };
36 
37     Operation *first = &module.getBody()->front();
38     llvm::outs() << first->getName().getStringRef() << " with attr "
39                  << first->getAttrDictionary();
40     OperationEquivalence::Flags flags{};
41     if (!first->hasAttr("strict_loc_check"))
42       flags |= OperationEquivalence::IgnoreLocations;
43     if (OperationEquivalence::isEquivalentTo(first, &module.getBody()->back(),
44                                              mapValue, mapValue, flags))
45       llvm::outs() << " compares equals.\n";
46     else
47       llvm::outs() << " compares NOT equals!\n";
48   }
49 };
50 } // namespace
51 
52 namespace mlir {
registerTestOperationEqualPass()53 void registerTestOperationEqualPass() {
54   PassRegistration<TestOperationEqualPass>();
55 }
56 } // namespace mlir
57