MLIR 24.0.0git
Utils.h
Go to the documentation of this file.
1//===- Utils.h - Utils for GPU transform ops --------------------*- 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
9#ifndef MLIR_DIALECT_GPU_TRANSFORMOPS_UTILS_H
10#define MLIR_DIALECT_GPU_TRANSFORMOPS_UTILS_H
11
17
18namespace mlir {
19namespace gpu {
20class LaunchOp;
21enum class MappingId : uint64_t;
22} // namespace gpu
23namespace scf {
24class ForallOp;
25} // namespace scf
26namespace transform {
27namespace gpu {
28
29/// Helper type for functions that generate ids for the mapping of a scf.forall.
31 /// Error message, if not empty then building the ids failed.
32 std::string errorMsg;
33 /// Values used to replace the forall induction variables.
35 /// Values used to predicate the forall body when activeMappingSizes is
36 /// smaller than the available mapping sizes.
38};
39
41 llvm::interleaveComma(res.mappingIdOps, os << "----mappingIdOps: ");
42 os << "\n";
43 llvm::interleaveComma(res.predicateOps, os << "----predicateOps: ");
44 os << "\n";
45 return os;
46}
47
48/// Common gpu id builder type, allows the configuration of lowering for various
49/// mapping schemes. Takes:
50/// - A rewriter with insertion point set before the forall op to rewrite.
51/// - The loc of the forall op to rewrite.
52/// - A list of positive integers carrying the mapping sizes for the current
53/// forall op to rewrite.
54using GpuIdBuilderFnType = std::function<IdBuilderResult(
56
57/// Helper struct for configuring the rewrite of mapped scf.forall ops to
58/// various gpu id configurations.
60 using MappingIdBuilderFnType = std::function<DeviceMappingAttrInterface(
61 MLIRContext *, mlir::gpu::MappingId)>;
62
63 GpuIdBuilder() = default;
64 GpuIdBuilder(MLIRContext *ctx, bool useLinearMapping,
65 const MappingIdBuilderFnType &builder);
66
67 /// The mapping attributes targeted by this generator.
69
70 /// The constructor that builds the concrete IR for mapping ids.
72};
73
74/// Builder for gpu::BlockIdOps used to map scf.forall to blocks.
75/// If `useLinearMapping` is false, the `idBuilder` method returns 3D values
76/// used for indexing rewrites as well as 3D sizes for predicate generation.
77/// If `useLinearMapping` is true, the `idBuilder` method returns nD values
78/// used for indexing rewrites as well as 1D sizes for predicate generation.
79/// If `mask` is provided, it will be used to filter the active blocks.
81 GpuBlockIdBuilder(MLIRContext *ctx, bool useLinearMapping = false,
82 DeviceMaskingAttrInterface mask = nullptr);
83};
84
85/// Builder for warpgroup ids used to map scf.forall to reindexed warpgroups.
86/// If `useLinearMapping` is false, the `idBuilder` method returns 3D values
87/// used for indexing rewrites as well as 3D sizes for predicate generation.
88/// If `useLinearMapping` is true, the `idBuilder` method returns nD values
89/// used for indexing rewrites as well as 1D sizes for predicate generation.
90/// If `mask` is provided, it will be used to filter the active warpgroups.
93 bool useLinearMapping = false,
94 DeviceMaskingAttrInterface mask = nullptr);
96 /// In the future this may be configured by the transformation.
97 static constexpr int64_t kNumWarpsPerGroup = 4;
98};
99
100/// Builder for warp ids used to map scf.forall to reindexed warps.
101/// If `useLinearMapping` is false, the `idBuilder` method returns 3D values
102/// used for indexing rewrites as well as 3D sizes for predicate generation.
103/// If `useLinearMapping` is true, the `idBuilder` method returns nD values
104/// used for indexing rewrites as well as 1D sizes for predicate generation.
105/// If `mask` is provided, it will be used to filter the active warps.
108 bool useLinearMapping = false,
109 DeviceMaskingAttrInterface mask = nullptr);
111};
112
113/// Builder for warp ids used to map scf.forall to reindexed threads.
114/// If `useLinearMapping` is false, the `idBuilder` method returns 3D values
115/// used for indexing rewrites as well as 3D sizes for predicate generation.
116/// If `useLinearMapping` is true, the `idBuilder` method returns nD values
117/// used for indexing rewrites as well as 1D sizes for predicate generation.
118/// If `mask` is provided, it will be used to filter the active threads.
120 GpuThreadIdBuilder(MLIRContext *ctx, bool useLinearMapping = false,
121 DeviceMaskingAttrInterface mask = nullptr);
122};
123
124/// Builder for lane id.
125/// The `idBuilder` method returns nD values used for indexing rewrites as well
126/// as 1D sizes for predicate generation.
127/// This `useLinearMapping` case is the only supported case.
128/// If `mask` is provided, it will be used to filter the active lanes.
130 GpuLaneIdBuilder(MLIRContext *ctx, int64_t warpSize, bool unused,
131 DeviceMaskingAttrInterface mask = nullptr);
133};
134
135/// Determine if the size of the kernel configuration is supported by the
136/// GPU architecture being used.
137/// TODO this is currently hardwired to CUDA, parameterize and generalize.
138DiagnosedSilenceableFailure checkGpuLimits(TransformOpInterface transformOp,
139 std::optional<int64_t> gridDimX,
140 std::optional<int64_t> gridDimY,
141 std::optional<int64_t> gridDimZ,
142 std::optional<int64_t> blockDimX,
143 std::optional<int64_t> blockDimY,
144 std::optional<int64_t> blockDimZ);
145
146/// Create an empty-body gpu::LaunchOp using the provided kernel settings
147/// and put a terminator within.
150 TransformOpInterface transformOp, mlir::gpu::LaunchOp &launchOp,
151 std::optional<int64_t> gridDimX = std::nullopt,
152 std::optional<int64_t> gridDimY = std::nullopt,
153 std::optional<int64_t> gridDimZ = std::nullopt,
154 std::optional<int64_t> blockDimX = std::nullopt,
155 std::optional<int64_t> blockDimY = std::nullopt,
156 std::optional<int64_t> blockDimZ = std::nullopt);
157
158/// Alter kernel configuration of the given kernel.
160alterGpuLaunch(RewriterBase &rewriter, mlir::gpu::LaunchOp gpuLaunch,
161 TransformOpInterface transformOp,
162 std::optional<int64_t> gridDimX = std::nullopt,
163 std::optional<int64_t> gridDimY = std::nullopt,
164 std::optional<int64_t> gridDimZ = std::nullopt,
165 std::optional<int64_t> blockDimX = std::nullopt,
166 std::optional<int64_t> blockDimY = std::nullopt,
167 std::optional<int64_t> blockDimZ = std::nullopt);
168
169/// Find the unique top level scf::ForallOp within a given target op.
171findTopLevelForallOp(Operation *target, scf::ForallOp &topLevelForallOp,
172 TransformOpInterface transformOp);
173
174} // namespace gpu
175} // namespace transform
176} // namespace mlir
177
178#endif // MLIR_DIALECT_GPU_TRANSFORMOPS_UTILS_H
The result of a transform IR operation application.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
DiagnosedSilenceableFailure findTopLevelForallOp(Operation *target, scf::ForallOp &topLevelForallOp, TransformOpInterface transformOp)
Find the unique top level scf::ForallOp within a given target op.
std::function< IdBuilderResult( RewriterBase &, Location, ArrayRef< int64_t >, ArrayRef< int64_t >)> GpuIdBuilderFnType
Common gpu id builder type, allows the configuration of lowering for various mapping schemes.
Definition Utils.h:54
DiagnosedSilenceableFailure alterGpuLaunch(RewriterBase &rewriter, mlir::gpu::LaunchOp gpuLaunch, TransformOpInterface transformOp, std::optional< int64_t > gridDimX=std::nullopt, std::optional< int64_t > gridDimY=std::nullopt, std::optional< int64_t > gridDimZ=std::nullopt, std::optional< int64_t > blockDimX=std::nullopt, std::optional< int64_t > blockDimY=std::nullopt, std::optional< int64_t > blockDimZ=std::nullopt)
Alter kernel configuration of the given kernel.
DiagnosedSilenceableFailure createGpuLaunch(RewriterBase &rewriter, Location loc, TransformOpInterface transformOp, mlir::gpu::LaunchOp &launchOp, std::optional< int64_t > gridDimX=std::nullopt, std::optional< int64_t > gridDimY=std::nullopt, std::optional< int64_t > gridDimZ=std::nullopt, std::optional< int64_t > blockDimX=std::nullopt, std::optional< int64_t > blockDimY=std::nullopt, std::optional< int64_t > blockDimZ=std::nullopt)
Create an empty-body gpu::LaunchOp using the provided kernel settings and put a terminator within.
raw_ostream & operator<<(raw_ostream &os, const IdBuilderResult &res)
Definition Utils.h:40
DiagnosedSilenceableFailure checkGpuLimits(TransformOpInterface transformOp, std::optional< int64_t > gridDimX, std::optional< int64_t > gridDimY, std::optional< int64_t > gridDimZ, std::optional< int64_t > blockDimX, std::optional< int64_t > blockDimY, std::optional< int64_t > blockDimZ)
Determine if the size of the kernel configuration is supported by the GPU architecture being used.
Definition Utils.cpp:362
Include the generated interface declarations.
GpuBlockIdBuilder(MLIRContext *ctx, bool useLinearMapping=false, DeviceMaskingAttrInterface mask=nullptr)
Definition Utils.cpp:298
std::function< DeviceMappingAttrInterface( MLIRContext *, mlir::gpu::MappingId)> MappingIdBuilderFnType
Definition Utils.h:60
SmallVector< DeviceMappingAttrInterface > mappingAttributes
The mapping attributes targeted by this generator.
Definition Utils.h:68
GpuIdBuilderFnType idBuilder
The constructor that builds the concrete IR for mapping ids.
Definition Utils.h:71
GpuLaneIdBuilder(MLIRContext *ctx, int64_t warpSize, bool unused, DeviceMaskingAttrInterface mask=nullptr)
Definition Utils.cpp:351
GpuThreadIdBuilder(MLIRContext *ctx, bool useLinearMapping=false, DeviceMaskingAttrInterface mask=nullptr)
Definition Utils.cpp:340
GpuWarpIdBuilder(MLIRContext *ctx, int64_t warpSize, bool useLinearMapping=false, DeviceMaskingAttrInterface mask=nullptr)
Definition Utils.cpp:325
GpuWarpgroupIdBuilder(MLIRContext *ctx, int64_t warpSize, bool useLinearMapping=false, DeviceMaskingAttrInterface mask=nullptr)
Definition Utils.cpp:309
static constexpr int64_t kNumWarpsPerGroup
In the future this may be configured by the transformation.
Definition Utils.h:97
Helper type for functions that generate ids for the mapping of a scf.forall.
Definition Utils.h:30
std::string errorMsg
Error message, if not empty then building the ids failed.
Definition Utils.h:32
SmallVector< Value > predicateOps
Values used to predicate the forall body when activeMappingSizes is smaller than the available mappin...
Definition Utils.h:37
SmallVector< Value > mappingIdOps
Values used to replace the forall induction variables.
Definition Utils.h:34