MLIR: lib/Interfaces/InferIntRangeInterface.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
12#include "mlir/Interfaces/InferIntRangeInterface.cpp.inc"
13#include
14
15using namespace mlir;
16
18 return umin().getBitWidth() == other.umin().getBitWidth() &&
21}
22
24
26
28
30
34 return IndexType::kInternalStorageBitWidth;
35 if (auto integerType = dyn_cast(type))
36 return integerType.getWidth();
37
38 return 0;
39}
40
42 return fromUnsigned(APInt::getZero(bitwidth), APInt::getMaxValue(bitwidth));
43}
44
46 return {value, value, value, value};
47}
48
50 bool isSigned) {
51 if (isSigned)
54}
55
57 const APInt &smax) {
58 unsigned int width = smin.getBitWidth();
60 if (smin.isNonNegative() == smax.isNonNegative()) {
63 } else {
64 umin = APInt::getMinValue(width);
65 umax = APInt::getMaxValue(width);
66 }
68}
69
71 const APInt &umax) {
72 unsigned int width = umin.getBitWidth();
74 if (umin.isNonNegative() == umax.isNonNegative()) {
77 } else {
78 smin = APInt::getSignedMinValue(width);
79 smax = APInt::getSignedMaxValue(width);
80 }
82}
83
86
87
89 return *this;
90 if (other.umin().getBitWidth() == 0)
91 return other;
92
93 const APInt &uminUnion = umin().ult(other.umin()) ? umin() : other.umin();
94 const APInt &umaxUnion = umax().ugt(other.umax()) ? umax() : other.umax();
95 const APInt &sminUnion = smin().slt(other.smin()) ? smin() : other.smin();
96 const APInt &smaxUnion = smax().sgt(other.smax()) ? smax() : other.smax();
97
98 return {uminUnion, umaxUnion, sminUnion, smaxUnion};
99}
100
103
104
106 return *this;
107 if (other.umin().getBitWidth() == 0)
108 return other;
109
110 const APInt &uminIntersect = umin().ugt(other.umin()) ? umin() : other.umin();
111 const APInt &umaxIntersect = umax().ult(other.umax()) ? umax() : other.umax();
112 const APInt &sminIntersect = smin().sgt(other.smin()) ? smin() : other.smin();
113 const APInt &smaxIntersect = smax().slt(other.smax()) ? smax() : other.smax();
114
115 return {uminIntersect, umaxIntersect, sminIntersect, smaxIntersect};
116}
117
119
121 return umin();
123 return smin();
124 return std::nullopt;
125}
126
128 os << "unsigned : [";
129 range.umin().print(os, false);
130 os << ", ";
131 range.umax().print(os, false);
132 return os << "] signed : [" << range.smin() << ", " << range.smax() << "]";
133}
134
137 APInt umin = APInt::getMinValue(width);
138 APInt umax = APInt::getMaxValue(width);
139 APInt smin = width != 0 ? APInt::getSignedMinValue(width) : umin;
140 APInt smax = width != 0 ? APInt::getSignedMaxValue(width) : umax;
142}
143
148
151 GetIntRangeFn getIntRange, int32_t indexBitwidth) {
153 ranges.reserve(values.size());
155 if (auto value = dyn_cast(ofr)) {
156 ranges.push_back(getIntRange(value));
157 continue;
158 }
159
160
161 auto attr = cast(cast(ofr));
163 attr.getValue().sextOrTrunc(indexBitwidth)));
164 }
165 return ranges;
166}
167
172 unpacked.reserve(argRanges.size());
173
175 if (range.isUninitialized())
176 return;
177 unpacked.push_back(range.getValue());
178 }
179
180 interface.inferResultRanges(
181 unpacked,
184 });
185}
186
190 auto ranges = llvm::to_vector_of(argRanges);
191 interface.inferResultRangesFromOptional(
192 ranges,
194 if (!argRanges.isUninitialized())
195 setResultRanges(value, argRanges.getValue());
196 });
197}
static unsigned getBitWidth(Type type)
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
A set of arbitrary-precision integers representing bounds on a given integer value.
static ConstantIntRanges maxRange(unsigned bitwidth)
Create a ConstantIntRanges with the maximum bounds for the width bitwidth, that is - [0,...
Definition InferIntRangeInterface.cpp:41
ConstantIntRanges(const APInt &umin, const APInt &umax, const APInt &smin, const APInt &smax)
Bound umin <= (unsigned)x <= umax and smin <= signed(x) <= smax.
const APInt & smax() const
The maximum value of an integer when it is interpreted as signed.
Definition InferIntRangeInterface.cpp:29
static ConstantIntRanges constant(const APInt &value)
Create a ConstantIntRanges with a constant value - that is, with the bounds [value,...
Definition InferIntRangeInterface.cpp:45
static ConstantIntRanges fromUnsigned(const APInt &umin, const APInt &umax)
Create an ConstantIntRanges with the unsigned minimum and maximum equal to umin and umax and the sign...
Definition InferIntRangeInterface.cpp:70
const APInt & smin() const
The minimum value of an integer when it is interpreted as signed.
Definition InferIntRangeInterface.cpp:27
static ConstantIntRanges range(const APInt &min, const APInt &max, bool isSigned)
Create a ConstantIntRanges whose minimum is min and maximum is max with isSigned specifying if the mi...
Definition InferIntRangeInterface.cpp:49
ConstantIntRanges intersection(const ConstantIntRanges &other) const
Returns the intersection (computed separately for signed and unsigned bounds) of this range and other...
Definition InferIntRangeInterface.cpp:102
static ConstantIntRanges fromSigned(const APInt &smin, const APInt &smax)
Create an ConstantIntRanges with the signed minimum and maximum equal to smin and smax,...
Definition InferIntRangeInterface.cpp:56
static unsigned getStorageBitwidth(Type type)
Return the bitwidth that should be used for integer ranges describing type.
Definition InferIntRangeInterface.cpp:31
std::optional< APInt > getConstantValue() const
If either the signed or unsigned interpretations of the range indicate that the value it bounds is a ...
Definition InferIntRangeInterface.cpp:118
const APInt & umax() const
The maximum value of an integer when it is interpreted as unsigned.
Definition InferIntRangeInterface.cpp:25
const APInt & umin() const
The minimum value of an integer when it is interpreted as unsigned.
Definition InferIntRangeInterface.cpp:23
ConstantIntRanges rangeUnion(const ConstantIntRanges &other) const
Returns the union (computed separately for signed and unsigned bounds) of this range and other.
Definition InferIntRangeInterface.cpp:85
bool operator==(const ConstantIntRanges &other) const
Definition InferIntRangeInterface.cpp:17
This lattice value represents the integer range of an SSA value.
IntegerValueRange(ConstantIntRanges value)
Create an integer value range lattice value.
void print(raw_ostream &os) const
Print the integer value range.
static IntegerValueRange getMaxRange(Value value)
Create a maximal range ([0, uint_max(t)] / [int_min(t), int_max(t)]) range that is used to mark the v...
Definition InferIntRangeInterface.cpp:135
This class represents a single result from folding an operation.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
void defaultInferResultRanges(InferIntRangeInterface interface, ArrayRef< IntegerValueRange > argRanges, SetIntLatticeFn setResultRanges)
Default implementation of inferResultRanges which dispatches to the inferResultRangesFromOptional.
Definition InferIntRangeInterface.cpp:168
void defaultInferResultRangesFromOptional(InferIntRangeInterface interface, ArrayRef< ConstantIntRanges > argRanges, SetIntRangeFn setResultRanges)
Default implementation of inferResultRangesFromOptional which dispatches to the inferResultRanges.
Definition InferIntRangeInterface.cpp:187
Include the generated interface declarations.
llvm::function_ref< void(Value, const ConstantIntRanges &)> SetIntRangeFn
The type of the setResultRanges callback provided to ops implementing InferIntRangeInterface.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
llvm::function_ref< void(Value, const IntegerValueRange &)> SetIntLatticeFn
Similar to SetIntRangeFn, but operating on IntegerValueRange lattice values.
SmallVector< IntegerValueRange > getIntValueRanges(ArrayRef< OpFoldResult > values, GetIntRangeFn getIntRange, int32_t indexBitwidth)
Helper function to collect the integer range values of an array of op fold results.
Definition InferIntRangeInterface.cpp:150
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
function_ref< IntegerValueRange(Value)> GetIntRangeFn
Helper callback type to get the integer range of a value.