MLIR  19.0.0git
InferEffects.cpp
Go to the documentation of this file.
1 //===- InferEffects.cpp - Infer memory effects for named symbols ----------===//
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 
11 
13 #include "mlir/IR/Visitors.h"
16 #include "llvm/ADT/DenseSet.h"
17 
18 using namespace mlir;
19 
20 namespace mlir {
21 namespace transform {
22 #define GEN_PASS_DEF_INFEREFFECTSPASS
23 #include "mlir/Dialect/Transform/Transforms/Passes.h.inc"
24 } // namespace transform
25 } // namespace mlir
26 
28  if (!isa<transform::TransformOpInterface>(op))
29  return success();
30 
31  auto func = dyn_cast<FunctionOpInterface>(op);
32  if (!func || func.isExternal())
33  return success();
34 
35  if (!func.getFunctionBody().hasOneBlock()) {
36  return op->emitError()
37  << "only single-block operations are currently supported";
38  }
39 
40  // Note that there can't be an inclusion of an unannotated symbol because it
41  // wouldn't have passed the verifier, so recursion isn't necessary here.
42  llvm::SmallDenseSet<unsigned> consumedArguments;
43  transform::getConsumedBlockArguments(func.getFunctionBody().front(),
44  consumedArguments);
45 
46  for (unsigned i = 0, e = func.getNumArguments(); i < e; ++i) {
47  func.setArgAttr(i,
48  consumedArguments.contains(i)
49  ? transform::TransformDialect::kArgConsumedAttrName
50  : transform::TransformDialect::kArgReadOnlyAttrName,
51  UnitAttr::get(op->getContext()));
52  }
53  return success();
54 }
55 
56 namespace {
57 class InferEffectsPass
58  : public transform::impl::InferEffectsPassBase<InferEffectsPass> {
59 public:
60  void runOnOperation() override {
61  WalkResult result = getOperation()->walk([](Operation *op) {
63  : WalkResult::advance();
64  });
65  if (result.wasInterrupted())
66  return signalPassFailure();
67  }
68 };
69 } // namespace
static LogicalResult inferSideEffectAnnotations(Operation *op)
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
MLIRContext * getContext()
Return the context this operation is associated with.
Definition: Operation.h:216
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:268
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: Visitors.h:34
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition: Visitors.h:56
static WalkResult interrupt()
Definition: Visitors.h:51
void getConsumedBlockArguments(Block &block, llvm::SmallDenseSet< unsigned > &consumedArguments)
Populates consumedArguments with positions of block arguments that are consumed by the operations in ...
Include the generated interface declarations.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26