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/Dialect/StandardOps/IR/Ops.h"
10 #include "mlir/IR/BuiltinOps.h"
11 #include "mlir/Pass/Pass.h"
12 
13 using namespace mlir;
14 
15 namespace {
16 /// This pass illustrates the IR def-use chains through printing.
17 struct TestOperationEqualPass
18     : public PassWrapper<TestOperationEqualPass, OperationPass<ModuleOp>> {
19   StringRef getArgument() const final { return "test-operations-equality"; }
20   StringRef getDescription() const final { return "Test operations equality."; }
21   void runOnOperation() override {
22     ModuleOp module = getOperation();
23     // Expects two operations at the top-level:
24     int opCount = module.getBody()->getOperations().size();
25     if (opCount != 2) {
26       module.emitError() << "expected 2 top-level ops in the module, got "
27                          << opCount;
28       return signalPassFailure();
29     }
30     DenseMap<Value, Value> valuesMap;
31     auto mapValue = [&](Value lhs, Value rhs) {
32       auto insertion = valuesMap.insert({lhs, rhs});
33       return success(insertion.first->second == rhs);
34     };
35 
36     Operation *first = &module.getBody()->front();
37     llvm::outs() << first->getName().getStringRef() << " with attr "
38                  << first->getAttrDictionary();
39     OperationEquivalence::Flags flags{};
40     if (!first->hasAttr("strict_loc_check"))
41       flags |= OperationEquivalence::IgnoreLocations;
42     if (OperationEquivalence::isEquivalentTo(first, &module.getBody()->back(),
43                                              mapValue, mapValue, flags))
44       llvm::outs() << " compares equals.\n";
45     else
46       llvm::outs() << " compares NOT equals!\n";
47   }
48 };
49 } // end anonymous namespace
50 
51 namespace mlir {
52 void registerTestOperationEqualPass() {
53   PassRegistration<TestOperationEqualPass>();
54 }
55 } // namespace mlir
56