14 #include "llvm/ADT/APSInt.h"
21 if (
auto attr = llvm::dyn_cast_if_present<Attribute>(v)) {
22 IntegerAttr intAttr = dyn_cast<IntegerAttr>(attr);
23 return intAttr && intAttr.getValue().isZero();
26 return cst.value() == 0;
30 std::tuple<SmallVector<OpFoldResult>, SmallVector<OpFoldResult>,
31 SmallVector<OpFoldResult>>
34 offsets.reserve(ranges.size());
35 sizes.reserve(ranges.size());
36 strides.reserve(ranges.size());
37 for (
const auto &[offset, size, stride] : ranges) {
38 offsets.push_back(offset);
39 sizes.push_back(size);
40 strides.push_back(stride);
42 return std::make_tuple(offsets, sizes, strides);
54 auto v = llvm::dyn_cast_if_present<Value>(ofr);
56 APInt apInt = cast<IntegerAttr>(ofr.get<
Attribute>()).getValue();
57 staticVec.push_back(apInt.getSExtValue());
60 dynamicVec.push_back(v);
61 staticVec.push_back(ShapedType::kDynamic);
85 return llvm::to_vector(
92 res.reserve(arrayAttr.size());
104 return llvm::to_vector(llvm::map_range(
111 if (
auto val = llvm::dyn_cast_if_present<Value>(ofr)) {
114 return intVal.getSExtValue();
118 Attribute attr = llvm::dyn_cast_if_present<Attribute>(ofr);
119 if (
auto intAttr = dyn_cast_or_null<IntegerAttr>(attr))
120 return intAttr.getValue().getSExtValue();
124 std::optional<SmallVector<int64_t>>
131 return cv.has_value() ? cv.value() : 0;
141 return val && *val == value;
150 if (cst1 && cst2 && *cst1 == *cst2)
152 auto v1 = llvm::dyn_cast_if_present<Value>(ofr1),
153 v2 = llvm::dyn_cast_if_present<Value>(ofr2);
154 return v1 && v1 == v2;
159 if (ofrs1.size() != ofrs2.size())
161 for (
auto [ofr1, ofr2] : llvm::zip_equal(ofrs1, ofrs2))
173 res.reserve(staticValues.size());
174 unsigned numDynamic = 0;
175 unsigned count =
static_cast<unsigned>(staticValues.size());
176 for (
unsigned idx = 0; idx < count; ++idx) {
177 int64_t value = staticValues[idx];
178 res.push_back(ShapedType::isDynamic(value)
187 std::pair<ArrayAttr, SmallVector<Value>>
192 for (
const auto &it : mixedValues) {
194 staticValues.push_back(cast<IntegerAttr>(it.get<
Attribute>()).getInt());
196 staticValues.push_back(ShapedType::kDynamic);
197 dynamicValues.push_back(it.get<
Value>());
204 template <
typename K,
typename V>
205 static SmallVector<V>
210 assert(keys.size() == values.size() &&
"unexpected mismatching sizes");
211 auto indices = llvm::to_vector(llvm::seq<int64_t>(0, values.size()));
212 std::sort(indices.begin(), indices.end(),
213 [&](int64_t i, int64_t
j) { return compare(keys[i], keys[j]); });
215 res.reserve(values.size());
216 for (int64_t i = 0, e = indices.size(); i < e; ++i)
217 res.push_back(values[indices[i]]);
227 SmallVector<OpFoldResult>
256 return mlir::ceilDiv(*ubConstant - *lbConstant, *stepConstant);
260 bool valuesChanged =
false;
267 valuesChanged =
true;
Attributes are known-constant values of operations.
This class is a general helper class for creating context-global objects like types,...
IntegerAttr getI64IntegerAttr(int64_t value)
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents a single result from folding an operation.
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Specialization of arith.constant op that returns an integer of index type.
int compare(const Fraction &x, const Fraction &y)
Three-way comparison between two fractions.
This header declares functions that assist transformations in the MemRef dialect.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
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.
detail::constant_int_value_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
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.
int64_t ceilDiv(int64_t lhs, int64_t rhs)
Returns the result of MLIR's ceildiv operation on constants.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
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.
static SmallVector< V > getValuesSortedByKeyImpl(ArrayRef< K > keys, ArrayRef< V > values, llvm::function_ref< bool(K, K)> compare)
Helper to sort values according to matching keys.
bool isEqualConstantIntOrValueArray(ArrayRef< OpFoldResult > ofrs1, ArrayRef< OpFoldResult > ofrs2)
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
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.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
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.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.