1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
10 #include "mlir/Dialect/MemRef/IR/MemRef.h"
11 #include "mlir/Dialect/MemRef/Utils/MemRefUtils.h"
12 #include "mlir/Dialect/StandardOps/IR/Ops.h"
13 #include "mlir/Dialect/StandardOps/Utils/Utils.h"
14 #include "mlir/Dialect/Utils/StaticValueUtils.h"
15 #include "mlir/IR/AffineMap.h"
16 #include "mlir/IR/Builders.h"
17 #include "mlir/IR/BuiltinTypes.h"
18 #include "mlir/IR/Matchers.h"
19 #include "mlir/IR/PatternMatch.h"
20 #include "mlir/IR/TypeUtilities.h"
21 #include "mlir/Interfaces/InferTypeOpInterface.h"
22 #include "mlir/Interfaces/ViewLikeInterface.h"
23 #include "llvm/ADT/STLExtras.h"
24 
25 using namespace mlir;
26 using namespace mlir::memref;
27 
28 /// Materialize a single constant operation from a given attribute value with
29 /// the desired resultant type.
30 Operation *MemRefDialect::materializeConstant(OpBuilder &builder,
31                                               Attribute value, Type type,
32                                               Location loc) {
33   if (arith::ConstantOp::isBuildableWith(value, type))
34     return builder.create<arith::ConstantOp>(loc, value, type);
35   if (ConstantOp::isBuildableWith(value, type))
36     return builder.create<ConstantOp>(loc, value, type);
37   return nullptr;
38 }
39 
40 //===----------------------------------------------------------------------===//
41 // Common canonicalization pattern support logic
42 //===----------------------------------------------------------------------===//
43 
44 /// This is a common class used for patterns of the form
45 /// "someop(memrefcast) -> someop".  It folds the source of any memref.cast
46 /// into the root operation directly.
47 LogicalResult mlir::memref::foldMemRefCast(Operation *op, Value inner) {
48   bool folded = false;
49   for (OpOperand &operand : op->getOpOperands()) {
50     auto cast = operand.get().getDefiningOp<CastOp>();
51     if (cast && operand.get() != inner &&
52         !cast.getOperand().getType().isa<UnrankedMemRefType>()) {
53       operand.set(cast.getOperand());
54       folded = true;
55     }
56   }
57   return success(folded);
58 }
59 
60 /// Return an unranked/ranked tensor type for the given unranked/ranked memref
61 /// type.
62 Type mlir::memref::getTensorTypeFromMemRefType(Type type) {
63   if (auto memref = type.dyn_cast<MemRefType>())
64     return RankedTensorType::get(memref.getShape(), memref.getElementType());
65   if (auto memref = type.dyn_cast<UnrankedMemRefType>())
66     return UnrankedTensorType::get(memref.getElementType());
67   return NoneType::get(type.getContext());
68 }
69 
70 //===----------------------------------------------------------------------===//
71 // AllocOp / AllocaOp
72 //===----------------------------------------------------------------------===//
73 
74 template <typename AllocLikeOp>
75 static LogicalResult verifyAllocLikeOp(AllocLikeOp op) {
76   static_assert(llvm::is_one_of<AllocLikeOp, AllocOp, AllocaOp>::value,
77                 "applies to only alloc or alloca");
78   auto memRefType = op.getResult().getType().template dyn_cast<MemRefType>();
79   if (!memRefType)
80     return op.emitOpError("result must be a memref");
81 
82   if (static_cast<int64_t>(op.dynamicSizes().size()) !=
83       memRefType.getNumDynamicDims())
84     return op.emitOpError("dimension operand count does not equal memref "
85                           "dynamic dimension count");
86 
87   unsigned numSymbols = 0;
88   if (!memRefType.getLayout().isIdentity())
89     numSymbols = memRefType.getLayout().getAffineMap().getNumSymbols();
90   if (op.symbolOperands().size() != numSymbols)
91     return op.emitOpError("symbol operand count does not equal memref symbol "
92                           "count: expected ")
93            << numSymbols << ", got " << op.symbolOperands().size();
94 
95   return success();
96 }
97 
98 static LogicalResult verify(AllocOp op) { return verifyAllocLikeOp(op); }
99 
100 static LogicalResult verify(AllocaOp op) {
101   // An alloca op needs to have an ancestor with an allocation scope trait.
102   if (!op->getParentWithTrait<OpTrait::AutomaticAllocationScope>())
103     return op.emitOpError(
104         "requires an ancestor op with AutomaticAllocationScope trait");
105 
106   return verifyAllocLikeOp(op);
107 }
108 
109 namespace {
110 /// Fold constant dimensions into an alloc like operation.
111 template <typename AllocLikeOp>
112 struct SimplifyAllocConst : public OpRewritePattern<AllocLikeOp> {
113   using OpRewritePattern<AllocLikeOp>::OpRewritePattern;
114 
115   LogicalResult matchAndRewrite(AllocLikeOp alloc,
116                                 PatternRewriter &rewriter) const override {
117     // Check to see if any dimensions operands are constants.  If so, we can
118     // substitute and drop them.
119     if (llvm::none_of(alloc.dynamicSizes(), [](Value operand) {
120           return matchPattern(operand, matchConstantIndex());
121         }))
122       return failure();
123 
124     auto memrefType = alloc.getType();
125 
126     // Ok, we have one or more constant operands.  Collect the non-constant ones
127     // and keep track of the resultant memref type to build.
128     SmallVector<int64_t, 4> newShapeConstants;
129     newShapeConstants.reserve(memrefType.getRank());
130     SmallVector<Value, 4> dynamicSizes;
131 
132     unsigned dynamicDimPos = 0;
133     for (unsigned dim = 0, e = memrefType.getRank(); dim < e; ++dim) {
134       int64_t dimSize = memrefType.getDimSize(dim);
135       // If this is already static dimension, keep it.
136       if (dimSize != -1) {
137         newShapeConstants.push_back(dimSize);
138         continue;
139       }
140       auto dynamicSize = alloc.dynamicSizes()[dynamicDimPos];
141       auto *defOp = dynamicSize.getDefiningOp();
142       if (auto constantIndexOp =
143               dyn_cast_or_null<arith::ConstantIndexOp>(defOp)) {
144         // Dynamic shape dimension will be folded.
145         newShapeConstants.push_back(constantIndexOp.value());
146       } else {
147         // Dynamic shape dimension not folded; copy dynamicSize from old memref.
148         newShapeConstants.push_back(-1);
149         dynamicSizes.push_back(dynamicSize);
150       }
151       dynamicDimPos++;
152     }
153 
154     // Create new memref type (which will have fewer dynamic dimensions).
155     MemRefType newMemRefType =
156         MemRefType::Builder(memrefType).setShape(newShapeConstants);
157     assert(static_cast<int64_t>(dynamicSizes.size()) ==
158            newMemRefType.getNumDynamicDims());
159 
160     // Create and insert the alloc op for the new memref.
161     auto newAlloc = rewriter.create<AllocLikeOp>(
162         alloc.getLoc(), newMemRefType, dynamicSizes, alloc.symbolOperands(),
163         alloc.alignmentAttr());
164     // Insert a cast so we have the same type as the old alloc.
165     auto resultCast =
166         rewriter.create<CastOp>(alloc.getLoc(), newAlloc, alloc.getType());
167 
168     rewriter.replaceOp(alloc, {resultCast});
169     return success();
170   }
171 };
172 
173 /// Fold alloc operations with no users or only store and dealloc uses.
174 template <typename T>
175 struct SimplifyDeadAlloc : public OpRewritePattern<T> {
176   using OpRewritePattern<T>::OpRewritePattern;
177 
178   LogicalResult matchAndRewrite(T alloc,
179                                 PatternRewriter &rewriter) const override {
180     if (llvm::any_of(alloc->getUsers(), [&](Operation *op) {
181           if (auto storeOp = dyn_cast<StoreOp>(op))
182             return storeOp.value() == alloc;
183           return !isa<DeallocOp>(op);
184         }))
185       return failure();
186 
187     for (Operation *user : llvm::make_early_inc_range(alloc->getUsers()))
188       rewriter.eraseOp(user);
189 
190     rewriter.eraseOp(alloc);
191     return success();
192   }
193 };
194 } // end anonymous namespace.
195 
196 void AllocOp::getCanonicalizationPatterns(RewritePatternSet &results,
197                                           MLIRContext *context) {
198   results.add<SimplifyAllocConst<AllocOp>, SimplifyDeadAlloc<AllocOp>>(context);
199 }
200 
201 void AllocaOp::getCanonicalizationPatterns(RewritePatternSet &results,
202                                            MLIRContext *context) {
203   results.add<SimplifyAllocConst<AllocaOp>, SimplifyDeadAlloc<AllocaOp>>(
204       context);
205 }
206 
207 //===----------------------------------------------------------------------===//
208 // AllocaScopeOp
209 //===----------------------------------------------------------------------===//
210 
211 static void print(OpAsmPrinter &p, AllocaScopeOp &op) {
212   bool printBlockTerminators = false;
213 
214   p << " ";
215   if (!op.results().empty()) {
216     p << " -> (" << op.getResultTypes() << ")";
217     printBlockTerminators = true;
218   }
219   p.printRegion(op.bodyRegion(),
220                 /*printEntryBlockArgs=*/false,
221                 /*printBlockTerminators=*/printBlockTerminators);
222   p.printOptionalAttrDict(op->getAttrs());
223 }
224 
225 static ParseResult parseAllocaScopeOp(OpAsmParser &parser,
226                                       OperationState &result) {
227   // Create a region for the body.
228   result.regions.reserve(1);
229   Region *bodyRegion = result.addRegion();
230 
231   // Parse optional results type list.
232   if (parser.parseOptionalArrowTypeList(result.types))
233     return failure();
234 
235   // Parse the body region.
236   if (parser.parseRegion(*bodyRegion, /*arguments=*/{}, /*argTypes=*/{}))
237     return failure();
238   AllocaScopeOp::ensureTerminator(*bodyRegion, parser.getBuilder(),
239                                   result.location);
240 
241   // Parse the optional attribute list.
242   if (parser.parseOptionalAttrDict(result.attributes))
243     return failure();
244 
245   return success();
246 }
247 
248 static LogicalResult verify(AllocaScopeOp op) {
249   if (failed(RegionBranchOpInterface::verifyTypes(op)))
250     return failure();
251 
252   return success();
253 }
254 
255 void AllocaScopeOp::getSuccessorRegions(
256     Optional<unsigned> index, ArrayRef<Attribute> operands,
257     SmallVectorImpl<RegionSuccessor> &regions) {
258   if (index.hasValue()) {
259     regions.push_back(RegionSuccessor(getResults()));
260     return;
261   }
262 
263   regions.push_back(RegionSuccessor(&bodyRegion()));
264 }
265 
266 //===----------------------------------------------------------------------===//
267 // AssumeAlignmentOp
268 //===----------------------------------------------------------------------===//
269 
270 static LogicalResult verify(AssumeAlignmentOp op) {
271   unsigned alignment = op.alignment();
272   if (!llvm::isPowerOf2_32(alignment))
273     return op.emitOpError("alignment must be power of 2");
274   return success();
275 }
276 
277 //===----------------------------------------------------------------------===//
278 // CastOp
279 //===----------------------------------------------------------------------===//
280 
281 /// Determines whether MemRef_CastOp casts to a more dynamic version of the
282 /// source memref. This is useful to to fold a memref.cast into a consuming op
283 /// and implement canonicalization patterns for ops in different dialects that
284 /// may consume the results of memref.cast operations. Such foldable memref.cast
285 /// operations are typically inserted as `view` and `subview` ops are
286 /// canonicalized, to preserve the type compatibility of their uses.
287 ///
288 /// Returns true when all conditions are met:
289 /// 1. source and result are ranked memrefs with strided semantics and same
290 /// element type and rank.
291 /// 2. each of the source's size, offset or stride has more static information
292 /// than the corresponding result's size, offset or stride.
293 ///
294 /// Example 1:
295 /// ```mlir
296 ///   %1 = memref.cast %0 : memref<8x16xf32> to memref<?x?xf32>
297 ///   %2 = consumer %1 ... : memref<?x?xf32> ...
298 /// ```
299 ///
300 /// may fold into:
301 ///
302 /// ```mlir
303 ///   %2 = consumer %0 ... : memref<8x16xf32> ...
304 /// ```
305 ///
306 /// Example 2:
307 /// ```
308 ///   %1 = memref.cast %0 : memref<?x16xf32, affine_map<(i, j)->(16 * i + j)>>
309 ///          to memref<?x?xf32>
310 ///   consumer %1 : memref<?x?xf32> ...
311 /// ```
312 ///
313 /// may fold into:
314 ///
315 /// ```
316 ///   consumer %0 ... : memref<?x16xf32, affine_map<(i, j)->(16 * i + j)>>
317 /// ```
318 bool CastOp::canFoldIntoConsumerOp(CastOp castOp) {
319   MemRefType sourceType = castOp.source().getType().dyn_cast<MemRefType>();
320   MemRefType resultType = castOp.getType().dyn_cast<MemRefType>();
321 
322   // Requires ranked MemRefType.
323   if (!sourceType || !resultType)
324     return false;
325 
326   // Requires same elemental type.
327   if (sourceType.getElementType() != resultType.getElementType())
328     return false;
329 
330   // Requires same rank.
331   if (sourceType.getRank() != resultType.getRank())
332     return false;
333 
334   // Only fold casts between strided memref forms.
335   int64_t sourceOffset, resultOffset;
336   SmallVector<int64_t, 4> sourceStrides, resultStrides;
337   if (failed(getStridesAndOffset(sourceType, sourceStrides, sourceOffset)) ||
338       failed(getStridesAndOffset(resultType, resultStrides, resultOffset)))
339     return false;
340 
341   // If cast is towards more static sizes along any dimension, don't fold.
342   for (auto it : llvm::zip(sourceType.getShape(), resultType.getShape())) {
343     auto ss = std::get<0>(it), st = std::get<1>(it);
344     if (ss != st)
345       if (MemRefType::isDynamic(ss) && !MemRefType::isDynamic(st))
346         return false;
347   }
348 
349   // If cast is towards more static offset along any dimension, don't fold.
350   if (sourceOffset != resultOffset)
351     if (MemRefType::isDynamicStrideOrOffset(sourceOffset) &&
352         !MemRefType::isDynamicStrideOrOffset(resultOffset))
353       return false;
354 
355   // If cast is towards more static strides along any dimension, don't fold.
356   for (auto it : llvm::zip(sourceStrides, resultStrides)) {
357     auto ss = std::get<0>(it), st = std::get<1>(it);
358     if (ss != st)
359       if (MemRefType::isDynamicStrideOrOffset(ss) &&
360           !MemRefType::isDynamicStrideOrOffset(st))
361         return false;
362   }
363 
364   return true;
365 }
366 
367 bool CastOp::areCastCompatible(TypeRange inputs, TypeRange outputs) {
368   if (inputs.size() != 1 || outputs.size() != 1)
369     return false;
370   Type a = inputs.front(), b = outputs.front();
371   auto aT = a.dyn_cast<MemRefType>();
372   auto bT = b.dyn_cast<MemRefType>();
373 
374   auto uaT = a.dyn_cast<UnrankedMemRefType>();
375   auto ubT = b.dyn_cast<UnrankedMemRefType>();
376 
377   if (aT && bT) {
378     if (aT.getElementType() != bT.getElementType())
379       return false;
380     if (aT.getLayout() != bT.getLayout()) {
381       int64_t aOffset, bOffset;
382       SmallVector<int64_t, 4> aStrides, bStrides;
383       if (failed(getStridesAndOffset(aT, aStrides, aOffset)) ||
384           failed(getStridesAndOffset(bT, bStrides, bOffset)) ||
385           aStrides.size() != bStrides.size())
386         return false;
387 
388       // Strides along a dimension/offset are compatible if the value in the
389       // source memref is static and the value in the target memref is the
390       // same. They are also compatible if either one is dynamic (see
391       // description of MemRefCastOp for details).
392       auto checkCompatible = [](int64_t a, int64_t b) {
393         return (a == MemRefType::getDynamicStrideOrOffset() ||
394                 b == MemRefType::getDynamicStrideOrOffset() || a == b);
395       };
396       if (!checkCompatible(aOffset, bOffset))
397         return false;
398       for (auto aStride : enumerate(aStrides))
399         if (!checkCompatible(aStride.value(), bStrides[aStride.index()]))
400           return false;
401     }
402     if (aT.getMemorySpace() != bT.getMemorySpace())
403       return false;
404 
405     // They must have the same rank, and any specified dimensions must match.
406     if (aT.getRank() != bT.getRank())
407       return false;
408 
409     for (unsigned i = 0, e = aT.getRank(); i != e; ++i) {
410       int64_t aDim = aT.getDimSize(i), bDim = bT.getDimSize(i);
411       if (aDim != -1 && bDim != -1 && aDim != bDim)
412         return false;
413     }
414     return true;
415   } else {
416     if (!aT && !uaT)
417       return false;
418     if (!bT && !ubT)
419       return false;
420     // Unranked to unranked casting is unsupported
421     if (uaT && ubT)
422       return false;
423 
424     auto aEltType = (aT) ? aT.getElementType() : uaT.getElementType();
425     auto bEltType = (bT) ? bT.getElementType() : ubT.getElementType();
426     if (aEltType != bEltType)
427       return false;
428 
429     auto aMemSpace = (aT) ? aT.getMemorySpace() : uaT.getMemorySpace();
430     auto bMemSpace = (bT) ? bT.getMemorySpace() : ubT.getMemorySpace();
431     if (aMemSpace != bMemSpace)
432       return false;
433 
434     return true;
435   }
436 
437   return false;
438 }
439 
440 OpFoldResult CastOp::fold(ArrayRef<Attribute> operands) {
441   return succeeded(foldMemRefCast(*this)) ? getResult() : Value();
442 }
443 
444 //===----------------------------------------------------------------------===//
445 // DeallocOp
446 //===----------------------------------------------------------------------===//
447 
448 LogicalResult DeallocOp::fold(ArrayRef<Attribute> cstOperands,
449                               SmallVectorImpl<OpFoldResult> &results) {
450   /// dealloc(memrefcast) -> dealloc
451   return foldMemRefCast(*this);
452 }
453 
454 //===----------------------------------------------------------------------===//
455 // DimOp
456 //===----------------------------------------------------------------------===//
457 
458 void DimOp::build(OpBuilder &builder, OperationState &result, Value source,
459                   int64_t index) {
460   auto loc = result.location;
461   Value indexValue = builder.create<arith::ConstantIndexOp>(loc, index);
462   build(builder, result, source, indexValue);
463 }
464 
465 void DimOp::build(OpBuilder &builder, OperationState &result, Value source,
466                   Value index) {
467   auto indexTy = builder.getIndexType();
468   build(builder, result, indexTy, source, index);
469 }
470 
471 Optional<int64_t> DimOp::getConstantIndex() {
472   if (auto constantOp = index().getDefiningOp<arith::ConstantOp>())
473     return constantOp.getValue().cast<IntegerAttr>().getInt();
474   return {};
475 }
476 
477 static LogicalResult verify(DimOp op) {
478   // Assume unknown index to be in range.
479   Optional<int64_t> index = op.getConstantIndex();
480   if (!index.hasValue())
481     return success();
482 
483   // Check that constant index is not knowingly out of range.
484   auto type = op.source().getType();
485   if (auto memrefType = type.dyn_cast<MemRefType>()) {
486     if (index.getValue() >= memrefType.getRank())
487       return op.emitOpError("index is out of range");
488   } else if (type.isa<UnrankedMemRefType>()) {
489     // Assume index to be in range.
490   } else {
491     llvm_unreachable("expected operand with memref type");
492   }
493   return success();
494 }
495 
496 /// Return a map with key being elements in `vals` and data being number of
497 /// occurences of it. Use std::map, since the `vals` here are strides and the
498 /// dynamic stride value is the same as the tombstone value for
499 /// `DenseMap<int64_t>`.
500 static std::map<int64_t, unsigned> getNumOccurences(ArrayRef<int64_t> vals) {
501   std::map<int64_t, unsigned> numOccurences;
502   for (auto val : vals)
503     numOccurences[val]++;
504   return numOccurences;
505 }
506 
507 /// Given the type of the un-rank reduced subview result type and the
508 /// rank-reduced result type, computes the dropped dimensions. This accounts for
509 /// cases where there are multiple unit-dims, but only a subset of those are
510 /// dropped. For MemRefTypes these can be disambiguated using the strides. If a
511 /// dimension is dropped the stride must be dropped too.
512 static llvm::Optional<llvm::SmallDenseSet<unsigned>>
513 computeMemRefRankReductionMask(MemRefType originalType, MemRefType reducedType,
514                                ArrayAttr staticSizes) {
515   llvm::SmallDenseSet<unsigned> unusedDims;
516   if (originalType.getRank() == reducedType.getRank())
517     return unusedDims;
518 
519   for (auto dim : llvm::enumerate(staticSizes))
520     if (dim.value().cast<IntegerAttr>().getInt() == 1)
521       unusedDims.insert(dim.index());
522   SmallVector<int64_t> originalStrides, candidateStrides;
523   int64_t originalOffset, candidateOffset;
524   if (failed(
525           getStridesAndOffset(originalType, originalStrides, originalOffset)) ||
526       failed(
527           getStridesAndOffset(reducedType, candidateStrides, candidateOffset)))
528     return llvm::None;
529 
530   // For memrefs, a dimension is truly dropped if its corresponding stride is
531   // also dropped. This is particularly important when more than one of the dims
532   // is 1. Track the number of occurences of the strides in the original type
533   // and the candidate type. For each unused dim that stride should not be
534   // present in the candidate type. Note that there could be multiple dimensions
535   // that have the same size. We dont need to exactly figure out which dim
536   // corresponds to which stride, we just need to verify that the number of
537   // reptitions of a stride in the original + number of unused dims with that
538   // stride == number of repititions of a stride in the candidate.
539   std::map<int64_t, unsigned> currUnaccountedStrides =
540       getNumOccurences(originalStrides);
541   std::map<int64_t, unsigned> candidateStridesNumOccurences =
542       getNumOccurences(candidateStrides);
543   llvm::SmallDenseSet<unsigned> prunedUnusedDims;
544   for (unsigned dim : unusedDims) {
545     int64_t originalStride = originalStrides[dim];
546     if (currUnaccountedStrides[originalStride] >
547         candidateStridesNumOccurences[originalStride]) {
548       // This dim can be treated as dropped.
549       currUnaccountedStrides[originalStride]--;
550       continue;
551     }
552     if (currUnaccountedStrides[originalStride] ==
553         candidateStridesNumOccurences[originalStride]) {
554       // The stride for this is not dropped. Keep as is.
555       prunedUnusedDims.insert(dim);
556       continue;
557     }
558     if (currUnaccountedStrides[originalStride] <
559         candidateStridesNumOccurences[originalStride]) {
560       // This should never happen. Cant have a stride in the reduced rank type
561       // that wasnt in the original one.
562       return llvm::None;
563     }
564   }
565 
566   for (auto prunedDim : prunedUnusedDims)
567     unusedDims.erase(prunedDim);
568   if (unusedDims.size() + reducedType.getRank() != originalType.getRank())
569     return llvm::None;
570   return unusedDims;
571 }
572 
573 llvm::SmallDenseSet<unsigned> SubViewOp::getDroppedDims() {
574   MemRefType sourceType = getSourceType();
575   MemRefType resultType = getType();
576   llvm::Optional<llvm::SmallDenseSet<unsigned>> unusedDims =
577       computeMemRefRankReductionMask(sourceType, resultType, static_sizes());
578   assert(unusedDims && "unable to find unused dims of subview");
579   return *unusedDims;
580 }
581 
582 OpFoldResult DimOp::fold(ArrayRef<Attribute> operands) {
583   // All forms of folding require a known index.
584   auto index = operands[1].dyn_cast_or_null<IntegerAttr>();
585   if (!index)
586     return {};
587 
588   // Folding for unranked types (UnrankedMemRefType) is not supported.
589   auto memrefType = source().getType().dyn_cast<MemRefType>();
590   if (!memrefType)
591     return {};
592 
593   // Fold if the shape extent along the given index is known.
594   if (!memrefType.isDynamicDim(index.getInt())) {
595     Builder builder(getContext());
596     return builder.getIndexAttr(memrefType.getShape()[index.getInt()]);
597   }
598 
599   // The size at the given index is now known to be a dynamic size.
600   unsigned unsignedIndex = index.getValue().getZExtValue();
601 
602   // Fold dim to the size argument for an `AllocOp`, `ViewOp`, or `SubViewOp`.
603   Operation *definingOp = source().getDefiningOp();
604 
605   if (auto alloc = dyn_cast_or_null<AllocOp>(definingOp))
606     return *(alloc.getDynamicSizes().begin() +
607              memrefType.getDynamicDimIndex(unsignedIndex));
608 
609   if (auto alloca = dyn_cast_or_null<AllocaOp>(definingOp))
610     return *(alloca.getDynamicSizes().begin() +
611              memrefType.getDynamicDimIndex(unsignedIndex));
612 
613   if (auto view = dyn_cast_or_null<ViewOp>(definingOp))
614     return *(view.getDynamicSizes().begin() +
615              memrefType.getDynamicDimIndex(unsignedIndex));
616 
617   if (auto subview = dyn_cast_or_null<SubViewOp>(definingOp)) {
618     llvm::SmallDenseSet<unsigned> unusedDims = subview.getDroppedDims();
619     unsigned resultIndex = 0;
620     unsigned sourceRank = subview.getSourceType().getRank();
621     unsigned sourceIndex = 0;
622     for (auto i : llvm::seq<unsigned>(0, sourceRank)) {
623       if (unusedDims.count(i))
624         continue;
625       if (resultIndex == unsignedIndex) {
626         sourceIndex = i;
627         break;
628       }
629       resultIndex++;
630     }
631     assert(subview.isDynamicSize(sourceIndex) &&
632            "expected dynamic subview size");
633     return subview.getDynamicSize(sourceIndex);
634   }
635 
636   if (auto sizeInterface =
637           dyn_cast_or_null<OffsetSizeAndStrideOpInterface>(definingOp)) {
638     assert(sizeInterface.isDynamicSize(unsignedIndex) &&
639            "Expected dynamic subview size");
640     return sizeInterface.getDynamicSize(unsignedIndex);
641   }
642 
643   // dim(memrefcast) -> dim
644   if (succeeded(foldMemRefCast(*this)))
645     return getResult();
646 
647   return {};
648 }
649 
650 namespace {
651 /// Fold dim of a memref reshape operation to a load into the reshape's shape
652 /// operand.
653 struct DimOfMemRefReshape : public OpRewritePattern<DimOp> {
654   using OpRewritePattern<DimOp>::OpRewritePattern;
655 
656   LogicalResult matchAndRewrite(DimOp dim,
657                                 PatternRewriter &rewriter) const override {
658     auto reshape = dim.source().getDefiningOp<ReshapeOp>();
659 
660     if (!reshape)
661       return failure();
662 
663     // Place the load directly after the reshape to ensure that the shape memref
664     // was not mutated.
665     rewriter.setInsertionPointAfter(reshape);
666     Location loc = dim.getLoc();
667     Value load = rewriter.create<LoadOp>(loc, reshape.shape(), dim.index());
668     if (load.getType() != dim.getType())
669       load = rewriter.create<arith::IndexCastOp>(loc, dim.getType(), load);
670     rewriter.replaceOp(dim, load);
671     return success();
672   }
673 };
674 
675 } // end anonymous namespace.
676 
677 void DimOp::getCanonicalizationPatterns(RewritePatternSet &results,
678                                         MLIRContext *context) {
679   results.add<DimOfMemRefReshape>(context);
680 }
681 
682 // ---------------------------------------------------------------------------
683 // DmaStartOp
684 // ---------------------------------------------------------------------------
685 
686 void DmaStartOp::build(OpBuilder &builder, OperationState &result,
687                        Value srcMemRef, ValueRange srcIndices, Value destMemRef,
688                        ValueRange destIndices, Value numElements,
689                        Value tagMemRef, ValueRange tagIndices, Value stride,
690                        Value elementsPerStride) {
691   result.addOperands(srcMemRef);
692   result.addOperands(srcIndices);
693   result.addOperands(destMemRef);
694   result.addOperands(destIndices);
695   result.addOperands({numElements, tagMemRef});
696   result.addOperands(tagIndices);
697   if (stride)
698     result.addOperands({stride, elementsPerStride});
699 }
700 
701 static void print(OpAsmPrinter &p, DmaStartOp op) {
702   p << " " << op.getSrcMemRef() << '[' << op.getSrcIndices() << "], "
703     << op.getDstMemRef() << '[' << op.getDstIndices() << "], "
704     << op.getNumElements() << ", " << op.getTagMemRef() << '['
705     << op.getTagIndices() << ']';
706   if (op.isStrided())
707     p << ", " << op.getStride() << ", " << op.getNumElementsPerStride();
708 
709   p.printOptionalAttrDict(op->getAttrs());
710   p << " : " << op.getSrcMemRef().getType() << ", "
711     << op.getDstMemRef().getType() << ", " << op.getTagMemRef().getType();
712 }
713 
714 // Parse DmaStartOp.
715 // Ex:
716 //   %dma_id = dma_start %src[%i, %j], %dst[%k, %l], %size,
717 //                       %tag[%index], %stride, %num_elt_per_stride :
718 //                     : memref<3076 x f32, 0>,
719 //                       memref<1024 x f32, 2>,
720 //                       memref<1 x i32>
721 //
722 static ParseResult parseDmaStartOp(OpAsmParser &parser,
723                                    OperationState &result) {
724   OpAsmParser::OperandType srcMemRefInfo;
725   SmallVector<OpAsmParser::OperandType, 4> srcIndexInfos;
726   OpAsmParser::OperandType dstMemRefInfo;
727   SmallVector<OpAsmParser::OperandType, 4> dstIndexInfos;
728   OpAsmParser::OperandType numElementsInfo;
729   OpAsmParser::OperandType tagMemrefInfo;
730   SmallVector<OpAsmParser::OperandType, 4> tagIndexInfos;
731   SmallVector<OpAsmParser::OperandType, 2> strideInfo;
732 
733   SmallVector<Type, 3> types;
734   auto indexType = parser.getBuilder().getIndexType();
735 
736   // Parse and resolve the following list of operands:
737   // *) source memref followed by its indices (in square brackets).
738   // *) destination memref followed by its indices (in square brackets).
739   // *) dma size in KiB.
740   if (parser.parseOperand(srcMemRefInfo) ||
741       parser.parseOperandList(srcIndexInfos, OpAsmParser::Delimiter::Square) ||
742       parser.parseComma() || parser.parseOperand(dstMemRefInfo) ||
743       parser.parseOperandList(dstIndexInfos, OpAsmParser::Delimiter::Square) ||
744       parser.parseComma() || parser.parseOperand(numElementsInfo) ||
745       parser.parseComma() || parser.parseOperand(tagMemrefInfo) ||
746       parser.parseOperandList(tagIndexInfos, OpAsmParser::Delimiter::Square))
747     return failure();
748 
749   // Parse optional stride and elements per stride.
750   if (parser.parseTrailingOperandList(strideInfo))
751     return failure();
752 
753   bool isStrided = strideInfo.size() == 2;
754   if (!strideInfo.empty() && !isStrided) {
755     return parser.emitError(parser.getNameLoc(),
756                             "expected two stride related operands");
757   }
758 
759   if (parser.parseColonTypeList(types))
760     return failure();
761   if (types.size() != 3)
762     return parser.emitError(parser.getNameLoc(), "fewer/more types expected");
763 
764   if (parser.resolveOperand(srcMemRefInfo, types[0], result.operands) ||
765       parser.resolveOperands(srcIndexInfos, indexType, result.operands) ||
766       parser.resolveOperand(dstMemRefInfo, types[1], result.operands) ||
767       parser.resolveOperands(dstIndexInfos, indexType, result.operands) ||
768       // size should be an index.
769       parser.resolveOperand(numElementsInfo, indexType, result.operands) ||
770       parser.resolveOperand(tagMemrefInfo, types[2], result.operands) ||
771       // tag indices should be index.
772       parser.resolveOperands(tagIndexInfos, indexType, result.operands))
773     return failure();
774 
775   if (isStrided) {
776     if (parser.resolveOperands(strideInfo, indexType, result.operands))
777       return failure();
778   }
779 
780   return success();
781 }
782 
783 static LogicalResult verify(DmaStartOp op) {
784   unsigned numOperands = op.getNumOperands();
785 
786   // Mandatory non-variadic operands are: src memref, dst memref, tag memref and
787   // the number of elements.
788   if (numOperands < 4)
789     return op.emitOpError("expected at least 4 operands");
790 
791   // Check types of operands. The order of these calls is important: the later
792   // calls rely on some type properties to compute the operand position.
793   // 1. Source memref.
794   if (!op.getSrcMemRef().getType().isa<MemRefType>())
795     return op.emitOpError("expected source to be of memref type");
796   if (numOperands < op.getSrcMemRefRank() + 4)
797     return op.emitOpError()
798            << "expected at least " << op.getSrcMemRefRank() + 4 << " operands";
799   if (!op.getSrcIndices().empty() &&
800       !llvm::all_of(op.getSrcIndices().getTypes(),
801                     [](Type t) { return t.isIndex(); }))
802     return op.emitOpError("expected source indices to be of index type");
803 
804   // 2. Destination memref.
805   if (!op.getDstMemRef().getType().isa<MemRefType>())
806     return op.emitOpError("expected destination to be of memref type");
807   unsigned numExpectedOperands =
808       op.getSrcMemRefRank() + op.getDstMemRefRank() + 4;
809   if (numOperands < numExpectedOperands)
810     return op.emitOpError()
811            << "expected at least " << numExpectedOperands << " operands";
812   if (!op.getDstIndices().empty() &&
813       !llvm::all_of(op.getDstIndices().getTypes(),
814                     [](Type t) { return t.isIndex(); }))
815     return op.emitOpError("expected destination indices to be of index type");
816 
817   // 3. Number of elements.
818   if (!op.getNumElements().getType().isIndex())
819     return op.emitOpError("expected num elements to be of index type");
820 
821   // 4. Tag memref.
822   if (!op.getTagMemRef().getType().isa<MemRefType>())
823     return op.emitOpError("expected tag to be of memref type");
824   numExpectedOperands += op.getTagMemRefRank();
825   if (numOperands < numExpectedOperands)
826     return op.emitOpError()
827            << "expected at least " << numExpectedOperands << " operands";
828   if (!op.getTagIndices().empty() &&
829       !llvm::all_of(op.getTagIndices().getTypes(),
830                     [](Type t) { return t.isIndex(); }))
831     return op.emitOpError("expected tag indices to be of index type");
832 
833   // Optional stride-related operands must be either both present or both
834   // absent.
835   if (numOperands != numExpectedOperands &&
836       numOperands != numExpectedOperands + 2)
837     return op.emitOpError("incorrect number of operands");
838 
839   // 5. Strides.
840   if (op.isStrided()) {
841     if (!op.getStride().getType().isIndex() ||
842         !op.getNumElementsPerStride().getType().isIndex())
843       return op.emitOpError(
844           "expected stride and num elements per stride to be of type index");
845   }
846 
847   return success();
848 }
849 
850 LogicalResult DmaStartOp::fold(ArrayRef<Attribute> cstOperands,
851                                SmallVectorImpl<OpFoldResult> &results) {
852   /// dma_start(memrefcast) -> dma_start
853   return foldMemRefCast(*this);
854 }
855 
856 // ---------------------------------------------------------------------------
857 // DmaWaitOp
858 // ---------------------------------------------------------------------------
859 
860 LogicalResult DmaWaitOp::fold(ArrayRef<Attribute> cstOperands,
861                               SmallVectorImpl<OpFoldResult> &results) {
862   /// dma_wait(memrefcast) -> dma_wait
863   return foldMemRefCast(*this);
864 }
865 
866 static LogicalResult verify(DmaWaitOp op) {
867   // Check that the number of tag indices matches the tagMemRef rank.
868   unsigned numTagIndices = op.tagIndices().size();
869   unsigned tagMemRefRank = op.getTagMemRefRank();
870   if (numTagIndices != tagMemRefRank)
871     return op.emitOpError() << "expected tagIndices to have the same number of "
872                                "elements as the tagMemRef rank, expected "
873                             << tagMemRefRank << ", but got " << numTagIndices;
874   return success();
875 }
876 
877 //===----------------------------------------------------------------------===//
878 // GlobalOp
879 //===----------------------------------------------------------------------===//
880 
881 static void printGlobalMemrefOpTypeAndInitialValue(OpAsmPrinter &p, GlobalOp op,
882                                                    TypeAttr type,
883                                                    Attribute initialValue) {
884   p << type;
885   if (!op.isExternal()) {
886     p << " = ";
887     if (op.isUninitialized())
888       p << "uninitialized";
889     else
890       p.printAttributeWithoutType(initialValue);
891   }
892 }
893 
894 static ParseResult
895 parseGlobalMemrefOpTypeAndInitialValue(OpAsmParser &parser, TypeAttr &typeAttr,
896                                        Attribute &initialValue) {
897   Type type;
898   if (parser.parseType(type))
899     return failure();
900 
901   auto memrefType = type.dyn_cast<MemRefType>();
902   if (!memrefType || !memrefType.hasStaticShape())
903     return parser.emitError(parser.getNameLoc())
904            << "type should be static shaped memref, but got " << type;
905   typeAttr = TypeAttr::get(type);
906 
907   if (parser.parseOptionalEqual())
908     return success();
909 
910   if (succeeded(parser.parseOptionalKeyword("uninitialized"))) {
911     initialValue = UnitAttr::get(parser.getContext());
912     return success();
913   }
914 
915   Type tensorType = getTensorTypeFromMemRefType(memrefType);
916   if (parser.parseAttribute(initialValue, tensorType))
917     return failure();
918   if (!initialValue.isa<ElementsAttr>())
919     return parser.emitError(parser.getNameLoc())
920            << "initial value should be a unit or elements attribute";
921   return success();
922 }
923 
924 static LogicalResult verify(GlobalOp op) {
925   auto memrefType = op.type().dyn_cast<MemRefType>();
926   if (!memrefType || !memrefType.hasStaticShape())
927     return op.emitOpError("type should be static shaped memref, but got ")
928            << op.type();
929 
930   // Verify that the initial value, if present, is either a unit attribute or
931   // an elements attribute.
932   if (op.initial_value().hasValue()) {
933     Attribute initValue = op.initial_value().getValue();
934     if (!initValue.isa<UnitAttr>() && !initValue.isa<ElementsAttr>())
935       return op.emitOpError("initial value should be a unit or elements "
936                             "attribute, but got ")
937              << initValue;
938 
939     // Check that the type of the initial value is compatible with the type of
940     // the global variable.
941     if (initValue.isa<ElementsAttr>()) {
942       Type initType = initValue.getType();
943       Type tensorType = getTensorTypeFromMemRefType(memrefType);
944       if (initType != tensorType)
945         return op.emitOpError("initial value expected to be of type ")
946                << tensorType << ", but was of type " << initType;
947     }
948   }
949 
950   if (Optional<uint64_t> alignAttr = op.alignment()) {
951     uint64_t alignment = alignAttr.getValue();
952 
953     if (!llvm::isPowerOf2_64(alignment))
954       return op->emitError() << "alignment attribute value " << alignment
955                              << " is not a power of 2";
956   }
957 
958   // TODO: verify visibility for declarations.
959   return success();
960 }
961 
962 //===----------------------------------------------------------------------===//
963 // GetGlobalOp
964 //===----------------------------------------------------------------------===//
965 
966 LogicalResult
967 GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
968   // Verify that the result type is same as the type of the referenced
969   // memref.global op.
970   auto global =
971       symbolTable.lookupNearestSymbolFrom<GlobalOp>(*this, nameAttr());
972   if (!global)
973     return emitOpError("'")
974            << name() << "' does not reference a valid global memref";
975 
976   Type resultType = result().getType();
977   if (global.type() != resultType)
978     return emitOpError("result type ")
979            << resultType << " does not match type " << global.type()
980            << " of the global memref @" << name();
981   return success();
982 }
983 
984 //===----------------------------------------------------------------------===//
985 // LoadOp
986 //===----------------------------------------------------------------------===//
987 
988 static LogicalResult verify(LoadOp op) {
989   if (op.getNumOperands() != 1 + op.getMemRefType().getRank())
990     return op.emitOpError("incorrect number of indices for load");
991   return success();
992 }
993 
994 OpFoldResult LoadOp::fold(ArrayRef<Attribute> cstOperands) {
995   /// load(memrefcast) -> load
996   if (succeeded(foldMemRefCast(*this)))
997     return getResult();
998   return OpFoldResult();
999 }
1000 
1001 //===----------------------------------------------------------------------===//
1002 // PrefetchOp
1003 //===----------------------------------------------------------------------===//
1004 
1005 static void print(OpAsmPrinter &p, PrefetchOp op) {
1006   p << " " << op.memref() << '[';
1007   p.printOperands(op.indices());
1008   p << ']' << ", " << (op.isWrite() ? "write" : "read");
1009   p << ", locality<" << op.localityHint();
1010   p << ">, " << (op.isDataCache() ? "data" : "instr");
1011   p.printOptionalAttrDict(
1012       op->getAttrs(),
1013       /*elidedAttrs=*/{"localityHint", "isWrite", "isDataCache"});
1014   p << " : " << op.getMemRefType();
1015 }
1016 
1017 static ParseResult parsePrefetchOp(OpAsmParser &parser,
1018                                    OperationState &result) {
1019   OpAsmParser::OperandType memrefInfo;
1020   SmallVector<OpAsmParser::OperandType, 4> indexInfo;
1021   IntegerAttr localityHint;
1022   MemRefType type;
1023   StringRef readOrWrite, cacheType;
1024 
1025   auto indexTy = parser.getBuilder().getIndexType();
1026   auto i32Type = parser.getBuilder().getIntegerType(32);
1027   if (parser.parseOperand(memrefInfo) ||
1028       parser.parseOperandList(indexInfo, OpAsmParser::Delimiter::Square) ||
1029       parser.parseComma() || parser.parseKeyword(&readOrWrite) ||
1030       parser.parseComma() || parser.parseKeyword("locality") ||
1031       parser.parseLess() ||
1032       parser.parseAttribute(localityHint, i32Type, "localityHint",
1033                             result.attributes) ||
1034       parser.parseGreater() || parser.parseComma() ||
1035       parser.parseKeyword(&cacheType) || parser.parseColonType(type) ||
1036       parser.resolveOperand(memrefInfo, type, result.operands) ||
1037       parser.resolveOperands(indexInfo, indexTy, result.operands))
1038     return failure();
1039 
1040   if (!readOrWrite.equals("read") && !readOrWrite.equals("write"))
1041     return parser.emitError(parser.getNameLoc(),
1042                             "rw specifier has to be 'read' or 'write'");
1043   result.addAttribute(
1044       PrefetchOp::getIsWriteAttrName(),
1045       parser.getBuilder().getBoolAttr(readOrWrite.equals("write")));
1046 
1047   if (!cacheType.equals("data") && !cacheType.equals("instr"))
1048     return parser.emitError(parser.getNameLoc(),
1049                             "cache type has to be 'data' or 'instr'");
1050 
1051   result.addAttribute(
1052       PrefetchOp::getIsDataCacheAttrName(),
1053       parser.getBuilder().getBoolAttr(cacheType.equals("data")));
1054 
1055   return success();
1056 }
1057 
1058 static LogicalResult verify(PrefetchOp op) {
1059   if (op.getNumOperands() != 1 + op.getMemRefType().getRank())
1060     return op.emitOpError("too few indices");
1061 
1062   return success();
1063 }
1064 
1065 LogicalResult PrefetchOp::fold(ArrayRef<Attribute> cstOperands,
1066                                SmallVectorImpl<OpFoldResult> &results) {
1067   // prefetch(memrefcast) -> prefetch
1068   return foldMemRefCast(*this);
1069 }
1070 
1071 //===----------------------------------------------------------------------===//
1072 // ReinterpretCastOp
1073 //===----------------------------------------------------------------------===//
1074 
1075 /// Build a ReinterpretCastOp with all dynamic entries: `staticOffsets`,
1076 /// `staticSizes` and `staticStrides` are automatically filled with
1077 /// source-memref-rank sentinel values that encode dynamic entries.
1078 void ReinterpretCastOp::build(OpBuilder &b, OperationState &result,
1079                               MemRefType resultType, Value source,
1080                               OpFoldResult offset, ArrayRef<OpFoldResult> sizes,
1081                               ArrayRef<OpFoldResult> strides,
1082                               ArrayRef<NamedAttribute> attrs) {
1083   SmallVector<int64_t> staticOffsets, staticSizes, staticStrides;
1084   SmallVector<Value> dynamicOffsets, dynamicSizes, dynamicStrides;
1085   dispatchIndexOpFoldResults(offset, dynamicOffsets, staticOffsets,
1086                              ShapedType::kDynamicStrideOrOffset);
1087   dispatchIndexOpFoldResults(sizes, dynamicSizes, staticSizes,
1088                              ShapedType::kDynamicSize);
1089   dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides,
1090                              ShapedType::kDynamicStrideOrOffset);
1091   build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
1092         dynamicStrides, b.getI64ArrayAttr(staticOffsets),
1093         b.getI64ArrayAttr(staticSizes), b.getI64ArrayAttr(staticStrides));
1094   result.addAttributes(attrs);
1095 }
1096 
1097 void ReinterpretCastOp::build(OpBuilder &b, OperationState &result,
1098                               MemRefType resultType, Value source,
1099                               int64_t offset, ArrayRef<int64_t> sizes,
1100                               ArrayRef<int64_t> strides,
1101                               ArrayRef<NamedAttribute> attrs) {
1102   SmallVector<OpFoldResult> sizeValues =
1103       llvm::to_vector<4>(llvm::map_range(sizes, [&](int64_t v) -> OpFoldResult {
1104         return b.getI64IntegerAttr(v);
1105       }));
1106   SmallVector<OpFoldResult> strideValues = llvm::to_vector<4>(
1107       llvm::map_range(strides, [&](int64_t v) -> OpFoldResult {
1108         return b.getI64IntegerAttr(v);
1109       }));
1110   build(b, result, resultType, source, b.getI64IntegerAttr(offset), sizeValues,
1111         strideValues, attrs);
1112 }
1113 
1114 void ReinterpretCastOp::build(OpBuilder &b, OperationState &result,
1115                               MemRefType resultType, Value source, Value offset,
1116                               ValueRange sizes, ValueRange strides,
1117                               ArrayRef<NamedAttribute> attrs) {
1118   SmallVector<OpFoldResult> sizeValues = llvm::to_vector<4>(
1119       llvm::map_range(sizes, [](Value v) -> OpFoldResult { return v; }));
1120   SmallVector<OpFoldResult> strideValues = llvm::to_vector<4>(
1121       llvm::map_range(strides, [](Value v) -> OpFoldResult { return v; }));
1122   build(b, result, resultType, source, offset, sizeValues, strideValues, attrs);
1123 }
1124 
1125 // TODO: ponder whether we want to allow missing trailing sizes/strides that are
1126 // completed automatically, like we have for subview and extract_slice.
1127 static LogicalResult verify(ReinterpretCastOp op) {
1128   // The source and result memrefs should be in the same memory space.
1129   auto srcType = op.source().getType().cast<BaseMemRefType>();
1130   auto resultType = op.getType().cast<MemRefType>();
1131   if (srcType.getMemorySpace() != resultType.getMemorySpace())
1132     return op.emitError("different memory spaces specified for source type ")
1133            << srcType << " and result memref type " << resultType;
1134   if (srcType.getElementType() != resultType.getElementType())
1135     return op.emitError("different element types specified for source type ")
1136            << srcType << " and result memref type " << resultType;
1137 
1138   // Match sizes in result memref type and in static_sizes attribute.
1139   for (auto &en :
1140        llvm::enumerate(llvm::zip(resultType.getShape(),
1141                                  extractFromI64ArrayAttr(op.static_sizes())))) {
1142     int64_t resultSize = std::get<0>(en.value());
1143     int64_t expectedSize = std::get<1>(en.value());
1144     if (resultSize != expectedSize)
1145       return op.emitError("expected result type with size = ")
1146              << expectedSize << " instead of " << resultSize
1147              << " in dim = " << en.index();
1148   }
1149 
1150   // Match offset and strides in static_offset and static_strides attributes if
1151   // result memref type has an affine map specified.
1152   if (!resultType.getLayout().isIdentity()) {
1153     int64_t resultOffset;
1154     SmallVector<int64_t, 4> resultStrides;
1155     if (failed(getStridesAndOffset(resultType, resultStrides, resultOffset)))
1156       return failure();
1157 
1158     // Match offset in result memref type and in static_offsets attribute.
1159     int64_t expectedOffset =
1160         extractFromI64ArrayAttr(op.static_offsets()).front();
1161     if (resultOffset != expectedOffset)
1162       return op.emitError("expected result type with offset = ")
1163              << resultOffset << " instead of " << expectedOffset;
1164 
1165     // Match strides in result memref type and in static_strides attribute.
1166     for (auto &en : llvm::enumerate(llvm::zip(
1167              resultStrides, extractFromI64ArrayAttr(op.static_strides())))) {
1168       int64_t resultStride = std::get<0>(en.value());
1169       int64_t expectedStride = std::get<1>(en.value());
1170       if (resultStride != expectedStride)
1171         return op.emitError("expected result type with stride = ")
1172                << expectedStride << " instead of " << resultStride
1173                << " in dim = " << en.index();
1174     }
1175   }
1176   return success();
1177 }
1178 
1179 //===----------------------------------------------------------------------===//
1180 // Reassociative reshape ops
1181 //===----------------------------------------------------------------------===//
1182 
1183 SmallVector<AffineMap, 4> CollapseShapeOp::getReassociationMaps() {
1184   return getSymbolLessAffineMaps(getReassociationExprs());
1185 }
1186 SmallVector<ReassociationExprs, 4> CollapseShapeOp::getReassociationExprs() {
1187   return convertReassociationIndicesToExprs(getContext(),
1188                                             getReassociationIndices());
1189 }
1190 
1191 SmallVector<AffineMap, 4> ExpandShapeOp::getReassociationMaps() {
1192   return getSymbolLessAffineMaps(getReassociationExprs());
1193 }
1194 SmallVector<ReassociationExprs, 4> ExpandShapeOp::getReassociationExprs() {
1195   return convertReassociationIndicesToExprs(getContext(),
1196                                             getReassociationIndices());
1197 }
1198 
1199 static void print(OpAsmPrinter &p, ExpandShapeOp op) {
1200   ::mlir::printReshapeOp<ExpandShapeOp>(p, op);
1201 }
1202 
1203 static void print(OpAsmPrinter &p, CollapseShapeOp op) {
1204   ::mlir::printReshapeOp<CollapseShapeOp>(p, op);
1205 }
1206 
1207 /// Detect whether memref dims [dim, dim + extent) can be reshaped without
1208 /// copies.
1209 static bool isReshapableDimBand(unsigned dim, unsigned extent,
1210                                 ArrayRef<int64_t> sizes,
1211                                 ArrayRef<AffineExpr> strides) {
1212   // Bands of extent one can be reshaped, as they are not reshaped at all.
1213   if (extent == 1)
1214     return true;
1215   // Otherwise, the size of the first dimension needs to be known.
1216   if (ShapedType::isDynamic(sizes[dim]))
1217     return false;
1218   assert(sizes.size() == strides.size() && "mismatched ranks");
1219   // off by 1 indexing to avoid out of bounds
1220   //                       V
1221   for (auto idx = dim, e = dim + extent; idx + 1 < e; ++idx) {
1222     // Only bands of static shapes are reshapable. This is due to the fact that
1223     // there is no relation between dynamic sizes and dynamic strides: we do not
1224     // have enough information to know whether a "-1" size corresponds to the
1225     // proper symbol in the AffineExpr of a stride.
1226     if (ShapedType::isDynamic(sizes[idx + 1]))
1227       return false;
1228     // TODO: Refine this by passing the proper nDims and nSymbols so we can
1229     // simplify on the fly and catch more reshapable cases.
1230     if (strides[idx] != strides[idx + 1] * sizes[idx + 1])
1231       return false;
1232   }
1233   return true;
1234 }
1235 
1236 /// Compute the MemRefType obtained by applying the `reassociation` (which is
1237 /// expected to be valid) to `type`.
1238 /// If `type` is Contiguous MemRefType, this always produce a contiguous
1239 /// MemRefType.
1240 static MemRefType
1241 computeReshapeCollapsedType(MemRefType type,
1242                             ArrayRef<AffineMap> reassociation) {
1243   auto sizes = type.getShape();
1244   AffineExpr offset;
1245   SmallVector<AffineExpr, 4> strides;
1246   auto status = getStridesAndOffset(type, strides, offset);
1247   (void)status;
1248   assert(succeeded(status) && "expected strided memref");
1249 
1250   SmallVector<int64_t, 4> newSizes;
1251   newSizes.reserve(reassociation.size());
1252   SmallVector<AffineExpr, 4> newStrides;
1253   newStrides.reserve(reassociation.size());
1254 
1255   // Use the fact that reassociation is valid to simplify the logic: only use
1256   // each map's rank.
1257   assert(isReassociationValid(reassociation) && "invalid reassociation");
1258   unsigned currentDim = 0;
1259   for (AffineMap m : reassociation) {
1260     unsigned dim = m.getNumResults();
1261     int64_t size = 1;
1262     AffineExpr stride = strides[currentDim + dim - 1];
1263     if (!isReshapableDimBand(currentDim, dim, sizes, strides)) {
1264       size = ShapedType::kDynamicSize;
1265       stride = AffineExpr();
1266     } else {
1267       for (unsigned d = 0; d < dim; ++d)
1268         size *= sizes[currentDim + d];
1269     }
1270     newSizes.push_back(size);
1271     newStrides.push_back(stride);
1272     currentDim += dim;
1273   }
1274 
1275   // Early-exit: if `type` is contiguous, the result must be contiguous.
1276   if (canonicalizeStridedLayout(type).getLayout().isIdentity())
1277     return MemRefType::Builder(type).setShape(newSizes).setLayout({});
1278 
1279   // Convert back to int64_t because we don't have enough information to create
1280   // new strided layouts from AffineExpr only. This corresponds to a case where
1281   // copies may be necessary.
1282   int64_t intOffset = ShapedType::kDynamicStrideOrOffset;
1283   if (auto o = offset.dyn_cast<AffineConstantExpr>())
1284     intOffset = o.getValue();
1285   SmallVector<int64_t, 4> intStrides;
1286   intStrides.reserve(strides.size());
1287   for (auto stride : newStrides) {
1288     if (auto cst = stride.dyn_cast_or_null<AffineConstantExpr>())
1289       intStrides.push_back(cst.getValue());
1290     else
1291       intStrides.push_back(ShapedType::kDynamicStrideOrOffset);
1292   }
1293   auto layout =
1294       makeStridedLinearLayoutMap(intStrides, intOffset, type.getContext());
1295   return canonicalizeStridedLayout(
1296       MemRefType::Builder(type).setShape(newSizes).setLayout(
1297           AffineMapAttr::get(layout)));
1298 }
1299 
1300 void ExpandShapeOp::build(OpBuilder &b, OperationState &result, Value src,
1301                           ArrayRef<ReassociationIndices> reassociation,
1302                           ArrayRef<NamedAttribute> attrs) {
1303   auto memRefType = src.getType().cast<MemRefType>();
1304   auto resultType = computeReshapeCollapsedType(
1305       memRefType, getSymbolLessAffineMaps(convertReassociationIndicesToExprs(
1306                       b.getContext(), reassociation)));
1307   build(b, result, resultType, src, attrs);
1308   result.addAttribute(getReassociationAttrName(),
1309                       getReassociationIndicesAttribute(b, reassociation));
1310 }
1311 
1312 void CollapseShapeOp::build(OpBuilder &b, OperationState &result, Value src,
1313                             ArrayRef<ReassociationIndices> reassociation,
1314                             ArrayRef<NamedAttribute> attrs) {
1315   auto memRefType = src.getType().cast<MemRefType>();
1316   auto resultType = computeReshapeCollapsedType(
1317       memRefType, getSymbolLessAffineMaps(convertReassociationIndicesToExprs(
1318                       b.getContext(), reassociation)));
1319   build(b, result, resultType, src, attrs);
1320   result.addAttribute(getReassociationAttrName(),
1321                       getReassociationIndicesAttribute(b, reassociation));
1322 }
1323 
1324 template <typename ReshapeOp,
1325           bool isExpansion = std::is_same<ReshapeOp, ExpandShapeOp>::value>
1326 static LogicalResult verifyReshapeOp(ReshapeOp op, MemRefType expandedType,
1327                                      MemRefType collapsedType) {
1328   if (failed(
1329           verifyReshapeLikeTypes(op, expandedType, collapsedType, isExpansion)))
1330     return failure();
1331   auto maps = op.getReassociationMaps();
1332   MemRefType expectedType = computeReshapeCollapsedType(expandedType, maps);
1333   if (collapsedType != expectedType)
1334     return op.emitOpError("expected collapsed type to be ")
1335            << expectedType << ", but got " << collapsedType;
1336   return success();
1337 }
1338 
1339 static LogicalResult verify(ExpandShapeOp op) {
1340   return verifyReshapeOp(op, op.getResultType(), op.getSrcType());
1341 }
1342 
1343 void ExpandShapeOp::getCanonicalizationPatterns(RewritePatternSet &results,
1344                                                 MLIRContext *context) {
1345   results.add<CollapseReshapeOps<ExpandShapeOp>,
1346               CollapseMixedReshapeOps<ExpandShapeOp, CollapseShapeOp>>(context);
1347 }
1348 
1349 static LogicalResult verify(CollapseShapeOp op) {
1350   return verifyReshapeOp(op, op.getSrcType(), op.getResultType());
1351 }
1352 
1353 struct CollapseShapeOpMemRefCastFolder
1354     : public OpRewritePattern<CollapseShapeOp> {
1355 public:
1356   using OpRewritePattern<CollapseShapeOp>::OpRewritePattern;
1357 
1358   LogicalResult matchAndRewrite(CollapseShapeOp op,
1359                                 PatternRewriter &rewriter) const override {
1360     auto cast = op.getOperand().getDefiningOp<CastOp>();
1361     if (!cast)
1362       return failure();
1363 
1364     if (!CastOp::canFoldIntoConsumerOp(cast))
1365       return failure();
1366 
1367     Type newResultType = computeReshapeCollapsedType(
1368         cast.getOperand().getType().cast<MemRefType>(),
1369         op.getReassociationMaps());
1370 
1371     if (newResultType == op.getResultType()) {
1372       rewriter.updateRootInPlace(
1373           op, [&]() { op.srcMutable().assign(cast.source()); });
1374     } else {
1375       Value newOp = rewriter.create<CollapseShapeOp>(
1376           op->getLoc(), cast.source(), op.getReassociationIndices());
1377       rewriter.replaceOpWithNewOp<CastOp>(op, op.getType(), newOp);
1378     }
1379     return success();
1380   }
1381 };
1382 
1383 void CollapseShapeOp::getCanonicalizationPatterns(RewritePatternSet &results,
1384                                                   MLIRContext *context) {
1385   results.add<CollapseReshapeOps<CollapseShapeOp>,
1386               CollapseMixedReshapeOps<CollapseShapeOp, ExpandShapeOp>,
1387               CollapseShapeOpMemRefCastFolder>(context);
1388 }
1389 OpFoldResult ExpandShapeOp::fold(ArrayRef<Attribute> operands) {
1390   return foldReshapeOp<ExpandShapeOp, CollapseShapeOp>(*this, operands);
1391 }
1392 OpFoldResult CollapseShapeOp::fold(ArrayRef<Attribute> operands) {
1393   return foldReshapeOp<CollapseShapeOp, ExpandShapeOp>(*this, operands);
1394 }
1395 
1396 //===----------------------------------------------------------------------===//
1397 // ReshapeOp
1398 //===----------------------------------------------------------------------===//
1399 
1400 static LogicalResult verify(ReshapeOp op) {
1401   Type operandType = op.source().getType();
1402   Type resultType = op.result().getType();
1403 
1404   Type operandElementType = operandType.cast<ShapedType>().getElementType();
1405   Type resultElementType = resultType.cast<ShapedType>().getElementType();
1406   if (operandElementType != resultElementType)
1407     return op.emitOpError("element types of source and destination memref "
1408                           "types should be the same");
1409 
1410   if (auto operandMemRefType = operandType.dyn_cast<MemRefType>())
1411     if (!operandMemRefType.getLayout().isIdentity())
1412       return op.emitOpError(
1413           "source memref type should have identity affine map");
1414 
1415   int64_t shapeSize = op.shape().getType().cast<MemRefType>().getDimSize(0);
1416   auto resultMemRefType = resultType.dyn_cast<MemRefType>();
1417   if (resultMemRefType) {
1418     if (!resultMemRefType.getLayout().isIdentity())
1419       return op.emitOpError(
1420           "result memref type should have identity affine map");
1421     if (shapeSize == ShapedType::kDynamicSize)
1422       return op.emitOpError("cannot use shape operand with dynamic length to "
1423                             "reshape to statically-ranked memref type");
1424     if (shapeSize != resultMemRefType.getRank())
1425       return op.emitOpError(
1426           "length of shape operand differs from the result's memref rank");
1427   }
1428   return success();
1429 }
1430 
1431 //===----------------------------------------------------------------------===//
1432 // StoreOp
1433 //===----------------------------------------------------------------------===//
1434 
1435 static LogicalResult verify(StoreOp op) {
1436   if (op.getNumOperands() != 2 + op.getMemRefType().getRank())
1437     return op.emitOpError("store index operand count not equal to memref rank");
1438 
1439   return success();
1440 }
1441 
1442 LogicalResult StoreOp::fold(ArrayRef<Attribute> cstOperands,
1443                             SmallVectorImpl<OpFoldResult> &results) {
1444   /// store(memrefcast) -> store
1445   return foldMemRefCast(*this, getValueToStore());
1446 }
1447 
1448 //===----------------------------------------------------------------------===//
1449 // SubViewOp
1450 //===----------------------------------------------------------------------===//
1451 
1452 namespace {
1453 /// Helpers to write more idiomatic operations.
1454 namespace saturated_arith {
1455 struct Wrapper {
1456   explicit Wrapper(int64_t v) : v(v) {}
1457   operator int64_t() { return v; }
1458   int64_t v;
1459 };
1460 Wrapper operator+(Wrapper a, int64_t b) {
1461   if (ShapedType::isDynamicStrideOrOffset(a) ||
1462       ShapedType::isDynamicStrideOrOffset(b))
1463     return Wrapper(ShapedType::kDynamicStrideOrOffset);
1464   return Wrapper(a.v + b);
1465 }
1466 Wrapper operator*(Wrapper a, int64_t b) {
1467   if (ShapedType::isDynamicStrideOrOffset(a) ||
1468       ShapedType::isDynamicStrideOrOffset(b))
1469     return Wrapper(ShapedType::kDynamicStrideOrOffset);
1470   return Wrapper(a.v * b);
1471 }
1472 } // end namespace saturated_arith
1473 } // end namespace
1474 
1475 /// A subview result type can be fully inferred from the source type and the
1476 /// static representation of offsets, sizes and strides. Special sentinels
1477 /// encode the dynamic case.
1478 Type SubViewOp::inferResultType(MemRefType sourceMemRefType,
1479                                 ArrayRef<int64_t> leadingStaticOffsets,
1480                                 ArrayRef<int64_t> leadingStaticSizes,
1481                                 ArrayRef<int64_t> leadingStaticStrides) {
1482   // A subview may specify only a leading subset of offset/sizes/strides in
1483   // which case we complete with offset=0, sizes from memref type and strides=1.
1484   unsigned rank = sourceMemRefType.getRank();
1485   assert(leadingStaticOffsets.size() <= rank &&
1486          "unexpected leadingStaticOffsets overflow");
1487   assert(leadingStaticSizes.size() <= rank &&
1488          "unexpected leadingStaticSizes overflow");
1489   assert(leadingStaticStrides.size() <= rank &&
1490          "unexpected leadingStaticStrides overflow");
1491   auto staticOffsets = llvm::to_vector<4>(leadingStaticOffsets);
1492   auto staticSizes = llvm::to_vector<4>(leadingStaticSizes);
1493   auto staticStrides = llvm::to_vector<4>(leadingStaticStrides);
1494   unsigned numTrailingOffsets = rank - staticOffsets.size();
1495   unsigned numTrailingSizes = rank - staticSizes.size();
1496   unsigned numTrailingStrides = rank - staticStrides.size();
1497   staticOffsets.append(numTrailingOffsets, 0);
1498   llvm::append_range(staticSizes,
1499                      sourceMemRefType.getShape().take_back(numTrailingSizes));
1500   staticStrides.append(numTrailingStrides, 1);
1501 
1502   // Extract source offset and strides.
1503   int64_t sourceOffset;
1504   SmallVector<int64_t, 4> sourceStrides;
1505   auto res = getStridesAndOffset(sourceMemRefType, sourceStrides, sourceOffset);
1506   assert(succeeded(res) && "SubViewOp expected strided memref type");
1507   (void)res;
1508 
1509   // Compute target offset whose value is:
1510   //   `sourceOffset + sum_i(staticOffset_i * sourceStrides_i)`.
1511   int64_t targetOffset = sourceOffset;
1512   for (auto it : llvm::zip(staticOffsets, sourceStrides)) {
1513     auto staticOffset = std::get<0>(it), targetStride = std::get<1>(it);
1514     using namespace saturated_arith;
1515     targetOffset = Wrapper(targetOffset) + Wrapper(staticOffset) * targetStride;
1516   }
1517 
1518   // Compute target stride whose value is:
1519   //   `sourceStrides_i * staticStrides_i`.
1520   SmallVector<int64_t, 4> targetStrides;
1521   targetStrides.reserve(staticOffsets.size());
1522   for (auto it : llvm::zip(sourceStrides, staticStrides)) {
1523     auto sourceStride = std::get<0>(it), staticStride = std::get<1>(it);
1524     using namespace saturated_arith;
1525     targetStrides.push_back(Wrapper(sourceStride) * staticStride);
1526   }
1527 
1528   // The type is now known.
1529   return MemRefType::get(
1530       staticSizes, sourceMemRefType.getElementType(),
1531       makeStridedLinearLayoutMap(targetStrides, targetOffset,
1532                                  sourceMemRefType.getContext()),
1533       sourceMemRefType.getMemorySpace());
1534 }
1535 
1536 Type SubViewOp::inferResultType(MemRefType sourceMemRefType,
1537                                 ArrayRef<OpFoldResult> leadingStaticOffsets,
1538                                 ArrayRef<OpFoldResult> leadingStaticSizes,
1539                                 ArrayRef<OpFoldResult> leadingStaticStrides) {
1540   SmallVector<int64_t> staticOffsets, staticSizes, staticStrides;
1541   SmallVector<Value> dynamicOffsets, dynamicSizes, dynamicStrides;
1542   dispatchIndexOpFoldResults(leadingStaticOffsets, dynamicOffsets,
1543                              staticOffsets, ShapedType::kDynamicStrideOrOffset);
1544   dispatchIndexOpFoldResults(leadingStaticSizes, dynamicSizes, staticSizes,
1545                              ShapedType::kDynamicSize);
1546   dispatchIndexOpFoldResults(leadingStaticStrides, dynamicStrides,
1547                              staticStrides, ShapedType::kDynamicStrideOrOffset);
1548   return SubViewOp::inferResultType(sourceMemRefType, staticOffsets,
1549                                     staticSizes, staticStrides)
1550       .cast<MemRefType>();
1551 }
1552 
1553 Type SubViewOp::inferRankReducedResultType(
1554     unsigned resultRank, MemRefType sourceRankedTensorType,
1555     ArrayRef<int64_t> leadingStaticOffsets,
1556     ArrayRef<int64_t> leadingStaticSizes,
1557     ArrayRef<int64_t> leadingStaticStrides) {
1558   auto inferredType =
1559       inferResultType(sourceRankedTensorType, leadingStaticOffsets,
1560                       leadingStaticSizes, leadingStaticStrides)
1561           .cast<MemRefType>();
1562   assert(inferredType.getRank() >= resultRank && "expected ");
1563   int rankDiff = inferredType.getRank() - resultRank;
1564   if (rankDiff > 0) {
1565     auto shape = inferredType.getShape();
1566     llvm::SmallDenseSet<unsigned> dimsToProject;
1567     mlir::getPositionsOfShapeOne(rankDiff, shape, dimsToProject);
1568     SmallVector<int64_t> projectedShape;
1569     for (unsigned pos = 0, e = shape.size(); pos < e; ++pos)
1570       if (!dimsToProject.contains(pos))
1571         projectedShape.push_back(shape[pos]);
1572 
1573     AffineMap map = inferredType.getLayout().getAffineMap();
1574     if (!map.isIdentity())
1575       map = getProjectedMap(map, dimsToProject);
1576     inferredType =
1577         MemRefType::get(projectedShape, inferredType.getElementType(), map,
1578                         inferredType.getMemorySpace());
1579   }
1580   return inferredType;
1581 }
1582 
1583 Type SubViewOp::inferRankReducedResultType(
1584     unsigned resultRank, MemRefType sourceRankedTensorType,
1585     ArrayRef<OpFoldResult> leadingStaticOffsets,
1586     ArrayRef<OpFoldResult> leadingStaticSizes,
1587     ArrayRef<OpFoldResult> leadingStaticStrides) {
1588   SmallVector<int64_t> staticOffsets, staticSizes, staticStrides;
1589   SmallVector<Value> dynamicOffsets, dynamicSizes, dynamicStrides;
1590   dispatchIndexOpFoldResults(leadingStaticOffsets, dynamicOffsets,
1591                              staticOffsets, ShapedType::kDynamicStrideOrOffset);
1592   dispatchIndexOpFoldResults(leadingStaticSizes, dynamicSizes, staticSizes,
1593                              ShapedType::kDynamicSize);
1594   dispatchIndexOpFoldResults(leadingStaticStrides, dynamicStrides,
1595                              staticStrides, ShapedType::kDynamicStrideOrOffset);
1596   return SubViewOp::inferRankReducedResultType(
1597       resultRank, sourceRankedTensorType, staticOffsets, staticSizes,
1598       staticStrides);
1599 }
1600 // Build a SubViewOp with mixed static and dynamic entries and custom result
1601 // type. If the type passed is nullptr, it is inferred.
1602 void SubViewOp::build(OpBuilder &b, OperationState &result,
1603                       MemRefType resultType, Value source,
1604                       ArrayRef<OpFoldResult> offsets,
1605                       ArrayRef<OpFoldResult> sizes,
1606                       ArrayRef<OpFoldResult> strides,
1607                       ArrayRef<NamedAttribute> attrs) {
1608   SmallVector<int64_t> staticOffsets, staticSizes, staticStrides;
1609   SmallVector<Value> dynamicOffsets, dynamicSizes, dynamicStrides;
1610   dispatchIndexOpFoldResults(offsets, dynamicOffsets, staticOffsets,
1611                              ShapedType::kDynamicStrideOrOffset);
1612   dispatchIndexOpFoldResults(sizes, dynamicSizes, staticSizes,
1613                              ShapedType::kDynamicSize);
1614   dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides,
1615                              ShapedType::kDynamicStrideOrOffset);
1616   auto sourceMemRefType = source.getType().cast<MemRefType>();
1617   // Structuring implementation this way avoids duplication between builders.
1618   if (!resultType) {
1619     resultType = SubViewOp::inferResultType(sourceMemRefType, staticOffsets,
1620                                             staticSizes, staticStrides)
1621                      .cast<MemRefType>();
1622   }
1623   build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
1624         dynamicStrides, b.getI64ArrayAttr(staticOffsets),
1625         b.getI64ArrayAttr(staticSizes), b.getI64ArrayAttr(staticStrides));
1626   result.addAttributes(attrs);
1627 }
1628 
1629 // Build a SubViewOp with mixed static and dynamic entries and inferred result
1630 // type.
1631 void SubViewOp::build(OpBuilder &b, OperationState &result, Value source,
1632                       ArrayRef<OpFoldResult> offsets,
1633                       ArrayRef<OpFoldResult> sizes,
1634                       ArrayRef<OpFoldResult> strides,
1635                       ArrayRef<NamedAttribute> attrs) {
1636   build(b, result, MemRefType(), source, offsets, sizes, strides, attrs);
1637 }
1638 
1639 // Build a SubViewOp with static entries and inferred result type.
1640 void SubViewOp::build(OpBuilder &b, OperationState &result, Value source,
1641                       ArrayRef<int64_t> offsets, ArrayRef<int64_t> sizes,
1642                       ArrayRef<int64_t> strides,
1643                       ArrayRef<NamedAttribute> attrs) {
1644   SmallVector<OpFoldResult> offsetValues = llvm::to_vector<4>(
1645       llvm::map_range(offsets, [&](int64_t v) -> OpFoldResult {
1646         return b.getI64IntegerAttr(v);
1647       }));
1648   SmallVector<OpFoldResult> sizeValues =
1649       llvm::to_vector<4>(llvm::map_range(sizes, [&](int64_t v) -> OpFoldResult {
1650         return b.getI64IntegerAttr(v);
1651       }));
1652   SmallVector<OpFoldResult> strideValues = llvm::to_vector<4>(
1653       llvm::map_range(strides, [&](int64_t v) -> OpFoldResult {
1654         return b.getI64IntegerAttr(v);
1655       }));
1656   build(b, result, source, offsetValues, sizeValues, strideValues, attrs);
1657 }
1658 
1659 // Build a SubViewOp with dynamic entries and custom result type. If the
1660 // type passed is nullptr, it is inferred.
1661 void SubViewOp::build(OpBuilder &b, OperationState &result,
1662                       MemRefType resultType, Value source,
1663                       ArrayRef<int64_t> offsets, ArrayRef<int64_t> sizes,
1664                       ArrayRef<int64_t> strides,
1665                       ArrayRef<NamedAttribute> attrs) {
1666   SmallVector<OpFoldResult> offsetValues = llvm::to_vector<4>(
1667       llvm::map_range(offsets, [&](int64_t v) -> OpFoldResult {
1668         return b.getI64IntegerAttr(v);
1669       }));
1670   SmallVector<OpFoldResult> sizeValues =
1671       llvm::to_vector<4>(llvm::map_range(sizes, [&](int64_t v) -> OpFoldResult {
1672         return b.getI64IntegerAttr(v);
1673       }));
1674   SmallVector<OpFoldResult> strideValues = llvm::to_vector<4>(
1675       llvm::map_range(strides, [&](int64_t v) -> OpFoldResult {
1676         return b.getI64IntegerAttr(v);
1677       }));
1678   build(b, result, resultType, source, offsetValues, sizeValues, strideValues,
1679         attrs);
1680 }
1681 
1682 // Build a SubViewOp with dynamic entries and custom result type. If the type
1683 // passed is nullptr, it is inferred.
1684 void SubViewOp::build(OpBuilder &b, OperationState &result,
1685                       MemRefType resultType, Value source, ValueRange offsets,
1686                       ValueRange sizes, ValueRange strides,
1687                       ArrayRef<NamedAttribute> attrs) {
1688   SmallVector<OpFoldResult> offsetValues = llvm::to_vector<4>(
1689       llvm::map_range(offsets, [](Value v) -> OpFoldResult { return v; }));
1690   SmallVector<OpFoldResult> sizeValues = llvm::to_vector<4>(
1691       llvm::map_range(sizes, [](Value v) -> OpFoldResult { return v; }));
1692   SmallVector<OpFoldResult> strideValues = llvm::to_vector<4>(
1693       llvm::map_range(strides, [](Value v) -> OpFoldResult { return v; }));
1694   build(b, result, resultType, source, offsetValues, sizeValues, strideValues);
1695 }
1696 
1697 // Build a SubViewOp with dynamic entries and inferred result type.
1698 void SubViewOp::build(OpBuilder &b, OperationState &result, Value source,
1699                       ValueRange offsets, ValueRange sizes, ValueRange strides,
1700                       ArrayRef<NamedAttribute> attrs) {
1701   build(b, result, MemRefType(), source, offsets, sizes, strides, attrs);
1702 }
1703 
1704 /// For ViewLikeOpInterface.
1705 Value SubViewOp::getViewSource() { return source(); }
1706 
1707 enum SubViewVerificationResult {
1708   Success,
1709   RankTooLarge,
1710   SizeMismatch,
1711   ElemTypeMismatch,
1712   MemSpaceMismatch,
1713   AffineMapMismatch
1714 };
1715 
1716 /// Checks if `original` Type type can be rank reduced to `reduced` type.
1717 /// This function is slight variant of `is subsequence` algorithm where
1718 /// not matching dimension must be 1.
1719 static SubViewVerificationResult
1720 isRankReducedType(Type originalType, Type candidateReducedType,
1721                   ArrayAttr staticSizes, std::string *errMsg = nullptr) {
1722   if (originalType == candidateReducedType)
1723     return SubViewVerificationResult::Success;
1724   if (!originalType.isa<MemRefType>())
1725     return SubViewVerificationResult::Success;
1726   if (originalType.isa<MemRefType>() && !candidateReducedType.isa<MemRefType>())
1727     return SubViewVerificationResult::Success;
1728 
1729   ShapedType originalShapedType = originalType.cast<ShapedType>();
1730   ShapedType candidateReducedShapedType =
1731       candidateReducedType.cast<ShapedType>();
1732 
1733   // Rank and size logic is valid for all ShapedTypes.
1734   ArrayRef<int64_t> originalShape = originalShapedType.getShape();
1735   ArrayRef<int64_t> candidateReducedShape =
1736       candidateReducedShapedType.getShape();
1737   unsigned originalRank = originalShape.size(),
1738            candidateReducedRank = candidateReducedShape.size();
1739   if (candidateReducedRank > originalRank)
1740     return SubViewVerificationResult::RankTooLarge;
1741 
1742   MemRefType original = originalType.cast<MemRefType>();
1743   MemRefType candidateReduced = candidateReducedType.cast<MemRefType>();
1744 
1745   auto optionalUnusedDimsMask =
1746       computeMemRefRankReductionMask(original, candidateReduced, staticSizes);
1747 
1748   // Sizes cannot be matched in case empty vector is returned.
1749   if (!optionalUnusedDimsMask.hasValue())
1750     return SubViewVerificationResult::SizeMismatch;
1751 
1752   if (originalShapedType.getElementType() !=
1753       candidateReducedShapedType.getElementType())
1754     return SubViewVerificationResult::ElemTypeMismatch;
1755 
1756   // Strided layout logic is relevant for MemRefType only.
1757   if (original.getMemorySpace() != candidateReduced.getMemorySpace())
1758     return SubViewVerificationResult::MemSpaceMismatch;
1759   return SubViewVerificationResult::Success;
1760 }
1761 
1762 template <typename OpTy>
1763 static LogicalResult produceSubViewErrorMsg(SubViewVerificationResult result,
1764                                             OpTy op, Type expectedType,
1765                                             StringRef errMsg = "") {
1766   auto memrefType = expectedType.cast<ShapedType>();
1767   switch (result) {
1768   case SubViewVerificationResult::Success:
1769     return success();
1770   case SubViewVerificationResult::RankTooLarge:
1771     return op.emitError("expected result rank to be smaller or equal to ")
1772            << "the source rank. " << errMsg;
1773   case SubViewVerificationResult::SizeMismatch:
1774     return op.emitError("expected result type to be ")
1775            << expectedType
1776            << " or a rank-reduced version. (mismatch of result sizes) "
1777            << errMsg;
1778   case SubViewVerificationResult::ElemTypeMismatch:
1779     return op.emitError("expected result element type to be ")
1780            << memrefType.getElementType() << errMsg;
1781   case SubViewVerificationResult::MemSpaceMismatch:
1782     return op.emitError("expected result and source memory spaces to match.")
1783            << errMsg;
1784   case SubViewVerificationResult::AffineMapMismatch:
1785     return op.emitError("expected result type to be ")
1786            << expectedType
1787            << " or a rank-reduced version. (mismatch of result affine map) "
1788            << errMsg;
1789   }
1790   llvm_unreachable("unexpected subview verification result");
1791 }
1792 
1793 /// Verifier for SubViewOp.
1794 static LogicalResult verify(SubViewOp op) {
1795   MemRefType baseType = op.getSourceType();
1796   MemRefType subViewType = op.getType();
1797 
1798   // The base memref and the view memref should be in the same memory space.
1799   if (baseType.getMemorySpace() != subViewType.getMemorySpace())
1800     return op.emitError("different memory spaces specified for base memref "
1801                         "type ")
1802            << baseType << " and subview memref type " << subViewType;
1803 
1804   // Verify that the base memref type has a strided layout map.
1805   if (!isStrided(baseType))
1806     return op.emitError("base type ") << baseType << " is not strided";
1807 
1808   // Verify result type against inferred type.
1809   auto expectedType = SubViewOp::inferResultType(
1810       baseType, extractFromI64ArrayAttr(op.static_offsets()),
1811       extractFromI64ArrayAttr(op.static_sizes()),
1812       extractFromI64ArrayAttr(op.static_strides()));
1813 
1814   std::string errMsg;
1815   auto result =
1816       isRankReducedType(expectedType, subViewType, op.static_sizes(), &errMsg);
1817   return produceSubViewErrorMsg(result, op, expectedType, errMsg);
1818 }
1819 
1820 raw_ostream &mlir::operator<<(raw_ostream &os, const Range &range) {
1821   return os << "range " << range.offset << ":" << range.size << ":"
1822             << range.stride;
1823 }
1824 
1825 /// Return the list of Range (i.e. offset, size, stride). Each Range
1826 /// entry contains either the dynamic value or a ConstantIndexOp constructed
1827 /// with `b` at location `loc`.
1828 SmallVector<Range, 8> mlir::getOrCreateRanges(OffsetSizeAndStrideOpInterface op,
1829                                               OpBuilder &b, Location loc) {
1830   std::array<unsigned, 3> ranks = op.getArrayAttrMaxRanks();
1831   assert(ranks[0] == ranks[1] && "expected offset and sizes of equal ranks");
1832   assert(ranks[1] == ranks[2] && "expected sizes and strides of equal ranks");
1833   SmallVector<Range, 8> res;
1834   unsigned rank = ranks[0];
1835   res.reserve(rank);
1836   for (unsigned idx = 0; idx < rank; ++idx) {
1837     Value offset =
1838         op.isDynamicOffset(idx)
1839             ? op.getDynamicOffset(idx)
1840             : b.create<arith::ConstantIndexOp>(loc, op.getStaticOffset(idx));
1841     Value size =
1842         op.isDynamicSize(idx)
1843             ? op.getDynamicSize(idx)
1844             : b.create<arith::ConstantIndexOp>(loc, op.getStaticSize(idx));
1845     Value stride =
1846         op.isDynamicStride(idx)
1847             ? op.getDynamicStride(idx)
1848             : b.create<arith::ConstantIndexOp>(loc, op.getStaticStride(idx));
1849     res.emplace_back(Range{offset, size, stride});
1850   }
1851   return res;
1852 }
1853 
1854 /// Infer the canonical type of the result of a subview operation. Returns a
1855 /// type with rank `resultRank` that is either the rank of the rank-reduced
1856 /// type, or the non-rank-reduced type.
1857 static MemRefType
1858 getCanonicalSubViewResultType(unsigned resultRank, MemRefType sourceType,
1859                               ArrayRef<OpFoldResult> mixedOffsets,
1860                               ArrayRef<OpFoldResult> mixedSizes,
1861                               ArrayRef<OpFoldResult> mixedStrides) {
1862   auto resultType =
1863       SubViewOp::inferRankReducedResultType(
1864           resultRank, sourceType, mixedOffsets, mixedSizes, mixedStrides)
1865           .cast<MemRefType>();
1866   if (resultType.getRank() != resultRank) {
1867     resultType = SubViewOp::inferResultType(sourceType, mixedOffsets,
1868                                             mixedSizes, mixedStrides)
1869                      .cast<MemRefType>();
1870   }
1871   return resultType;
1872 }
1873 
1874 namespace {
1875 /// Pattern to rewrite a subview op with MemRefCast arguments.
1876 /// This essentially pushes memref.cast past its consuming subview when
1877 /// `canFoldIntoConsumerOp` is true.
1878 ///
1879 /// Example:
1880 /// ```
1881 ///   %0 = memref.cast %V : memref<16x16xf32> to memref<?x?xf32>
1882 ///   %1 = memref.subview %0[0, 0][3, 4][1, 1] :
1883 ///     memref<?x?xf32> to memref<3x4xf32, offset:?, strides:[?, 1]>
1884 /// ```
1885 /// is rewritten into:
1886 /// ```
1887 ///   %0 = memref.subview %V: memref<16x16xf32> to memref<3x4xf32, #[[map0]]>
1888 ///   %1 = memref.cast %0: memref<3x4xf32, offset:0, strides:[16, 1]> to
1889 ///     memref<3x4xf32, offset:?, strides:[?, 1]>
1890 /// ```
1891 class SubViewOpMemRefCastFolder final : public OpRewritePattern<SubViewOp> {
1892 public:
1893   using OpRewritePattern<SubViewOp>::OpRewritePattern;
1894 
1895   LogicalResult matchAndRewrite(SubViewOp subViewOp,
1896                                 PatternRewriter &rewriter) const override {
1897     // Any constant operand, just return to let SubViewOpConstantFolder kick in.
1898     if (llvm::any_of(subViewOp.getOperands(), [](Value operand) {
1899           return matchPattern(operand, matchConstantIndex());
1900         }))
1901       return failure();
1902 
1903     auto castOp = subViewOp.source().getDefiningOp<CastOp>();
1904     if (!castOp)
1905       return failure();
1906 
1907     if (!CastOp::canFoldIntoConsumerOp(castOp))
1908       return failure();
1909 
1910     /// Deduce the resultType of the SubViewOp using `inferSubViewResultType` on
1911     /// the cast source operand type and the SubViewOp static information. This
1912     /// is the resulting type if the MemRefCastOp were folded.
1913     auto resultType = getCanonicalSubViewResultType(
1914         subViewOp.getType().getRank(),
1915         castOp.source().getType().cast<MemRefType>(),
1916         subViewOp.getMixedOffsets(), subViewOp.getMixedSizes(),
1917         subViewOp.getMixedStrides());
1918     Value newSubView = rewriter.create<SubViewOp>(
1919         subViewOp.getLoc(), resultType, castOp.source(), subViewOp.offsets(),
1920         subViewOp.sizes(), subViewOp.strides(), subViewOp.static_offsets(),
1921         subViewOp.static_sizes(), subViewOp.static_strides());
1922     rewriter.replaceOpWithNewOp<CastOp>(subViewOp, subViewOp.getType(),
1923                                         newSubView);
1924     return success();
1925   }
1926 };
1927 } // namespace
1928 
1929 /// Return the canonical type of the result of a subview.
1930 struct SubViewReturnTypeCanonicalizer {
1931   MemRefType operator()(SubViewOp op, ArrayRef<OpFoldResult> mixedOffsets,
1932                         ArrayRef<OpFoldResult> mixedSizes,
1933                         ArrayRef<OpFoldResult> mixedStrides) {
1934     return getCanonicalSubViewResultType(op.getType().getRank(),
1935                                          op.getSourceType(), mixedOffsets,
1936                                          mixedSizes, mixedStrides);
1937   }
1938 };
1939 
1940 /// A canonicalizer wrapper to replace SubViewOps.
1941 struct SubViewCanonicalizer {
1942   void operator()(PatternRewriter &rewriter, SubViewOp op, SubViewOp newOp) {
1943     rewriter.replaceOpWithNewOp<CastOp>(op, newOp, op.getType());
1944   }
1945 };
1946 
1947 void SubViewOp::getCanonicalizationPatterns(RewritePatternSet &results,
1948                                             MLIRContext *context) {
1949   results
1950       .add<OpWithOffsetSizesAndStridesConstantArgumentFolder<
1951                SubViewOp, SubViewReturnTypeCanonicalizer, SubViewCanonicalizer>,
1952            SubViewOpMemRefCastFolder>(context);
1953 }
1954 
1955 OpFoldResult SubViewOp::fold(ArrayRef<Attribute> operands) {
1956   auto resultShapedType = getResult().getType().cast<ShapedType>();
1957   auto sourceShapedType = source().getType().cast<ShapedType>();
1958 
1959   if (resultShapedType.hasStaticShape() &&
1960       resultShapedType == sourceShapedType) {
1961     return getViewSource();
1962   }
1963 
1964   return {};
1965 }
1966 
1967 //===----------------------------------------------------------------------===//
1968 // TransposeOp
1969 //===----------------------------------------------------------------------===//
1970 
1971 /// Build a strided memref type by applying `permutationMap` tp `memRefType`.
1972 static MemRefType inferTransposeResultType(MemRefType memRefType,
1973                                            AffineMap permutationMap) {
1974   auto rank = memRefType.getRank();
1975   auto originalSizes = memRefType.getShape();
1976   // Compute permuted sizes.
1977   SmallVector<int64_t, 4> sizes(rank, 0);
1978   for (auto en : llvm::enumerate(permutationMap.getResults()))
1979     sizes[en.index()] =
1980         originalSizes[en.value().cast<AffineDimExpr>().getPosition()];
1981 
1982   // Compute permuted strides.
1983   int64_t offset;
1984   SmallVector<int64_t, 4> strides;
1985   auto res = getStridesAndOffset(memRefType, strides, offset);
1986   assert(succeeded(res) && strides.size() == static_cast<unsigned>(rank));
1987   (void)res;
1988   auto map =
1989       makeStridedLinearLayoutMap(strides, offset, memRefType.getContext());
1990   map = permutationMap ? map.compose(permutationMap) : map;
1991   return MemRefType::Builder(memRefType)
1992       .setShape(sizes)
1993       .setLayout(AffineMapAttr::get(map));
1994 }
1995 
1996 void TransposeOp::build(OpBuilder &b, OperationState &result, Value in,
1997                         AffineMapAttr permutation,
1998                         ArrayRef<NamedAttribute> attrs) {
1999   auto permutationMap = permutation.getValue();
2000   assert(permutationMap);
2001 
2002   auto memRefType = in.getType().cast<MemRefType>();
2003   // Compute result type.
2004   MemRefType resultType = inferTransposeResultType(memRefType, permutationMap);
2005 
2006   build(b, result, resultType, in, attrs);
2007   result.addAttribute(TransposeOp::getPermutationAttrName(), permutation);
2008 }
2009 
2010 // transpose $in $permutation attr-dict : type($in) `to` type(results)
2011 static void print(OpAsmPrinter &p, TransposeOp op) {
2012   p << " " << op.in() << " " << op.permutation();
2013   p.printOptionalAttrDict(op->getAttrs(),
2014                           {TransposeOp::getPermutationAttrName()});
2015   p << " : " << op.in().getType() << " to " << op.getType();
2016 }
2017 
2018 static ParseResult parseTransposeOp(OpAsmParser &parser,
2019                                     OperationState &result) {
2020   OpAsmParser::OperandType in;
2021   AffineMap permutation;
2022   MemRefType srcType, dstType;
2023   if (parser.parseOperand(in) || parser.parseAffineMap(permutation) ||
2024       parser.parseOptionalAttrDict(result.attributes) ||
2025       parser.parseColonType(srcType) ||
2026       parser.resolveOperand(in, srcType, result.operands) ||
2027       parser.parseKeywordType("to", dstType) ||
2028       parser.addTypeToList(dstType, result.types))
2029     return failure();
2030 
2031   result.addAttribute(TransposeOp::getPermutationAttrName(),
2032                       AffineMapAttr::get(permutation));
2033   return success();
2034 }
2035 
2036 static LogicalResult verify(TransposeOp op) {
2037   if (!op.permutation().isPermutation())
2038     return op.emitOpError("expected a permutation map");
2039   if (op.permutation().getNumDims() != op.getShapedType().getRank())
2040     return op.emitOpError(
2041         "expected a permutation map of same rank as the input");
2042 
2043   auto srcType = op.in().getType().cast<MemRefType>();
2044   auto dstType = op.getType().cast<MemRefType>();
2045   auto transposedType = inferTransposeResultType(srcType, op.permutation());
2046   if (dstType != transposedType)
2047     return op.emitOpError("output type ")
2048            << dstType << " does not match transposed input type " << srcType
2049            << ", " << transposedType;
2050   return success();
2051 }
2052 
2053 OpFoldResult TransposeOp::fold(ArrayRef<Attribute>) {
2054   if (succeeded(foldMemRefCast(*this)))
2055     return getResult();
2056   return {};
2057 }
2058 
2059 //===----------------------------------------------------------------------===//
2060 // ViewOp
2061 //===----------------------------------------------------------------------===//
2062 
2063 static ParseResult parseViewOp(OpAsmParser &parser, OperationState &result) {
2064   OpAsmParser::OperandType srcInfo;
2065   SmallVector<OpAsmParser::OperandType, 1> offsetInfo;
2066   SmallVector<OpAsmParser::OperandType, 4> sizesInfo;
2067   auto indexType = parser.getBuilder().getIndexType();
2068   Type srcType, dstType;
2069   llvm::SMLoc offsetLoc;
2070   if (parser.parseOperand(srcInfo) || parser.getCurrentLocation(&offsetLoc) ||
2071       parser.parseOperandList(offsetInfo, OpAsmParser::Delimiter::Square))
2072     return failure();
2073 
2074   if (offsetInfo.size() != 1)
2075     return parser.emitError(offsetLoc) << "expects 1 offset operand";
2076 
2077   return failure(
2078       parser.parseOperandList(sizesInfo, OpAsmParser::Delimiter::Square) ||
2079       parser.parseOptionalAttrDict(result.attributes) ||
2080       parser.parseColonType(srcType) ||
2081       parser.resolveOperand(srcInfo, srcType, result.operands) ||
2082       parser.resolveOperands(offsetInfo, indexType, result.operands) ||
2083       parser.resolveOperands(sizesInfo, indexType, result.operands) ||
2084       parser.parseKeywordType("to", dstType) ||
2085       parser.addTypeToList(dstType, result.types));
2086 }
2087 
2088 static void print(OpAsmPrinter &p, ViewOp op) {
2089   p << ' ' << op.getOperand(0) << '[';
2090   p.printOperand(op.byte_shift());
2091   p << "][" << op.sizes() << ']';
2092   p.printOptionalAttrDict(op->getAttrs());
2093   p << " : " << op.getOperand(0).getType() << " to " << op.getType();
2094 }
2095 
2096 static LogicalResult verify(ViewOp op) {
2097   auto baseType = op.getOperand(0).getType().cast<MemRefType>();
2098   auto viewType = op.getType();
2099 
2100   // The base memref should have identity layout map (or none).
2101   if (!baseType.getLayout().isIdentity())
2102     return op.emitError("unsupported map for base memref type ") << baseType;
2103 
2104   // The result memref should have identity layout map (or none).
2105   if (!viewType.getLayout().isIdentity())
2106     return op.emitError("unsupported map for result memref type ") << viewType;
2107 
2108   // The base memref and the view memref should be in the same memory space.
2109   if (baseType.getMemorySpace() != viewType.getMemorySpace())
2110     return op.emitError("different memory spaces specified for base memref "
2111                         "type ")
2112            << baseType << " and view memref type " << viewType;
2113 
2114   // Verify that we have the correct number of sizes for the result type.
2115   unsigned numDynamicDims = viewType.getNumDynamicDims();
2116   if (op.sizes().size() != numDynamicDims)
2117     return op.emitError("incorrect number of size operands for type ")
2118            << viewType;
2119 
2120   return success();
2121 }
2122 
2123 Value ViewOp::getViewSource() { return source(); }
2124 
2125 namespace {
2126 
2127 struct ViewOpShapeFolder : public OpRewritePattern<ViewOp> {
2128   using OpRewritePattern<ViewOp>::OpRewritePattern;
2129 
2130   LogicalResult matchAndRewrite(ViewOp viewOp,
2131                                 PatternRewriter &rewriter) const override {
2132     // Return if none of the operands are constants.
2133     if (llvm::none_of(viewOp.getOperands(), [](Value operand) {
2134           return matchPattern(operand, matchConstantIndex());
2135         }))
2136       return failure();
2137 
2138     // Get result memref type.
2139     auto memrefType = viewOp.getType();
2140 
2141     // Get offset from old memref view type 'memRefType'.
2142     int64_t oldOffset;
2143     SmallVector<int64_t, 4> oldStrides;
2144     if (failed(getStridesAndOffset(memrefType, oldStrides, oldOffset)))
2145       return failure();
2146     assert(oldOffset == 0 && "Expected 0 offset");
2147 
2148     SmallVector<Value, 4> newOperands;
2149 
2150     // Offset cannot be folded into result type.
2151 
2152     // Fold any dynamic dim operands which are produced by a constant.
2153     SmallVector<int64_t, 4> newShapeConstants;
2154     newShapeConstants.reserve(memrefType.getRank());
2155 
2156     unsigned dynamicDimPos = 0;
2157     unsigned rank = memrefType.getRank();
2158     for (unsigned dim = 0, e = rank; dim < e; ++dim) {
2159       int64_t dimSize = memrefType.getDimSize(dim);
2160       // If this is already static dimension, keep it.
2161       if (!ShapedType::isDynamic(dimSize)) {
2162         newShapeConstants.push_back(dimSize);
2163         continue;
2164       }
2165       auto *defOp = viewOp.sizes()[dynamicDimPos].getDefiningOp();
2166       if (auto constantIndexOp =
2167               dyn_cast_or_null<arith::ConstantIndexOp>(defOp)) {
2168         // Dynamic shape dimension will be folded.
2169         newShapeConstants.push_back(constantIndexOp.value());
2170       } else {
2171         // Dynamic shape dimension not folded; copy operand from old memref.
2172         newShapeConstants.push_back(dimSize);
2173         newOperands.push_back(viewOp.sizes()[dynamicDimPos]);
2174       }
2175       dynamicDimPos++;
2176     }
2177 
2178     // Create new memref type with constant folded dims.
2179     MemRefType newMemRefType =
2180         MemRefType::Builder(memrefType).setShape(newShapeConstants);
2181     // Nothing new, don't fold.
2182     if (newMemRefType == memrefType)
2183       return failure();
2184 
2185     // Create new ViewOp.
2186     auto newViewOp = rewriter.create<ViewOp>(viewOp.getLoc(), newMemRefType,
2187                                              viewOp.getOperand(0),
2188                                              viewOp.byte_shift(), newOperands);
2189     // Insert a cast so we have the same type as the old memref type.
2190     rewriter.replaceOpWithNewOp<CastOp>(viewOp, newViewOp, viewOp.getType());
2191     return success();
2192   }
2193 };
2194 
2195 struct ViewOpMemrefCastFolder : public OpRewritePattern<ViewOp> {
2196   using OpRewritePattern<ViewOp>::OpRewritePattern;
2197 
2198   LogicalResult matchAndRewrite(ViewOp viewOp,
2199                                 PatternRewriter &rewriter) const override {
2200     Value memrefOperand = viewOp.getOperand(0);
2201     CastOp memrefCastOp = memrefOperand.getDefiningOp<CastOp>();
2202     if (!memrefCastOp)
2203       return failure();
2204     Value allocOperand = memrefCastOp.getOperand();
2205     AllocOp allocOp = allocOperand.getDefiningOp<AllocOp>();
2206     if (!allocOp)
2207       return failure();
2208     rewriter.replaceOpWithNewOp<ViewOp>(viewOp, viewOp.getType(), allocOperand,
2209                                         viewOp.byte_shift(), viewOp.sizes());
2210     return success();
2211   }
2212 };
2213 
2214 } // end anonymous namespace
2215 
2216 void ViewOp::getCanonicalizationPatterns(RewritePatternSet &results,
2217                                          MLIRContext *context) {
2218   results.add<ViewOpShapeFolder, ViewOpMemrefCastFolder>(context);
2219 }
2220 
2221 //===----------------------------------------------------------------------===//
2222 // TableGen'd op method definitions
2223 //===----------------------------------------------------------------------===//
2224 
2225 #define GET_OP_CLASSES
2226 #include "mlir/Dialect/MemRef/IR/MemRefOps.cpp.inc"
2227