MLIR 23.0.0git
IndexIntrinsicsOpLowering.cpp
Go to the documentation of this file.
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
10
13
14using namespace mlir;
15using namespace mlir::gpu::index_lowering;
16
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, 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(op->getContext(), bitWidth, *upperBound,
34 *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(*upperBound, (intrType == IntrType::Id ? 0u : 1u));
45 return LLVM::ConstantRangeAttr::get(op->getContext(), bitWidth, min, max);
46}
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:234
LLVM::ConstantRangeAttr getIndexOpRange(Operation *op, gpu::Dimension dim, std::optional< uint32_t > opUpperBound, IndexKind indexKind, IntrType intrType, unsigned bitWidth)
Returns a ConstantRangeAttr for a GPU index op, or nullptr if no bounds are found.
std::optional< uint32_t > getKnownDimensionSizeAround(Operation *op, DimensionKind kind, Dimension dim)
Retrieve the constant bounds for a given dimension and dimension kind from the context surrounding op...
Include the generated interface declarations.