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   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     SmallVector<TestEffects::EffectInstance, 1> testEffects;
52     module.walk([&](TestEffectOpInterface op) {
53       testEffects.clear();
54       op.getEffects(testEffects);
55 
56       if (testEffects.empty())
57         return;
58 
59       for (const TestEffects::EffectInstance &instance : testEffects) {
60         op.emitRemark() << "found a parametric effect with "
61                         << instance.getParameters();
62       }
63     });
64   }
65 };
66 } // end anonymous namespace
67 
68 namespace mlir {
69 void registerSideEffectTestPasses() {
70   PassRegistration<SideEffectsPass>("test-side-effects",
71                                     "Test side effects interfaces");
72 }
73 } // namespace mlir
74