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 : public ModulePass<SideEffectsPass> { 16 void runOnModule() override { 17 auto module = getModule(); 18 19 // Walk operations detecting side effects. 20 SmallVector<MemoryEffects::EffectInstance, 8> effects; 21 module.walk([&](MemoryEffectOpInterface op) { 22 effects.clear(); 23 op.getEffects(effects); 24 25 // Check to see if this operation has any memory effects. 26 if (effects.empty()) { 27 op.emitRemark() << "operation has no memory effects"; 28 return; 29 } 30 31 for (MemoryEffects::EffectInstance instance : effects) { 32 auto diag = op.emitRemark() << "found an instance of "; 33 34 if (isa<MemoryEffects::Allocate>(instance.getEffect())) 35 diag << "'allocate'"; 36 else if (isa<MemoryEffects::Free>(instance.getEffect())) 37 diag << "'free'"; 38 else if (isa<MemoryEffects::Read>(instance.getEffect())) 39 diag << "'read'"; 40 else if (isa<MemoryEffects::Write>(instance.getEffect())) 41 diag << "'write'"; 42 43 if (instance.getValue()) 44 diag << " on a value,"; 45 46 diag << " on resource '" << instance.getResource()->getName() << "'"; 47 } 48 }); 49 } 50 }; 51 } // end anonymous namespace 52 53 namespace mlir { 54 void registerSideEffectTestPasses() { 55 PassRegistration<SideEffectsPass>("test-side-effects", 56 "Test side effects interfaces"); 57 } 58 } // namespace mlir 59