MLIR 24.0.0git
AffineOps.h
Go to the documentation of this file.
1//===- AffineOps.h - MLIR Affine Operations -------------------------------===//
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 defines convenience types for working with Affine operations
10// in the MLIR operation set.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_DIALECT_AFFINE_IR_AFFINEOPS_H
15#define MLIR_DIALECT_AFFINE_IR_AFFINEOPS_H
16
21#include "mlir/IR/AffineMap.h"
22#include "mlir/IR/Builders.h"
31
32namespace mlir {
33namespace affine {
34
35class AffineApplyOp;
36class AffineBound;
37class AffineMaxOp;
38class AffineMinOp;
39class AffineValueMap;
40
41/// A utility function to check if a value is defined at the top level of an
42/// op with trait `AffineScope` or is a region argument for such an op. A value
43/// of index type defined at the top level is always a valid symbol for all its
44/// uses.
45bool isTopLevelValue(Value value);
46
47/// A utility function to check if a value is defined at the top level of
48/// `region` or is an argument of `region`. A value of index type defined at the
49/// top level of a `AffineScope` region is always a valid symbol for all
50/// uses in that region.
51bool isTopLevelValue(Value value, Region *region);
52
53/// Returns the closest region enclosing `op` that is held by an operation with
54/// trait `AffineScope`; `nullptr` if there is no such region.
55Region *getAffineScope(Operation *op);
56
57/// Returns the closest region enclosing `op` that is held by a non-affine
58/// operation; `nullptr` if there is no such region. This method is meant to
59/// be used by affine analysis methods (e.g. dependence analysis) which are
60/// only meaningful when performed among/between operations from the same
61/// analysis scope.
62Region *getAffineAnalysisScope(Operation *op);
63
64/// Return the product of `terms`, creating an `affine.apply` if any of them are
65/// non-constant values. If any of `terms` is `nullptr`, return `nullptr`.
68
69/// Returns true if the given Value can be used as a dimension id in the region
70/// of the closest surrounding op that has the trait `AffineScope`.
71bool isValidDim(Value value);
72
73/// Returns true if the given Value can be used as a dimension id in `region`,
74/// i.e., for all its uses in `region`.
75bool isValidDim(Value value, Region *region);
76
77/// Returns true if the given value can be used as a symbol in the region of the
78/// closest surrounding op that has the trait `AffineScope`.
79bool isValidSymbol(Value value);
80
81/// Returns true if the given Value can be used as a symbol for `region`, i.e.,
82/// for all its uses in `region`.
83bool isValidSymbol(Value value, Region *region);
84
85/// Parses dimension and symbol list. `numDims` is set to the number of
86/// dimensions in the list parsed.
87ParseResult parseDimAndSymbolList(OpAsmParser &parser,
88 SmallVectorImpl<Value> &operands,
89 unsigned &numDims);
90
91/// Modifies both `map` and `operands` in-place so as to:
92/// 1. drop duplicate operands
93/// 2. drop unused dims and symbols from map
94/// 3. promote valid symbols to symbolic operands in case they appeared as
95/// dimensional operands
96/// 4. propagate constant operands and drop them
98 SmallVectorImpl<Value> *operands);
99
100/// Canonicalizes an integer set the same way canonicalizeMapAndOperands does
101/// for affine maps.
103 SmallVectorImpl<Value> *operands);
104
105/// Returns a composed AffineApplyOp by composing `map` and `operands` with
106/// other AffineApplyOps supplying those operands. The operands of the resulting
107/// AffineApplyOp do not change the length of AffineApplyOp chains.
108AffineApplyOp makeComposedAffineApply(OpBuilder &b, Location loc, AffineMap map,
109 ArrayRef<OpFoldResult> operands,
110 bool composeAffineMin = false);
112 ArrayRef<OpFoldResult> operands,
113 bool composeAffineMin = false);
114
115/// Constructs an AffineApplyOp that applies `map` to `operands` after composing
116/// the map with the maps of any other AffineApplyOp supplying the operands,
117/// then immediately attempts to fold it. If folding results in a constant
118/// value, no ops are actually created. The `map` must be a single-result affine
119/// map.
121 AffineMap map,
122 ArrayRef<OpFoldResult> operands,
123 bool composeAffineMin = false);
124/// Variant of `makeComposedFoldedAffineApply` that applies to an expression.
126 AffineExpr expr,
127 ArrayRef<OpFoldResult> operands,
128 bool composeAffineMin = false);
129/// Variant of `makeComposedFoldedAffineApply` suitable for multi-result maps.
130/// Note that this may create as many affine.apply operations as the map has
131/// results given that affine.apply must be single-result.
134 bool composeAffineMin = false);
135
136/// Returns an AffineMinOp obtained by composing `map` and `operands` with
137/// AffineApplyOps supplying those operands.
138AffineMinOp makeComposedAffineMin(OpBuilder &b, Location loc, AffineMap map,
139 ArrayRef<OpFoldResult> operands);
140
141/// Constructs an AffineMinOp that computes a minimum across the results of
142/// applying `map` to `operands`, then immediately attempts to fold it. If
143/// folding results in a constant value, no ops are actually created.
145 AffineMap map,
146 ArrayRef<OpFoldResult> operands);
147
148/// Constructs an AffineMinOp that computes a maximum across the results of
149/// applying `map` to `operands`, then immediately attempts to fold it. If
150/// folding results in a constant value, no ops are actually created.
152 AffineMap map,
153 ArrayRef<OpFoldResult> operands);
154
155/// Given an affine map `map` and its input `operands`, this method composes
156/// into `map`, maps of AffineApplyOps whose results are the values in
157/// `operands`, iteratively until no more of `operands` are the result of an
158/// AffineApplyOp. When this function returns, `map` becomes the composed affine
159/// map, and each Value in `operands` is guaranteed to be either a loop IV or a
160/// terminal symbol, i.e., a symbol defined at the top level or a block/function
161/// argument.
163 SmallVectorImpl<Value> *operands,
164 bool composeAffineMin = false);
165
166} // namespace affine
167} // namespace mlir
168
169#define GET_OP_CLASSES
170#include "mlir/Dialect/Affine/IR/AffineOps.h.inc"
171
172namespace mlir {
173namespace affine {
174
175/// Returns true if the provided value is the induction variable of an
176/// AffineForOp.
177bool isAffineForInductionVar(Value val);
178
179/// Returns true if `val` is the induction variable of an AffineParallelOp.
180bool isAffineParallelInductionVar(Value val);
181
182/// Returns true if the provided value is the induction variable of an
183/// AffineForOp or AffineParallelOp.
184bool isAffineInductionVar(Value val);
185
186/// Returns the loop parent of an induction variable. If the provided value is
187/// not an induction variable, then return nullptr.
188AffineForOp getForInductionVarOwner(Value val);
189
190/// Returns true if the provided value is among the induction variables of an
191/// AffineParallelOp.
192AffineParallelOp getAffineParallelInductionVarOwner(Value val);
193
194/// Extracts the induction variables from a list of AffineForOps and places them
195/// in the output argument `ivs`.
196void extractForInductionVars(ArrayRef<AffineForOp> forInsts,
197 SmallVectorImpl<Value> *ivs);
198
199/// Extracts the induction variables from a list of either AffineForOp or
200/// AffineParallelOp and places them in the output argument `ivs`.
201void extractInductionVars(ArrayRef<Operation *> affineOps,
202 SmallVectorImpl<Value> &ivs);
203
204/// Builds a perfect nest of affine.for loops, i.e., each loop except the
205/// innermost one contains only another loop and a terminator. The loops iterate
206/// from "lbs" to "ubs" with "steps". The body of the innermost loop is
207/// populated by calling "bodyBuilderFn" and providing it with an OpBuilder, a
208/// Location and a list of loop induction variables.
209void buildAffineLoopNest(OpBuilder &builder, Location loc,
210 ArrayRef<int64_t> lbs, ArrayRef<int64_t> ubs,
211 ArrayRef<int64_t> steps,
212 function_ref<void(OpBuilder &, Location, ValueRange)>
213 bodyBuilderFn = nullptr);
214void buildAffineLoopNest(OpBuilder &builder, Location loc, ValueRange lbs,
215 ValueRange ubs, ArrayRef<int64_t> steps,
216 function_ref<void(OpBuilder &, Location, ValueRange)>
217 bodyBuilderFn = nullptr);
218
219/// AffineBound represents a lower or upper bound in the for operation.
220/// This class does not own the underlying operands. Instead, it refers
221/// to the operands stored in the AffineForOp. Its life span should not exceed
222/// that of the for operation it refers to.
223class AffineBound {
224public:
226 AffineMap getMap() { return map; }
227
228 unsigned getNumOperands() { return operands.size(); }
229 Value getOperand(unsigned idx) {
230 return op.getOperand(operands.getBeginOperandIndex() + idx);
231 }
232
233 using operand_iterator = AffineForOp::operand_iterator;
234 using operand_range = AffineForOp::operand_range;
235
236 operand_iterator operandBegin() { return operands.begin(); }
237 operand_iterator operandEnd() { return operands.end(); }
239
240private:
241 // 'affine.for' operation that contains this bound.
242 AffineForOp op;
243 // Operands of the affine map.
244 OperandRange operands;
245 // Affine map for this bound.
246 AffineMap map;
247
248 AffineBound(AffineForOp op, OperandRange operands, AffineMap map)
249 : op(op), operands(operands), map(map) {}
250
251 friend class AffineForOp;
252};
253
254} // namespace affine
255} // namespace mlir
256
257#endif
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
Base type for affine expression.
Definition AffineExpr.h:68
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition AffineMap.h:46
An integer set representing a conjunction of one or more affine equalities and inequalities.
Definition IntegerSet.h:44
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
This class helps build Operations.
Definition Builders.h:210
This class represents a single result from folding an operation.
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:44
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 represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
AffineBound represents a lower or upper bound in the for operation.
Definition AffineOps.h:223
Value getOperand(unsigned idx)
Definition AffineOps.h:229
operand_iterator operandBegin()
Definition AffineOps.h:236
AffineForOp::operand_iterator operand_iterator
Definition AffineOps.h:233
AffineForOp::operand_range operand_range
Definition AffineOps.h:234
AffineForOp getAffineForOp()
Definition AffineOps.h:225
operand_range getOperands()
Definition AffineOps.h:238
operand_iterator operandEnd()
Definition AffineOps.h:237
An AffineValueMap is an affine map plus its ML value operands and results for analysis purposes.
void buildAffineLoopNest(OpBuilder &builder, Location loc, ArrayRef< int64_t > lbs, ArrayRef< int64_t > ubs, ArrayRef< int64_t > steps, function_ref< void(OpBuilder &, Location, ValueRange)> bodyBuilderFn=nullptr)
Builds a perfect nest of affine.for loops, i.e., each loop except the innermost one contains only ano...
AffineApplyOp makeComposedAffineApply(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands, bool composeAffineMin=false)
Returns a composed AffineApplyOp by composing map and operands with other AffineApplyOps supplying th...
void extractForInductionVars(ArrayRef< AffineForOp > forInsts, SmallVectorImpl< Value > *ivs)
Extracts the induction variables from a list of AffineForOps and places them in the output argument i...
bool isValidDim(Value value)
Returns true if the given Value can be used as a dimension id in the region of the closest surroundin...
bool isAffineInductionVar(Value val)
Returns true if the provided value is the induction variable of an AffineForOp or AffineParallelOp.
SmallVector< OpFoldResult > makeComposedFoldedMultiResultAffineApply(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands, bool composeAffineMin=false)
Variant of makeComposedFoldedAffineApply suitable for multi-result maps.
OpFoldResult computeProduct(Location loc, OpBuilder &builder, ArrayRef< OpFoldResult > terms)
Return the product of terms, creating an affine.apply if any of them are non-constant values.
AffineForOp getForInductionVarOwner(Value val)
Returns the loop parent of an induction variable.
void canonicalizeMapAndOperands(AffineMap *map, SmallVectorImpl< Value > *operands)
Modifies both map and operands in-place so as to:
OpFoldResult makeComposedFoldedAffineMax(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands)
Constructs an AffineMinOp that computes a maximum across the results of applying map to operands,...
bool isAffineForInductionVar(Value val)
Returns true if the provided value is the induction variable of an AffineForOp.
OpFoldResult makeComposedFoldedAffineApply(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands, bool composeAffineMin=false)
Constructs an AffineApplyOp that applies map to operands after composing the map with the maps of any...
OpFoldResult makeComposedFoldedAffineMin(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands)
Constructs an AffineMinOp that computes a minimum across the results of applying map to operands,...
bool isTopLevelValue(Value value)
A utility function to check if a value is defined at the top level of an op with trait AffineScope or...
Region * getAffineAnalysisScope(Operation *op)
Returns the closest region enclosing op that is held by a non-affine operation; nullptr if there is n...
void fullyComposeAffineMapAndOperands(AffineMap *map, SmallVectorImpl< Value > *operands, bool composeAffineMin=false)
Given an affine map map and its input operands, this method composes into map, maps of AffineApplyOps...
void canonicalizeSetAndOperands(IntegerSet *set, SmallVectorImpl< Value > *operands)
Canonicalizes an integer set the same way canonicalizeMapAndOperands does for affine maps.
void extractInductionVars(ArrayRef< Operation * > affineOps, SmallVectorImpl< Value > &ivs)
Extracts the induction variables from a list of either AffineForOp or AffineParallelOp and places the...
bool isValidSymbol(Value value)
Returns true if the given value can be used as a symbol in the region of the closest surrounding op t...
AffineParallelOp getAffineParallelInductionVarOwner(Value val)
Returns true if the provided value is among the induction variables of an AffineParallelOp.
Region * getAffineScope(Operation *op)
Returns the closest region enclosing op that is held by an operation with trait AffineScope; nullptr ...
ParseResult parseDimAndSymbolList(OpAsmParser &parser, SmallVectorImpl< Value > &operands, unsigned &numDims)
Parses dimension and symbol list.
bool isAffineParallelInductionVar(Value val)
Returns true if val is the induction variable of an AffineParallelOp.
AffineMinOp makeComposedAffineMin(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands)
Returns an AffineMinOp obtained by composing map and operands with AffineApplyOps supplying those ope...
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:147