Files
clang-p2996/mlir/test/lib/IR/TestSideEffects.cpp
River Riddle f8923584da [mlir][SideEffects] Define a set of interfaces and traits for defining side effects
This revision introduces the infrastructure for defining side-effects and attaching them to operations. This infrastructure allows for defining different types of side effects, that don't interact with each other, but use the same internal mechanisms. At the base of this is an interface that allows operations to specify the different effect instances that are exhibited by a specific operation instance. An effect instance is comprised of the following:

* Effect: The specific effect being applied.
  For memory related effects this may be reading from memory, storing to memory, etc.

* Value: A specific value, either operand/result/region argument, the effect pertains to.

* Resource: This is a global entity that represents the domain within which the effect is being applied.

MLIR serves many different abstractions, which cover many different domains. Simple effects are may have very different context, for example writing to an in-memory buffer vs a database. This revision defines uses this infrastructure to define a set of initial MemoryEffects. The are effects that generally correspond to memory of some kind; Allocate, Free, Read, Write.

This set of memory effects will be used in follow revisions to generalize various parts of the compiler, and make others more powerful(e.g. DCE).

This infrastructure was originally proposed here:
https://groups.google.com/a/tensorflow.org/g/mlir/c/v2mNl4vFCUM

Differential Revision: https://reviews.llvm.org/D74439
2020-03-06 14:04:36 -08:00

59 lines
1.8 KiB
C++

//===- TestSidEffects.cpp - Pass to test side effects ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "TestDialect.h"
#include "mlir/Pass/Pass.h"
using namespace mlir;
namespace {
struct SideEffectsPass : public ModulePass<SideEffectsPass> {
void runOnModule() override {
auto module = getModule();
// Walk operations detecting side effects.
SmallVector<MemoryEffects::EffectInstance, 8> effects;
module.walk([&](MemoryEffectOpInterface op) {
effects.clear();
op.getEffects(effects);
// Check to see if this operation has any memory effects.
if (effects.empty()) {
op.emitRemark() << "operation has no memory effects";
return;
}
for (MemoryEffects::EffectInstance instance : effects) {
auto diag = op.emitRemark() << "found an instance of ";
if (isa<MemoryEffects::Allocate>(instance.getEffect()))
diag << "'allocate'";
else if (isa<MemoryEffects::Free>(instance.getEffect()))
diag << "'free'";
else if (isa<MemoryEffects::Read>(instance.getEffect()))
diag << "'read'";
else if (isa<MemoryEffects::Write>(instance.getEffect()))
diag << "'write'";
if (instance.getValue())
diag << " on a value,";
diag << " on resource '" << instance.getResource()->getName() << "'";
}
});
}
};
} // end anonymous namespace
namespace mlir {
void registerSideEffectTestPasses() {
PassRegistration<SideEffectsPass>("test-side-effects",
"Test side effects interfaces");
}
} // namespace mlir