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>> { 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 }; 48 } // namespace 49 50 namespace mlir { 51 void registerTestOperationEqualPass() { 52 PassRegistration<TestOperationEqualPass>(); 53 } 54 } // namespace mlir 55