MLIR 22.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 "llvm/ADT/BitmaskEnum.h"
20#include <optional>
21
22namespace mlir {
23class AffineExpr;
24class ShapedDimOpInterface;
25
26namespace intrange {
27/// Function that performs inference on an array of `ConstantIntRanges`,
28/// abstracted away here to permit writing the function that handles both
29/// 64- and 32-bit index types.
32
33/// Function that performs inferrence on an array of `IntegerValueRange`.
36
37static constexpr unsigned indexMinWidth = 32;
38static constexpr unsigned indexMaxWidth = 64;
39
40enum class CmpMode : uint32_t { Both, Signed, Unsigned };
41
42enum class OverflowFlags : uint32_t {
43 None = 0,
44 Nsw = 1,
45 Nuw = 2,
46 LLVM_MARK_AS_BITMASK_ENUM(Nuw)
47};
48
49/// Function that performs inference on an array of `ConstantIntRanges` while
50/// taking special overflow behavior into account.
53
54/// Compute `inferFn` on `ranges`, whose size should be the index storage
55/// bitwidth. Then, compute the function on `argRanges` again after truncating
56/// the ranges to 32 bits. Finally, if the truncation of the 64-bit result is
57/// equal to the 32-bit result, use it (to preserve compatibility with folders
58/// and inference precision), and take the union of the results otherwise.
59///
60/// The `mode` argument specifies if the unsigned, signed, or both results of
61/// the inference computation should be used when comparing the results.
64 CmpMode mode);
65
66/// Independently zero-extend the unsigned values and sign-extend the signed
67/// values in `range` to `destWidth` bits, returning the resulting range.
68ConstantIntRanges extRange(const ConstantIntRanges &range, unsigned destWidth);
69
70/// Use the unsigned values in `range` to zero-extend it to `destWidth`.
72 unsigned destWidth);
73
74/// Use the signed values in `range` to sign-extend it to `destWidth`.
76 unsigned destWidth);
77
78/// Truncate `range` to `destWidth` bits, taking care to handle cases such as
79/// the truncation of [255, 256] to i8 not being a uniform range.
81 unsigned destWidth);
82
85
88
91
93
95
97
99
101
103
105
107
109
111
113
115
117
119
122
124
126
127/// Copy of the enum from `arith` and `index` to allow the common integer range
128/// infrastructure to not depend on either dialect.
141
142/// Returns a boolean value if `pred` is statically true or false for
143/// anypossible inputs falling within `lhs` and `rhs`, and std::nullopt if the
144/// value of the predicate cannot be determined.
145std::optional<bool> evaluatePred(CmpPredicate pred,
146 const ConstantIntRanges &lhs,
147 const ConstantIntRanges &rhs);
148
149/// Returns the integer range for the result of a `ShapedDimOpInterface` given
150/// the optional inferred ranges for the `dimension` index `maybeDim`. When a
151/// dynamic dimension is encountered, returns [0, signed_max(type(result))].
152ConstantIntRanges inferShapedDimOpInterface(ShapedDimOpInterface op,
153 const IntegerValueRange &maybeDim);
154
155/// Infer the integer range for an affine expression given ranges for its
156/// dimensions and symbols.
159 ArrayRef<ConstantIntRanges> symbolRanges);
160
161} // namespace intrange
162} // namespace mlir
163
164#endif // MLIR_INTERFACES_UTILS_INFERINTRANGECOMMON_H
lhs
Base type for affine expression.
Definition AffineExpr.h:68
A set of arbitrary-precision integers representing bounds on a given integer value.
This lattice value represents the integer range of an SSA value.
ConstantIntRanges inferAnd(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferShl(ArrayRef< ConstantIntRanges > argRanges, OverflowFlags ovfFlags=OverflowFlags::None)
ConstantIntRanges inferIndexOp(const InferRangeFn &inferFn, ArrayRef< ConstantIntRanges > argRanges, CmpMode mode)
Compute inferFn on ranges, whose size should be the index storage bitwidth.
ConstantIntRanges inferShrS(ArrayRef< ConstantIntRanges > argRanges)
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 inferAffineExpr(AffineExpr expr, ArrayRef< ConstantIntRanges > dimRanges, ArrayRef< ConstantIntRanges > symbolRanges)
Infer the integer range for an affine expression given ranges for its dimensions and symbols.
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 inferSub(ArrayRef< ConstantIntRanges > argRanges, OverflowFlags ovfFlags=OverflowFlags::None)
std::function< ConstantIntRanges(ArrayRef< ConstantIntRanges >)> InferRangeFn
Function that performs inference on an array of ConstantIntRanges, abstracted away here to permit wri...
ConstantIntRanges inferAdd(ArrayRef< ConstantIntRanges > argRanges, OverflowFlags ovfFlags=OverflowFlags::None)
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 inferMul(ArrayRef< ConstantIntRanges > argRanges, OverflowFlags ovfFlags=OverflowFlags::None)
ConstantIntRanges inferXor(ArrayRef< ConstantIntRanges > argRanges)
std::function< IntegerValueRange(ArrayRef< IntegerValueRange >)> InferIntegerValueRangeFn
Function that performs inferrence on an array of IntegerValueRange.
ConstantIntRanges inferDivS(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferShrU(ArrayRef< ConstantIntRanges > argRanges)
ConstantIntRanges inferRemU(ArrayRef< ConstantIntRanges > argRanges)
static constexpr unsigned indexMinWidth
ConstantIntRanges inferFloorDivS(ArrayRef< ConstantIntRanges > argRanges)
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 inferShapedDimOpInterface(ShapedDimOpInterface op, const IntegerValueRange &maybeDim)
Returns the integer range for the result of a ShapedDimOpInterface given the optional inferred ranges...
function_ref< ConstantIntRanges(ArrayRef< ConstantIntRanges >, OverflowFlags)> InferRangeWithOvfFlagsFn
Function that performs inference on an array of ConstantIntRanges while taking special overflow behav...
ConstantIntRanges inferMaxS(ArrayRef< ConstantIntRanges > argRanges)
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:152