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"
23 #include <type_traits>
33 struct AffineExprStorage;
34 struct AffineBinaryOpExprStorage;
35 struct AffineDimExprStorage;
36 struct AffineConstantExprStorage;
79 bool operator!=(int64_t v)
const {
return !(*
this == v); }
80 explicit operator bool()
const {
return expr; }
85 [[deprecated(
"Use llvm::isa<U>() instead")]] constexpr
bool isa()
const;
88 [[deprecated(
"Use llvm::dyn_cast<U>() instead")]] U
dyn_cast()
const;
91 [[deprecated(
"Use llvm::dyn_cast_or_null<U>() instead")]] U
95 [[deprecated(
"Use llvm::cast<U>() instead")]] U
cast()
const;
102 void print(raw_ostream &os)
const;
129 template <
typename FnT,
typename RetT = detail::walkResultType<FnT>>
130 RetT
walk(FnT &&callback)
const {
131 return walk<RetT>(*
this, callback);
159 unsigned offset = 0)
const;
164 unsigned offset = 0)
const;
199 return static_cast<const void *
>(
expr);
203 reinterpret_cast<ImplType *
>(
const_cast<void *
>(pointer)));
216 template <
typename WalkRetTy>
267 return expr * (-1) + val;
275 MLIRContext *context);
285 unsigned numDims,
unsigned numSymbols,
286 ArrayRef<AffineExpr> localExprs,
287 MLIRContext *context);
289 raw_ostream &
operator<<(raw_ostream &os, AffineExpr expr);
291 template <
typename U>
293 if constexpr (std::is_same_v<U, AffineBinaryOpExpr>)
295 if constexpr (std::is_same_v<U, AffineDimExpr>)
297 if constexpr (std::is_same_v<U, AffineSymbolExpr>)
299 if constexpr (std::is_same_v<U, AffineConstantExpr>)
302 template <
typename U>
304 return llvm::dyn_cast<U>(*
this);
306 template <
typename U>
308 return llvm::dyn_cast_or_null<U>(*
this);
310 template <
typename U>
312 return llvm::cast<U>(*
this);
322 unsigned numSymbols);
328 template <
int N,
typename AffineExprTy,
typename... AffineExprTy2>
331 bindDims<N + 1, AffineExprTy2 &...>(ctx, exprs...);
337 template <
int N,
typename AffineExprTy,
typename... AffineExprTy2>
340 bindSymbols<N + 1, AffineExprTy2 &...>(ctx, exprs...);
347 template <
typename... AffineExprTy>
349 detail::bindDims<0>(ctx, exprs...);
352 template <
typename AffineExprTy>
355 for (AffineExprTy &e : exprs)
361 template <
typename... AffineExprTy>
363 detail::bindSymbols<0>(ctx, exprs...);
366 template <
typename AffineExprTy>
369 for (AffineExprTy &e : exprs)
381 std::optional<int64_t>
383 ArrayRef<std::optional<int64_t>> constLowerBounds,
384 ArrayRef<std::optional<int64_t>> constUpperBounds,
412 template <
typename To,
typename From>
414 std::enable_if_t<std::is_same_v<mlir::AffineExpr,
415 std::remove_const_t<From>> ||
416 std::is_base_of_v<mlir::AffineExpr, From>>>
423 if constexpr (std::is_base_of_v<To, From>) {
426 if constexpr (std::is_same_v<To, ::mlir::AffineBinaryOpExpr>)
427 return expr.
getKind() <= ::mlir::AffineExprKind::LAST_AFFINE_BINARY_OP;
428 if constexpr (std::is_same_v<To, ::mlir::AffineDimExpr>)
429 return expr.
getKind() == ::mlir::AffineExprKind::DimId;
430 if constexpr (std::is_same_v<To, ::mlir::AffineSymbolExpr>)
431 return expr.
getKind() == ::mlir::AffineExprKind::SymbolId;
432 if constexpr (std::is_same_v<To, ::mlir::AffineConstantExpr>)
433 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.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
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.