| 1 | //===- IndexIntrinsicsOpLowering.cpp - GPU Index Op Lowering --------------===// |
| 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 "IndexIntrinsicsOpLowering.h" |
| 10 | |
| 11 | #include "mlir/Dialect/GPU/IR/GPUDialect.h" |
| 12 | #include "mlir/IR/BuiltinAttributes.h" |
| 13 | |
| 14 | using namespace mlir; |
| 15 | using namespace mlir::gpu::index_lowering; |
| 16 | |
| 17 | LLVM::ConstantRangeAttr mlir::gpu::index_lowering::getIndexOpRange( |
| 18 | Operation *op, gpu::Dimension dim, std::optional<uint32_t> opUpperBound, |
| 19 | IndexKind indexKind, IntrType intrType, unsigned bitWidth) { |
| 20 | // Order of priority for bounds: |
| 21 | // 1. The upper_bound attribute |
| 22 | // 2. Inherent attributes on a surrounding gpu.func |
| 23 | // 3. Discardable attributes on a surrounding function of any kind |
| 24 | // The below code handles these in reverse order so that more important |
| 25 | // sources overwrite less important ones. As an exception, dimension-size |
| 26 | // getters will return exact bounds if known. |
| 27 | std::optional<uint32_t> upperBound = |
| 28 | getKnownDimensionSizeAround(op, kind: indexKind, dim); |
| 29 | // If our upper bound is the maximum possible value, we can't easily construct |
| 30 | // the constant range for it. |
| 31 | if (upperBound && intrType == IntrType::Dim && |
| 32 | *upperBound < std::numeric_limits<uint32_t>::max()) |
| 33 | return LLVM::ConstantRangeAttr::get(context: op->getContext(), bitWidth, lower: *upperBound, |
| 34 | upper: *upperBound + 1); |
| 35 | |
| 36 | if (opUpperBound) |
| 37 | upperBound = *opUpperBound; |
| 38 | |
| 39 | if (!upperBound || intrType == IntrType::None) |
| 40 | return nullptr; |
| 41 | |
| 42 | uint32_t min = (intrType == IntrType::Dim ? 1u : 0u); |
| 43 | uint32_t max = |
| 44 | llvm::SaturatingAdd(X: *upperBound, Y: (intrType == IntrType::Id ? 0u : 1u)); |
| 45 | return LLVM::ConstantRangeAttr::get(context: op->getContext(), bitWidth, lower: min, upper: max); |
| 46 | } |
| 47 | |