MLIR 24.0.0git
LoopLikeInterface.h
Go to the documentation of this file.
1//===- LoopLikeInterface.h - Loop-like operations interface ---------------===//
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// This file implements the operation interface for loop like operations.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_INTERFACES_LOOPLIKEINTERFACE_H_
14#define MLIR_INTERFACES_LOOPLIKEINTERFACE_H_
15
17
18namespace mlir {
19class RewriterBase;
20
21/// A function that returns the additional yielded values during
22/// `replaceWithAdditionalYields`. `newBbArgs` are the newly added region
23/// iter_args. This function should return as many values as there are block
24/// arguments in `newBbArgs`.
25using NewYieldValuesFn = std::function<SmallVector<Value>(
27
28namespace detail {
29/// Verify invariants of the LoopLikeOpInterface.
30LogicalResult verifyLoopLikeOpInterface(Operation *op);
31} // namespace detail
32
33//===----------------------------------------------------------------------===//
34// Traits
35//===----------------------------------------------------------------------===//
36
37namespace OpTrait {
38// A trait indicating that the single region contained in the operation may have
39// parallel execution semantics. Whether it executes in parallel may depend on
40// the operation's attributes or enclosing context. Transformations must account
41// for possible parallel execution unless they can prove sequential execution.
42// For example, hoisting a buffer allocation out of the region may cause
43// parallel executions to share a buffer that was previously private.
44// TODO: Turn this trait into an op interface.
45template <typename ConcreteType>
46class HasParallelRegion : public TraitBase<ConcreteType, HasParallelRegion> {
47public:
48 static LogicalResult verifyTrait(Operation *op) {
49 return impl::verifyOneRegion(op);
50 }
51};
52
53} // namespace OpTrait
54
55// Gathers all maximal sub-blocks of operations that do not themselves
56// include a `OpTy` (an operation could have a descendant `OpTy` though
57// in its tree). Ignores the block terminators.
58template <typename OpTy>
60 // Store iterators to the first and last op of each sub-block found.
62
63 // This is a linear time walk.
64 void walk(Operation *op) {
65 for (Region &region : op->getRegions())
66 for (Block &block : region)
67 walk(block);
68 }
69
70 void walk(Block &block) {
71 assert(!block.empty() && block.back().hasTrait<OpTrait::IsTerminator>() &&
72 "expected block to have a terminator");
73 for (Block::iterator it = block.begin(), e = std::prev(block.end());
74 it != e;) {
75 Block::iterator subBlockStart = it;
76 while (it != e && !isa<OpTy>(&*it))
77 ++it;
78 if (it != subBlockStart)
79 subBlocks.emplace_back(subBlockStart, std::prev(it));
80 // Process all for ops that appear next.
81 while (it != e && isa<OpTy>(&*it))
82 walk(&*it++);
83 }
84 }
85};
86
87} // namespace mlir
88
89//===----------------------------------------------------------------------===//
90// Interfaces
91//===----------------------------------------------------------------------===//
92
93/// Include the generated interface declarations.
94#include "mlir/Interfaces/LoopLikeInterface.h.inc"
95
96#endif // MLIR_INTERFACES_LOOPLIKEINTERFACE_H_
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
Block represents an ordered list of Operations.
Definition Block.h:34
OpListType::iterator iterator
Definition Block.h:165
bool empty()
Definition Block.h:173
Operation & back()
Definition Block.h:177
iterator end()
Definition Block.h:169
iterator begin()
Definition Block.h:168
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:210
static LogicalResult verifyTrait(Operation *op)
This class provides the API for ops that are known to be terminators.
Helper class for implementing traits.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition Operation.h:801
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Definition Operation.h:729
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
LogicalResult verifyOneRegion(Operation *op)
AttrTypeReplacer.
LogicalResult verifyLoopLikeOpInterface(Operation *op)
Verify invariants of the LoopLikeOpInterface.
Include the generated interface declarations.
std::function< SmallVector< Value >( OpBuilder &b, Location loc, ArrayRef< BlockArgument > newBbArgs)> NewYieldValuesFn
A function that returns the additional yielded values during replaceWithAdditionalYields.
void walk(Operation *op)
SmallVector< std::pair< Block::iterator, Block::iterator > > subBlocks
void walk(Block &block)