MLIR  20.0.0git
ScalableValueBoundsConstraintSet.h
Go to the documentation of this file.
1 //===- ScalableValueBoundsConstraintSet.h - Scalable Value Bounds ---------===//
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 #ifndef MLIR_DIALECT_VECTOR_IR_SCALABLEVALUEBOUNDSCONSTRAINTSET_H
10 #define MLIR_DIALECT_VECTOR_IR_SCALABLEVALUEBOUNDSCONSTRAINTSET_H
11 
15 
16 namespace mlir::vector {
17 
18 namespace detail {
19 
20 /// Parent class for the value bounds RTTIExtends. Uses protected inheritance to
21 /// hide all ValueBoundsConstraintSet methods by default (as some do not use the
22 /// ScalableValueBoundsConstraintSet, so may produce unexpected results).
24  using ::mlir::ValueBoundsConstraintSet::ValueBoundsConstraintSet;
25 };
26 } // namespace detail
27 
28 /// A version of `ValueBoundsConstraintSet` that can solve for scalable bounds.
30  : public llvm::RTTIExtends<ScalableValueBoundsConstraintSet,
31  detail::ValueBoundsConstraintSet> {
33  MLIRContext *context,
35  unsigned vscaleMin, unsigned vscaleMax)
36  : RTTIExtends(context, stopCondition,
37  /*addConservativeSemiAffineBounds=*/true),
38  vscaleMin(vscaleMin), vscaleMax(vscaleMax) {};
39 
40  using RTTIExtends::bound;
42 
43  /// A thin wrapper over an `AffineMap` which can represent a constant bound,
44  /// or a scalable bound (in terms of vscale). The `AffineMap` will always
45  /// take at most one parameter, vscale, and returns a single result, which is
46  /// the bound of value.
49 
50  struct BoundSize {
51  int64_t baseSize{0};
52  bool scalable{false};
53  };
54 
55  /// Get the (possibly) scalable size of the bound, returns failure if
56  /// the bound cannot be represented as a single quantity.
57  FailureOr<BoundSize> getSize() const;
58  };
59 
60  /// Computes a (possibly) scalable bound for a given value. This is
61  /// similar to `ValueBoundsConstraintSet::computeConstantBound()`, but
62  /// uses knowledge of the range of vscale to compute either a constant
63  /// bound, an expression in terms of vscale, or failure if no bound can
64  /// be computed.
65  ///
66  /// The resulting `AffineMap` will always take at most one parameter,
67  /// vscale, and return a single result, which is the bound of `value`.
68  ///
69  /// Note: `vscaleMin` must be `<=` to `vscaleMax`. If `vscaleMin` ==
70  /// `vscaleMax`, the resulting bound (if found), will be constant.
71  static FailureOr<ConstantOrScalableBound>
72  computeScalableBound(Value value, std::optional<int64_t> dim,
73  unsigned vscaleMin, unsigned vscaleMax,
74  presburger::BoundType boundType, bool closedUB = true,
75  StopConditionFn stopCondition = nullptr);
76 
77  /// Get the value of vscale. Returns `nullptr` vscale as not been encountered.
78  Value getVscaleValue() const { return vscale; }
79 
80  /// Sets the value of vscale. Asserts if vscale has already been set.
81  void setVscale(vector::VectorScaleOp vscaleOp) {
82  assert(!vscale && "expected vscale to be unset");
83  vscale = vscaleOp.getResult();
84  }
85 
86  /// The minimum possible value of vscale.
87  unsigned getVscaleMin() const { return vscaleMin; }
88 
89  /// The maximum possible value of vscale.
90  unsigned getVscaleMax() const { return vscaleMax; }
91 
92  static char ID;
93 
94 private:
95  const unsigned vscaleMin;
96  const unsigned vscaleMax;
97 
98  // This will be set when the first `vector.vscale` operation is found within
99  // the `ValueBoundsOpInterface` implementation then reused from there on.
100  Value vscale = nullptr;
101 };
102 
105 
106 } // namespace mlir::vector
107 
108 #endif // MLIR_DIALECT_VECTOR_IR_SCALABLEVALUEBOUNDSCONSTRAINTSET_H
function_ref< bool(Region *, ArrayRef< bool > visited)> StopConditionFn
Stop condition for traverseRegionGraph.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition: AffineMap.h:46
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
A helper class to be used with ValueBoundsOpInterface.
std::function< bool(Value, std::optional< int64_t >, ValueBoundsConstraintSet &cstr)> StopConditionFn
The stop condition when traversing the backward slice of a shaped value/ index-type value.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
BoundType
The type of bound: equal, lower bound or upper bound.
ScalableValueBoundsConstraintSet::ConstantOrScalableBound ConstantOrScalableBound
A thin wrapper over an AffineMap which can represent a constant bound, or a scalable bound (in terms ...
FailureOr< BoundSize > getSize() const
Get the (possibly) scalable size of the bound, returns failure if the bound cannot be represented as ...
A version of ValueBoundsConstraintSet that can solve for scalable bounds.
static FailureOr< ConstantOrScalableBound > computeScalableBound(Value value, std::optional< int64_t > dim, unsigned vscaleMin, unsigned vscaleMax, presburger::BoundType boundType, bool closedUB=true, StopConditionFn stopCondition=nullptr)
Computes a (possibly) scalable bound for a given value.
unsigned getVscaleMin() const
The minimum possible value of vscale.
Value getVscaleValue() const
Get the value of vscale. Returns nullptr vscale as not been encountered.
ScalableValueBoundsConstraintSet(MLIRContext *context, ValueBoundsConstraintSet::StopConditionFn stopCondition, unsigned vscaleMin, unsigned vscaleMax)
unsigned getVscaleMax() const
The maximum possible value of vscale.
void setVscale(vector::VectorScaleOp vscaleOp)
Sets the value of vscale. Asserts if vscale has already been set.
Parent class for the value bounds RTTIExtends.