29 struct ComposeSubViewOpPattern :
public OpRewritePattern<memref::SubViewOp> {
32 LogicalResult matchAndRewrite(memref::SubViewOp op,
38 auto sourceOp = op.getSource().getDefiningOp<memref::SubViewOp>();
48 if (sourceOp.getSourceType().getRank() != sourceOp.getType().getRank()) {
54 opStrides = op.getMixedStrides(),
55 sourceStrides = sourceOp.getMixedStrides();
59 int64_t sourceStrideValue;
60 for (
auto &&[opStride, sourceStride] :
61 llvm::zip(opStrides, sourceStrides)) {
62 Attribute opStrideAttr = dyn_cast_if_present<Attribute>(opStride);
63 Attribute sourceStrideAttr = dyn_cast_if_present<Attribute>(sourceStride);
64 if (!opStrideAttr || !sourceStrideAttr)
66 sourceStrideValue = cast<IntegerAttr>(sourceStrideAttr).getInt();
68 cast<IntegerAttr>(opStrideAttr).getInt() * sourceStrideValue));
81 for (
auto &&[opOffset, sourceOffset, sourceStride, opSize] :
82 llvm::zip(op.getMixedOffsets(), sourceOp.getMixedOffsets(),
83 sourceOp.getMixedStrides(), op.getMixedSizes())) {
85 if (isa<Value>(opSize)) {
88 sizes.push_back(opSize);
89 Attribute opOffsetAttr = llvm::dyn_cast_if_present<Attribute>(opOffset),
91 llvm::dyn_cast_if_present<Attribute>(sourceOffset),
93 llvm::dyn_cast_if_present<Attribute>(sourceStride);
94 if (opOffsetAttr && sourceOffsetAttr) {
99 cast<IntegerAttr>(opOffsetAttr).getInt() *
100 cast<IntegerAttr>(sourceStrideAttr).getInt() +
101 cast<IntegerAttr>(sourceOffsetAttr).getInt()));
107 if (
auto attr = llvm::dyn_cast_if_present<Attribute>(sourceOffset)) {
112 affineApplyOperands.push_back(cast<Value>(sourceOffset));
117 if (
auto attr = llvm::dyn_cast_if_present<Attribute>(opOffset)) {
118 expr = expr + cast<IntegerAttr>(attr).getInt() *
119 cast<IntegerAttr>(sourceStrideAttr).getInt();
123 cast<IntegerAttr>(sourceStrideAttr).getInt();
124 affineApplyOperands.push_back(cast<Value>(opOffset));
128 Value result = rewriter.
create<affine::AffineApplyOp>(
129 op.getLoc(), map, affineApplyOperands);
130 offsets.push_back(result);
137 op, op.getType(), sourceOp.getSource(), offsets, sizes, strides);
146 patterns.add<ComposeSubViewOpPattern>(context);
Base type for affine expression.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
Attributes are known-constant values of operations.
AffineExpr getAffineSymbolExpr(unsigned position)
AffineExpr getAffineConstantExpr(int64_t constant)
IntegerAttr getI64IntegerAttr(int64_t value)
MLIRContext is the top-level object for a collection of MLIR operations.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
void populateComposeSubViewPatterns(RewritePatternSet &patterns, MLIRContext *context)
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...