16#include "llvm/Support/MathExtras.h"
27 assert(
value !=
nullptr &&
"value cannot be null");
28 indexType = cast<LLVM::LLVMStructType>(
value.getType())
34 Type descriptorType) {
36 Value descriptor = LLVM::PoisonOp::create(builder, loc, descriptorType);
46 MemRefType type,
Value memory) {
47 return fromStaticShape(builder, loc, typeConverter, type, memory, memory);
52 MemRefType type,
Value memory,
Value alignedMemory) {
53 assert(type.hasStaticShape() &&
"unexpected dynamic shape");
56 auto [strides,
offset] = type.getStridesAndOffset();
57 assert(ShapedType::isStatic(
offset) &&
"expected static offset");
58 assert(!llvm::any_of(strides, ShapedType::isDynamic) &&
59 "expected static strides");
61 auto convertedType = typeConverter.convertType(type);
62 assert(convertedType &&
"unexpected failure in memref type conversion");
65 descr.setAllocatedPtr(builder, loc, memory);
66 descr.setAlignedPtr(builder, loc, alignedMemory);
67 descr.setConstantOffset(builder, loc,
offset);
70 for (
unsigned i = 0, e = type.getRank(); i != e; ++i) {
71 descr.setConstantSize(builder, loc, i, type.getDimSize(i));
72 descr.setConstantStride(builder, loc, i, strides[i]);
101 return LLVM::ExtractValueOp::create(builder, loc,
value,
121 return LLVM::ExtractValueOp::create(
128 auto arrayTy = LLVM::LLVMArrayType::get(indexType, rank);
130 auto ptrTy = LLVM::LLVMPointerType::get(builder.
getContext());
134 auto sizes = LLVM::ExtractValueOp::create(
137 auto sizesPtr = LLVM::AllocaOp::create(builder, loc, ptrTy, arrayTy, one,
139 LLVM::StoreOp::create(builder, loc, sizes, sizesPtr);
142 auto resultPtr = LLVM::GEPOp::create(builder, loc, ptrTy, arrayTy, sizesPtr,
144 return LLVM::LoadOp::create(builder, loc, indexType, resultPtr);
150 value = LLVM::InsertValueOp::create(
156 unsigned pos, uint64_t
size) {
163 return LLVM::ExtractValueOp::create(
171 value = LLVM::InsertValueOp::create(
177 unsigned pos, uint64_t
stride) {
183 return cast<LLVM::LLVMPointerType>(
184 cast<LLVM::LLVMStructType>(
value.getType())
193 auto [strides, offsetCst] = type.getStridesAndOffset();
203 ShapedType::isDynamic(offsetCst)
206 Type elementType = converter.convertType(type.getElementType());
207 ptr = LLVM::GEPOp::create(builder, loc,
ptr.getType(), elementType,
ptr,
223 Type llvmType = converter.convertType(type);
231 for (
unsigned i = 0; i < rank; ++i) {
249 results.push_back(d.
alignedPtr(builder, loc));
250 results.push_back(d.
offset(builder, loc));
251 for (
int64_t i = 0; i < rank; ++i)
252 results.push_back(d.
size(builder, loc, i));
253 for (
int64_t i = 0; i < rank; ++i)
254 results.push_back(d.
stride(builder, loc, i));
261 return 3 + 2 * type.getRank();
302 Type descriptorType) {
303 Value descriptor = LLVM::PoisonOp::create(builder, loc, descriptorType);
330 Type llvmType = converter.convertType(type);
344 results.reserve(results.size() + 2);
345 results.push_back(d.
rank(builder, loc));
359 builder, loc, indexType,
370 builder, loc, indexType,
372 Value doublePointerSize =
373 LLVM::MulOp::create(builder, loc, indexType, two, pointerSize);
377 Value doubleRank = LLVM::MulOp::create(builder, loc, indexType, two,
rank);
378 Value doubleRankIncremented =
379 LLVM::AddOp::create(builder, loc, indexType, doubleRank, one);
380 Value rankIndexSize = LLVM::MulOp::create(builder, loc, indexType,
381 doubleRankIncremented, indexSize);
384 Value allocationSize = LLVM::AddOp::create(builder, loc, indexType,
385 doublePointerSize, rankIndexSize);
386 return allocationSize;
391 LLVM::LLVMPointerType elemPtrType) {
392 return LLVM::LoadOp::create(builder, loc, elemPtrType,
memRefDescPtr);
401static std::pair<Value, Type>
403 LLVM::LLVMPointerType elemPtrType) {
404 auto elemPtrPtrType = LLVM::LLVMPointerType::get(builder.
getContext());
405 return {memRefDescPtr, elemPtrPtrType};
411 auto [elementPtrPtr, elemPtrPtrType] =
415 LLVM::GEPOp::create(builder, loc, elemPtrPtrType, elemPtrType,
417 return LLVM::LoadOp::create(builder, loc, elemPtrType, alignedGep);
423 auto [elementPtrPtr, elemPtrPtrType] =
427 LLVM::GEPOp::create(builder, loc, elemPtrPtrType, elemPtrType,
429 LLVM::StoreOp::create(builder, loc,
alignedPtr, alignedGep);
435 auto [elementPtrPtr, elemPtrPtrType] =
438 return LLVM::GEPOp::create(builder, loc, elemPtrPtrType, elemPtrType,
445 LLVM::LLVMPointerType elemPtrType) {
448 return LLVM::LoadOp::create(builder, loc, typeConverter.
getIndexType(),
455 LLVM::LLVMPointerType elemPtrType,
459 LLVM::StoreOp::create(builder, loc,
offset, offsetPtr);
466 Type structTy = LLVM::LLVMStructType::getLiteral(
467 indexTy.
getContext(), {elemPtrType, elemPtrType, indexTy, indexTy});
468 auto resultType = LLVM::LLVMPointerType::get(builder.
getContext());
469 return LLVM::GEPOp::create(builder, loc, resultType, structTy,
memRefDescPtr,
478 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
482 return LLVM::LoadOp::create(builder, loc, indexTy, sizeStoreGep);
490 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
494 LLVM::StoreOp::create(builder, loc,
size, sizeStoreGep);
501 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
503 return LLVM::GEPOp::create(builder, loc, ptrType, indexTy,
sizeBasePtr,
rank);
511 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
513 Value strideStoreGep =
515 return LLVM::LoadOp::create(builder, loc, indexTy, strideStoreGep);
523 auto ptrType = LLVM::LLVMPointerType::get(builder.
getContext());
525 Value strideStoreGep =
527 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.