| 1 | //===- IndexIntrinsicsOpLowering.h - GPU Index Op Lowering ------*- C++ -*-===// |
| 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 | #ifndef MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_ |
| 9 | #define MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_ |
| 10 | |
| 11 | #include "mlir/Conversion/LLVMCommon/Pattern.h" |
| 12 | #include "mlir/Dialect/GPU/IR/GPUDialect.h" |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMConversionOps.h" |
| 14 | #include "mlir/IR/BuiltinAttributes.h" |
| 15 | |
| 16 | namespace mlir { |
| 17 | namespace gpu { |
| 18 | namespace index_lowering { |
| 19 | // Alias so existing call sites don't need updating. |
| 20 | using IndexKind = gpu::DimensionKind; |
| 21 | |
| 22 | enum class IntrType : uint32_t { |
| 23 | None = 0, |
| 24 | Id = 1, |
| 25 | Dim = 2, |
| 26 | }; |
| 27 | |
| 28 | /// Returns a ConstantRangeAttr for a GPU index op, or nullptr if no bounds |
| 29 | /// are found. `bitWidth` controls the width of the returned range. |
| 30 | /// Checks the provided upper_bound from the op (highest priority), inherent |
| 31 | /// attrs on enclosing `gpu.func`s, and discardable attributes on other |
| 32 | /// enclosing function ops (lowest priority). However, in the case where |
| 33 | /// a dimension is known to have a constant value, returns a range indicating |
| 34 | /// that value. |
| 35 | LLVM::ConstantRangeAttr getIndexOpRange(Operation *op, gpu::Dimension dim, |
| 36 | std::optional<uint32_t> opUpperBound, |
| 37 | IndexKind indexKind, IntrType intrType, |
| 38 | unsigned bitWidth); |
| 39 | |
| 40 | // Rewriting that replaces Op with XOp, YOp, or ZOp depending on the dimension |
| 41 | // that Op operates on. Op is assumed to return an `index` value and |
| 42 | // XOp, YOp and ZOp are assumed to return an `llvm.i32` value. Depending on |
| 43 | // `indexBitwidth`, sign-extend or truncate the resulting value to match the |
| 44 | // bitwidth expected by the consumers of the value. |
| 45 | template <typename Op, typename XOp, typename YOp, typename ZOp> |
| 46 | struct OpLowering : public ConvertOpToLLVMPattern<Op> { |
| 47 | private: |
| 48 | unsigned indexBitwidth; |
| 49 | IndexKind indexKind; |
| 50 | IntrType intrType; |
| 51 | |
| 52 | public: |
| 53 | explicit OpLowering(const LLVMTypeConverter &typeConverter, |
| 54 | PatternBenefit benefit = 1) |
| 55 | : ConvertOpToLLVMPattern<Op>(typeConverter, benefit), |
| 56 | indexBitwidth(typeConverter.getIndexTypeBitwidth()), |
| 57 | indexKind(IndexKind::Other), intrType(IntrType::None) {} |
| 58 | |
| 59 | explicit OpLowering(const LLVMTypeConverter &typeConverter, |
| 60 | IndexKind indexKind, IntrType intrType, |
| 61 | PatternBenefit benefit = 1) |
| 62 | : ConvertOpToLLVMPattern<Op>(typeConverter, benefit), |
| 63 | indexBitwidth(typeConverter.getIndexTypeBitwidth()), |
| 64 | indexKind(indexKind), intrType(intrType) {} |
| 65 | |
| 66 | // Convert the kernel arguments to an LLVM type, preserve the rest. |
| 67 | LogicalResult |
| 68 | matchAndRewrite(Op op, typename Op::Adaptor adaptor, |
| 69 | ConversionPatternRewriter &rewriter) const override { |
| 70 | Location loc = op->getLoc(); |
| 71 | MLIRContext *context = rewriter.getContext(); |
| 72 | Operation *newOp; |
| 73 | switch (op.getDimension()) { |
| 74 | case gpu::Dimension::x: |
| 75 | newOp = XOp::create(rewriter, loc, IntegerType::get(context, width: 32)); |
| 76 | break; |
| 77 | case gpu::Dimension::y: |
| 78 | newOp = YOp::create(rewriter, loc, IntegerType::get(context, width: 32)); |
| 79 | break; |
| 80 | case gpu::Dimension::z: |
| 81 | newOp = ZOp::create(rewriter, loc, IntegerType::get(context, width: 32)); |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | std::optional<uint32_t> opBound; |
| 86 | if (auto bound = op.getUpperBound()) |
| 87 | opBound = static_cast<uint32_t>(bound->getZExtValue()); |
| 88 | if (auto range = getIndexOpRange(op, op.getDimension(), opBound, indexKind, |
| 89 | intrType, /*bitWidth=*/32)) |
| 90 | newOp->setInherentAttr(name: rewriter.getStringAttr(bytes: "range" ), value: range); |
| 91 | |
| 92 | if (indexBitwidth > 32) { |
| 93 | newOp = LLVM::SExtOp::create(builder&: rewriter, location: loc, |
| 94 | res: IntegerType::get(context, width: indexBitwidth), |
| 95 | arg: newOp->getResult(idx: 0)); |
| 96 | } else if (indexBitwidth < 32) { |
| 97 | newOp = LLVM::TruncOp::create(builder&: rewriter, location: loc, |
| 98 | res: IntegerType::get(context, width: indexBitwidth), |
| 99 | arg: newOp->getResult(idx: 0)); |
| 100 | } |
| 101 | |
| 102 | rewriter.replaceOp(op, newOp->getResults()); |
| 103 | return success(); |
| 104 | } |
| 105 | }; |
| 106 | } // namespace index_lowering |
| 107 | } // namespace gpu |
| 108 | } // namespace mlir |
| 109 | |
| 110 | #endif // MLIR_CONVERSION_GPUCOMMON_INDEXINTRINSICSOPLOWERING_H_ |
| 111 | |