MLIR  19.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 and size to use for the linearized `memref`.
36 /// - If the linearization is done for emulating load/stores of
37 /// element type with bitwidth `srcBits` using element type with
38 /// bitwidth `dstBits`, the linearized offset and size are
39 /// scaled down by `dstBits`/`srcBits`.
40 /// - If `indices` is provided, it represents the position in the
41 /// original `memref` being accessed. The method then returns the
42 /// index to use in the linearized `memref`. The linearized index
43 /// is also scaled down by `dstBits`/`srcBits`. If `indices` is not provided
44 /// 0, is returned for the linearized index.
48 };
49 std::pair<LinearizedMemRefInfo, OpFoldResult> getLinearizedMemRefOffsetAndSize(
50  OpBuilder &builder, Location loc, int srcBits, int dstBits,
52  ArrayRef<OpFoldResult> strides, ArrayRef<OpFoldResult> indices = {});
53 
54 /// For a `memref` with `offset` and `sizes`, returns the
55 /// offset and size to use for the linearized `memref`, assuming that
56 /// the strides are computed from a row-major ordering of the sizes;
57 /// - If the linearization is done for emulating load/stores of
58 /// element type with bitwidth `srcBits` using element type with
59 /// bitwidth `dstBits`, the linearized offset and size are
60 /// scaled down by `dstBits`/`srcBits`.
61 LinearizedMemRefInfo
62 getLinearizedMemRefOffsetAndSize(OpBuilder &builder, Location loc, int srcBits,
63  int dstBits, OpFoldResult offset,
65 
66 // Track temporary allocations that are never read from. If this is the case
67 // it means both the allocations and associated stores can be removed.
68 void eraseDeadAllocAndStores(RewriterBase &rewriter, Operation *parentOp);
69 
70 /// Given a set of sizes, return the suffix product.
71 ///
72 /// When applied to slicing, this is the calculation needed to derive the
73 /// strides (i.e. the number of linear indices to skip along the (k-1) most
74 /// minor dimensions to get the next k-slice).
75 ///
76 /// This is the basis to linearize an n-D offset confined to `[0 ... sizes]`.
77 ///
78 /// Assuming `sizes` is `[s0, .. sn]`, return the vector<Value>
79 /// `[s1 * ... * sn, s2 * ... * sn, ..., sn, 1]`.
80 ///
81 /// It is the caller's responsibility to provide valid OpFoldResult type values
82 /// and construct valid IR in the end.
83 ///
84 /// `sizes` elements are asserted to be non-negative.
85 ///
86 /// Return an empty vector if `sizes` is empty.
87 ///
88 /// The function emits an IR block which computes suffix product for provided
89 /// sizes.
95  ArrayRef<OpFoldResult> sizes) {
96  return computeSuffixProductIRBlock(loc, builder, sizes);
97 }
98 
99 /// Walk up the source chain until an operation that changes/defines the view of
100 /// memory is found (i.e. skip operations that alias the entire view).
102 
103 /// Checks if two (memref) values are the same or are statically known to alias
104 /// the same region of memory.
107 }
108 
109 /// Walk up the source chain until something an op other than a `memref.subview`
110 /// or `memref.cast` is found.
112 
113 } // namespace memref
114 } // namespace mlir
115 
116 #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:63
This class helps build Operations.
Definition: Builders.h:209
This class represents a single result from folding an operation.
Definition: OpDefinition.h:268
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:400
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 are statically known to alias the same region of memory...
Definition: MemRefUtils.h:105
void eraseDeadAllocAndStores(RewriterBase &rewriter, Operation *parentOp)
MemrefValue skipSubViewsAndCasts(MemrefValue source)
Walk up the source chain until something an op other than a memref.subview or memref....
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
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:94
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:498
TypedValue< BaseMemRefType > MemrefValue
A value with a memref type.
Definition: MemRefUtils.h:26
For a memref with offset, sizes and strides, returns the offset and size to use for the linearized me...
Definition: MemRefUtils.h:45