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
12using namespace mlir;
13
14namespace {
15/// This pass illustrates the IR def-use chains through printing.
16struct TestOperationEqualPass
17 : public PassWrapper<TestOperationEqualPass, OperationPass<ModuleOp>> {
18 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestOperationEqualPass)
19
20 StringRef getArgument() const final { return "test-operations-equality"; }
21 StringRef getDescription() const final { return "Test operations equality."; }
22 void runOnOperation() override {
23 ModuleOp module = getOperation();
24 // Expects two operations at the top-level:
25 int opCount = module.getBody()->getOperations().size();
26 if (module->hasDiscardableAttr(name: "test.includes_setup")) {
27 if (opCount < 2) {
28 module.emitError()
29 << "expected at least 2 top-level ops in the module, got "
30 << opCount;
31 return signalPassFailure();
32 }
33 } else if (opCount != 2) {
34 module.emitError() << "expected 2 top-level ops in the module, got "
35 << opCount;
36 return signalPassFailure();
37 }
38 Operation *second = &module.getBody()->back();
39 Operation *first = second->getPrevNode();
40
41 llvm::outs() << first->getName().getStringRef() << " with attr "
42 << first->getDiscardableAttrDictionary();
43 OperationEquivalence::Flags flags{};
44 if (!first->hasDiscardableAttr(name: "strict_loc_check"))
45 flags |= OperationEquivalence::IgnoreLocations;
46 if (first->hasDiscardableAttr(name: "ignore_commutativity"))
47 flags |= OperationEquivalence::IgnoreCommutativity;
48 if (OperationEquivalence::isEquivalentTo(lhs: first, rhs: &module.getBody()->back(),
49 flags))
50 llvm::outs() << " compares equals.\n";
51 else
52 llvm::outs() << " compares NOT equals!\n";
53 }
54};
55} // namespace
56
57namespace mlir {
58void registerTestOperationEqualPass() {
59 PassRegistration<TestOperationEqualPass>();
60}
61} // namespace mlir
62