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__anondf3c03950111::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 
47         diag << " on resource '" << instance.getResource()->getName() << "'";
48       }
49     });
50   }
51 };
52 } // end anonymous namespace
53 
54 namespace mlir {
registerSideEffectTestPasses()55 void registerSideEffectTestPasses() {
56   PassRegistration<SideEffectsPass>("test-side-effects",
57                                     "Test side effects interfaces");
58 }
59 } // namespace mlir
60