MLIR  18.0.0git
StaticValueUtils.h
Go to the documentation of this file.
1 //===- StaticValueUtils.h - Utilities for static values ---------*- C++ -*-===//
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 header file defines utilities for dealing with static values, e.g.,
10 // converting back and forth between Value and OpFoldResult. Such functionality
11 // is used in multiple dialects.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_DIALECT_UTILS_STATICVALUEUTILS_H
16 #define MLIR_DIALECT_UTILS_STATICVALUEUTILS_H
17 
19 #include "mlir/IR/OpDefinition.h"
20 #include "mlir/Support/LLVM.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/SmallVectorExtras.h"
23 
24 namespace mlir {
25 
26 /// Return true if `v` is an IntegerAttr with value `0` of a ConstantIndexOp
27 /// with attribute with value `0`.
28 bool isZeroIndex(OpFoldResult v);
29 
30 /// Represents a range (offset, size, and stride) where each element of the
31 /// triple may be dynamic or static.
32 struct Range {
36 };
37 
38 /// Given an array of Range values, return a tuple of (offset vector, sizes
39 /// vector, and strides vector) formed by separating out the individual
40 /// elements of each range.
41 std::tuple<SmallVector<OpFoldResult>, SmallVector<OpFoldResult>,
44 
45 /// Helper function to dispatch an OpFoldResult into `staticVec` if:
46 /// a) it is an IntegerAttr
47 /// In other cases, the OpFoldResult is dispached to the `dynamicVec`.
48 /// In such dynamic cases, ShapedType::kDynamic is also pushed to
49 /// `staticVec`. This is useful to extract mixed static and dynamic entries
50 /// that come from an AttrSizedOperandSegments trait.
52  SmallVectorImpl<Value> &dynamicVec,
53  SmallVectorImpl<int64_t> &staticVec);
54 
55 /// Helper function to dispatch multiple OpFoldResults according to the
56 /// behavior of `dispatchIndexOpFoldResult(OpFoldResult ofr` for a single
57 /// OpFoldResult.
59  SmallVectorImpl<Value> &dynamicVec,
60  SmallVectorImpl<int64_t> &staticVec);
61 
62 /// Extract integer values from the assumed ArrayAttr of IntegerAttr.
63 template <typename IntTy>
65  return llvm::to_vector(
66  llvm::map_range(cast<ArrayAttr>(attr), [](Attribute a) -> IntTy {
67  return cast<IntegerAttr>(a).getInt();
68  }));
69 }
70 
71 /// Given a value, try to extract a constant Attribute. If this fails, return
72 /// the original value.
73 OpFoldResult getAsOpFoldResult(Value val);
74 /// Given an array of values, try to extract a constant Attribute from each
75 /// value. If this fails, return the original value.
76 SmallVector<OpFoldResult> getAsOpFoldResult(ValueRange values);
77 /// Convert `arrayAttr` to a vector of OpFoldResult.
78 SmallVector<OpFoldResult> getAsOpFoldResult(ArrayAttr arrayAttr);
79 
80 /// Convert int64_t to integer attributes of index type and return them as
81 /// OpFoldResult.
82 OpFoldResult getAsIndexOpFoldResult(MLIRContext *ctx, int64_t val);
83 SmallVector<OpFoldResult> getAsIndexOpFoldResult(MLIRContext *ctx,
84  ArrayRef<int64_t> values);
85 
86 /// If ofr is a constant integer or an IntegerAttr, return the integer.
87 std::optional<int64_t> getConstantIntValue(OpFoldResult ofr);
88 /// If all ofrs are constant integers or IntegerAttrs, return the integers.
89 std::optional<SmallVector<int64_t>>
90 getConstantIntValues(ArrayRef<OpFoldResult> ofrs);
91 
92 /// Return true if `ofr` is constant integer equal to `value`.
93 bool isConstantIntValue(OpFoldResult ofr, int64_t value);
94 
95 /// Return true if ofr1 and ofr2 are the same integer constant attribute
96 /// values or the same SSA value. Ignore integer bitwitdh and type mismatch
97 /// that come from the fact there is no IndexAttr and that IndexType have no
98 /// bitwidth.
99 bool isEqualConstantIntOrValue(OpFoldResult ofr1, OpFoldResult ofr2);
100 bool isEqualConstantIntOrValueArray(ArrayRef<OpFoldResult> ofrs1,
101  ArrayRef<OpFoldResult> ofrs2);
102 
103 // To convert an OpFoldResult to a Value of index type, see:
104 // mlir/include/mlir/Dialect/Arith/Utils/Utils.h
105 // TODO: find a better common landing place.
106 //
107 // Value getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc,
108 // OpFoldResult ofr);
109 
110 // To convert an OpFoldResult to a Value of index type, see:
111 // mlir/include/mlir/Dialect/Arith/Utils/Utils.h
112 // TODO: find a better common landing place.
113 //
114 // SmallVector<Value>
115 // getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc,
116 // ArrayRef<OpFoldResult> valueOrAttrVec);
117 
118 /// Return a vector of OpFoldResults with the same size a staticValues, but
119 /// all elements for which ShapedType::isDynamic is true, will be replaced by
120 /// dynamicValues.
121 SmallVector<OpFoldResult> getMixedValues(ArrayRef<int64_t> staticValues,
122  ValueRange dynamicValues, Builder &b);
123 
124 /// Decompose a vector of mixed static or dynamic values into the
125 /// corresponding pair of arrays. This is the inverse function of
126 /// `getMixedValues`.
127 std::pair<ArrayAttr, SmallVector<Value>>
128 decomposeMixedValues(Builder &b,
129  const SmallVectorImpl<OpFoldResult> &mixedValues);
130 
131 /// Helper to sort `values` according to matching `keys`.
132 SmallVector<Value>
133 getValuesSortedByKey(ArrayRef<Attribute> keys, ArrayRef<Value> values,
134  llvm::function_ref<bool(Attribute, Attribute)> compare);
135 SmallVector<OpFoldResult>
136 getValuesSortedByKey(ArrayRef<Attribute> keys, ArrayRef<OpFoldResult> values,
137  llvm::function_ref<bool(Attribute, Attribute)> compare);
138 SmallVector<int64_t>
139 getValuesSortedByKey(ArrayRef<Attribute> keys, ArrayRef<int64_t> values,
140  llvm::function_ref<bool(Attribute, Attribute)> compare);
141 
142 /// Returns "success" when any of the elements in `ofrs` is a constant value. In
143 /// that case the value is replaced by an attribute. Returns "failure" when no
144 /// folding happened.
145 LogicalResult foldDynamicIndexList(SmallVectorImpl<OpFoldResult> &ofrs);
146 
147 /// Return the number of iterations for a loop with a lower bound `lb`, upper
148 /// bound `ub` and step `step`.
149 std::optional<int64_t> constantTripCount(OpFoldResult lb, OpFoldResult ub,
150  OpFoldResult step);
151 
152 } // namespace mlir
153 
154 #endif // MLIR_DIALECT_UTILS_STATICVALUEUTILS_H
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class represents a single result from folding an operation.
Definition: OpDefinition.h:266
int compare(const Fraction &x, const Fraction &y)
Three-way comparison between two fractions.
Definition: Fraction.h:63
This header declares functions that assist transformations in the MemRef dialect.
bool isConstantIntValue(OpFoldResult ofr, int64_t value)
Return true if ofr is constant integer equal to value.
bool isZeroIndex(OpFoldResult v)
Return true if v is an IntegerAttr with value 0 of a ConstantIndexOp with attribute with value 0.
OpFoldResult getAsIndexOpFoldResult(MLIRContext *ctx, int64_t val)
Convert int64_t to integer attributes of index type and return them as OpFoldResult.
std::tuple< SmallVector< OpFoldResult >, SmallVector< OpFoldResult >, SmallVector< OpFoldResult > > getOffsetsSizesAndStrides(ArrayRef< Range > ranges)
Given an array of Range values, return a tuple of (offset vector, sizes vector, and strides vector) f...
std::optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
bool isEqualConstantIntOrValue(OpFoldResult ofr1, OpFoldResult ofr2)
Return true if ofr1 and ofr2 are the same integer constant attribute values or the same SSA value.
SmallVector< IntTy > extractFromIntegerArrayAttr(Attribute attr)
Extract integer values from the assumed ArrayAttr of IntegerAttr.
std::optional< int64_t > constantTripCount(OpFoldResult lb, OpFoldResult ub, OpFoldResult step)
Return the number of iterations for a loop with a lower bound lb, upper bound ub and step step.
std::pair< ArrayAttr, SmallVector< Value > > decomposeMixedValues(Builder &b, const SmallVectorImpl< OpFoldResult > &mixedValues)
Decompose a vector of mixed static or dynamic values into the corresponding pair of arrays.
void dispatchIndexOpFoldResults(ArrayRef< OpFoldResult > ofrs, SmallVectorImpl< Value > &dynamicVec, SmallVectorImpl< int64_t > &staticVec)
Helper function to dispatch multiple OpFoldResults according to the behavior of dispatchIndexOpFoldRe...
LogicalResult foldDynamicIndexList(SmallVectorImpl< OpFoldResult > &ofrs)
Returns "success" when any of the elements in ofrs is a constant value.
bool isEqualConstantIntOrValueArray(ArrayRef< OpFoldResult > ofrs1, ArrayRef< OpFoldResult > ofrs2)
SmallVector< OpFoldResult > getMixedValues(ArrayRef< int64_t > staticValues, ValueRange dynamicValues, Builder &b)
Return a vector of OpFoldResults with the same size a staticValues, but all elements for which Shaped...
void dispatchIndexOpFoldResult(OpFoldResult ofr, SmallVectorImpl< Value > &dynamicVec, SmallVectorImpl< int64_t > &staticVec)
Helper function to dispatch an OpFoldResult into staticVec if: a) it is an IntegerAttr In other cases...
OpFoldResult getAsOpFoldResult(Value val)
Given a value, try to extract a constant Attribute.
std::optional< SmallVector< int64_t > > getConstantIntValues(ArrayRef< OpFoldResult > ofrs)
If all ofrs are constant integers or IntegerAttrs, return the integers.
SmallVector< Value > getValuesSortedByKey(ArrayRef< Attribute > keys, ArrayRef< Value > values, llvm::function_ref< bool(Attribute, Attribute)> compare)
Helper to sort values according to matching keys.
Represents a range (offset, size, and stride) where each element of the triple may be dynamic or stat...
OpFoldResult stride
OpFoldResult size
OpFoldResult offset