MLIR  19.0.0git
InferIntRangeCommon.h
Go to the documentation of this file.
1 //===- InferIntRangeCommon.cpp - Inference for common ops --*- 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 file declares implementations of range inference for operations that are
10 // common to both the `arith` and `index` dialects to facilitate reuse.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_INTERFACES_UTILS_INFERINTRANGECOMMON_H
15 #define MLIR_INTERFACES_UTILS_INFERINTRANGECOMMON_H
16 
18 #include "llvm/ADT/ArrayRef.h"
19 #include <optional>
20 
21 namespace mlir {
22 namespace intrange {
23 /// Function that performs inference on an array of `ConstantIntRanges`,
24 /// abstracted away here to permit writing the function that handles both
25 /// 64- and 32-bit index types.
26 using InferRangeFn =
28 
29 static constexpr unsigned indexMinWidth = 32;
30 static constexpr unsigned indexMaxWidth = 64;
31 
32 enum class CmpMode : uint32_t { Both, Signed, Unsigned };
33 
34 /// Compute `inferFn` on `ranges`, whose size should be the index storage
35 /// bitwidth. Then, compute the function on `argRanges` again after truncating
36 /// the ranges to 32 bits. Finally, if the truncation of the 64-bit result is
37 /// equal to the 32-bit result, use it (to preserve compatibility with folders
38 /// and inference precision), and take the union of the results otherwise.
39 ///
40 /// The `mode` argument specifies if the unsigned, signed, or both results of
41 /// the inference computation should be used when comparing the results.
44  CmpMode mode);
45 
46 /// Independently zero-extend the unsigned values and sign-extend the signed
47 /// values in `range` to `destWidth` bits, returning the resulting range.
48 ConstantIntRanges extRange(const ConstantIntRanges &range, unsigned destWidth);
49 
50 /// Use the unsigned values in `range` to zero-extend it to `destWidth`.
52  unsigned destWidth);
53 
54 /// Use the signed values in `range` to sign-extend it to `destWidth`.
56  unsigned destWidth);
57 
58 /// Truncate `range` to `destWidth` bits, taking care to handle cases such as
59 /// the truncation of [255, 256] to i8 not being a uniform range.
61  unsigned destWidth);
62 
64 
66 
68 
70 
72 
74 
76 
78 
80 
82 
84 
86 
88 
90 
92 
94 
96 
98 
100 
102 
103 /// Copy of the enum from `arith` and `index` to allow the common integer range
104 /// infrastructure to not depend on either dialect.
105 enum class CmpPredicate : uint64_t {
106  eq,
107  ne,
108  slt,
109  sle,
110  sgt,
111  sge,
112  ult,
113  ule,
114  ugt,
115  uge,
116 };
117 
118 /// Returns a boolean value if `pred` is statically true or false for
119 /// anypossible inputs falling within `lhs` and `rhs`, and std::nullopt if the
120 /// value of the predicate cannot be determined.
121 std::optional<bool> evaluatePred(CmpPredicate pred,
122  const ConstantIntRanges &lhs,
123  const ConstantIntRanges &rhs);
124 
125 } // namespace intrange
126 } // namespace mlir
127 
128 #endif // MLIR_INTERFACES_UTILS_INFERINTRANGECOMMON_H
A set of arbitrary-precision integers representing bounds on a given integer value.
ConstantIntRanges inferAnd(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferAdd(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferSub(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferShrS(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferIndexOp(InferRangeFn inferFn, ArrayRef< ConstantIntRanges > argRanges, CmpMode mode)
Compute inferFn on ranges, whose size should be the index storage bitwidth.
ConstantIntRanges extSIRange(const ConstantIntRanges &range, unsigned destWidth)
Use the signed values in range to sign-extend it to destWidth.
std::optional< bool > evaluatePred(CmpPredicate pred, const ConstantIntRanges &lhs, const ConstantIntRanges &rhs)
Returns a boolean value if pred is statically true or false for anypossible inputs falling within lhs...
ConstantIntRanges inferMinS(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferMaxU(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferRemS(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferMul(ArrayRef< ConstantIntRanges > argRanges)
CmpPredicate
Copy of the enum from arith and index to allow the common integer range infrastructure to not depend ...
ConstantIntRanges inferOr(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges extRange(const ConstantIntRanges &range, unsigned destWidth)
Independently zero-extend the unsigned values and sign-extend the signed values in range to destWidth...
ConstantIntRanges truncRange(const ConstantIntRanges &range, unsigned destWidth)
Truncate range to destWidth bits, taking care to handle cases such as the truncation of [255,...
ConstantIntRanges inferDivU(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferCeilDivS(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferMinU(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferXor(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferDivS(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferShrU(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferRemU(ArrayRef< ConstantIntRanges > argRanges)
static constexpr unsigned indexMinWidth
ConstantIntRanges inferFloorDivS(ArrayRef< ConstantIntRanges > argRanges)
function_ref< ConstantIntRanges(ArrayRef< ConstantIntRanges >)> InferRangeFn
Function that performs inference on an array of ConstantIntRanges, abstracted away here to permit wri...
static constexpr unsigned indexMaxWidth
ConstantIntRanges inferCeilDivU(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges extUIRange(const ConstantIntRanges &range, unsigned destWidth)
Use the unsigned values in range to zero-extend it to destWidth.
ConstantIntRanges inferShl(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferMaxS(ArrayRef< ConstantIntRanges > argRanges)
Include the generated interface declarations.