blob: ede4da7775d86a1cd9a1bfe9a8136671095a74dd [file] [log] [blame]
Mehdi Amini0be5d1a2021-07-29 05:01:041//===- 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
Mehdi Amini0be5d1a2021-07-29 05:01:049#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 StringRef getArgument() const final { return "test-operations-equality"; }
19 StringRef getDescription() const final { return "Test operations equality."; }
20 void runOnOperation() override {
21 ModuleOp module = getOperation();
22 // Expects two operations at the top-level:
23 int opCount = module.getBody()->getOperations().size();
24 if (opCount != 2) {
25 module.emitError() << "expected 2 top-level ops in the module, got "
26 << opCount;
27 return signalPassFailure();
28 }
29 DenseMap<Value, Value> valuesMap;
30 auto mapValue = [&](Value lhs, Value rhs) {
31 auto insertion = valuesMap.insert({lhs, rhs});
32 return success(insertion.first->second == rhs);
33 };
34
35 Operation *first = &module.getBody()->front();
36 llvm::outs() << first->getName().getStringRef() << " with attr "
37 << first->getAttrDictionary();
38 OperationEquivalence::Flags flags{};
39 if (!first->hasAttr("strict_loc_check"))
40 flags |= OperationEquivalence::IgnoreLocations;
41 if (OperationEquivalence::isEquivalentTo(first, &module.getBody()->back(),
42 mapValue, mapValue, flags))
43 llvm::outs() << " compares equals.\n";
44 else
45 llvm::outs() << " compares NOT equals!\n";
46 }
47};
Mehdi Aminibe0a7e92021-12-07 18:27:5848} // namespace
Mehdi Amini0be5d1a2021-07-29 05:01:0449
50namespace mlir {
51void registerTestOperationEqualPass() {
52 PassRegistration<TestOperationEqualPass>();
53}
54} // namespace mlir