MLIR 24.0.0git
Utils.cpp
Go to the documentation of this file.
1//===- Utils.cpp - GPU transforms utils -----------------------------------===//
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// Implements GPU dialect transforms utils.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/Support/ErrorHandling.h"
15#include "llvm/Support/MathExtras.h"
16
17namespace mlir::gpu {
18
19vector::CombiningKind convertReductionKind(gpu::AllReduceOperation mode) {
20 switch (mode) {
21#define MAP_CASE(X) \
22 case gpu::AllReduceOperation::X: \
23 return vector::CombiningKind::X
24
25 MAP_CASE(ADD);
26 MAP_CASE(MUL);
28 MAP_CASE(MINSI);
29 MAP_CASE(MINNUMF);
30 MAP_CASE(MAXSI);
31 MAP_CASE(MAXUI);
32 MAP_CASE(MAXNUMF);
33 MAP_CASE(AND);
34 MAP_CASE(OR);
35 MAP_CASE(XOR);
36 MAP_CASE(MINIMUMF);
37 MAP_CASE(MAXIMUMF);
38 MAP_CASE(MINIMUMNUMF);
39 MAP_CASE(MAXIMUMNUMF);
40
41#undef MAP_CASE
42 }
43
44 llvm_unreachable("Vector and GPU reduction kinds should match 1:1");
45}
46
47FailureOr<SubgroupReduceClusterInfo>
48getSubgroupReduceClusterInfo(SubgroupReduceOp op, unsigned subgroupSize) {
49 assert(llvm::isPowerOf2_32(subgroupSize));
50
51 std::optional<uint32_t> clusterSize = op.getClusterSize();
52 assert(!clusterSize ||
53 llvm::isPowerOf2_32(*clusterSize)); // Verifier should've caught this.
54 if (clusterSize && *clusterSize > subgroupSize)
55 return op.emitOpError()
56 << "cluster size " << *clusterSize
57 << " is greater than subgroup size " << subgroupSize;
58 unsigned effectiveClusterSize = clusterSize.value_or(subgroupSize);
59
60 auto clusterStride = op.getClusterStride();
61 assert(llvm::isPowerOf2_32(clusterStride)); // Verifier should've caught this.
62 if (clusterStride >= subgroupSize)
63 return op.emitOpError()
64 << "cluster stride " << clusterStride
65 << " is not less than subgroup size " << subgroupSize;
66
67 return SubgroupReduceClusterInfo{clusterStride, effectiveClusterSize,
68 subgroupSize};
69}
70} // namespace mlir::gpu
#define MAP_CASE(X)
#define MINUI(lhs, rhs)
FailureOr< SubgroupReduceClusterInfo > getSubgroupReduceClusterInfo(SubgroupReduceOp op, unsigned subgroupSize)
Returns the cluster layout of op in a subgroup of subgroupSize lanes, which must be a power of two.
Definition Utils.cpp:48
vector::CombiningKind convertReductionKind(gpu::AllReduceOperation mode)
Returns the matching vector combining kind.
Definition Utils.cpp:19
Lane layout of the clusters reduced by a gpu.subgroup_reduce op.
Definition GPUUtils.h:34