19#include "llvm/Support/MathExtras.h"
30 assert(
value !=
nullptr &&
"value cannot be null");
31 indexType = cast<LLVM::LLVMStructType>(
value.getType())
37 Type descriptorType) {
39 Value descriptor = LLVM::PoisonOp::create(builder, loc, descriptorType);
49 MemRefType type,
Value memory) {
50 return fromStaticShape(builder, loc, typeConverter, type, memory, memory);
55 MemRefType type,
Value memory,
Value alignedMemory) {
56 assert(type.hasStaticShape() &&
"unexpected dynamic shape");
59 auto [strides,
offset] = type.getStridesAndOffset();
60 assert(ShapedType::isStatic(
offset) &&
"expected static offset");
61 assert(!llvm::any_of(strides, ShapedType::isDynamic) &&
62 "expected static strides");
64 auto convertedType = typeConverter.convertType(type);
65 assert(convertedType &&
"unexpected failure in memref type conversion");
68 descr.setAllocatedPtr(builder, loc, memory);
69 descr.setAlignedPtr(builder, loc, alignedMemory);
70 descr.setConstantOffset(builder, loc,
offset);
73 for (
unsigned i = 0, e = type.getRank(); i != e; ++i) {
74 descr.setConstantSize(builder, loc, i, type.getDimSize(i));
75 descr.setConstantStride(builder, loc, i, strides[i]);
104 return LLVM::ExtractValueOp::create(builder, loc,
value,
124 return LLVM::ExtractValueOp::create(
131 auto arrayTy = LLVM::LLVMArrayType::get(indexType, rank);
133 auto ptrTy = LLVM::LLVMPointerType::get(builder.
getContext());
137 auto sizes = LLVM::ExtractValueOp::create(
140 auto sizesPtr = LLVM::AllocaOp::create(builder, loc, ptrTy, arrayTy, one,
142 LLVM::StoreOp::create(builder, loc, sizes, sizesPtr);
145 auto resultPtr = LLVM::GEPOp::create(builder, loc, ptrTy, arrayTy, sizesPtr,
147 return LLVM::LoadOp::create(builder, loc, indexType, resultPtr);
153 value = LLVM::InsertValueOp::create(
159 unsigned pos, uint64_t
size) {
166 return LLVM::ExtractValueOp::create(
174 value = LLVM::InsertValueOp::create(
180 unsigned pos, uint64_t
stride) {
186 return cast<LLVM::LLVMPointerType>(
187 cast<LLVM::LLVMStructType>(
value.getType())
196 auto [strides, offsetCst] = type.getStridesAndOffset();
206 ShapedType::isDynamic(offsetCst)
209 Type elementType = converter.convertType(type.getElementType());
210 ptr = LLVM::GEPOp::create(builder, loc,
ptr.getType(), elementType,
ptr,
226 Type llvmType = converter.convertType(type);
234 for (
unsigned i = 0; i < rank; ++i) {
252 results.push_back(d.
alignedPtr(builder, loc));
253 results.push_back(d.
offset(builder, loc));
254 for (
int64_t i = 0; i < rank; ++i)
255 results.push_back(d.
size(builder, loc, i));
256 for (
int64_t i = 0; i < rank; ++i)
257 results.push_back(d.
stride(builder, loc, i));
264 return 3 + 2 * type.getRank();
305 Type descriptorType) {
306 Value descriptor = LLVM::PoisonOp::create(builder, loc, descriptorType);
333 Type llvmType = converter.convertType(type);
347 results.reserve(results.size() + 2);
348 results.push_back(d.
rank(builder, loc));
362 builder, loc, indexType,
373 builder, loc, indexType,
375 Value doublePointerSize =
376 LLVM::MulOp::create(builder, loc, indexType, two, pointerSize);
380 Value doubleRank = LLVM::MulOp::create(builder, loc, indexType, two,
rank);
381 Value doubleRankIncremented =
382 LLVM::AddOp::create(builder, loc, indexType, doubleRank, one);
383 Value rankIndexSize = LLVM::MulOp::create(builder, loc, indexType,
384 doubleRankIncremented, indexSize);
387 Value allocationSize = LLVM::AddOp::create(builder, loc, indexType,
388 doublePointerSize, rankIndexSize);
389 return allocationSize;
394 LLVM::LLVMPointerType elemPtrType) {
395 return LLVM::LoadOp::create(builder, loc, elemPtrType,
memRefDescPtr);
404static std::pair<Value, Type>
406 LLVM::LLVMPointerType elemPtrType) {
407 auto elemPtrPtrType = LLVM::LLVMPointerType::get(builder.
getContext());
408 return {memRefDescPtr, elemPtrPtrType};
414 auto [elementPtrPtr, elemPtrPtrType] =
418 LLVM::GEPOp::create(builder, loc, elemPtrPtrType, elemPtrType,
420 return LLVM::LoadOp::create(builder, loc, elemPtrType, alignedGep);
426 auto [elementPtrPtr, elemPtrPtrType] =
430 LLVM::GEPOp::create(builder, loc, elemPtrPtrType, elemPtrType,
432 LLVM::StoreOp::create(builder, loc,
alignedPtr, alignedGep);
438 auto [elementPtrPtr, elemPtrPtrType] =
441 return LLVM::GEPOp::create(builder, loc, elemPtrPtrType, elemPtrType,
448 LLVM::LLVMPointerType elemPtrType) {
451 return LLVM::LoadOp::create(builder, loc, typeConverter.
getIndexType(),
458 LLVM::LLVMPointerType elemPtrType,
462 LLVM::StoreOp::create(builder, loc,
offset, offsetPtr);
469 Type structTy = LLVM::LLVMStructType::getLiteral(
470 indexTy.
getContext(), {elemPtrType, elemPtrType, indexTy, indexTy});
471 auto resultType = LLVM::LLVMPointerType::get(builder.
getContext());
472 return LLVM::GEPOp::create(builder, loc, resultType, structTy,
memRefDescPtr,
481 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
485 return LLVM::LoadOp::create(builder, loc, indexTy, sizeStoreGep);
493 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
497 LLVM::StoreOp::create(builder, loc,
size, sizeStoreGep);
504 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
506 return LLVM::GEPOp::create(builder, loc, ptrType, indexTy,
sizeBasePtr,
rank);
514 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
516 Value strideStoreGep =
518 return LLVM::LoadOp::create(builder, loc, indexTy, strideStoreGep);
526 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
528 Value strideStoreGep =
530 LLVM::StoreOp::create(builder, loc,
stride, strideStoreGep);
static std::pair< Value, Type > castToElemPtrPtr(OpBuilder &builder, Location loc, Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType)
static constexpr unsigned kSizePosInMemRefDescriptor
static constexpr unsigned kStridePosInMemRefDescriptor
static constexpr unsigned kOffsetPosInMemRefDescriptor
static constexpr unsigned kAllocatedPtrPosInMemRefDescriptor
static constexpr unsigned kPtrInUnrankedMemRefDescriptor
static constexpr unsigned kAlignedPtrPosInMemRefDescriptor
static constexpr unsigned kRankInUnrankedMemRefDescriptor
MLIRContext * getContext() const
Conversion from types to the LLVM IR dialect.
unsigned getPointerBitwidth(unsigned addressSpace=0) const
Gets the pointer bitwidth.
unsigned getIndexTypeBitwidth() const
Gets the bitwidth of the index type when converted to LLVM.
Type getIndexType() const
Gets the LLVM representation of the index type.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
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...
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.
void setPtr(OpBuilder &builder, Location loc, unsigned pos, Value ptr)
Builds IR to set a value in the struct at position pos.
StructBuilder(Value v)
Construct a helper for the given value.
Value extractPtr(OpBuilder &builder, Location loc, unsigned pos) const
Builds IR to extract a value from the struct at position pos.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
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 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.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Value createIndexAttrConstant(OpBuilder &builder, Location loc, Type resultType, int64_t value)
Creates an llvm.mlir.constant producing value as resultType, which is expected to be the converted in...
Include the generated interface declarations.