MLIR  16.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 
18 #include "mlir/IR/OpDefinition.h"
19 #include "mlir/Support/LLVM.h"
20 #include "llvm/ADT/SmallVector.h"
21 
22 namespace mlir {
23 
24 /// Represents a range (offset, size, and stride) where each element of the
25 /// triple may be dynamic or static.
26 struct Range {
30 };
31 
32 /// Given an array of Range values, return a tuple of (offset vector, sizes
33 /// vector, and strides vector) formed by separating out the individual elements
34 /// of each range.
35 std::tuple<SmallVector<OpFoldResult>, SmallVector<OpFoldResult>,
38 
39 /// Helper function to dispatch an OpFoldResult into `staticVec` if:
40 /// a) it is an IntegerAttr
41 /// In other cases, the OpFoldResult is dispached to the `dynamicVec`.
42 /// In such dynamic cases, a copy of the `sentinel` value is also pushed to
43 /// `staticVec`. This is useful to extract mixed static and dynamic entries that
44 /// come from an AttrSizedOperandSegments trait.
46  SmallVectorImpl<Value> &dynamicVec,
47  SmallVectorImpl<int64_t> &staticVec,
48  int64_t sentinel);
49 
50 /// Helper function to dispatch multiple OpFoldResults according to the behavior
51 /// of `dispatchIndexOpFoldResult(OpFoldResult ofr` for a single OpFoldResult.
53  SmallVectorImpl<Value> &dynamicVec,
54  SmallVectorImpl<int64_t> &staticVec,
55  int64_t sentinel);
56 
57 /// Extract int64_t values from the assumed ArrayAttr of IntegerAttr.
59 
60 /// Given a value, try to extract a constant Attribute. If this fails, return
61 /// the original value.
63 
64 /// Given an array of values, try to extract a constant Attribute from each
65 /// value. If this fails, return the original value.
67 
68 /// Convert `arrayAttr` to a vector of OpFoldResult.
69 SmallVector<OpFoldResult> getAsOpFoldResult(ArrayAttr arrayAttr);
70 
71 /// If ofr is a constant integer or an IntegerAttr, return the integer.
73 
74 /// Return true if `ofr` is constant integer equal to `value`.
75 bool isConstantIntValue(OpFoldResult ofr, int64_t value);
76 
77 /// Return true if ofr1 and ofr2 are the same integer constant attribute values
78 /// or the same SSA value.
79 /// Ignore integer bitwitdh and type mismatch that come from the fact there is
80 /// no IndexAttr and that IndexType have no bitwidth.
82 
83 /// Helper function to convert a vector of `OpFoldResult`s into a vector of
84 /// `Value`s. For each `OpFoldResult` in `valueOrAttrVec` return the fold result
85 /// if it casts to a `Value` or create an index-type constant if it casts to
86 /// `IntegerAttr`. No other attribute types are supported.
88  ArrayRef<OpFoldResult> valueOrAttrVec);
89 
90 /// Return a vector of OpFoldResults with the same size a staticValues, but all
91 /// elements for which ShapedType::isDynamic is true, will be replaced by
92 /// dynamicValues.
94  ValueRange dynamicValues, Builder &b);
95 
96 /// Decompose a vector of mixed static or dynamic values into the corresponding
97 /// pair of arrays. This is the inverse function of `getMixedValues`.
98 std::pair<ArrayAttr, SmallVector<Value>>
100  const SmallVectorImpl<OpFoldResult> &mixedValues);
101 
102 } // namespace mlir
103 
104 #endif // MLIR_DIALECT_UTILS_STATICVALUEUTILS_H
static constexpr const bool value
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:49
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:64
This class helps build Operations.
Definition: Builders.h:198
This class represents a single result from folding an operation.
Definition: OpDefinition.h:233
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:349
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:85
Include the generated interface declarations.
bool isConstantIntValue(OpFoldResult ofr, int64_t value)
Return true if ofr is constant integer equal to value.
SmallVector< Value > getAsValues(OpBuilder &b, Location loc, ArrayRef< OpFoldResult > valueOrAttrVec)
Helper function to convert a vector of OpFoldResults into a vector of Values.
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...
void dispatchIndexOpFoldResult(OpFoldResult ofr, SmallVectorImpl< Value > &dynamicVec, SmallVectorImpl< int64_t > &staticVec, int64_t sentinel)
Helper function to dispatch an OpFoldResult into staticVec if: a) it is an IntegerAttr In other cases...
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< int64_t, 4 > extractFromI64ArrayAttr(Attribute attr)
Extract int64_t values from the assumed ArrayAttr of IntegerAttr.
void dispatchIndexOpFoldResults(ArrayRef< OpFoldResult > ofrs, SmallVectorImpl< Value > &dynamicVec, SmallVectorImpl< int64_t > &staticVec, int64_t sentinel)
Helper function to dispatch multiple OpFoldResults according to the behavior of dispatchIndexOpFoldRe...
Optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
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.
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...
OpFoldResult getAsOpFoldResult(Value val)
Given a value, try to extract a constant Attribute.
Represents a range (offset, size, and stride) where each element of the triple may be dynamic or stat...
OpFoldResult stride
OpFoldResult size
OpFoldResult offset