MLIR 24.0.0git
IndexOps.cpp File Reference
#include "mlir/Dialect/Index/IR/IndexOps.h"
#include "mlir/Dialect/Index/IR/IndexAttrs.h"
#include "mlir/Dialect/Index/IR/IndexDialect.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Interfaces/Utils/InferIntRangeCommon.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/TypeSwitch.h"
#include "mlir/Dialect/Index/IR/IndexOps.cpp.inc"

Go to the source code of this file.

Macros

#define GET_OP_CLASSES

Functions

static OpFoldResult foldBinaryOpUnchecked (ArrayRef< Attribute > operands, function_ref< std::optional< APInt >(const APInt &, const APInt &)> calculate)
 Fold an index operation irrespective of the target bitwidth.
static OpFoldResult foldBinaryOpChecked (ArrayRef< Attribute > operands, function_ref< std::optional< APInt >(const APInt &, const APInt &lhs)> calculate)
 Fold an index operation only if the truncated 64-bit result matches the 32-bit result for operations that don't satisfy the above property.
template<typename BinaryOp>
static LogicalResult canonicalizeAssociativeCommutativeBinaryOp (BinaryOp op, PatternRewriter &rewriter)
 Helper for associative and commutative binary ops that can be transformed: x = op(v, c1); y = op(x, c2) -> tmp = op(c1, c2); y = op(v, tmp) where c1 and c2 are constants.
static std::optional< APInt > calculateCeilDivS (const APInt &n, const APInt &m)
 Compute ceildivs(n, m) as x = m > 0 ?
static std::optional< APInt > calculateFloorDivS (const APInt &n, const APInt &m)
 Compute floordivs(n, m) as x = m < 0 ?
static OpFoldResult foldCastOp (Attribute input, Type type, function_ref< APInt(const APInt &, unsigned)> extFn, function_ref< APInt(const APInt &, unsigned)> extOrTruncFn)
static bool compareIndices (const APInt &lhs, const APInt &rhs, IndexCmpPredicate pred)
 Compare two integers according to the comparison predicate.
static std::optional< bool > foldCmpOfMaxOrMin (Operation *lhsOp, const APInt &cstA, const APInt &cstB, unsigned width, IndexCmpPredicate pred)
 cmp(max/min(x, cstA), cstB) can be folded to a constant depending on the values of cstA and cstB, the max or min operation, and the comparison predicate.
static bool compareSameArgs (IndexCmpPredicate pred)
 Return the result of cmp(pred, x, x)

Macro Definition Documentation

◆ GET_OP_CLASSES

#define GET_OP_CLASSES

Definition at line 765 of file IndexOps.cpp.

Function Documentation

◆ calculateCeilDivS()

std::optional< APInt > calculateCeilDivS ( const APInt & n,
const APInt & m )
static

Compute ceildivs(n, m) as x = m > 0 ?

-1 : 1 and then n*m > 0 ? (n+x)/m + 1 : -(-n/m).

Definition at line 243 of file IndexOps.cpp.

◆ calculateFloorDivS()

std::optional< APInt > calculateFloorDivS ( const APInt & n,
const APInt & m )
static

Compute floordivs(n, m) as x = m < 0 ?

1 : -1 and then n*m < 0 ? -1 - (x-n)/m : n/m.

Definition at line 294 of file IndexOps.cpp.

◆ canonicalizeAssociativeCommutativeBinaryOp()

template<typename BinaryOp>
LogicalResult canonicalizeAssociativeCommutativeBinaryOp ( BinaryOp op,
PatternRewriter & rewriter )
static

Helper for associative and commutative binary ops that can be transformed: x = op(v, c1); y = op(x, c2) -> tmp = op(c1, c2); y = op(v, tmp) where c1 and c2 are constants.

It is expected that tmp will be folded.

Definition at line 118 of file IndexOps.cpp.

References mlir::OpBuilder::createOrFold(), mlir::Value::getDefiningOp(), mlir::m_Constant(), mlir::matchPattern(), mlir::RewriterBase::notifyMatchFailure(), mlir::RewriterBase::replaceOpWithNewOp(), and success().

◆ compareIndices()

bool compareIndices ( const APInt & lhs,
const APInt & rhs,
IndexCmpPredicate pred )
static

Compare two integers according to the comparison predicate.

Definition at line 579 of file IndexOps.cpp.

◆ compareSameArgs()

bool compareSameArgs ( IndexCmpPredicate pred)
static

Return the result of cmp(pred, x, x)

Definition at line 636 of file IndexOps.cpp.

◆ foldBinaryOpChecked()

OpFoldResult foldBinaryOpChecked ( ArrayRef< Attribute > operands,
function_ref< std::optional< APInt >(const APInt &, const APInt &lhs)> calculate )
static

Fold an index operation only if the truncated 64-bit result matches the 32-bit result for operations that don't satisfy the above property.

These are operations where the upper bits of the operands can affect the lower bits of the results.

The function accepts a lambda that computes the integer result in both 64-bit and 32-bit. If either call returns std::nullopt, the operation is not folded.

Definition at line 87 of file IndexOps.cpp.

◆ foldBinaryOpUnchecked()

OpFoldResult foldBinaryOpUnchecked ( ArrayRef< Attribute > operands,
function_ref< std::optional< APInt >(const APInt &, const APInt &)> calculate )
static

Fold an index operation irrespective of the target bitwidth.

The operation must satisfy the property:

trunc(f(a, b)) = f(trunc(a), trunc(b))
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...

For all values of a and b. The function accepts a lambda that computes the integer result, which in turn must satisfy the above property.

Definition at line 61 of file IndexOps.cpp.

References result.

◆ foldCastOp()

OpFoldResult foldCastOp ( Attribute input,
Type type,
function_ref< APInt(const APInt &, unsigned)> extFn,
function_ref< APInt(const APInt &, unsigned)> extOrTruncFn )
static

Definition at line 502 of file IndexOps.cpp.

References result.

◆ foldCmpOfMaxOrMin()

std::optional< bool > foldCmpOfMaxOrMin ( Operation * lhsOp,
const APInt & cstA,
const APInt & cstB,
unsigned width,
IndexCmpPredicate pred )
static

cmp(max/min(x, cstA), cstB) can be folded to a constant depending on the values of cstA and cstB, the max or min operation, and the comparison predicate.

Check whether the value folds in both 32-bit and 64-bit arithmetic and to the same value.

Definition at line 610 of file IndexOps.cpp.

References mlir::ConstantIntRanges::constant(), mlir::intrange::evaluatePred(), mlir::ConstantIntRanges::fromSigned(), and mlir::ConstantIntRanges::fromUnsigned().