14 #ifndef MLIR_IR_AFFINEEXPR_H
15 #define MLIR_IR_AFFINEEXPR_H
19 #include "llvm/ADT/DenseMapInfo.h"
20 #include "llvm/ADT/Hashing.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/Casting.h"
24 #include <type_traits>
34 struct AffineExprStorage;
35 struct AffineBinaryOpExprStorage;
36 struct AffineDimExprStorage;
37 struct AffineConstantExprStorage;
80 bool operator!=(int64_t v)
const {
return !(*
this == v); }
81 explicit operator bool()
const {
return expr; }
86 [[deprecated(
"Use llvm::isa<U>() instead")]] constexpr
bool isa()
const;
89 [[deprecated(
"Use llvm::dyn_cast<U>() instead")]] U
dyn_cast()
const;
92 [[deprecated(
"Use llvm::dyn_cast_or_null<U>() instead")]] U
96 [[deprecated(
"Use llvm::cast<U>() instead")]] U
cast()
const;
103 void print(raw_ostream &os)
const;
130 template <
typename FnT,
typename RetT = detail::walkResultType<FnT>>
131 RetT
walk(FnT &&callback)
const {
132 return walk<RetT>(*
this, callback);
160 unsigned offset = 0)
const;
165 unsigned offset = 0)
const;
200 return static_cast<const void *
>(
expr);
204 reinterpret_cast<ImplType *
>(
const_cast<void *
>(pointer)));
217 template <
typename WalkRetTy>
268 return expr * (-1) + val;
276 MLIRContext *context);
286 unsigned numDims,
unsigned numSymbols,
287 ArrayRef<AffineExpr> localExprs,
288 MLIRContext *context);
290 raw_ostream &
operator<<(raw_ostream &os, AffineExpr expr);
292 template <
typename U>
294 if constexpr (std::is_same_v<U, AffineBinaryOpExpr>)
296 if constexpr (std::is_same_v<U, AffineDimExpr>)
298 if constexpr (std::is_same_v<U, AffineSymbolExpr>)
300 if constexpr (std::is_same_v<U, AffineConstantExpr>)
303 template <
typename U>
305 return llvm::dyn_cast<U>(*
this);
307 template <
typename U>
309 return llvm::dyn_cast_or_null<U>(*
this);
311 template <
typename U>
313 return llvm::cast<U>(*
this);
323 unsigned numSymbols);
329 template <
int N,
typename AffineExprTy,
typename... AffineExprTy2>
332 bindDims<N + 1, AffineExprTy2 &...>(ctx, exprs...);
338 template <
int N,
typename AffineExprTy,
typename... AffineExprTy2>
341 bindSymbols<N + 1, AffineExprTy2 &...>(ctx, exprs...);
348 template <
typename... AffineExprTy>
350 detail::bindDims<0>(ctx, exprs...);
353 template <
typename AffineExprTy>
356 for (AffineExprTy &e : exprs)
362 template <
typename... AffineExprTy>
364 detail::bindSymbols<0>(ctx, exprs...);
367 template <
typename AffineExprTy>
370 for (AffineExprTy &e : exprs)
382 std::optional<int64_t>
384 ArrayRef<std::optional<int64_t>> constLowerBounds,
385 ArrayRef<std::optional<int64_t>> constUpperBounds,
413 template <
typename To,
typename From>
415 std::enable_if_t<std::is_same_v<mlir::AffineExpr,
416 std::remove_const_t<From>> ||
417 std::is_base_of_v<mlir::AffineExpr, From>>>
424 if constexpr (std::is_base_of_v<To, From>) {
427 if constexpr (std::is_same_v<To, ::mlir::AffineBinaryOpExpr>)
428 return expr.
getKind() <= ::mlir::AffineExprKind::LAST_AFFINE_BINARY_OP;
429 if constexpr (std::is_same_v<To, ::mlir::AffineDimExpr>)
430 return expr.
getKind() == ::mlir::AffineExprKind::DimId;
431 if constexpr (std::is_same_v<To, ::mlir::AffineSymbolExpr>)
432 return expr.
getKind() == ::mlir::AffineExprKind::SymbolId;
433 if constexpr (std::is_same_v<To, ::mlir::AffineConstantExpr>)
434 return expr.
getKind() == ::mlir::AffineExprKind::Constant;
Affine binary operation expression.
AffineExpr getLHS() const
AffineBinaryOpExpr(AffineExpr::ImplType *ptr)
AffineExpr getRHS() const
An integer constant appearing in affine expression.
AffineConstantExpr(AffineExpr::ImplType *ptr=nullptr)
A dimensional identifier appearing in an affine expression.
AffineDimExpr(AffineExpr::ImplType *ptr)
unsigned getPosition() const
Base type for affine expression.
static AffineExpr getFromOpaquePointer(const void *pointer)
AffineExpr replaceDimsAndSymbols(ArrayRef< AffineExpr > dimReplacements, ArrayRef< AffineExpr > symReplacements) const
This method substitutes any uses of dimensions and symbols (e.g.
AffineExpr shiftDims(unsigned numDims, unsigned shift, unsigned offset=0) const
Replace dims[offset ...
ImplType * getImpl() const
AffineExpr operator+(int64_t v) const
U dyn_cast_or_null() const
friend ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
bool isSymbolicOrConstant() const
Returns true if this expression is made out of only symbols and constants, i.e., it does not involve ...
AffineExpr operator*(int64_t v) const
bool operator==(AffineExpr other) const
bool isPureAffine() const
Returns true if this is a pure affine expression, i.e., multiplication, floordiv, ceildiv,...
AffineExpr shiftSymbols(unsigned numSymbols, unsigned shift, unsigned offset=0) const
Replace symbols[offset ...
AffineExpr operator-() const
AffineExpr floorDiv(uint64_t v) const
RetT walk(FnT &&callback) const
Walk all of the AffineExpr's in this expression in postorder.
bool operator!=(int64_t v) const
AffineExprKind getKind() const
Return the classification for this type.
bool isMultipleOf(int64_t factor) const
Return true if the affine expression is a multiple of 'factor'.
int64_t getLargestKnownDivisor() const
Returns the greatest known integral divisor of this affine expression.
AffineExpr compose(AffineMap map) const
Compose with an AffineMap.
constexpr bool isa() const
bool isFunctionOfDim(unsigned position) const
Return true if the affine expression involves AffineDimExpr position.
const void * getAsOpaquePointer() const
Methods supporting C API.
bool isFunctionOfSymbol(unsigned position) const
Return true if the affine expression involves AffineSymbolExpr position.
AffineExpr replaceDims(ArrayRef< AffineExpr > dimReplacements) const
Dim-only version of replaceDimsAndSymbols.
bool operator!=(AffineExpr other) const
AffineExpr operator%(uint64_t v) const
MLIRContext * getContext() const
AffineExpr replace(AffineExpr expr, AffineExpr replacement) const
Sparse replace method.
AffineExpr replaceSymbols(ArrayRef< AffineExpr > symReplacements) const
Symbol-only version of replaceDimsAndSymbols.
AffineExpr(const ImplType *expr)
AffineExpr ceilDiv(uint64_t v) const
void print(raw_ostream &os) const
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
A symbolic identifier appearing in an affine expression.
AffineSymbolExpr(AffineExpr::ImplType *ptr)
unsigned getPosition() const
MLIRContext is the top-level object for a collection of MLIR operations.
Include the generated interface declarations.
void bindDims(MLIRContext *ctx)
void bindSymbols(MLIRContext *ctx)
Include the generated interface declarations.
std::optional< int64_t > getBoundForAffineExpr(AffineExpr expr, unsigned numDims, unsigned numSymbols, ArrayRef< std::optional< int64_t >> constLowerBounds, ArrayRef< std::optional< int64_t >> constUpperBounds, bool isUpper)
Get a lower or upper (depending on isUpper) bound for expr while using the constant lower and upper b...
void bindDimsList(MLIRContext *ctx, MutableArrayRef< AffineExprTy > exprs)
void bindDims(MLIRContext *ctx, AffineExprTy &...exprs)
Bind a list of AffineExpr references to DimExpr at positions: [0 .
@ CeilDiv
RHS of ceildiv is always a constant or a symbolic expression.
@ LAST_AFFINE_BINARY_OP
This is a marker for the last affine binary op.
@ Mul
RHS of mul is always a constant or a symbolic expression.
@ Mod
RHS of mod is always a constant or a symbolic expression with a positive value.
@ DimId
Dimensional identifier.
@ FloorDiv
RHS of floordiv is always a constant or a symbolic expression.
@ Constant
Constant integer.
@ SymbolId
Symbolic identifier.
AffineExpr getAffineBinaryOpExpr(AffineExprKind kind, AffineExpr lhs, AffineExpr rhs)
AffineExpr operator-(int64_t val, AffineExpr expr)
AffineExpr getAffineExprFromFlatForm(ArrayRef< int64_t > flatExprs, unsigned numDims, unsigned numSymbols, ArrayRef< AffineExpr > localExprs, MLIRContext *context)
Constructs an affine expression from a flat ArrayRef.
void bindSymbols(MLIRContext *ctx, AffineExprTy &...exprs)
Bind a list of AffineExpr references to SymbolExpr at positions: [0 .
AffineExpr operator+(int64_t val, AffineExpr expr)
AffineExpr operator*(int64_t val, AffineExpr expr)
AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)
AffineExpr simplifyAffineExpr(AffineExpr expr, unsigned numDims, unsigned numSymbols)
Simplify an affine expression by flattening and some amount of simple analysis.
SmallVector< AffineExpr > getAffineConstantExprs(ArrayRef< int64_t > constants, MLIRContext *context)
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
AffineExpr getAffineDimExpr(unsigned position, MLIRContext *context)
These free functions allow clients of the API to not use classes in detail.
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
void bindSymbolsList(MLIRContext *ctx, MutableArrayRef< AffineExprTy > exprs)
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
static bool isPossible(mlir::AffineExpr expr)
static To doCast(mlir::AffineExpr expr)
static unsigned getHashValue(mlir::AffineExpr val)
static mlir::AffineExpr getEmptyKey()
static mlir::AffineExpr getTombstoneKey()
static bool isEqual(mlir::AffineExpr LHS, mlir::AffineExpr RHS)
A binary operation appearing in an affine expression.
An integer constant appearing in affine expression.
A dimensional or symbolic identifier appearing in an affine expression.
Base storage class appearing in an affine expression.