MLIR  22.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"
15 #include "llvm/ADT/DenseSet.h"
16 
17 using namespace mlir;
18 
19 namespace mlir {
20 namespace transform {
21 #define GEN_PASS_DEF_INFEREFFECTSPASS
22 #include "mlir/Dialect/Transform/Transforms/Passes.h.inc"
23 } // namespace transform
24 } // namespace mlir
25 
26 static LogicalResult inferSideEffectAnnotations(Operation *op) {
27  if (!isa<transform::TransformOpInterface>(op))
28  return success();
29 
30  auto func = dyn_cast<FunctionOpInterface>(op);
31  if (!func || func.isExternal())
32  return success();
33 
34  if (!func.getFunctionBody().hasOneBlock()) {
35  return op->emitError()
36  << "only single-block operations are currently supported";
37  }
38 
39  // Note that there can't be an inclusion of an unannotated symbol because it
40  // wouldn't have passed the verifier, so recursion isn't necessary here.
41  llvm::SmallDenseSet<unsigned> consumedArguments;
42  transform::getConsumedBlockArguments(func.getFunctionBody().front(),
43  consumedArguments);
44 
45  for (unsigned i = 0, e = func.getNumArguments(); i < e; ++i) {
46  func.setArgAttr(i,
47  consumedArguments.contains(i)
48  ? transform::TransformDialect::kArgConsumedAttrName
49  : transform::TransformDialect::kArgReadOnlyAttrName,
50  UnitAttr::get(op->getContext()));
51  }
52  return success();
53 }
54 
55 namespace {
56 class InferEffectsPass
57  : public transform::impl::InferEffectsPassBase<InferEffectsPass> {
58 public:
59  void runOnOperation() override {
60  WalkResult result = getOperation()->walk([](Operation *op) {
62  : WalkResult::advance();
63  });
64  if (result.wasInterrupted())
65  return signalPassFailure();
66  }
67 };
68 } // 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:267
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: WalkResult.h:29
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition: WalkResult.h:51
static WalkResult interrupt()
Definition: WalkResult.h:46
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
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.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...