|
1 | 1 | #include "triton/Conversion/TritonGPUToLLVM/AllocateSharedMemoryUtility.h" |
| 2 | +#include "triton/Analysis/Allocation.h" |
| 3 | +#include "triton/Dialect/TritonGPU/IR/Dialect.h" |
| 4 | +#include "triton/Tools/Sys/GetEnv.hpp" |
| 5 | +#include <cstdlib> |
| 6 | +#include <string> |
2 | 7 |
|
3 | 8 | namespace mlir::triton::gpu { |
4 | 9 |
|
| 10 | +// Helper function to compute allocation size from MemDescType |
| 11 | +inline size_t computeAllocationSize(MemDescType memdescTy) { |
| 12 | + auto elemTy = memdescTy.getElementType(); |
| 13 | + auto shape = memdescTy.getShape(); |
| 14 | + size_t elemSize = elemTy.getIntOrFloatBitWidth() / 8; |
| 15 | + size_t totalElements = 1; |
| 16 | + for (auto dim : shape) { |
| 17 | + totalElements *= dim; |
| 18 | + } |
| 19 | + return totalElements * elemSize; |
| 20 | +} |
| 21 | + |
| 22 | +// Helper function to add allocation information as IR annotations |
| 23 | +void addAllocationAnnotations(Operation *op) { |
| 24 | + MLIRContext *ctx = op->getContext(); |
| 25 | + IntegerAttr offsetAttr; |
| 26 | + MemDescType memdescTy; |
| 27 | + |
| 28 | + // Try to get allocation.offset from the operation itself |
| 29 | + if (auto attr = op->getAttrOfType<IntegerAttr>("allocation.offset")) { |
| 30 | + offsetAttr = attr; |
| 31 | + // Find MemDescType from result or operands |
| 32 | + for (auto result : op->getResults()) { |
| 33 | + if (auto ty = dyn_cast<MemDescType>(result.getType())) { |
| 34 | + memdescTy = ty; |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + if (!memdescTy) { |
| 39 | + for (auto operand : op->getOperands()) { |
| 40 | + if (auto ty = dyn_cast<MemDescType>(operand.getType())) { |
| 41 | + memdescTy = ty; |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } else { |
| 47 | + // Try to find it through operands |
| 48 | + for (auto operand : op->getOperands()) { |
| 49 | + if (auto definingOp = operand.getDefiningOp()) { |
| 50 | + if (auto allocOp = dyn_cast<triton::gpu::LocalAllocOp>(definingOp)) { |
| 51 | + if (auto attr = |
| 52 | + allocOp->getAttrOfType<IntegerAttr>("allocation.offset")) { |
| 53 | + offsetAttr = attr; |
| 54 | + memdescTy = cast<MemDescType>(allocOp.getType()); |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (!offsetAttr || !memdescTy) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + auto offset = offsetAttr.getInt(); |
| 67 | + size_t totalSize = computeAllocationSize(memdescTy); |
| 68 | + op->setAttr("shared_memory.offset", |
| 69 | + IntegerAttr::get(IntegerType::get(ctx, 64), offset)); |
| 70 | + op->setAttr("shared_memory.size_bytes", |
| 71 | + IntegerAttr::get(IntegerType::get(ctx, 64), totalSize)); |
| 72 | +} |
| 73 | + |
| 74 | +// Function to add shared memory access annotations to all operations that use |
| 75 | +// shared memory |
| 76 | +void addSharedMemoryAnnotations(ModuleOp mod) { |
| 77 | + if (!triton::tools::getBoolEnv("MLIR_ENABLE_DUMP")) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + mod.walk([&](Operation *op) { |
| 82 | + if (isa<triton::gpu::LocalStoreOp, triton::gpu::LocalLoadOp, |
| 83 | + triton::gpu::MemDescSubsliceOp, triton::gpu::MemDescIndexOp>(op)) { |
| 84 | + addAllocationAnnotations(op); |
| 85 | + } |
| 86 | + }); |
| 87 | +} |
| 88 | + |
5 | 89 | void attachAllocationSizeAndOffsetAttr(ModuleOp mod, |
6 | 90 | ModuleAllocation &allocation) { |
7 | 91 | MLIRContext *ctx = mod.getContext(); |
|
0 commit comments