14 #ifndef MLIR_IR_AFFINEEXPR_H
15 #define MLIR_IR_AFFINEEXPR_H
18 #include "llvm/ADT/DenseMapInfo.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/ADT/SmallVector.h"
21 #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;
154 unsigned offset = 0)
const;
159 unsigned offset = 0)
const;
194 return static_cast<const void *
>(
expr);
198 reinterpret_cast<ImplType *
>(
const_cast<void *
>(pointer)));
253 return expr * (-1) + val;
261 MLIRContext *context);
271 unsigned numDims,
unsigned numSymbols,
272 ArrayRef<AffineExpr> localExprs,
273 MLIRContext *context);
275 raw_ostream &
operator<<(raw_ostream &os, AffineExpr expr);
277 template <
typename U>
279 if constexpr (std::is_same_v<U, AffineBinaryOpExpr>)
281 if constexpr (std::is_same_v<U, AffineDimExpr>)
283 if constexpr (std::is_same_v<U, AffineSymbolExpr>)
285 if constexpr (std::is_same_v<U, AffineConstantExpr>)
288 template <
typename U>
290 return llvm::dyn_cast<U>(*
this);
292 template <
typename U>
294 return llvm::dyn_cast_or_null<U>(*
this);
296 template <
typename U>
298 return llvm::cast<U>(*
this);
308 unsigned numSymbols);
314 template <
int N,
typename AffineExprTy,
typename... AffineExprTy2>
317 bindDims<N + 1, AffineExprTy2 &...>(ctx, exprs...);
323 template <
int N,
typename AffineExprTy,
typename... AffineExprTy2>
326 bindSymbols<N + 1, AffineExprTy2 &...>(ctx, exprs...);
333 template <
typename... AffineExprTy>
335 detail::bindDims<0>(ctx, exprs...);
338 template <
typename AffineExprTy>
341 for (AffineExprTy &e : exprs)
347 template <
typename... AffineExprTy>
349 detail::bindSymbols<0>(ctx, exprs...);
352 template <
typename AffineExprTy>
355 for (AffineExprTy &e : exprs)
367 std::optional<int64_t>
369 ArrayRef<std::optional<int64_t>> constLowerBounds,
370 ArrayRef<std::optional<int64_t>> constUpperBounds,
398 template <
typename To,
typename From>
400 std::enable_if_t<std::is_same_v<mlir::AffineExpr,
401 std::remove_const_t<From>> ||
402 std::is_base_of_v<mlir::AffineExpr, From>>>
409 if constexpr (std::is_base_of_v<To, From>) {
412 if constexpr (std::is_same_v<To, ::mlir::AffineBinaryOpExpr>)
413 return expr.
getKind() <= ::mlir::AffineExprKind::LAST_AFFINE_BINARY_OP;
414 if constexpr (std::is_same_v<To, ::mlir::AffineDimExpr>)
415 return expr.
getKind() == ::mlir::AffineExprKind::DimId;
416 if constexpr (std::is_same_v<To, ::mlir::AffineSymbolExpr>)
417 return expr.
getKind() == ::mlir::AffineExprKind::SymbolId;
418 if constexpr (std::is_same_v<To, ::mlir::AffineConstantExpr>)
419 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
void walk(std::function< void(AffineExpr)> callback) const
Walk all of the AffineExpr's in this expression in postorder.
AffineExpr floorDiv(uint64_t v) const
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.