| 1 | //===- MemRefBuilder.cpp - Helper for LLVM MemRef equivalents -------------===// |
| 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 | #include "mlir/Conversion/LLVMCommon/MemRefBuilder.h" |
| 10 | #include "MemRefDescriptor.h" |
| 11 | #include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 12 | #include "mlir/Conversion/LLVMCommon/TypeConverter.h" |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMAggregateOps.h" |
| 14 | #include "mlir/Dialect/LLVMIR/LLVMArithOps.h" |
| 15 | #include "mlir/Dialect/LLVMIR/LLVMConstantOps.h" |
| 16 | #include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h" |
| 17 | #include "mlir/Dialect/LLVMIR/LLVMTypes.h" |
| 18 | #include "mlir/IR/Builders.h" |
| 19 | #include "llvm/Support/MathExtras.h" |
| 20 | |
| 21 | using namespace mlir; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // MemRefDescriptor implementation |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | /// Construct a helper for the given descriptor value. |
| 28 | MemRefDescriptor::MemRefDescriptor(Value descriptor) |
| 29 | : StructBuilder(descriptor) { |
| 30 | assert(value != nullptr && "value cannot be null" ); |
| 31 | indexType = cast<LLVM::LLVMStructType>(Val: value.getType()) |
| 32 | .getBody()[kOffsetPosInMemRefDescriptor]; |
| 33 | } |
| 34 | |
| 35 | /// Builds IR creating an `undef` value of the descriptor type. |
| 36 | MemRefDescriptor MemRefDescriptor::poison(OpBuilder &builder, Location loc, |
| 37 | Type descriptorType) { |
| 38 | |
| 39 | Value descriptor = LLVM::PoisonOp::create(builder, location: loc, res: descriptorType); |
| 40 | return MemRefDescriptor(descriptor); |
| 41 | } |
| 42 | |
| 43 | /// Builds IR creating a MemRef descriptor that represents `type` and |
| 44 | /// populates it with static shape and stride information extracted from the |
| 45 | /// type. |
| 46 | MemRefDescriptor |
| 47 | MemRefDescriptor::fromStaticShape(OpBuilder &builder, Location loc, |
| 48 | const LLVMTypeConverter &typeConverter, |
| 49 | MemRefType type, Value memory) { |
| 50 | return fromStaticShape(builder, loc, typeConverter, type, memory, alignedMemory: memory); |
| 51 | } |
| 52 | |
| 53 | MemRefDescriptor MemRefDescriptor::fromStaticShape( |
| 54 | OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, |
| 55 | MemRefType type, Value memory, Value alignedMemory) { |
| 56 | assert(type.hasStaticShape() && "unexpected dynamic shape" ); |
| 57 | |
| 58 | // Extract all strides and offsets and verify they are static. |
| 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" ); |
| 63 | |
| 64 | auto convertedType = typeConverter.convertType(t: type); |
| 65 | assert(convertedType && "unexpected failure in memref type conversion" ); |
| 66 | |
| 67 | auto descr = MemRefDescriptor::poison(builder, loc, descriptorType: convertedType); |
| 68 | descr.setAllocatedPtr(builder, loc, ptr: memory); |
| 69 | descr.setAlignedPtr(builder, loc, ptr: alignedMemory); |
| 70 | descr.setConstantOffset(builder, loc, offset); |
| 71 | |
| 72 | // Fill in sizes and strides |
| 73 | for (unsigned i = 0, e = type.getRank(); i != e; ++i) { |
| 74 | descr.setConstantSize(builder, loc, pos: i, size: type.getDimSize(idx: i)); |
| 75 | descr.setConstantStride(builder, loc, pos: i, stride: strides[i]); |
| 76 | } |
| 77 | return descr; |
| 78 | } |
| 79 | |
| 80 | /// Builds IR extracting the allocated pointer from the descriptor. |
| 81 | Value MemRefDescriptor::allocatedPtr(OpBuilder &builder, Location loc) { |
| 82 | return extractPtr(builder, loc, pos: kAllocatedPtrPosInMemRefDescriptor); |
| 83 | } |
| 84 | |
| 85 | /// Builds IR inserting the allocated pointer into the descriptor. |
| 86 | void MemRefDescriptor::setAllocatedPtr(OpBuilder &builder, Location loc, |
| 87 | Value ptr) { |
| 88 | setPtr(builder, loc, pos: kAllocatedPtrPosInMemRefDescriptor, ptr); |
| 89 | } |
| 90 | |
| 91 | /// Builds IR extracting the aligned pointer from the descriptor. |
| 92 | Value MemRefDescriptor::alignedPtr(OpBuilder &builder, Location loc) { |
| 93 | return extractPtr(builder, loc, pos: kAlignedPtrPosInMemRefDescriptor); |
| 94 | } |
| 95 | |
| 96 | /// Builds IR inserting the aligned pointer into the descriptor. |
| 97 | void MemRefDescriptor::setAlignedPtr(OpBuilder &builder, Location loc, |
| 98 | Value ptr) { |
| 99 | setPtr(builder, loc, pos: kAlignedPtrPosInMemRefDescriptor, ptr); |
| 100 | } |
| 101 | |
| 102 | /// Builds IR extracting the offset from the descriptor. |
| 103 | Value MemRefDescriptor::offset(OpBuilder &builder, Location loc) { |
| 104 | return LLVM::ExtractValueOp::create(builder, location: loc, container: value, |
| 105 | position: kOffsetPosInMemRefDescriptor); |
| 106 | } |
| 107 | |
| 108 | /// Builds IR inserting the offset into the descriptor. |
| 109 | void MemRefDescriptor::setOffset(OpBuilder &builder, Location loc, |
| 110 | Value offset) { |
| 111 | value = LLVM::InsertValueOp::create(builder, location: loc, container: value, value: offset, |
| 112 | position: kOffsetPosInMemRefDescriptor); |
| 113 | } |
| 114 | |
| 115 | /// Builds IR inserting the offset into the descriptor. |
| 116 | void MemRefDescriptor::setConstantOffset(OpBuilder &builder, Location loc, |
| 117 | uint64_t offset) { |
| 118 | setOffset(builder, loc, |
| 119 | offset: LLVM::createIndexAttrConstant(builder, loc, resultType: indexType, value: offset)); |
| 120 | } |
| 121 | |
| 122 | /// Builds IR extracting the pos-th size from the descriptor. |
| 123 | Value MemRefDescriptor::size(OpBuilder &builder, Location loc, unsigned pos) { |
| 124 | return LLVM::ExtractValueOp::create( |
| 125 | builder, location: loc, container: value, |
| 126 | position: ArrayRef<int64_t>({kSizePosInMemRefDescriptor, pos})); |
| 127 | } |
| 128 | |
| 129 | Value MemRefDescriptor::size(OpBuilder &builder, Location loc, Value pos, |
| 130 | int64_t rank) { |
| 131 | auto arrayTy = LLVM::LLVMArrayType::get(elementType: indexType, numElements: rank); |
| 132 | |
| 133 | auto ptrTy = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 134 | |
| 135 | // Copy size values to stack-allocated memory. |
| 136 | auto one = LLVM::createIndexAttrConstant(builder, loc, resultType: indexType, value: 1); |
| 137 | auto sizes = LLVM::ExtractValueOp::create( |
| 138 | builder, location: loc, container: value, |
| 139 | position: llvm::ArrayRef<int64_t>({kSizePosInMemRefDescriptor})); |
| 140 | auto sizesPtr = LLVM::AllocaOp::create(builder, location: loc, resultType: ptrTy, elementType: arrayTy, arraySize: one, |
| 141 | /*alignment=*/0); |
| 142 | LLVM::StoreOp::create(builder, location: loc, value: sizes, addr: sizesPtr); |
| 143 | |
| 144 | // Load an return size value of interest. |
| 145 | auto resultPtr = LLVM::GEPOp::create(builder, location: loc, resultType: ptrTy, elementType: arrayTy, basePtr: sizesPtr, |
| 146 | indices: ArrayRef<LLVM::GEPArg>{0, pos}); |
| 147 | return LLVM::LoadOp::create(builder, location: loc, type: indexType, addr: resultPtr); |
| 148 | } |
| 149 | |
| 150 | /// Builds IR inserting the pos-th size into the descriptor |
| 151 | void MemRefDescriptor::setSize(OpBuilder &builder, Location loc, unsigned pos, |
| 152 | Value size) { |
| 153 | value = LLVM::InsertValueOp::create( |
| 154 | builder, location: loc, container: value, value: size, |
| 155 | position: ArrayRef<int64_t>({kSizePosInMemRefDescriptor, pos})); |
| 156 | } |
| 157 | |
| 158 | void MemRefDescriptor::setConstantSize(OpBuilder &builder, Location loc, |
| 159 | unsigned pos, uint64_t size) { |
| 160 | setSize(builder, loc, pos, |
| 161 | size: LLVM::createIndexAttrConstant(builder, loc, resultType: indexType, value: size)); |
| 162 | } |
| 163 | |
| 164 | /// Builds IR extracting the pos-th stride from the descriptor. |
| 165 | Value MemRefDescriptor::stride(OpBuilder &builder, Location loc, unsigned pos) { |
| 166 | return LLVM::ExtractValueOp::create( |
| 167 | builder, location: loc, container: value, |
| 168 | position: ArrayRef<int64_t>({kStridePosInMemRefDescriptor, pos})); |
| 169 | } |
| 170 | |
| 171 | /// Builds IR inserting the pos-th stride into the descriptor |
| 172 | void MemRefDescriptor::setStride(OpBuilder &builder, Location loc, unsigned pos, |
| 173 | Value stride) { |
| 174 | value = LLVM::InsertValueOp::create( |
| 175 | builder, location: loc, container: value, value: stride, |
| 176 | position: ArrayRef<int64_t>({kStridePosInMemRefDescriptor, pos})); |
| 177 | } |
| 178 | |
| 179 | void MemRefDescriptor::setConstantStride(OpBuilder &builder, Location loc, |
| 180 | unsigned pos, uint64_t stride) { |
| 181 | setStride(builder, loc, pos, |
| 182 | stride: LLVM::createIndexAttrConstant(builder, loc, resultType: indexType, value: stride)); |
| 183 | } |
| 184 | |
| 185 | LLVM::LLVMPointerType MemRefDescriptor::getElementPtrType() { |
| 186 | return cast<LLVM::LLVMPointerType>( |
| 187 | Val: cast<LLVM::LLVMStructType>(Val: value.getType()) |
| 188 | .getBody()[kAlignedPtrPosInMemRefDescriptor]); |
| 189 | } |
| 190 | |
| 191 | Value MemRefDescriptor::bufferPtr(OpBuilder &builder, Location loc, |
| 192 | const LLVMTypeConverter &converter, |
| 193 | MemRefType type) { |
| 194 | // When we convert to LLVM, the input memref must have been normalized |
| 195 | // beforehand. Hence, this call is guaranteed to work. |
| 196 | auto [strides, offsetCst] = type.getStridesAndOffset(); |
| 197 | |
| 198 | Value ptr = alignedPtr(builder, loc); |
| 199 | // For zero offsets, we already have the base pointer. |
| 200 | if (offsetCst == 0) |
| 201 | return ptr; |
| 202 | |
| 203 | // Otherwise add the offset to the aligned base. |
| 204 | Type indexType = converter.getIndexType(); |
| 205 | Value offsetVal = |
| 206 | ShapedType::isDynamic(dValue: offsetCst) |
| 207 | ? offset(builder, loc) |
| 208 | : LLVM::createIndexAttrConstant(builder, loc, resultType: indexType, value: offsetCst); |
| 209 | Type elementType = converter.convertType(t: type.getElementType()); |
| 210 | ptr = LLVM::GEPOp::create(builder, location: loc, resultType: ptr.getType(), elementType, basePtr: ptr, |
| 211 | indices: offsetVal); |
| 212 | return ptr; |
| 213 | } |
| 214 | |
| 215 | /// Creates a MemRef descriptor structure from a list of individual values |
| 216 | /// composing that descriptor, in the following order: |
| 217 | /// - allocated pointer; |
| 218 | /// - aligned pointer; |
| 219 | /// - offset; |
| 220 | /// - <rank> sizes; |
| 221 | /// - <rank> strides; |
| 222 | /// where <rank> is the MemRef rank as provided in `type`. |
| 223 | Value MemRefDescriptor::pack(OpBuilder &builder, Location loc, |
| 224 | const LLVMTypeConverter &converter, |
| 225 | MemRefType type, ValueRange values) { |
| 226 | Type llvmType = converter.convertType(t: type); |
| 227 | auto d = MemRefDescriptor::poison(builder, loc, descriptorType: llvmType); |
| 228 | |
| 229 | d.setAllocatedPtr(builder, loc, ptr: values[kAllocatedPtrPosInMemRefDescriptor]); |
| 230 | d.setAlignedPtr(builder, loc, ptr: values[kAlignedPtrPosInMemRefDescriptor]); |
| 231 | d.setOffset(builder, loc, offset: values[kOffsetPosInMemRefDescriptor]); |
| 232 | |
| 233 | int64_t rank = type.getRank(); |
| 234 | for (unsigned i = 0; i < rank; ++i) { |
| 235 | d.setSize(builder, loc, pos: i, size: values[kSizePosInMemRefDescriptor + i]); |
| 236 | d.setStride(builder, loc, pos: i, stride: values[kSizePosInMemRefDescriptor + rank + i]); |
| 237 | } |
| 238 | |
| 239 | return d; |
| 240 | } |
| 241 | |
| 242 | /// Builds IR extracting individual elements of a MemRef descriptor structure |
| 243 | /// and returning them as `results` list. |
| 244 | void MemRefDescriptor::unpack(OpBuilder &builder, Location loc, Value packed, |
| 245 | MemRefType type, |
| 246 | SmallVectorImpl<Value> &results) { |
| 247 | int64_t rank = type.getRank(); |
| 248 | results.reserve(N: results.size() + getNumUnpackedValues(type)); |
| 249 | |
| 250 | MemRefDescriptor d(packed); |
| 251 | results.push_back(Elt: d.allocatedPtr(builder, loc)); |
| 252 | results.push_back(Elt: d.alignedPtr(builder, loc)); |
| 253 | results.push_back(Elt: d.offset(builder, loc)); |
| 254 | for (int64_t i = 0; i < rank; ++i) |
| 255 | results.push_back(Elt: d.size(builder, loc, pos: i)); |
| 256 | for (int64_t i = 0; i < rank; ++i) |
| 257 | results.push_back(Elt: d.stride(builder, loc, pos: i)); |
| 258 | } |
| 259 | |
| 260 | /// Returns the number of non-aggregate values that would be produced by |
| 261 | /// `unpack`. |
| 262 | unsigned MemRefDescriptor::getNumUnpackedValues(MemRefType type) { |
| 263 | // Two pointers, offset, <rank> sizes, <rank> strides. |
| 264 | return 3 + 2 * type.getRank(); |
| 265 | } |
| 266 | |
| 267 | //===----------------------------------------------------------------------===// |
| 268 | // MemRefDescriptorView implementation. |
| 269 | //===----------------------------------------------------------------------===// |
| 270 | |
| 271 | MemRefDescriptorView::MemRefDescriptorView(ValueRange range) |
| 272 | : rank((range.size() - kSizePosInMemRefDescriptor) / 2), elements(range) {} |
| 273 | |
| 274 | Value MemRefDescriptorView::allocatedPtr() { |
| 275 | return elements[kAllocatedPtrPosInMemRefDescriptor]; |
| 276 | } |
| 277 | |
| 278 | Value MemRefDescriptorView::alignedPtr() { |
| 279 | return elements[kAlignedPtrPosInMemRefDescriptor]; |
| 280 | } |
| 281 | |
| 282 | Value MemRefDescriptorView::offset() { |
| 283 | return elements[kOffsetPosInMemRefDescriptor]; |
| 284 | } |
| 285 | |
| 286 | Value MemRefDescriptorView::size(unsigned pos) { |
| 287 | return elements[kSizePosInMemRefDescriptor + pos]; |
| 288 | } |
| 289 | |
| 290 | Value MemRefDescriptorView::stride(unsigned pos) { |
| 291 | return elements[kSizePosInMemRefDescriptor + rank + pos]; |
| 292 | } |
| 293 | |
| 294 | //===----------------------------------------------------------------------===// |
| 295 | // UnrankedMemRefDescriptor implementation |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | |
| 298 | /// Construct a helper for the given descriptor value. |
| 299 | UnrankedMemRefDescriptor::UnrankedMemRefDescriptor(Value descriptor) |
| 300 | : StructBuilder(descriptor) {} |
| 301 | |
| 302 | /// Builds IR creating an `undef` value of the descriptor type. |
| 303 | UnrankedMemRefDescriptor UnrankedMemRefDescriptor::poison(OpBuilder &builder, |
| 304 | Location loc, |
| 305 | Type descriptorType) { |
| 306 | Value descriptor = LLVM::PoisonOp::create(builder, location: loc, res: descriptorType); |
| 307 | return UnrankedMemRefDescriptor(descriptor); |
| 308 | } |
| 309 | Value UnrankedMemRefDescriptor::rank(OpBuilder &builder, Location loc) const { |
| 310 | return extractPtr(builder, loc, pos: kRankInUnrankedMemRefDescriptor); |
| 311 | } |
| 312 | void UnrankedMemRefDescriptor::setRank(OpBuilder &builder, Location loc, |
| 313 | Value v) { |
| 314 | setPtr(builder, loc, pos: kRankInUnrankedMemRefDescriptor, ptr: v); |
| 315 | } |
| 316 | Value UnrankedMemRefDescriptor::memRefDescPtr(OpBuilder &builder, |
| 317 | Location loc) const { |
| 318 | return extractPtr(builder, loc, pos: kPtrInUnrankedMemRefDescriptor); |
| 319 | } |
| 320 | void UnrankedMemRefDescriptor::setMemRefDescPtr(OpBuilder &builder, |
| 321 | Location loc, Value v) { |
| 322 | setPtr(builder, loc, pos: kPtrInUnrankedMemRefDescriptor, ptr: v); |
| 323 | } |
| 324 | |
| 325 | /// Builds IR populating an unranked MemRef descriptor structure from a list |
| 326 | /// of individual constituent values in the following order: |
| 327 | /// - rank of the memref; |
| 328 | /// - pointer to the memref descriptor. |
| 329 | Value UnrankedMemRefDescriptor::pack(OpBuilder &builder, Location loc, |
| 330 | const LLVMTypeConverter &converter, |
| 331 | UnrankedMemRefType type, |
| 332 | ValueRange values) { |
| 333 | Type llvmType = converter.convertType(t: type); |
| 334 | auto d = UnrankedMemRefDescriptor::poison(builder, loc, descriptorType: llvmType); |
| 335 | |
| 336 | d.setRank(builder, loc, v: values[kRankInUnrankedMemRefDescriptor]); |
| 337 | d.setMemRefDescPtr(builder, loc, v: values[kPtrInUnrankedMemRefDescriptor]); |
| 338 | return d; |
| 339 | } |
| 340 | |
| 341 | /// Builds IR extracting individual elements that compose an unranked memref |
| 342 | /// descriptor and returns them as `results` list. |
| 343 | void UnrankedMemRefDescriptor::unpack(OpBuilder &builder, Location loc, |
| 344 | Value packed, |
| 345 | SmallVectorImpl<Value> &results) { |
| 346 | UnrankedMemRefDescriptor d(packed); |
| 347 | results.reserve(N: results.size() + 2); |
| 348 | results.push_back(Elt: d.rank(builder, loc)); |
| 349 | results.push_back(Elt: d.memRefDescPtr(builder, loc)); |
| 350 | } |
| 351 | |
| 352 | Value UnrankedMemRefDescriptor::computeSize( |
| 353 | OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, |
| 354 | UnrankedMemRefDescriptor desc, unsigned addressSpace) { |
| 355 | // Cache the index type. |
| 356 | Type indexType = typeConverter.getIndexType(); |
| 357 | |
| 358 | // Initialize shared constants. |
| 359 | Value one = LLVM::createIndexAttrConstant(builder, loc, resultType: indexType, value: 1); |
| 360 | Value two = LLVM::createIndexAttrConstant(builder, loc, resultType: indexType, value: 2); |
| 361 | Value indexSize = LLVM::createIndexAttrConstant( |
| 362 | builder, loc, resultType: indexType, |
| 363 | value: llvm::divideCeil(Numerator: typeConverter.getIndexTypeBitwidth(), Denominator: 8)); |
| 364 | |
| 365 | // Emit IR computing the memory necessary to store the descriptor. This |
| 366 | // assumes the descriptor to be |
| 367 | // { type*, type*, index, index[rank], index[rank] } |
| 368 | // and densely packed, so the total size is |
| 369 | // 2 * sizeof(pointer) + (1 + 2 * rank) * sizeof(index). |
| 370 | // TODO: consider including the actual size (including eventual padding due |
| 371 | // to data layout) into the unranked descriptor. |
| 372 | Value pointerSize = LLVM::createIndexAttrConstant( |
| 373 | builder, loc, resultType: indexType, |
| 374 | value: llvm::divideCeil(Numerator: typeConverter.getPointerBitwidth(addressSpace), Denominator: 8)); |
| 375 | Value doublePointerSize = |
| 376 | LLVM::MulOp::create(builder, location: loc, res: indexType, lhs: two, rhs: pointerSize); |
| 377 | |
| 378 | // (1 + 2 * rank) * sizeof(index) |
| 379 | Value rank = desc.rank(builder, loc); |
| 380 | Value doubleRank = LLVM::MulOp::create(builder, location: loc, res: indexType, lhs: two, rhs: rank); |
| 381 | Value doubleRankIncremented = |
| 382 | LLVM::AddOp::create(builder, location: loc, res: indexType, lhs: doubleRank, rhs: one); |
| 383 | Value rankIndexSize = LLVM::MulOp::create(builder, location: loc, res: indexType, |
| 384 | lhs: doubleRankIncremented, rhs: indexSize); |
| 385 | |
| 386 | // Total allocation size. |
| 387 | Value allocationSize = LLVM::AddOp::create(builder, location: loc, res: indexType, |
| 388 | lhs: doublePointerSize, rhs: rankIndexSize); |
| 389 | return allocationSize; |
| 390 | } |
| 391 | |
| 392 | Value UnrankedMemRefDescriptor::allocatedPtr( |
| 393 | OpBuilder &builder, Location loc, Value memRefDescPtr, |
| 394 | LLVM::LLVMPointerType elemPtrType) { |
| 395 | return LLVM::LoadOp::create(builder, location: loc, type: elemPtrType, addr: memRefDescPtr); |
| 396 | } |
| 397 | |
| 398 | void UnrankedMemRefDescriptor::setAllocatedPtr( |
| 399 | OpBuilder &builder, Location loc, Value memRefDescPtr, |
| 400 | LLVM::LLVMPointerType elemPtrType, Value allocatedPtr) { |
| 401 | LLVM::StoreOp::create(builder, location: loc, value: allocatedPtr, addr: memRefDescPtr); |
| 402 | } |
| 403 | |
| 404 | static std::pair<Value, Type> |
| 405 | castToElemPtrPtr(OpBuilder &builder, Location loc, Value memRefDescPtr, |
| 406 | LLVM::LLVMPointerType elemPtrType) { |
| 407 | auto elemPtrPtrType = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 408 | return {memRefDescPtr, elemPtrPtrType}; |
| 409 | } |
| 410 | |
| 411 | Value UnrankedMemRefDescriptor::alignedPtr( |
| 412 | OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, |
| 413 | Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType) { |
| 414 | auto [elementPtrPtr, elemPtrPtrType] = |
| 415 | castToElemPtrPtr(builder, loc, memRefDescPtr, elemPtrType); |
| 416 | |
| 417 | Value alignedGep = |
| 418 | LLVM::GEPOp::create(builder, location: loc, resultType: elemPtrPtrType, elementType: elemPtrType, |
| 419 | basePtr: elementPtrPtr, indices: ArrayRef<LLVM::GEPArg>{1}); |
| 420 | return LLVM::LoadOp::create(builder, location: loc, type: elemPtrType, addr: alignedGep); |
| 421 | } |
| 422 | |
| 423 | void UnrankedMemRefDescriptor::setAlignedPtr( |
| 424 | OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, |
| 425 | Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType, Value alignedPtr) { |
| 426 | auto [elementPtrPtr, elemPtrPtrType] = |
| 427 | castToElemPtrPtr(builder, loc, memRefDescPtr, elemPtrType); |
| 428 | |
| 429 | Value alignedGep = |
| 430 | LLVM::GEPOp::create(builder, location: loc, resultType: elemPtrPtrType, elementType: elemPtrType, |
| 431 | basePtr: elementPtrPtr, indices: ArrayRef<LLVM::GEPArg>{1}); |
| 432 | LLVM::StoreOp::create(builder, location: loc, value: alignedPtr, addr: alignedGep); |
| 433 | } |
| 434 | |
| 435 | Value UnrankedMemRefDescriptor::offsetBasePtr( |
| 436 | OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, |
| 437 | Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType) { |
| 438 | auto [elementPtrPtr, elemPtrPtrType] = |
| 439 | castToElemPtrPtr(builder, loc, memRefDescPtr, elemPtrType); |
| 440 | |
| 441 | return LLVM::GEPOp::create(builder, location: loc, resultType: elemPtrPtrType, elementType: elemPtrType, |
| 442 | basePtr: elementPtrPtr, indices: ArrayRef<LLVM::GEPArg>{2}); |
| 443 | } |
| 444 | |
| 445 | Value UnrankedMemRefDescriptor::offset(OpBuilder &builder, Location loc, |
| 446 | const LLVMTypeConverter &typeConverter, |
| 447 | Value memRefDescPtr, |
| 448 | LLVM::LLVMPointerType elemPtrType) { |
| 449 | Value offsetPtr = |
| 450 | offsetBasePtr(builder, loc, typeConverter, memRefDescPtr, elemPtrType); |
| 451 | return LLVM::LoadOp::create(builder, location: loc, type: typeConverter.getIndexType(), |
| 452 | addr: offsetPtr); |
| 453 | } |
| 454 | |
| 455 | void UnrankedMemRefDescriptor::setOffset(OpBuilder &builder, Location loc, |
| 456 | const LLVMTypeConverter &typeConverter, |
| 457 | Value memRefDescPtr, |
| 458 | LLVM::LLVMPointerType elemPtrType, |
| 459 | Value offset) { |
| 460 | Value offsetPtr = |
| 461 | offsetBasePtr(builder, loc, typeConverter, memRefDescPtr, elemPtrType); |
| 462 | LLVM::StoreOp::create(builder, location: loc, value: offset, addr: offsetPtr); |
| 463 | } |
| 464 | |
| 465 | Value UnrankedMemRefDescriptor::sizeBasePtr( |
| 466 | OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, |
| 467 | Value memRefDescPtr, LLVM::LLVMPointerType elemPtrType) { |
| 468 | Type indexTy = typeConverter.getIndexType(); |
| 469 | Type structTy = LLVM::LLVMStructType::getLiteral( |
| 470 | context: indexTy.getContext(), types: {elemPtrType, elemPtrType, indexTy, indexTy}); |
| 471 | auto resultType = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 472 | return LLVM::GEPOp::create(builder, location: loc, resultType, elementType: structTy, basePtr: memRefDescPtr, |
| 473 | indices: ArrayRef<LLVM::GEPArg>{0, 3}); |
| 474 | } |
| 475 | |
| 476 | Value UnrankedMemRefDescriptor::size(OpBuilder &builder, Location loc, |
| 477 | const LLVMTypeConverter &typeConverter, |
| 478 | Value sizeBasePtr, Value index) { |
| 479 | |
| 480 | Type indexTy = typeConverter.getIndexType(); |
| 481 | auto ptrType = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 482 | |
| 483 | Value sizeStoreGep = |
| 484 | LLVM::GEPOp::create(builder, location: loc, resultType: ptrType, elementType: indexTy, basePtr: sizeBasePtr, indices: index); |
| 485 | return LLVM::LoadOp::create(builder, location: loc, type: indexTy, addr: sizeStoreGep); |
| 486 | } |
| 487 | |
| 488 | void UnrankedMemRefDescriptor::setSize(OpBuilder &builder, Location loc, |
| 489 | const LLVMTypeConverter &typeConverter, |
| 490 | Value sizeBasePtr, Value index, |
| 491 | Value size) { |
| 492 | Type indexTy = typeConverter.getIndexType(); |
| 493 | auto ptrType = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 494 | |
| 495 | Value sizeStoreGep = |
| 496 | LLVM::GEPOp::create(builder, location: loc, resultType: ptrType, elementType: indexTy, basePtr: sizeBasePtr, indices: index); |
| 497 | LLVM::StoreOp::create(builder, location: loc, value: size, addr: sizeStoreGep); |
| 498 | } |
| 499 | |
| 500 | Value UnrankedMemRefDescriptor::strideBasePtr( |
| 501 | OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, |
| 502 | Value sizeBasePtr, Value rank) { |
| 503 | Type indexTy = typeConverter.getIndexType(); |
| 504 | auto ptrType = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 505 | |
| 506 | return LLVM::GEPOp::create(builder, location: loc, resultType: ptrType, elementType: indexTy, basePtr: sizeBasePtr, indices: rank); |
| 507 | } |
| 508 | |
| 509 | Value UnrankedMemRefDescriptor::stride(OpBuilder &builder, Location loc, |
| 510 | const LLVMTypeConverter &typeConverter, |
| 511 | Value strideBasePtr, Value index, |
| 512 | Value stride) { |
| 513 | Type indexTy = typeConverter.getIndexType(); |
| 514 | auto ptrType = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 515 | |
| 516 | Value strideStoreGep = |
| 517 | LLVM::GEPOp::create(builder, location: loc, resultType: ptrType, elementType: indexTy, basePtr: strideBasePtr, indices: index); |
| 518 | return LLVM::LoadOp::create(builder, location: loc, type: indexTy, addr: strideStoreGep); |
| 519 | } |
| 520 | |
| 521 | void UnrankedMemRefDescriptor::setStride(OpBuilder &builder, Location loc, |
| 522 | const LLVMTypeConverter &typeConverter, |
| 523 | Value strideBasePtr, Value index, |
| 524 | Value stride) { |
| 525 | Type indexTy = typeConverter.getIndexType(); |
| 526 | auto ptrType = LLVM::LLVMPointerType::get(context: builder.getContext()); |
| 527 | |
| 528 | Value strideStoreGep = |
| 529 | LLVM::GEPOp::create(builder, location: loc, resultType: ptrType, elementType: indexTy, basePtr: strideBasePtr, indices: index); |
| 530 | LLVM::StoreOp::create(builder, location: loc, value: stride, addr: strideStoreGep); |
| 531 | } |
| 532 | |