MLIR  19.0.0git
Functions
AffineExpr.cpp File Reference
#include <utility>
#include "AffineExprDetail.h"
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/AffineExprVisitor.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/IntegerSet.h"
#include "mlir/Support/MathExtras.h"
#include "mlir/Support/TypeID.h"
#include "llvm/ADT/STLExtras.h"
#include <numeric>
#include <optional>

Go to the source code of this file.

Functions

static bool isDivisibleBySymbol (AffineExpr expr, unsigned symbolPos, AffineExprKind opKind)
 Returns true if the expression is divisible by the given symbol with position symbolPos. More...
 
static AffineExpr symbolicDivide (AffineExpr expr, unsigned symbolPos, AffineExprKind opKind)
 Divides the given expression by the given symbol at position symbolPos. More...
 
static void getSummandExprs (AffineExpr expr, SmallVector< AffineExpr > &result)
 Populate result with all summand operands of given (potentially nested) addition. More...
 
static bool isNegatedAffineExpr (AffineExpr candidate, AffineExpr &expr)
 Return "true" if candidate is a negated expression, i.e., Mul(-1, expr). More...
 
static bool isModOfModSubtraction (AffineExpr lhs, AffineExpr rhs, unsigned numDims, unsigned numSymbols)
 Return "true" if lhs % rhs is guaranteed to evaluate to zero based on the fact that lhs contains another modulo expression that ensures that lhs is divisible by rhs. More...
 
static AffineExpr simplifySemiAffine (AffineExpr expr, unsigned numDims, unsigned numSymbols)
 Simplify a semi-affine expression by handling modulo, floordiv, or ceildiv operations when the second operand simplifies to a symbol and the first operand is divisible by that symbol. More...
 
static AffineExpr getAffineDimOrSymbol (AffineExprKind kind, unsigned position, MLIRContext *context)
 
static AffineExpr simplifyAdd (AffineExpr lhs, AffineExpr rhs)
 Simplify add expression. Return nullptr if it can't be simplified. More...
 
static AffineExpr simplifyMul (AffineExpr lhs, AffineExpr rhs)
 Simplify a multiply expression. Return nullptr if it can't be simplified. More...
 
static AffineExpr simplifyFloorDiv (AffineExpr lhs, AffineExpr rhs)
 
static AffineExpr simplifyCeilDiv (AffineExpr lhs, AffineExpr rhs)
 
static AffineExpr simplifyMod (AffineExpr lhs, AffineExpr rhs)
 
static AffineExpr getSemiAffineExprFromFlatForm (ArrayRef< int64_t > flatExprs, unsigned numDims, unsigned numSymbols, ArrayRef< AffineExpr > localExprs, MLIRContext *context)
 Constructs a semi-affine expression from a flat ArrayRef. More...
 

Function Documentation

◆ getAffineDimOrSymbol()

static AffineExpr getAffineDimOrSymbol ( AffineExprKind  kind,
unsigned  position,
MLIRContext context 
)
static

Definition at line 588 of file AffineExpr.cpp.

Referenced by mlir::getAffineDimExpr(), and mlir::getAffineSymbolExpr().

◆ getSemiAffineExprFromFlatForm()

static AffineExpr getSemiAffineExprFromFlatForm ( ArrayRef< int64_t >  flatExprs,
unsigned  numDims,
unsigned  numSymbols,
ArrayRef< AffineExpr localExprs,
MLIRContext context 
)
static

Constructs a semi-affine expression from a flat ArrayRef.

If there are local identifiers (neither dimensional nor symbolic) that appear in the sum of products expression, localExprs is expected to have the AffineExprs for it, and is substituted into. The ArrayRef flatExprs is expected to be in the format [dims, symbols, locals, constant term]. The semi-affine expression is constructed in the sorted order of dimension and symbol position numbers. Note: local expressions/ids are used for mod, div as well as symbolic RHS terms for terms that are not pure affine.

Definition at line 1052 of file AffineExpr.cpp.

References mlir::detail::enumerate(), mlir::getAffineConstantExpr(), mlir::getAffineDimExpr(), mlir::getAffineSymbolExpr(), and max().

Referenced by mlir::simplifyAffineExpr().

◆ getSummandExprs()

static void getSummandExprs ( AffineExpr  expr,
SmallVector< AffineExpr > &  result 
)
static

Populate result with all summand operands of given (potentially nested) addition.

If the given expression is not an addition, just populate the expression itself. Example: Add(Add(7, 8), Mul(9, 10)) will return [7, 8, Mul(9, 10)].

Definition at line 466 of file AffineExpr.cpp.

References mlir::Add.

Referenced by isModOfModSubtraction().

◆ isDivisibleBySymbol()

static bool isDivisibleBySymbol ( AffineExpr  expr,
unsigned  symbolPos,
AffineExprKind  opKind 
)
static

Returns true if the expression is divisible by the given symbol with position symbolPos.

The argument opKind specifies here what kind of division or mod operation called this division. It helps in implementing the commutative property of the floordiv and ceildiv operations. If the argument exprKind is floordiv and expr is also a binary expression of a floordiv operation, then the commutative property can be used otherwise, the floordiv operation is not divisible. The same argument holds for ceildiv operation.

