MLIR 22.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
20namespace mlir {
21
23class MemRefType;
24class UnrankedMemRefType;
25
26namespace LLVM {
27class 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.
34public:
35 /// Construct a helper for the given descriptor value.
36 explicit MemRefDescriptor(Value descriptor);
37 /// Builds IR creating a `poison` value of the descriptor type.
38 static MemRefDescriptor poison(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.
43 static MemRefDescriptor
45 const LLVMTypeConverter &typeConverter, MemRefType type,
46 Value memory);
47 static MemRefDescriptor
49 const LLVMTypeConverter &typeConverter, MemRefType type,
50 Value memory, Value alignedMemory);
51
52 /// Builds IR extracting the allocated pointer from the descriptor.
53 Value allocatedPtr(OpBuilder &builder, Location loc);
54 /// Builds IR inserting the allocated pointer into the descriptor.
55 void setAllocatedPtr(OpBuilder &builder, Location loc, Value ptr);
56
57 /// Builds IR extracting the aligned pointer from the descriptor.
58 Value alignedPtr(OpBuilder &builder, Location loc);
59
60 /// Builds IR inserting the aligned pointer into the descriptor.
61 void setAlignedPtr(OpBuilder &builder, Location loc, Value ptr);
62
63 /// Builds IR extracting the offset from the descriptor.
64 Value offset(OpBuilder &builder, Location loc);
65
66 /// Builds IR inserting the offset into the descriptor.
67 void setOffset(OpBuilder &builder, Location loc, Value offset);
68 void setConstantOffset(OpBuilder &builder, Location loc, uint64_t offset);
69
70 /// Builds IR extracting the pos-th size from the descriptor.
71 Value size(OpBuilder &builder, Location loc, unsigned pos);
72 Value size(OpBuilder &builder, Location loc, Value pos, int64_t rank);
73
74 /// Builds IR inserting the pos-th size into the descriptor
75 void setSize(OpBuilder &builder, Location loc, unsigned pos, Value size);
76 void setConstantSize(OpBuilder &builder, Location loc, unsigned pos,
77 uint64_t size);
78
79 /// Builds IR extracting the pos-th size from the descriptor.
80 Value stride(OpBuilder &builder, Location loc, unsigned pos);
81
82 /// Builds IR inserting the pos-th stride into the descriptor
83 void setStride(OpBuilder &builder, Location loc, unsigned pos, Value stride);
84 void setConstantStride(OpBuilder &builder, Location loc, unsigned pos,
85 uint64_t stride);
86
87 /// Returns the type of array element in this descriptor.
88 Type getIndexType() { return indexType; };
89
90 /// Returns the (LLVM) pointer type this descriptor contains.
91 LLVM::LLVMPointerType getElementPtrType();
92
93 /// Builds IR for getting the start address of the buffer represented
94 /// by this memref:
95 /// `memref.alignedPtr + memref.offset * sizeof(type.getElementType())`.
96 /// \note there is no setter for this one since it is derived from alignedPtr
97 /// and offset.
98 Value bufferPtr(OpBuilder &builder, Location loc,
99 const LLVMTypeConverter &converter, MemRefType type);
100
101 /// Builds IR populating a MemRef descriptor structure from a list of
102 /// individual values composing that descriptor, in the following order:
103 /// - allocated pointer;
104 /// - aligned pointer;
105 /// - offset;
106 /// - <rank> sizes;
107 /// - <rank> strides;
108 /// where <rank> is the MemRef rank as provided in `type`.
109 static Value pack(OpBuilder &builder, Location loc,
110 const LLVMTypeConverter &converter, MemRefType type,
111 ValueRange values);
112
113 /// Builds IR extracting individual elements of a MemRef descriptor structure
114 /// and returning them as `results` list.
115 static void unpack(OpBuilder &builder, Location loc, Value packed,
116 MemRefType type, SmallVectorImpl<Value> &results);
117
118 /// Returns the number of non-aggregate values that would be produced by
119 /// `unpack`.
120 static unsigned getNumUnpackedValues(MemRefType type);
121
122private:
123 // Cached index type.
124 Type indexType;
125};
126
127/// Helper class allowing the user to access a range of Values that correspond
128/// to an unpacked memref descriptor using named accessors. This does not own
129/// the values.
131public:
132 /// Constructs the view from a range of values. Infers the rank from the size
133 /// of the range.
134 explicit MemRefDescriptorView(ValueRange range);
135
136 /// Returns the allocated pointer Value.
138
139 /// Returns the aligned pointer Value.
141
142 /// Returns the offset Value.
143 Value offset();
144
145 /// Returns the pos-th size Value.
146 Value size(unsigned pos);
147
148 /// Returns the pos-th stride Value.
149 Value stride(unsigned pos);
150
151private:
152 /// Rank of the memref the descriptor is pointing to.
153 int rank;
154 /// Underlying range of Values.
155 ValueRange elements;
156};
157
159public:
160 /// Construct a helper for the given descriptor value.
161 explicit UnrankedMemRefDescriptor(Value descriptor);
162 /// Builds IR creating an `undef` value of the descriptor type.
164 Type descriptorType);
165
166 /// Builds IR extracting the rank from the descriptor
167 Value rank(OpBuilder &builder, Location loc) const;
168 /// Builds IR setting the rank in the descriptor
169 void setRank(OpBuilder &builder, Location loc, Value value);
170 /// Builds IR extracting ranked memref descriptor ptr
171 Value memRefDescPtr(OpBuilder &builder, Location loc) const;
172 /// Builds IR setting ranked memref descriptor ptr
173 void setMemRefDescPtr(OpBuilder &builder, Location loc, Value value);
174
175 /// Builds IR populating an unranked MemRef descriptor structure from a list
176 /// of individual constituent values in the following order:
177 /// - rank of the memref;
178 /// - pointer to the memref descriptor.
179 static Value pack(OpBuilder &builder, Location loc,
180 const LLVMTypeConverter &converter, UnrankedMemRefType type,
181 ValueRange values);
182
183 /// Builds IR extracting individual elements that compose an unranked memref
184 /// descriptor and returns them as `results` list.
185 static void unpack(OpBuilder &builder, Location loc, Value packed,
186 SmallVectorImpl<Value> &results);
187
188 /// Returns the number of non-aggregate values that would be produced by
189 /// `unpack`.
190 static unsigned getNumUnpackedValues() { return 2; }
191
192 /// Builds and returns IR computing the size in bytes (suitable for opaque
193 /// allocation). `addressSpace` is needed to handle layouts where
194 /// sizeof(ptr addrspace(N)) != sizeof(ptr addrspace(0)).
195 static Value computeSize(OpBuilder &builder, Location loc,
196 const LLVMTypeConverter &typeConverter,
198 unsigned addressSpace);
199
200 /// TODO: The following accessors don't take alignment rules between elements
201 /// of the descriptor struct into account. For some architectures, it might be
202 /// necessary to extend them and to use `llvm::DataLayout` contained in
203 /// `LLVMTypeConverter`.
204
205 /// Builds IR extracting the allocated pointer from the descriptor.
206 static Value allocatedPtr(OpBuilder &builder, Location loc,
208 LLVM::LLVMPointerType elemPtrType);
209 /// Builds IR inserting the allocated pointer into the descriptor.
210 static void setAllocatedPtr(OpBuilder &builder, Location loc,
212 LLVM::LLVMPointerType elemPtrType,
214
215 /// Builds IR extracting the aligned pointer from the descriptor.
216 static Value alignedPtr(OpBuilder &builder, Location loc,
217 const LLVMTypeConverter &typeConverter,
219 LLVM::LLVMPointerType elemPtrType);
220 /// Builds IR inserting the aligned pointer into the descriptor.
221 static void setAlignedPtr(OpBuilder &builder, Location loc,
222 const LLVMTypeConverter &typeConverter,
224 LLVM::LLVMPointerType elemPtrType,
226
227 /// Builds IR for getting the pointer to the offset's location.
228 /// Returns a pointer to a convertType(index), which points to the beggining
229 /// of a struct {index, index[rank], index[rank]}.
230 static Value offsetBasePtr(OpBuilder &builder, Location loc,
231 const LLVMTypeConverter &typeConverter,
233 LLVM::LLVMPointerType elemPtrType);
234 /// Builds IR extracting the offset from the descriptor.
235 static Value offset(OpBuilder &builder, Location loc,
236 const LLVMTypeConverter &typeConverter,
237 Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType);
238 /// Builds IR inserting the offset into the descriptor.
239 static void setOffset(OpBuilder &builder, Location loc,
240 const LLVMTypeConverter &typeConverter,
241 Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType,
242 Value offset);
243
244 /// Builds IR extracting the pointer to the first element of the size array.
245 static Value sizeBasePtr(OpBuilder &builder, Location loc,
246 const LLVMTypeConverter &typeConverter,
248 LLVM::LLVMPointerType elemPtrType);
249 /// Builds IR extracting the size[index] from the descriptor.
250 static Value size(OpBuilder &builder, Location loc,
251 const LLVMTypeConverter &typeConverter, Value sizeBasePtr,
252 Value index);
253 /// Builds IR inserting the size[index] into the descriptor.
254 static void setSize(OpBuilder &builder, Location loc,
255 const LLVMTypeConverter &typeConverter, Value sizeBasePtr,
257
258 /// Builds IR extracting the pointer to the first element of the stride array.
259 static Value strideBasePtr(OpBuilder &builder, Location loc,
260 const LLVMTypeConverter &typeConverter,
262 /// Builds IR extracting the stride[index] from the descriptor.
263 static Value stride(OpBuilder &builder, Location loc,
264 const LLVMTypeConverter &typeConverter,
266 /// Builds IR inserting the stride[index] into the descriptor.
267 static void setStride(OpBuilder &builder, Location loc,
268 const LLVMTypeConverter &typeConverter,
270};
271
272} // namespace mlir
273
274#endif // MLIR_CONVERSION_LLVMCOMMON_MEMREFBUILDER_H
Conversion from types to the LLVM IR dialect.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
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.
Type getIndexType()
Returns the type of array element in this descriptor.
Value bufferPtr(OpBuilder &builder, Location loc, const LLVMTypeConverter &converter, MemRefType type)
Builds IR for getting the start address of the buffer represented by this memref: memref....
Value alignedPtr(OpBuilder &builder, Location loc)
Builds IR extracting the aligned pointer from the descriptor.
void setOffset(OpBuilder &builder, Location loc, Value offset)
Builds IR inserting the offset into the descriptor.
LLVM::LLVMPointerType getElementPtrType()
Returns the (LLVM) pointer type this descriptor contains.
static MemRefDescriptor fromStaticShape(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, MemRefType type, Value memory)
Builds IR creating a MemRef descriptor that represents type and populates it with static shape and st...
Value stride(OpBuilder &builder, Location loc, unsigned pos)
Builds IR extracting the pos-th size from the descriptor.
void setConstantSize(OpBuilder &builder, Location loc, unsigned pos, uint64_t size)
MemRefDescriptor(Value descriptor)
Construct a helper for the given descriptor value.
static MemRefDescriptor poison(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating a poison value of the descriptor type.
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...
static unsigned getNumUnpackedValues(MemRefType type)
Returns the number of non-aggregate values that would be produced by unpack.
void setSize(OpBuilder &builder, Location loc, unsigned pos, Value size)
Builds IR inserting the pos-th size into the descriptor.
void setAllocatedPtr(OpBuilder &builder, Location loc, Value ptr)
Builds IR inserting the allocated pointer into the descriptor.
void setStride(OpBuilder &builder, Location loc, unsigned pos, Value stride)
Builds IR inserting the pos-th stride into the descriptor.
Value allocatedPtr(OpBuilder &builder, Location loc)
Builds IR extracting the allocated pointer from the descriptor.
Value offset(OpBuilder &builder, Location loc)
Builds IR extracting the offset from the descriptor.
void setConstantStride(OpBuilder &builder, Location loc, unsigned pos, uint64_t stride)
void setAlignedPtr(OpBuilder &builder, Location loc, Value ptr)
Builds IR inserting the aligned pointer into the descriptor.
Value size(OpBuilder &builder, Location loc, unsigned pos)
Builds IR extracting the pos-th size from the descriptor.
void setConstantOffset(OpBuilder &builder, Location loc, uint64_t offset)
Builds IR inserting the offset into the descriptor.
static Value pack(OpBuilder &builder, Location loc, const LLVMTypeConverter &converter, MemRefType type, ValueRange values)
Builds IR populating a MemRef descriptor structure from a list of individual values composing that de...
This class helps build Operations.
Definition Builders.h:207
StructBuilder(Value v)
Construct a helper for the given value.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
static void setOffset(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType, Value offset)
Builds IR inserting the offset into the descriptor.
static Value allocatedPtr(OpBuilder &builder, Location loc, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType)
TODO: The following accessors don't take alignment rules between elements of the descriptor struct in...
static Value computeSize(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, UnrankedMemRefDescriptor desc, unsigned addressSpace)
Builds and returns IR computing the size in bytes (suitable for opaque allocation).
void setRank(OpBuilder &builder, Location loc, Value value)
Builds IR setting the rank in the descriptor.
Value memRefDescPtr(OpBuilder &builder, Location loc) const
Builds IR extracting ranked memref descriptor ptr.
static void setAllocatedPtr(OpBuilder &builder, Location loc, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType, Value allocatedPtr)
Builds IR inserting the allocated pointer into the descriptor.
static void setSize(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value sizeBasePtr, Value index, Value size)
Builds IR inserting the size[index] into the descriptor.
static Value pack(OpBuilder &builder, Location loc, const LLVMTypeConverter &converter, UnrankedMemRefType type, ValueRange values)
Builds IR populating an unranked MemRef descriptor structure from a list of individual constituent va...
static Value stride(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value strideBasePtr, Value index, Value stride)
Builds IR extracting the stride[index] from the descriptor.
static Value size(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value sizeBasePtr, Value index)
Builds IR extracting the size[index] from the descriptor.
UnrankedMemRefDescriptor(Value descriptor)
Construct a helper for the given descriptor value.
static UnrankedMemRefDescriptor poison(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating an undef value of the descriptor type.
static void setAlignedPtr(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType, Value alignedPtr)
Builds IR inserting the aligned pointer into the descriptor.
Value rank(OpBuilder &builder, Location loc) const
Builds IR extracting the rank from the descriptor.
static Value offsetBasePtr(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType)
Builds IR for getting the pointer to the offset's location.
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 unsigned getNumUnpackedValues()
Returns the number of non-aggregate values that would be produced by unpack.
static Value offset(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType)
Builds IR extracting the offset from the descriptor.
static Value strideBasePtr(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value sizeBasePtr, Value rank)
Builds IR extracting the pointer to the first element of the stride array.
void setMemRefDescPtr(OpBuilder &builder, Location loc, Value value)
Builds IR setting ranked memref descriptor ptr.
static void setStride(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value strideBasePtr, Value index, Value stride)
Builds IR inserting the stride[index] into the descriptor.
static Value sizeBasePtr(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType)
Builds IR extracting the pointer to the first element of the size array.
static Value alignedPtr(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType)
Builds IR extracting the aligned pointer from the descriptor.
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Include the generated interface declarations.