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