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
12using namespace mlir;
13
14namespace {
15struct SideEffectsPass
16 : public PassWrapper<SideEffectsPass, OperationPass<ModuleOp>> {
17 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SideEffectsPass)
18
19 StringRef getArgument() const final { return "test-side-effects"; }
20 StringRef getDescription() const final {
21 return "Test side effects interfaces";
22 }
23 void runOnOperation() override {
24 auto module = getOperation();
25
26 // Emit recursive memory effects.
27 module.walk(callback: [](Operation *op) {
28 if (op->hasTrait<OpTrait::HasRecursiveMemoryEffects>())
29 op->emitRemark() << "recursive memory effects";
30 });
31
32 // Walk operations detecting side effects.
33 SmallVector<MemoryEffects::EffectInstance, 8> effects;
34 module.walk(callback: [&](MemoryEffectOpInterface op) {
35 effects.clear();
36 op.getEffects(effects);
37
38 if (op->hasTrait<OpTrait::IsTerminator>()) {
39 return;
40 }
41
42 // Check to see if this operation has any memory effects.
43 if (effects.empty()) {
44 op.emitRemark() << "operation has no memory effects";
45 return;
46 }
47
48 for (MemoryEffects::EffectInstance instance : effects) {
49 auto diag = op.emitRemark() << "found an instance of ";
50
51 if (isa<MemoryEffects::Allocate>(Val: instance.getEffect()))
52 diag << "'allocate'";
53 else if (isa<MemoryEffects::Free>(Val: instance.getEffect()))
54 diag << "'free'";
55 else if (isa<MemoryEffects::Read>(Val: instance.getEffect()))
56 diag << "'read'";
57 else if (isa<MemoryEffects::Write>(Val: instance.getEffect()))
58 diag << "'write'";
59
60 if (instance.getValue()) {
61 if (auto *opOpd = instance.getEffectValue<OpOperand *>())
62 diag << " on op operand " << opOpd->getOperandNumber() << ",";
63 else if (auto opRes = instance.getEffectValue<OpResult>())
64 diag << " on op result " << opRes.getResultNumber() << ",";
65 else if (auto opBlk = instance.getEffectValue<BlockArgument>())
66 diag << " on block argument " << opBlk.getArgNumber() << ",";
67 } else if (SymbolRefAttr symbolRef = instance.getSymbolRef())
68 diag << " on a symbol '" << symbolRef << "',";
69
70 diag << " on resource '" << instance.getResource()->getName() << "'";
71 if (Attribute parameters = instance.getParameters())
72 diag << " with parameters " << parameters;
73 }
74 });
75
76 SmallVector<TestEffects::EffectInstance, 1> testEffects;
77 module.walk(callback: [&](TestEffectOpInterface op) {
78 testEffects.clear();
79 op.getEffects(testEffects);
80
81 if (testEffects.empty())
82 return;
83
84 for (const TestEffects::EffectInstance &instance : testEffects) {
85 op.emitRemark() << "found a parametric effect with "
86 << instance.getParameters();
87 }
88 });
89 }
90};
91} // namespace
92
93namespace mlir {
94void registerSideEffectTestPasses() { PassRegistration<SideEffectsPass>(); }
95} // namespace mlir
96