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 (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(name: "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
47namespace mlir {
48void registerTestOperationEqualPass() {
49 PassRegistration<TestOperationEqualPass>();
50}
51} // namespace mlir
52

source code of mlir/test/lib/IR/TestOperationEquals.cpp