xref: /llvm-project/mlir/test/lib/IR/TestOperationEquals.cpp (revision bbe5bf1788b55e3c7020d50ee0fd5956f261cfec)
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__anon70e82eb10111::TestOperationEqualPass18   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOperationEqualPass)
19 
20   StringRef getArgument() const final { return "test-operations-equality"; }
getDescription__anon70e82eb10111::TestOperationEqualPass21   StringRef getDescription() const final { return "Test operations equality."; }
runOnOperation__anon70e82eb10111::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 
32     Operation *first = &module.getBody()->front();
33     llvm::outs() << first->getName().getStringRef() << " with attr "
34                  << first->getDiscardableAttrDictionary();
35     OperationEquivalence::Flags flags{};
36     if (!first->hasAttr("strict_loc_check"))
37       flags |= OperationEquivalence::IgnoreLocations;
38     if (OperationEquivalence::isEquivalentTo(first, &module.getBody()->back(),
39                                              flags))
40       llvm::outs() << " compares equals.\n";
41     else
42       llvm::outs() << " compares NOT equals!\n";
43   }
44 };
45 } // namespace
46 
47 namespace mlir {
registerTestOperationEqualPass()48 void registerTestOperationEqualPass() {
49   PassRegistration<TestOperationEqualPass>();
50 }
51 } // namespace mlir
52