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