MLIR  22.0.0git
MemRefUtils.h
Go to the documentation of this file.
1 //===- MemRefUtils.h - MemRef transformation utilities ----------*- C++ -*-===//
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 // This header file defines prototypes for various transformation utilities for
10 // the MemRefOps dialect. These are not passes by themselves but are used
11 // either by passes, optimization sequences, or in turn by other transformation
12 // utilities.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef MLIR_DIALECT_MEMREF_UTILS_MEMREFUTILS_H
17 #define MLIR_DIALECT_MEMREF_UTILS_MEMREFUTILS_H
18 
20 
21 namespace mlir {
22 
23 class MemRefType;
24 
25 /// A value with a memref type.
27 
28 namespace memref {
29 
30 /// Returns true, if the memref type has static shapes and represents a
31 /// contiguous chunk of memory.
32 bool isStaticShapeAndContiguousRowMajor(MemRefType type);
33 
34 /// For a `memref` with `offset`, `sizes` and `strides`, returns the
35 /// offset, size, and potentially the size padded at the front to use for the
36 /// linearized `memref`.
37 /// - If the linearization is done for emulating load/stores of
38 /// element type with bitwidth `srcBits` using element type with
39 /// bitwidth `dstBits`, the linearized offset and size are
40 /// scaled down by `dstBits`/`srcBits`.
41 /// - If `indices` is provided, it represents the position in the
42 /// original `memref` being accessed. The method then returns the
43 /// index to use in the linearized `memref`. The linearized index
44 /// is also scaled down by `dstBits`/`srcBits`. If `indices` is not provided
45 /// 0, is returned for the linearized index.
46 /// - If the size of the load/store is smaller than the linearized memref
47 /// load/store, the memory region emulated is larger than the actual memory
48 /// region needed. `intraDataOffset` returns the element offset of the data
49 /// relevant at the beginning.
54 };
55 std::pair<LinearizedMemRefInfo, OpFoldResult> getLinearizedMemRefOffsetAndSize(
56  OpBuilder &builder, Location loc, int srcBits, int dstBits,
58  ArrayRef<OpFoldResult> strides, ArrayRef<OpFoldResult> indices = {});
59 
60 /// For a `memref` with `offset` and `sizes`, returns the
61 /// offset and size to use for the linearized `memref`, assuming that
62 /// the strides are computed from a row-major ordering of the sizes;
63 /// - If the linearization is done for emulating load/stores of
64 /// element type with bitwidth `srcBits` using element type with
65 /// bitwidth `dstBits`, the linearized offset and size are
66 /// scaled down by `dstBits`/`srcBits`.
67 LinearizedMemRefInfo
68 getLinearizedMemRefOffsetAndSize(OpBuilder &builder, Location loc, int srcBits,
69  int dstBits, OpFoldResult offset,
71 
72 /// Track temporary allocations that are never read from. If this is the case
73 /// it means both the allocations and associated stores can be removed.
74 void eraseDeadAllocAndStores(RewriterBase &rewriter, Operation *parentOp);
75 
76 /// Given a set of sizes, return the suffix product.
77 ///
78 /// When applied to slicing, this is the calculation needed to derive the
79 /// strides (i.e. the number of linear indices to skip along the (k-1) most
80 /// minor dimensions to get the next k-slice).
81 ///
82 /// This is the basis to linearize an n-D offset confined to `[0 ... sizes]`.
83 ///
84 /// Assuming `sizes` is `[s0, .. sn]`, return the vector<Value>
85 /// `[s1 * ... * sn, s2 * ... * sn, ..., sn, 1]`.
86 ///
87 /// It is the caller's responsibility to provide valid OpFoldResult type values
88 /// and construct valid IR in the end.
89 ///
90 /// `sizes` elements are asserted to be non-negative.
91 ///
92 /// Return an empty vector if `sizes` is empty.
93 ///
94 /// The function emits an IR block which computes suffix product for provided
95 /// sizes.
101  ArrayRef<OpFoldResult> sizes) {
102  return computeSuffixProductIRBlock(loc, builder, sizes);
103 }
104 
105 /// Walk up the source chain until an operation that changes/defines the view of
106 /// memory is found (i.e. skip operations that alias the entire view).
108 
109 /// Checks if two (memref) values are the same or statically known to alias
110 /// the same region of memory.
113 }
114 
115 /// Walk up the source chain until we find an operation that is not a view of
116 /// the source memref (i.e. implements ViewLikeOpInterface).
118 
119 /// Given the 'indices' of a load/store operation where the memref is a result
120 /// of a expand_shape op, returns the indices w.r.t to the source memref of the
121 /// expand_shape op. For example
122 ///
123 /// %0 = ... : memref<12x42xf32>
124 /// %1 = memref.expand_shape %0 [[0, 1], [2]]
125 /// : memref<12x42xf32> into memref<2x6x42xf32>
126 /// %2 = load %1[%i1, %i2, %i3] : memref<2x6x42xf32
127 ///
128 /// could be folded into
129 ///
130 /// %2 = load %0[6 * i1 + i2, %i3] :
131 /// memref<12x42xf32>
132 LogicalResult resolveSourceIndicesExpandShape(
133  Location loc, PatternRewriter &rewriter,
134  memref::ExpandShapeOp expandShapeOp, ValueRange indices,
135  SmallVectorImpl<Value> &sourceIndices, bool startsInbounds);
136 
137 /// Given the 'indices' of a load/store operation where the memref is a result
138 /// of a collapse_shape op, returns the indices w.r.t to the source memref of
139 /// the collapse_shape op. For example
140 ///
141 /// %0 = ... : memref<2x6x42xf32>
142 /// %1 = memref.collapse_shape %0 [[0, 1], [2]]
143 /// : memref<2x6x42xf32> into memref<12x42xf32>
144 /// %2 = load %1[%i1, %i2] : memref<12x42xf32>
145 ///
146 /// could be folded into
147 ///
148 /// %2 = load %0[%i1 / 6, %i1 % 6, %i2] :
149 /// memref<2x6x42xf32>
150 LogicalResult
152  memref::CollapseShapeOp collapseShapeOp,
153  ValueRange indices,
154  SmallVectorImpl<Value> &sourceIndices);
155 
156 } // namespace memref
157 } // namespace mlir
158 
159 #endif // MLIR_DIALECT_MEMREF_UTILS_MEMREFUTILS_H
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
This class helps build Operations.
Definition: Builders.h:207
This class represents a single result from folding an operation.
Definition: OpDefinition.h:272
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:783
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:358
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
MemrefValue skipFullyAliasingOperations(MemrefValue source)
Walk up the source chain until an operation that changes/defines the view of memory is found (i....
bool isSameViewOrTrivialAlias(MemrefValue a, MemrefValue b)
Checks if two (memref) values are the same or statically known to alias the same region of memory.
Definition: MemRefUtils.h:111
void eraseDeadAllocAndStores(RewriterBase &rewriter, Operation *parentOp)
Track temporary allocations that are never read from.
LogicalResult resolveSourceIndicesCollapseShape(Location loc, PatternRewriter &rewriter, memref::CollapseShapeOp collapseShapeOp, ValueRange indices, SmallVectorImpl< Value > &sourceIndices)
Given the 'indices' of a load/store operation where the memref is a result of a collapse_shape op,...
std::pair< LinearizedMemRefInfo, OpFoldResult > getLinearizedMemRefOffsetAndSize(OpBuilder &builder, Location loc, int srcBits, int dstBits, OpFoldResult offset, ArrayRef< OpFoldResult > sizes, ArrayRef< OpFoldResult > strides, ArrayRef< OpFoldResult > indices={})
Definition: MemRefUtils.cpp:51
MemrefValue skipViewLikeOps(MemrefValue source)
Walk up the source chain until we find an operation that is not a view of the source memref (i....
bool isStaticShapeAndContiguousRowMajor(MemRefType type)
Returns true, if the memref type has static shapes and represents a contiguous chunk of memory.
Definition: MemRefUtils.cpp:23
SmallVector< OpFoldResult > computeStridesIRBlock(Location loc, OpBuilder &builder, ArrayRef< OpFoldResult > sizes)
Definition: MemRefUtils.h:100
LogicalResult resolveSourceIndicesExpandShape(Location loc, PatternRewriter &rewriter, memref::ExpandShapeOp expandShapeOp, ValueRange indices, SmallVectorImpl< Value > &sourceIndices, bool startsInbounds)
Given the 'indices' of a load/store operation where the memref is a result of a expand_shape op,...
SmallVector< OpFoldResult > computeSuffixProductIRBlock(Location loc, OpBuilder &builder, ArrayRef< OpFoldResult > sizes)
Given a set of sizes, return the suffix product.
Include the generated interface declarations.
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
Definition: Value.h:488
TypedValue< BaseMemRefType > MemrefValue
A value with a memref type.
Definition: MemRefUtils.h:26
For a memref with offset, sizes and strides, returns the offset, size, and potentially the size padde...
Definition: MemRefUtils.h:50