25 assert(
value !=
nullptr &&
"value cannot be null");
33 Type descriptorType) {
35 Value descriptor = builder.
create<LLVM::UndefOp>(loc, descriptorType);
45 MemRefType type,
Value memory) {
46 assert(type.hasStaticShape() &&
"unexpected dynamic shape");
53 assert(
succeeded(result) &&
"unexpected failure in stride computation");
54 assert(!ShapedType::isDynamicStrideOrOffset(offset) &&
55 "expected static offset");
56 assert(!llvm::any_of(strides, [](int64_t
stride) {
57 return ShapedType::isDynamicStrideOrOffset(stride);
58 }) &&
"expected static strides");
60 auto convertedType = typeConverter.
convertType(type);
61 assert(convertedType &&
"unexpected failure in memref type conversion");
64 descr.setAllocatedPtr(builder, loc, memory);
65 descr.setAlignedPtr(builder, loc, memory);
66 descr.setConstantOffset(builder, loc, offset);
69 for (
unsigned i = 0, e = type.getRank(); i != e; ++i) {
70 descr.setConstantSize(builder, loc, i, type.getDimSize(i));
71 descr.setConstantStride(builder, loc, i, strides[i]);
102 return builder.
create<LLVM::ConstantOp>(
108 return builder.
create<LLVM::ExtractValueOp>(
109 loc, indexType,
value,
130 return builder.
create<LLVM::ExtractValueOp>(
131 loc, indexType,
value,
144 auto sizes = builder.
create<LLVM::ExtractValueOp>(
148 builder.
create<LLVM::AllocaOp>(loc, arrayPtrTy, one, 0);
149 builder.
create<LLVM::StoreOp>(loc, sizes, sizesPtr);
152 auto resultPtr = builder.
create<LLVM::GEPOp>(loc, indexPtrTy, sizesPtr,
154 return builder.
create<LLVM::LoadOp>(loc, resultPtr);
166 unsigned pos, uint64_t
size) {
173 return builder.
create<LLVM::ExtractValueOp>(
174 loc, indexType,
value,
187 unsigned pos, uint64_t
stride) {
217 int64_t rank = type.getRank();
218 for (
unsigned i = 0; i < rank; ++i) {
231 int64_t rank = type.getRank();
236 results.push_back(d.
alignedPtr(builder, loc));
237 results.push_back(d.
offset(builder, loc));
238 for (int64_t i = 0; i < rank; ++i)
239 results.push_back(d.
size(builder, loc, i));
240 for (int64_t i = 0; i < rank; ++i)
241 results.push_back(d.
stride(builder, loc, i));
248 return 3 + 2 * type.getRank();
289 Type descriptorType) {
290 Value descriptor = builder.
create<LLVM::UndefOp>(loc, descriptorType);
331 results.reserve(results.size() + 2);
332 results.push_back(d.
rank(builder, loc));
354 sizes.reserve(sizes.size() + values.size());
363 Value doublePointerSize =
364 builder.
create<LLVM::MulOp>(loc, indexType, two, pointerSize);
368 Value doubleRank = builder.
create<LLVM::MulOp>(loc, indexType, two,
rank);
369 Value doubleRankIncremented =
370 builder.
create<LLVM::AddOp>(loc, indexType, doubleRank, one);
372 loc, indexType, doubleRankIncremented, indexSize);
375 Value allocationSize = builder.
create<LLVM::AddOp>(
376 loc, indexType, doublePointerSize, rankIndexSize);
377 sizes.push_back(allocationSize);
383 Type elemPtrPtrType) {
385 Value elementPtrPtr =
387 return builder.
create<LLVM::LoadOp>(loc, elementPtrPtr);
394 Value elementPtrPtr =
402 Type elemPtrPtrType) {
403 Value elementPtrPtr =
409 loc, elemPtrPtrType, elementPtrPtr,
ValueRange({one}));
410 return builder.
create<LLVM::LoadOp>(loc, alignedGep);
418 Value elementPtrPtr =
424 loc, elemPtrPtrType, elementPtrPtr,
ValueRange({one}));
431 Type elemPtrPtrType) {
432 Value elementPtrPtr =
438 loc, elemPtrPtrType, elementPtrPtr,
ValueRange({two}));
439 offsetGep = builder.
create<LLVM::BitcastOp>(
441 return builder.
create<LLVM::LoadOp>(loc, offsetGep);
448 Value elementPtrPtr =
454 loc, elemPtrPtrType, elementPtrPtr,
ValueRange({two}));
455 offsetGep = builder.
create<LLVM::BitcastOp>(
467 indexTy.
getContext(), {elemPtrTy, elemPtrTy, indexTy, indexTy}));
474 Value three = builder.
create<LLVM::ConstantOp>(loc, int32Type,
486 return builder.
create<LLVM::LoadOp>(loc, sizeStoreGep);
496 builder.
create<LLVM::StoreOp>(loc,
size, sizeStoreGep);
512 Value strideStoreGep = builder.
create<LLVM::GEPOp>(
514 return builder.
create<LLVM::LoadOp>(loc, strideStoreGep);
522 Value strideStoreGep = builder.
create<LLVM::GEPOp>(
524 builder.
create<LLVM::StoreOp>(loc,
stride, strideStoreGep);
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
static constexpr unsigned kSizePosInMemRefDescriptor
void setMemRefDescPtr(OpBuilder &builder, Location loc, Value value)
Builds IR setting ranked memref descriptor ptr.
static Value offset(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value memRefDescPtr, Type elemPtrPtrType)
Builds IR extracting the offset from the descriptor.
static constexpr unsigned kStridePosInMemRefDescriptor
static Value size(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value sizeBasePtr, Value index)
Builds IR extracting the size[index] from the descriptor.
static constexpr unsigned kAlignedPtrPosInMemRefDescriptor
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 LLVMStructType getLiteral(MLIRContext *context, ArrayRef< Type > types, bool isPacked=false)
Gets or creates a literal struct with the given body in the provided context.
void setAllocatedPtr(OpBuilder &builder, Location loc, Value ptr)
Builds IR inserting the allocated pointer into the descriptor.
static LLVMArrayType get(Type elementType, unsigned numElements)
Gets or creates an instance of LLVM dialect array type containing numElements of elementType, in the same context as elementType.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results)
Convert the given type.
Value offset()
Returns the offset Value.
static constexpr unsigned kRankInUnrankedMemRefDescriptor
UnrankedMemRefDescriptor(Value descriptor)
Construct a helper for the given descriptor value.
static unsigned getNumUnpackedValues(MemRefType type)
Returns the number of non-aggregate values that would be produced by unpack.
static Value createIndexAttrConstant(OpBuilder &builder, Location loc, Type resultType, int64_t value)
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 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.
Value allocatedPtr(OpBuilder &builder, Location loc)
Builds IR extracting the allocated pointer 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 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...
unsigned getPointerBitwidth(unsigned addressSpace=0)
Gets the pointer bitwidth.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value...
LLVM::LLVMPointerType getElementPtrType()
Returns the (LLVM) pointer type this descriptor contains.
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
Value stride(unsigned pos)
Returns the pos-th stride Value.
Value offset(OpBuilder &builder, Location loc)
Builds IR extracting the offset from the descriptor.
static constexpr unsigned kOffsetPosInMemRefDescriptor
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
void setAlignedPtr(OpBuilder &builder, Location loc, Value ptr)
Builds IR inserting the aligned pointer into the descriptor.
IntegerAttr getI32IntegerAttr(int32_t value)
static void setStride(OpBuilder &builder, Location loc, LLVMTypeConverter &typeConverter, Value strideBasePtr, Value index, Value stride)
Builds IR inserting the stride[index] into the descriptor.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
LogicalResult getStridesAndOffset(MemRefType t, SmallVectorImpl< int64_t > &strides, int64_t &offset)
Returns the strides of the MemRef if the layout map is in strided form.
void setConstantStride(OpBuilder &builder, Location loc, unsigned pos, uint64_t stride)
IntegerAttr getIntegerAttr(Type type, int64_t value)
Helper class to produce LLVM dialect operations extracting or inserting elements of a MemRef descript...
void setConstantSize(OpBuilder &builder, Location loc, unsigned pos, uint64_t size)
Value stride(OpBuilder &builder, Location loc, unsigned pos)
Builds IR extracting the pos-th size from the descriptor.
Helper class to produce LLVM dialect operations extracting or inserting values to a struct...
int64_t ceilDiv(int64_t lhs, int64_t rhs)
Returns the result of MLIR's ceildiv operation on constants.
static constexpr unsigned kPtrInUnrankedMemRefDescriptor
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 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...
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 allocatedPtr()
Returns the allocated pointer Value.
static LLVMPointerType get(MLIRContext *context, unsigned addressSpace=0)
Gets or creates an instance of LLVM dialect pointer type pointing to an object of pointee type in the...
static MemRefDescriptor undef(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating an undef value of the descriptor type.
Value alignedPtr()
Returns the aligned pointer Value.
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 UnrankedMemRefDescriptor undef(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating an undef value of the descriptor type.
static constexpr unsigned kAllocatedPtrPosInMemRefDescriptor
MemRefDescriptor(Value descriptor)
Construct a helper for the given descriptor value.
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.
Value memRefDescPtr(OpBuilder &builder, Location loc)
Builds IR extracting ranked memref descriptor ptr.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
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.
void setOffset(OpBuilder &builder, Location loc, Value offset)
Builds IR inserting the offset into the descriptor.
Value alignedPtr(OpBuilder &builder, Location loc)
Builds IR extracting the aligned pointer from the descriptor.
Type getType() const
Return the type of this value.
LLVM dialect structure type representing a collection of different-typed elements manipulated togethe...
Conversion from types to the LLVM IR dialect.
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 setRank(OpBuilder &builder, Location loc, Value value)
Builds IR setting the rank in 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.
LLVM dialect pointer type.
Value extractPtr(OpBuilder &builder, Location loc, unsigned pos)
Builds IR to extract a value from the struct at position pos.
unsigned getIndexTypeBitwidth()
Gets the bitwidth of the index type when converted to LLVM.
Value size(unsigned pos)
Returns the pos-th size Value.
void setStride(OpBuilder &builder, Location loc, unsigned pos, Value stride)
Builds IR inserting the pos-th stride into the descriptor.
Type getIndexType()
Gets the LLVM representation of the index type.
void setSize(OpBuilder &builder, Location loc, unsigned pos, Value size)
Builds IR inserting the pos-th size into 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 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...
void setPtr(OpBuilder &builder, Location loc, unsigned pos, Value ptr)
Builds IR to set a value in the struct at position pos.
Value rank(OpBuilder &builder, Location loc)
Builds IR extracting the rank from the descriptor.
This class helps build Operations.
This class provides an abstraction over the different types of ranges over Values.
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 ...
MemRefDescriptorView(ValueRange range)
Constructs the view from a range of values.
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...
Type getElementType() const
Returns the pointed-to type. It may be null if the pointer is opaque.