MLIR  17.0.0git
MemRefBuilder.h
Go to the documentation of this file.
1 //===- MemRefBuilder.h - Helper for LLVM MemRef equivalents -----*- 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 // Provides a convenience API for emitting IR that inspects or constructs values
10 // of LLVM dialect structure type that correspond to ranked or unranked memref.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_CONVERSION_LLVMCOMMON_MEMREFBUILDER_H
15 #define MLIR_CONVERSION_LLVMCOMMON_MEMREFBUILDER_H
16 
19 
20 namespace mlir {
21 
22 class LLVMTypeConverter;
23 class MemRefType;
24 class UnrankedMemRefType;
25 
26 namespace LLVM {
27 class LLVMPointerType;
28 } // namespace LLVM
29 
30 /// Helper class to produce LLVM dialect operations extracting or inserting
31 /// elements of a MemRef descriptor. Wraps a Value pointing to the descriptor.
32 /// The Value may be null, in which case none of the operations are valid.
34 public:
35  /// Construct a helper for the given descriptor value.
36  explicit MemRefDescriptor(Value descriptor);
37  /// Builds IR creating an `undef` value of the descriptor type.
38  static MemRefDescriptor undef(OpBuilder &builder, Location loc,
39  Type descriptorType);
40  /// Builds IR creating a MemRef descriptor that represents `type` and
41  /// populates it with static shape and stride information extracted from the
42  /// type.
44  LLVMTypeConverter &typeConverter,
45  MemRefType type, Value memory);
47  LLVMTypeConverter &typeConverter,
48  MemRefType type, Value memory,
49  Value alignedMemory);
50 
51  /// Builds IR extracting the allocated pointer from the descriptor.
52  Value allocatedPtr(OpBuilder &builder, Location loc);
53  /// Builds IR inserting the allocated pointer into the descriptor.
54  void setAllocatedPtr(OpBuilder &builder, Location loc, Value ptr);
55 
56  /// Builds IR extracting the aligned pointer from the descriptor.
57  Value alignedPtr(OpBuilder &builder, Location loc);
58 
59  /// Builds IR inserting the aligned pointer into the descriptor.
60  void setAlignedPtr(OpBuilder &builder, Location loc, Value ptr);
61 
62  /// Builds IR extracting the offset from the descriptor.
63  Value offset(OpBuilder &builder, Location loc);
64 
65  /// Builds IR inserting the offset into the descriptor.
66  void setOffset(OpBuilder &builder, Location loc, Value offset);
67  void setConstantOffset(OpBuilder &builder, Location loc, uint64_t offset);
68 
69  /// Builds IR extracting the pos-th size from the descriptor.
70  Value size(OpBuilder &builder, Location loc, unsigned pos);
71  Value size(OpBuilder &builder, Location loc, Value pos, int64_t rank);
72 
73  /// Builds IR inserting the pos-th size into the descriptor
74  void setSize(OpBuilder &builder, Location loc, unsigned pos, Value size);
75  void setConstantSize(OpBuilder &builder, Location loc, unsigned pos,
76  uint64_t size);
77 
78  /// Builds IR extracting the pos-th size from the descriptor.
79  Value stride(OpBuilder &builder, Location loc, unsigned pos);
80 
81  /// Builds IR inserting the pos-th stride into the descriptor
82  void setStride(OpBuilder &builder, Location loc, unsigned pos, Value stride);
83  void setConstantStride(OpBuilder &builder, Location loc, unsigned pos,
84  uint64_t stride);
85 
86  /// Returns the type of array element in this descriptor.
87  Type getIndexType() { return indexType; };
88 
89  /// Returns the (LLVM) pointer type this descriptor contains.
90  LLVM::LLVMPointerType getElementPtrType();
91 
92  /// Builds IR populating a MemRef descriptor structure from a list of
93  /// individual values composing that descriptor, in the following order:
94  /// - allocated pointer;
95  /// - aligned pointer;
96  /// - offset;
97  /// - <rank> sizes;
98  /// - <rank> shapes;
99  /// where <rank> is the MemRef rank as provided in `type`.
100  static Value pack(OpBuilder &builder, Location loc,
101  LLVMTypeConverter &converter, MemRefType type,
102  ValueRange values);
103 
104  /// Builds IR extracting individual elements of a MemRef descriptor structure
105  /// and returning them as `results` list.
106  static void unpack(OpBuilder &builder, Location loc, Value packed,
107  MemRefType type, SmallVectorImpl<Value> &results);
108 
109  /// Returns the number of non-aggregate values that would be produced by
110  /// `unpack`.
111  static unsigned getNumUnpackedValues(MemRefType type);
112 
113 private:
114  // Cached index type.
115  Type indexType;
116 };
117 
118 /// Helper class allowing the user to access a range of Values that correspond
119 /// to an unpacked memref descriptor using named accessors. This does not own
120 /// the values.
122 public:
123  /// Constructs the view from a range of values. Infers the rank from the size
124  /// of the range.
125  explicit MemRefDescriptorView(ValueRange range);
126 
127  /// Returns the allocated pointer Value.
129 
130  /// Returns the aligned pointer Value.
131  Value alignedPtr();
132 
133  /// Returns the offset Value.
134  Value offset();
135 
136  /// Returns the pos-th size Value.
137  Value size(unsigned pos);
138 
139  /// Returns the pos-th stride Value.
140  Value stride(unsigned pos);
141 
142 private:
143  /// Rank of the memref the descriptor is pointing to.
144  int rank;
145  /// Underlying range of Values.
146  ValueRange elements;
147 };
148 
150 public:
151  /// Construct a helper for the given descriptor value.
152  explicit UnrankedMemRefDescriptor(Value descriptor);
153  /// Builds IR creating an `undef` value of the descriptor type.
154  static UnrankedMemRefDescriptor undef(OpBuilder &builder, Location loc,
155  Type descriptorType);
156 
157  /// Builds IR extracting the rank from the descriptor
158  Value rank(OpBuilder &builder, Location loc);
159  /// Builds IR setting the rank in the descriptor
160  void setRank(OpBuilder &builder, Location loc, Value value);
161  /// Builds IR extracting ranked memref descriptor ptr
162  Value memRefDescPtr(OpBuilder &builder, Location loc);
163  /// Builds IR setting ranked memref descriptor ptr
164  void setMemRefDescPtr(OpBuilder &builder, Location loc, Value value);
165 
166  /// Builds IR populating an unranked MemRef descriptor structure from a list
167  /// of individual constituent values in the following order:
168  /// - rank of the memref;
169  /// - pointer to the memref descriptor.
170  static Value pack(OpBuilder &builder, Location loc,
171  LLVMTypeConverter &converter, UnrankedMemRefType type,
172  ValueRange values);
173 
174  /// Builds IR extracting individual elements that compose an unranked memref
175  /// descriptor and returns them as `results` list.
176  static void unpack(OpBuilder &builder, Location loc, Value packed,
177  SmallVectorImpl<Value> &results);
178 
179  /// Returns the number of non-aggregate values that would be produced by
180  /// `unpack`.
181  static unsigned getNumUnpackedValues() { return 2; }
182 
183  /// Builds IR computing the sizes in bytes (suitable for opaque allocation)
184  /// and appends the corresponding values into `sizes`.
185  static void computeSizes(OpBuilder &builder, Location loc,
186  LLVMTypeConverter &typeConverter,
188  SmallVectorImpl<Value> &sizes);
189 
190  /// TODO: The following accessors don't take alignment rules between elements
191  /// of the descriptor struct into account. For some architectures, it might be
192  /// necessary to extend them and to use `llvm::DataLayout` contained in
193  /// `LLVMTypeConverter`.
194 
195  /// Builds IR extracting the allocated pointer from the descriptor.
196  static Value allocatedPtr(OpBuilder &builder, Location loc,
197  Value memRefDescPtr, Type elemPtrPtrType);
198  /// Builds IR inserting the allocated pointer into the descriptor.
199  static void setAllocatedPtr(OpBuilder &builder, Location loc,
200  Value memRefDescPtr, Type elemPtrPtrType,
202 
203  /// Builds IR extracting the aligned pointer from the descriptor.
204  static Value alignedPtr(OpBuilder &builder, Location loc,
205  LLVMTypeConverter &typeConverter, Value memRefDescPtr,
206  Type elemPtrPtrType);
207  /// Builds IR inserting the aligned pointer into the descriptor.
208  static void setAlignedPtr(OpBuilder &builder, Location loc,
209  LLVMTypeConverter &typeConverter,
210  Value memRefDescPtr, Type elemPtrPtrType,
211  Value alignedPtr);
212 
213  /// Builds IR extracting the offset from the descriptor.
214  static Value offset(OpBuilder &builder, Location loc,
215  LLVMTypeConverter &typeConverter, Value memRefDescPtr,
216  Type elemPtrPtrType);
217  /// Builds IR inserting the offset into the descriptor.
218  static void setOffset(OpBuilder &builder, Location loc,
219  LLVMTypeConverter &typeConverter, Value memRefDescPtr,
220  Type elemPtrPtrType, Value offset);
221 
222  /// Builds IR extracting the pointer to the first element of the size array.
223  static Value sizeBasePtr(OpBuilder &builder, Location loc,
224  LLVMTypeConverter &typeConverter,
226  LLVM::LLVMPointerType elemPtrPtrType);
227  /// Builds IR extracting the size[index] from the descriptor.
228  static Value size(OpBuilder &builder, Location loc,
229  LLVMTypeConverter &typeConverter, Value sizeBasePtr,
230  Value index);
231  /// Builds IR inserting the size[index] into the descriptor.
232  static void setSize(OpBuilder &builder, Location loc,
233  LLVMTypeConverter &typeConverter, Value sizeBasePtr,
234  Value index, Value size);
235 
236  /// Builds IR extracting the pointer to the first element of the stride array.
237  static Value strideBasePtr(OpBuilder &builder, Location loc,
238  LLVMTypeConverter &typeConverter,
240  /// Builds IR extracting the stride[index] from the descriptor.
241  static Value stride(OpBuilder &builder, Location loc,
242  LLVMTypeConverter &typeConverter, Value strideBasePtr,
243  Value index, Value stride);
244  /// Builds IR inserting the stride[index] into the descriptor.
245  static void setStride(OpBuilder &builder, Location loc,
246  LLVMTypeConverter &typeConverter, Value strideBasePtr,
247  Value index, Value stride);
248 };
249 
250 } // namespace mlir
251 
252 #endif // MLIR_CONVERSION_LLVMCOMMON_MEMREFBUILDER_H
Conversion from types to the LLVM IR dialect.
Definition: TypeConverter.h:30
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
Helper class allowing the user to access a range of Values that correspond to an unpacked memref desc...
Value size(unsigned pos)
Returns the pos-th size Value.
Value alignedPtr()
Returns the aligned pointer Value.
Value stride(unsigned pos)
Returns the pos-th stride Value.
Value allocatedPtr()
Returns the allocated pointer Value.
MemRefDescriptorView(ValueRange range)
Constructs the view from a range of values.
Value offset()
Returns the offset Value.
Helper class to produce LLVM dialect operations extracting or inserting elements of a MemRef descript...
Definition: MemRefBuilder.h:33
Type getIndexType()
Returns the type of array element in this descriptor.
Definition: MemRefBuilder.h:87
void setConstantSize(OpBuilder &builder, Location loc, unsigned pos, uint64_t size)
void setSize(OpBuilder &builder, Location loc, unsigned pos, Value size)
Builds IR inserting the pos-th size into the descriptor.
Value size(OpBuilder &builder, Location loc, unsigned pos)
Builds IR extracting the pos-th size from the descriptor.
Value offset(OpBuilder &builder, Location loc)
Builds IR extracting the offset from the descriptor.
Value allocatedPtr(OpBuilder &builder, Location loc)
Builds IR extracting the allocated pointer from the descriptor.
static Value pack(OpBuilder &builder, Location loc, LLVMTypeConverter &converter, MemRefType type, ValueRange values)
Builds IR populating a MemRef descriptor structure from a list of individual values composing that de...
static MemRefDescriptor undef(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating an undef value of the descriptor type.
static unsigned getNumUnpackedValues(MemRefType type)
Returns the number of non-aggregate values that would be produced by unpack.
Value stride(OpBuilder &builder, Location loc, unsigned pos)
Builds IR extracting the pos-th size from the descriptor.
void setOffset(OpBuilder &builder, Location loc, Value offset)
Builds IR inserting the offset into the descriptor.
void setStride(OpBuilder &builder, Location loc, unsigned pos, Value stride)
Builds IR inserting the pos-th stride into the descriptor.
void setAlignedPtr(OpBuilder &builder, Location loc, Value ptr)
Builds IR inserting the aligned pointer into the descriptor.
static MemRefDescriptor fromStaticShape(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, MemRefType type, Value memory)
Builds IR creating a MemRef descriptor that represents type and populates it with static shape and st...
LLVM::LLVMPointerType getElementPtrType()
Returns the (LLVM) pointer type this descriptor contains.
void setConstantOffset(OpBuilder &builder, Location loc, uint64_t offset)
Builds IR inserting the offset into the descriptor.
Value alignedPtr(OpBuilder &builder, Location loc)
Builds IR extracting the aligned pointer from the descriptor.
static void unpack(OpBuilder &builder, Location loc, Value packed, MemRefType type, SmallVectorImpl< Value > &results)
Builds IR extracting individual elements of a MemRef descriptor structure and returning them as resul...
void setConstantStride(OpBuilder &builder, Location loc, unsigned pos, uint64_t stride)
MemRefDescriptor(Value descriptor)
Construct a helper for the given descriptor value.
void setAllocatedPtr(OpBuilder &builder, Location loc, Value ptr)
Builds IR inserting the allocated pointer into the descriptor.
This class helps build Operations.
Definition: Builders.h:199
Helper class to produce LLVM dialect operations extracting or inserting values to a struct.
Definition: StructBuilder.h:26
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
void setRank(OpBuilder &builder, Location loc, Value value)
Builds IR setting the rank in the descriptor.
static Value stride(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value strideBasePtr, Value index, Value stride)
Builds IR extracting the stride[index] from the descriptor.
static Value strideBasePtr(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value sizeBasePtr, Value rank)
Builds IR extracting the pointer to the first element of the stride array.
Value rank(OpBuilder &builder, Location loc)
Builds IR extracting the rank from the descriptor.
static void setOffset(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value memRefDescPtr, Type elemPtrPtrType, Value offset)
Builds IR inserting the offset into the descriptor.
static void setSize(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value sizeBasePtr, Value index, Value size)
Builds IR inserting the size[index] into the descriptor.
static UnrankedMemRefDescriptor undef(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating an undef value of the descriptor type.
UnrankedMemRefDescriptor(Value descriptor)
Construct a helper for the given descriptor value.
Value memRefDescPtr(OpBuilder &builder, Location loc)
Builds IR extracting ranked memref descriptor ptr.
static Value sizeBasePtr(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrPtrType)
Builds IR extracting the pointer to the first element of the size array.
static void computeSizes(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, ArrayRef< UnrankedMemRefDescriptor > values, SmallVectorImpl< Value > &sizes)
Builds IR computing the sizes in bytes (suitable for opaque allocation) and appends the corresponding...
static Value offset(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value memRefDescPtr, Type elemPtrPtrType)
Builds IR extracting the offset from the descriptor.
static Value alignedPtr(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value memRefDescPtr, Type elemPtrPtrType)
Builds IR extracting the aligned pointer from the descriptor.
static Value size(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value sizeBasePtr, Value index)
Builds IR extracting the size[index] from the descriptor.
static void setAllocatedPtr(OpBuilder &builder, Location loc, Value memRefDescPtr, Type elemPtrPtrType, Value allocatedPtr)
Builds IR inserting the allocated pointer into the descriptor.
static Value allocatedPtr(OpBuilder &builder, Location loc, Value memRefDescPtr, Type elemPtrPtrType)
TODO: The following accessors don't take alignment rules between elements of the descriptor struct in...
static void unpack(OpBuilder &builder, Location loc, Value packed, SmallVectorImpl< Value > &results)
Builds IR extracting individual elements that compose an unranked memref descriptor and returns them ...
static Value pack(OpBuilder &builder, Location loc, LLVMTypeConverter &converter, UnrankedMemRefType type, ValueRange values)
Builds IR populating an unranked MemRef descriptor structure from a list of individual constituent va...
static unsigned getNumUnpackedValues()
Returns the number of non-aggregate values that would be produced by unpack.
static void setAlignedPtr(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value memRefDescPtr, Type elemPtrPtrType, Value alignedPtr)
Builds IR inserting the aligned pointer into the descriptor.
static void setStride(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value strideBasePtr, Value index, Value stride)
Builds IR inserting the stride[index] into the descriptor.
void setMemRefDescPtr(OpBuilder &builder, Location loc, Value value)
Builds IR setting ranked memref descriptor ptr.
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:350
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:93
Include the generated interface declarations.