1 //===- TestSidEffects.cpp - Pass to test side effects ---------------------===// 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 "TestDialect.h" 10 #include "mlir/Pass/Pass.h" 11 12 using namespace mlir; 13 14 namespace { 15 struct SideEffectsPass 16 : public PassWrapper<SideEffectsPass, OperationPass<ModuleOp>> { 17 StringRef getArgument() const final { return "test-side-effects"; } 18 StringRef getDescription() const final { 19 return "Test side effects interfaces"; 20 } 21 void runOnOperation() override { 22 auto module = getOperation(); 23 24 // Walk operations detecting side effects. 25 SmallVector<MemoryEffects::EffectInstance, 8> effects; 26 module.walk([&](MemoryEffectOpInterface op) { 27 effects.clear(); 28 op.getEffects(effects); 29 30 // Check to see if this operation has any memory effects. 31 if (effects.empty()) { 32 op.emitRemark() << "operation has no memory effects"; 33 return; 34 } 35 36 for (MemoryEffects::EffectInstance instance : effects) { 37 auto diag = op.emitRemark() << "found an instance of "; 38 39 if (isa<MemoryEffects::Allocate>(instance.getEffect())) 40 diag << "'allocate'"; 41 else if (isa<MemoryEffects::Free>(instance.getEffect())) 42 diag << "'free'"; 43 else if (isa<MemoryEffects::Read>(instance.getEffect())) 44 diag << "'read'"; 45 else if (isa<MemoryEffects::Write>(instance.getEffect())) 46 diag << "'write'"; 47 48 if (instance.getValue()) 49 diag << " on a value,"; 50 else if (SymbolRefAttr symbolRef = instance.getSymbolRef()) 51 diag << " on a symbol '" << symbolRef << "',"; 52 53 diag << " on resource '" << instance.getResource()->getName() << "'"; 54 } 55 }); 56 57 SmallVector<TestEffects::EffectInstance, 1> testEffects; 58 module.walk([&](TestEffectOpInterface op) { 59 testEffects.clear(); 60 op.getEffects(testEffects); 61 62 if (testEffects.empty()) 63 return; 64 65 for (const TestEffects::EffectInstance &instance : testEffects) { 66 op.emitRemark() << "found a parametric effect with " 67 << instance.getParameters(); 68 } 69 }); 70 } 71 }; 72 } // end anonymous namespace 73 74 namespace mlir { 75 void registerSideEffectTestPasses() { PassRegistration<SideEffectsPass>(); } 76 } // namespace mlir 77