|
| 1 | +#include "triton/Conversion/TritonGPUToLLVM/PatternTritonGPUOpToLLVM.h" |
| 2 | +#include "triton/Conversion/TritonGPUToLLVM/Utility.h" |
| 3 | + |
| 4 | +using namespace mlir; |
| 5 | +using namespace mlir::triton; |
| 6 | + |
| 7 | +namespace { |
| 8 | +class GatherOpConversion : public ConvertOpToLLVMPattern<GatherOp> { |
| 9 | +public: |
| 10 | + GatherOpConversion(LLVMTypeConverter &typeConverter, |
| 11 | + const TargetInfoBase &targetInfo, PatternBenefit benefit) |
| 12 | + : ConvertOpToLLVMPattern(typeConverter, benefit), targetInfo(targetInfo) { |
| 13 | + } |
| 14 | + |
| 15 | + LogicalResult |
| 16 | + matchAndRewrite(GatherOp op, OpAdaptor adaptor, |
| 17 | + ConversionPatternRewriter &rewriter) const override; |
| 18 | + |
| 19 | +private: |
| 20 | + const TargetInfoBase &targetInfo; |
| 21 | +}; |
| 22 | + |
| 23 | +LogicalResult |
| 24 | +GatherOpConversion::matchAndRewrite(GatherOp op, OpAdaptor adaptor, |
| 25 | + ConversionPatternRewriter &rewriter) const { |
| 26 | + Location loc = op.getLoc(); |
| 27 | + RankedTensorType srcType = op.getSrc().getType(); |
| 28 | + |
| 29 | + // Compute the src subtensor shape owned by this CTA. |
| 30 | + SmallVector<unsigned> srcShapePerCTA = |
| 31 | + convertType<unsigned>(triton::gpu::getShapePerCTA(srcType)); |
| 32 | + |
| 33 | + // Grab the src values in this thread. |
| 34 | + SmallVector<Value> srcValues = |
| 35 | + unpackLLElements(loc, adaptor.getSrc(), rewriter); |
| 36 | + |
| 37 | + // Emit the indices of the src values owned by this thread. |
| 38 | + SmallVector<SmallVector<Value>> srcIndices = |
| 39 | + emitIndices(loc, rewriter, targetInfo, srcType.getEncoding(), |
| 40 | + op.getSrc().getType(), /*withCTAOffset=*/true); |
| 41 | + |
| 42 | + // Store the src values owned by the thread into their respective location in |
| 43 | + // the scratch memory. |
| 44 | + assert(srcValues.size() == srcIndices.size()); |
| 45 | + |
| 46 | + // Get the base pointer to the scratch memory. |
| 47 | + Value smemBase = LLVM::getSharedMemoryBase(loc, rewriter, targetInfo, op); |
| 48 | + |
| 49 | + // For each src element owned by the thread, index into the scratch memory and |
| 50 | + // then store it. |
| 51 | + Type elemType = getTypeConverter()->convertType(srcType.getElementType()); |
| 52 | + for (auto [value, indices] : llvm::zip(srcValues, srcIndices)) { |
| 53 | + // Convert the index at each dim into a single offset given the shape of the |
| 54 | + // tensor. |
| 55 | + Value offset = LLVM::linearize(rewriter, loc, indices, srcShapePerCTA); |
| 56 | + // Emit the offset into the shared memory and then store the value. |
| 57 | + Value ptr = gep(smemBase.getType(), elemType, smemBase, offset); |
| 58 | + store(value, ptr); |
| 59 | + } |
| 60 | + |
| 61 | + // Synchronize the whole CTA. |
| 62 | + barrier(); |
| 63 | + |
| 64 | + // Grab the index values owned by this thread. |
| 65 | + SmallVector<Value> idxValues = |
| 66 | + unpackLLElements(loc, adaptor.getIndices(), rewriter); |
| 67 | + |
| 68 | + // Apply the layout of the destination tensor to obtain the indices of the |
| 69 | + // column to gather along, then for each column, replace the index along the |
| 70 | + // gather axis with the appropriate index value. |
| 71 | + // |
| 72 | + // I = LL(pid) |
| 73 | + // idx = indices[I] |
| 74 | + // I_gather = [I[d] if d != axis else idx for d in range(len(I))] |
| 75 | + // out[I] = src[I_gather] |
| 76 | + RankedTensorType dstType = op.getType(); |
| 77 | + SmallVector<SmallVector<Value>> dstIndices = |
| 78 | + emitIndices(loc, rewriter, targetInfo, dstType.getEncoding(), dstType, |
| 79 | + /*withCTAOffset=*/true); |
| 80 | + |
| 81 | + unsigned idxWidth = op.getIndices().getType().getElementTypeBitWidth(); |
| 82 | + unsigned axis = op.getAxis(); |
| 83 | + SmallVector<Value> results(dstIndices.size()); |
| 84 | + for (auto [i, idx, indices] : llvm::enumerate(idxValues, dstIndices)) { |
| 85 | + // The LL index computations are performed with 32 bit integers. If the |
| 86 | + // indices are something else, cast them to i32. |
| 87 | + if (idxWidth > 32) { |
| 88 | + idx = trunc(i32_ty, idx); |
| 89 | + } else if (idxWidth < 32) { |
| 90 | + // Negative indices don't make sense, so zero-extend. |
| 91 | + idx = zext(i32_ty, idx); |
| 92 | + } |
| 93 | + indices[axis] = idx; |
| 94 | + Value offset = LLVM::linearize(rewriter, loc, indices, srcShapePerCTA); |
| 95 | + Value ptr = gep(smemBase.getType(), elemType, smemBase, offset); |
| 96 | + results[i] = load(elemType, ptr); |
| 97 | + } |
| 98 | + |
| 99 | + Value packed = |
| 100 | + packLLElements(loc, getTypeConverter(), results, rewriter, dstType); |
| 101 | + rewriter.replaceOp(op, packed); |
| 102 | + return success(); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace |
| 106 | + |
| 107 | +void triton::populateGatherOpToLLVMPatterns(LLVMTypeConverter &typeConverter, |
| 108 | + RewritePatternSet &patterns, |
| 109 | + const TargetInfoBase &targetInfo, |
| 110 | + PatternBenefit benefit) { |
| 111 | + patterns.insert<GatherOpConversion>(typeConverter, targetInfo, benefit); |
| 112 | +} |
0 commit comments