Definition at line 351 of file AffineExpr.cpp.

References mlir::Add, mlir::CeilDiv, mlir::Constant, mlir::DimId, mlir::FloorDiv, mlir::AffineExpr::getKind(), mlir::AffineBinaryOpExpr::getLHS(), mlir::AffineBinaryOpExpr::getRHS(), mlir::Mod, mlir::Mul, and mlir::SymbolId.

Referenced by simplifySemiAffine(), and symbolicDivide().

◆ isModOfModSubtraction()

static bool isModOfModSubtraction ( AffineExpr  lhs,
AffineExpr  rhs,
unsigned  numDims,
unsigned  numSymbols 
)
static

Return "true" if lhs % rhs is guaranteed to evaluate to zero based on the fact that lhs contains another modulo expression that ensures that lhs is divisible by rhs.

This is a common pattern in the resulting IR after loop peeling.

Example: lhs = ub - ub % step rhs = step => (ub - ub % step) % step is guaranteed to evaluate to 0.

Definition at line 505 of file AffineExpr.cpp.

References mlir::getAffineConstantExpr(), mlir::AffineExpr::getContext(), mlir::AffineExpr::getKind(), mlir::AffineBinaryOpExpr::getLHS(), mlir::AffineBinaryOpExpr::getRHS(), getSummandExprs(), isNegatedAffineExpr(), mlir::Mod, and mlir::simplifyAffineExpr().

Referenced by simplifySemiAffine().

◆ isNegatedAffineExpr()

static bool isNegatedAffineExpr ( AffineExpr  candidate,
AffineExpr expr 
)
static

Return "true" if candidate is a negated expression, i.e., Mul(-1, expr).

If so, also return the non-negated expression via expr.

Definition at line 478 of file AffineExpr.cpp.

References mlir::Mul.

Referenced by isModOfModSubtraction().

◆ simplifyAdd()

static AffineExpr simplifyAdd ( AffineExpr  lhs,
AffineExpr  rhs 
)
static

◆ simplifyCeilDiv()

static AffineExpr simplifyCeilDiv ( AffineExpr  lhs,
AffineExpr  rhs 
)
static

◆ simplifyFloorDiv()

static AffineExpr simplifyFloorDiv ( AffineExpr  lhs,
AffineExpr  rhs 
)
static

◆ simplifyMod()

static AffineExpr simplifyMod ( AffineExpr  lhs,
AffineExpr  rhs 
)
static

◆ simplifyMul()

static AffineExpr simplifyMul ( AffineExpr  lhs,
AffineExpr  rhs 
)
static

Simplify a multiply expression. Return nullptr if it can't be simplified.

Definition at line 769 of file AffineExpr.cpp.

References mlir::getAffineConstantExpr(), mlir::AffineExpr::getContext(), mlir::AffineExpr::isSymbolicOrConstant(), and mlir::Mul.

Referenced by mlir::AffineExpr::operator*().

◆ simplifySemiAffine()

static AffineExpr simplifySemiAffine ( AffineExpr  expr,
unsigned  numDims,
unsigned  numSymbols 
)
static

Simplify a semi-affine expression by handling modulo, floordiv, or ceildiv operations when the second operand simplifies to a symbol and the first operand is divisible by that symbol.

It can be applied to any semi-affine expression. Returned expression can either be a semi-affine or pure affine expression.

Definition at line 543 of file AffineExpr.cpp.

References mlir::Add, mlir::CeilDiv, mlir::Constant, mlir::DimId, mlir::FloorDiv, mlir::getAffineBinaryOpExpr(), mlir::getAffineConstantExpr(), mlir::AffineExpr::getContext(), mlir::AffineExpr::getKind(), mlir::AffineBinaryOpExpr::getLHS(), mlir::AffineSymbolExpr::getPosition(), mlir::AffineBinaryOpExpr::getRHS(), isDivisibleBySymbol(), isModOfModSubtraction(), mlir::Mod, mlir::Mul, symbolicDivide(), and mlir::SymbolId.

Referenced by mlir::simplifyAffineExpr().

◆ symbolicDivide()

static AffineExpr symbolicDivide ( AffineExpr  expr,
unsigned  symbolPos,
AffineExprKind  opKind 
)
static

Divides the given expression by the given symbol at position symbolPos.

It considers the divisibility condition is checked before calling itself. A null expression is returned whenever the divisibility condition fails.

Definition at line 410 of file AffineExpr.cpp.

References mlir::Add, mlir::CeilDiv, mlir::Constant, mlir::DimId, mlir::FloorDiv, mlir::getAffineBinaryOpExpr(), mlir::getAffineConstantExpr(), mlir::AffineExpr::getContext(), mlir::AffineExpr::getKind(), mlir::AffineBinaryOpExpr::getLHS(), mlir::AffineBinaryOpExpr::getRHS(), isDivisibleBySymbol(), mlir::Mod, mlir::Mul, and mlir::SymbolId.

Referenced by simplifySemiAffine().