| 1 | //===- Utils.cpp - Utils for GPU transform ops ----------------------------===// |
| 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 "mlir/Dialect/GPU/TransformOps/Utils.h" |
| 10 | |
| 11 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 12 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 13 | #include "mlir/Dialect/GPU/IR/GPUDialect.h" |
| 14 | #include "mlir/Dialect/GPU/TransformOps/GPUTransformOps.h" |
| 15 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 16 | #include "mlir/Dialect/NVGPU/IR/NVGPUDialect.h" |
| 17 | #include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.h" |
| 18 | #include "mlir/Dialect/Utils/IndexingUtils.h" |
| 19 | #include "mlir/Dialect/Vector/IR/VectorOps.h" |
| 20 | #include "mlir/IR/AffineExpr.h" |
| 21 | #include "mlir/IR/Builders.h" |
| 22 | #include "mlir/IR/BuiltinAttributes.h" |
| 23 | #include "mlir/IR/MLIRContext.h" |
| 24 | #include "mlir/IR/OpDefinition.h" |
| 25 | #include "mlir/IR/Value.h" |
| 26 | #include "mlir/IR/Visitors.h" |
| 27 | #include "mlir/Support/LLVM.h" |
| 28 | #include "llvm/ADT/STLExtras.h" |
| 29 | #include "llvm/ADT/SmallVector.h" |
| 30 | #include "llvm/Support/DebugLog.h" |
| 31 | #include "llvm/Support/InterleavedRange.h" |
| 32 | |
| 33 | #include <utility> |
| 34 | |
| 35 | using namespace mlir; |
| 36 | using namespace mlir::gpu; |
| 37 | using namespace mlir::transform; |
| 38 | using namespace mlir::transform::gpu; |
| 39 | |
| 40 | #define DEBUG_TYPE "gpu-transforms" |
| 41 | |
| 42 | /// Build predicates to filter execution by only the activeIds. Along each |
| 43 | /// dimension, 3 cases appear: |
| 44 | /// 1. activeMappingSize > availableMappingSize: this is an unsupported case |
| 45 | /// as this requires additional looping. An error message is produced to |
| 46 | /// advise the user to tile more or to use more threads. |
| 47 | /// 2. activeMappingSize == availableMappingSize: no predication is needed. |
| 48 | /// 3. activeMappingSize < availableMappingSize: only a subset of threads |
| 49 | /// should be active and we produce the boolean `id < activeMappingSize` |
| 50 | /// for further use in building predicated execution. |
| 51 | static FailureOr<SmallVector<Value>> |
| 52 | buildPredicates(RewriterBase &rewriter, Location loc, ArrayRef<Value> activeIds, |
| 53 | ArrayRef<int64_t> activeMappingSizes, |
| 54 | ArrayRef<int64_t> availableMappingSizes, |
| 55 | std::string &errorMsg) { |
| 56 | LDBG() << "----activeMappingSizes: " << llvm::interleaved(R: activeMappingSizes); |
| 57 | LDBG() << "----availableMappingSizes: " |
| 58 | << llvm::interleaved(R: availableMappingSizes); |
| 59 | |
| 60 | SmallVector<Value> predicateOps; |
| 61 | for (auto [activeId, activeMappingSize, availableMappingSize] : |
| 62 | llvm::zip_equal(t&: activeIds, u&: activeMappingSizes, args&: availableMappingSizes)) { |
| 63 | if (activeMappingSize > availableMappingSize) { |
| 64 | errorMsg = "Trying to map to fewer GPU threads than loop iterations but " |
| 65 | "overprovisioning is not yet supported. Try additional tiling " |
| 66 | "before mapping or map to more threads." ; |
| 67 | return failure(); |
| 68 | } |
| 69 | if (activeMappingSize == availableMappingSize) |
| 70 | continue; |
| 71 | Value idx = |
| 72 | arith::ConstantIndexOp::create(builder&: rewriter, location: loc, value: activeMappingSize); |
| 73 | Value pred = arith::CmpIOp::create(builder&: rewriter, location: loc, predicate: arith::CmpIPredicate::ult, |
| 74 | lhs: activeId, rhs: idx); |
| 75 | predicateOps.push_back(Elt: pred); |
| 76 | } |
| 77 | return predicateOps; |
| 78 | } |
| 79 | |
| 80 | /// Return a flattened thread id for the workgroup with given sizes. |
| 81 | template <typename ThreadOrBlockIdOp> |
| 82 | static Value buildLinearId(RewriterBase &rewriter, Location loc, |
| 83 | ArrayRef<OpFoldResult> originalBasisOfr) { |
| 84 | LDBG() << "----buildLinearId with originalBasisOfr: " |
| 85 | << llvm::interleaved(R: originalBasisOfr); |
| 86 | assert(originalBasisOfr.size() == 3 && "expected 3 sizes" ); |
| 87 | IndexType indexType = rewriter.getIndexType(); |
| 88 | AffineExpr tx, ty, tz, bdx, bdy; |
| 89 | bindDims(ctx: rewriter.getContext(), exprs&: tx, exprs&: ty, exprs&: tz); |
| 90 | bindSymbols(ctx: rewriter.getContext(), exprs&: bdx, exprs&: bdy); |
| 91 | SmallVector<OpFoldResult> vals{ |
| 92 | ThreadOrBlockIdOp::create(rewriter, loc, indexType, Dimension::x) |
| 93 | .getResult(), |
| 94 | ThreadOrBlockIdOp::create(rewriter, loc, indexType, Dimension::y) |
| 95 | .getResult(), |
| 96 | ThreadOrBlockIdOp::create(rewriter, loc, indexType, Dimension::z) |
| 97 | .getResult(), |
| 98 | originalBasisOfr[0], originalBasisOfr[1]}; |
| 99 | OpFoldResult ofr = affine::makeComposedFoldedAffineApply( |
| 100 | b&: rewriter, loc, expr: tx + ty * bdx + tz * bdx * bdy, operands: vals); |
| 101 | return getValueOrCreateConstantIndexOp(b&: rewriter, loc, ofr); |
| 102 | } |
| 103 | |
| 104 | /// Create a linear id builder that takes the `originalBasisOfr` and decompose |
| 105 | /// it in the basis of `forallMappingSizes`. The linear id builder returns an |
| 106 | /// n-D vector of ids for indexing and 1-D size + id for predicate generation. |
| 107 | template <typename ThreadOrBlockIdOp> |
| 108 | static GpuIdBuilderFnType |
| 109 | commonLinearIdBuilderFn(int64_t multiplicity = 1, |
| 110 | DeviceMaskingAttrInterface mask = nullptr) { |
| 111 | auto res = [multiplicity, mask](RewriterBase &rewriter, Location loc, |
| 112 | ArrayRef<int64_t> forallMappingSizes, |
| 113 | ArrayRef<int64_t> originalBasis) { |
| 114 | // 0. Early-exit mask case. |
| 115 | if (mask) { |
| 116 | if (computeProduct(basis: originalBasis) > |
| 117 | mask.getMaxNumPhysicalIds() * multiplicity) { |
| 118 | return IdBuilderResult{ |
| 119 | /*errorMsg=*/std::string( |
| 120 | "mask representation too short to capture all physical ids: " ) + |
| 121 | std::to_string(val: mask.getMaxNumPhysicalIds()), |
| 122 | /*mappingIdOps=*/{}, |
| 123 | /*predicateOps=*/{}}; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // 1. Compute linearId. |
| 128 | SmallVector<OpFoldResult> originalBasisOfr = |
| 129 | getAsIndexOpFoldResult(ctx: rewriter.getContext(), values: originalBasis); |
| 130 | Value physicalLinearId = |
| 131 | buildLinearId<ThreadOrBlockIdOp>(rewriter, loc, originalBasisOfr); |
| 132 | |
| 133 | // 2. Compute scaledLinearId. |
| 134 | AffineExpr d0 = getAffineDimExpr(position: 0, context: rewriter.getContext()); |
| 135 | OpFoldResult scaledLinearIdOfr = affine::makeComposedFoldedAffineApply( |
| 136 | b&: rewriter, loc, expr: d0.floorDiv(v: multiplicity), operands: {physicalLinearId}); |
| 137 | |
| 138 | // 2.b. Adjust with mask if needed. |
| 139 | Value scaledLinearIdI64; |
| 140 | Value scaledLinearId = |
| 141 | getValueOrCreateConstantIndexOp(b&: rewriter, loc, ofr: scaledLinearIdOfr); |
| 142 | if (mask) { |
| 143 | scaledLinearId = |
| 144 | getValueOrCreateConstantIndexOp(b&: rewriter, loc, ofr: scaledLinearIdOfr); |
| 145 | scaledLinearIdI64 = arith::IndexCastUIOp::create( |
| 146 | builder&: rewriter, location: loc, out: rewriter.getI64Type(), in: scaledLinearId); |
| 147 | Value logicalLinearIdI64 = |
| 148 | mask.createLogicalLinearMappingId(builder&: rewriter, physicalLinearMappingId: scaledLinearIdI64); |
| 149 | scaledLinearId = arith::IndexCastUIOp::create( |
| 150 | builder&: rewriter, location: loc, out: rewriter.getIndexType(), in: logicalLinearIdI64); |
| 151 | LDBG() << "------adjusting linearId with mask: " << scaledLinearId; |
| 152 | } |
| 153 | |
| 154 | // 3. Compute remapped indices. |
| 155 | SmallVector<Value> ids; |
| 156 | // Sizes in [0 .. n] -> [n .. 0] order to properly compute strides in |
| 157 | // "row-major" order. |
| 158 | SmallVector<int64_t> reverseBasisSizes(llvm::reverse(C&: forallMappingSizes)); |
| 159 | SmallVector<int64_t> strides = computeStrides(sizes: reverseBasisSizes); |
| 160 | SmallVector<AffineExpr> delinearizingExprs = delinearize(linearIndex: d0, strides); |
| 161 | // Reverse back to be in [0 .. n] order. |
| 162 | for (AffineExpr e : llvm::reverse(C&: delinearizingExprs)) { |
| 163 | ids.push_back( |
| 164 | Elt: affine::makeComposedAffineApply(b&: rewriter, loc, e, operands: {scaledLinearId})); |
| 165 | } |
| 166 | |
| 167 | std::string errorMsg; |
| 168 | SmallVector<Value> predicateOps; |
| 169 | // 4. If mask present, it takes precedence to determine predication. |
| 170 | if (mask) { |
| 171 | Value isActiveIdPredicate = |
| 172 | mask.createIsActiveIdPredicate(builder&: rewriter, physicalLinearMappingId: scaledLinearIdI64); |
| 173 | LDBG() << "------adjusting predicate with mask: " << isActiveIdPredicate; |
| 174 | predicateOps.push_back(Elt: isActiveIdPredicate); |
| 175 | } else { |
| 176 | // 4.b. Otherwise, handle predicates using physicalLinearId. |
| 177 | FailureOr<SmallVector<Value>> maybePredicateOps = |
| 178 | buildPredicates(rewriter, loc, activeIds: physicalLinearId, |
| 179 | activeMappingSizes: computeProduct(basis: forallMappingSizes) * multiplicity, |
| 180 | availableMappingSizes: computeProduct(basis: originalBasis), errorMsg); |
| 181 | if (succeeded(Result: maybePredicateOps)) |
| 182 | predicateOps = std::move(*maybePredicateOps); |
| 183 | } |
| 184 | |
| 185 | return IdBuilderResult{/*errorMsg=*/std::move(errorMsg), |
| 186 | /*mappingIdOps=*/std::move(ids), |
| 187 | /*predicateOps=*/std::move(predicateOps)}; |
| 188 | }; |
| 189 | |
| 190 | return res; |
| 191 | } |
| 192 | |
| 193 | /// Create a simple 3-D id builder that takes the `originalBasisOfr` |
| 194 | /// The 3-D id builder returns a 3-D vector of ids for indexing and 3-D sizes |
| 195 | /// + ids for predicate generation. |
| 196 | template <typename ThreadOrBlockIdOp> |
| 197 | static GpuIdBuilderFnType common3DIdBuilderFn(int64_t multiplicity = 1) { |
| 198 | auto res = [multiplicity](RewriterBase &rewriter, Location loc, |
| 199 | ArrayRef<int64_t> forallMappingSizes, |
| 200 | ArrayRef<int64_t> originalBasis) { |
| 201 | IndexType indexType = rewriter.getIndexType(); |
| 202 | SmallVector<Value> ids{ |
| 203 | ThreadOrBlockIdOp::create(rewriter, loc, indexType, Dimension::x), |
| 204 | ThreadOrBlockIdOp::create(rewriter, loc, indexType, Dimension::y), |
| 205 | ThreadOrBlockIdOp::create(rewriter, loc, indexType, Dimension::z)}; |
| 206 | // In the 3-D mapping case, scale the first dimension by the multiplicity. |
| 207 | SmallVector<Value> scaledIds = ids; |
| 208 | AffineExpr d0 = getAffineDimExpr(position: 0, context: rewriter.getContext()); |
| 209 | scaledIds[0] = cast<Value>(Val: affine::makeComposedFoldedAffineApply( |
| 210 | b&: rewriter, loc, expr: d0.floorDiv(v: multiplicity), operands: {scaledIds[0]})); |
| 211 | // In the 3-D mapping case, unscale the first dimension by the multiplicity. |
| 212 | SmallVector<int64_t> forallMappingSizeInOriginalBasis(forallMappingSizes); |
| 213 | forallMappingSizeInOriginalBasis[0] *= multiplicity; |
| 214 | |
| 215 | std::string errorMsg; |
| 216 | SmallVector<Value> predicateOps; |
| 217 | FailureOr<SmallVector<Value>> maybePredicateOps = |
| 218 | buildPredicates(rewriter, loc, activeIds: ids, activeMappingSizes: forallMappingSizeInOriginalBasis, |
| 219 | availableMappingSizes: originalBasis, errorMsg); |
| 220 | if (succeeded(Result: maybePredicateOps)) |
| 221 | predicateOps = std::move(*maybePredicateOps); |
| 222 | |
| 223 | return IdBuilderResult{/*errorMsg=*/std::move(errorMsg), |
| 224 | /*mappingIdOps=*/std::move(scaledIds), |
| 225 | /*predicateOps=*/std::move(predicateOps)}; |
| 226 | }; |
| 227 | return res; |
| 228 | } |
| 229 | |
| 230 | /// Create a lane id builder that takes the `originalBasis` and decompose |
| 231 | /// it in the basis of `forallMappingSizes`. The linear id builder returns an |
| 232 | /// n-D vector of ids for indexing and 1-D size + id for predicate generation. |
| 233 | static GpuIdBuilderFnType laneIdBuilderFn(int64_t warpSize) { |
| 234 | auto res = [warpSize](RewriterBase &rewriter, Location loc, |
| 235 | ArrayRef<int64_t> forallMappingSizes, |
| 236 | ArrayRef<int64_t> originalBasis) { |
| 237 | // 1. Compute linearId. |
| 238 | SmallVector<OpFoldResult> originalBasisOfr = |
| 239 | getAsIndexOpFoldResult(ctx: rewriter.getContext(), values: originalBasis); |
| 240 | Value physicalLinearId = |
| 241 | buildLinearId<ThreadIdOp>(rewriter, loc, originalBasisOfr); |
| 242 | |
| 243 | // 2. Compute laneId. |
| 244 | AffineExpr d0 = getAffineDimExpr(position: 0, context: rewriter.getContext()); |
| 245 | OpFoldResult laneId = affine::makeComposedFoldedAffineApply( |
| 246 | b&: rewriter, loc, expr: d0 % warpSize, operands: {physicalLinearId}); |
| 247 | |
| 248 | // 3. Compute remapped indices. |
| 249 | SmallVector<Value> ids; |
| 250 | // Sizes in [0 .. n] -> [n .. 0] order to properly compute strides in |
| 251 | // "row-major" order. |
| 252 | SmallVector<int64_t> reverseBasisSizes(llvm::reverse(C&: forallMappingSizes)); |
| 253 | SmallVector<int64_t> strides = computeStrides(sizes: reverseBasisSizes); |
| 254 | SmallVector<AffineExpr> delinearizingExprs = delinearize(linearIndex: d0, strides); |
| 255 | // Reverse back to be in [0 .. n] order. |
| 256 | for (AffineExpr e : llvm::reverse(C&: delinearizingExprs)) { |
| 257 | ids.push_back( |
| 258 | Elt: affine::makeComposedAffineApply(b&: rewriter, loc, e, operands: {laneId})); |
| 259 | } |
| 260 | |
| 261 | // 4. Handle predicates using laneId. |
| 262 | std::string errorMsg; |
| 263 | SmallVector<Value> predicateOps; |
| 264 | FailureOr<SmallVector<Value>> maybePredicateOps = buildPredicates( |
| 265 | rewriter, loc, activeIds: cast<Value>(Val&: laneId), activeMappingSizes: computeProduct(basis: forallMappingSizes), |
| 266 | availableMappingSizes: computeProduct(basis: originalBasis), errorMsg); |
| 267 | if (succeeded(Result: maybePredicateOps)) |
| 268 | predicateOps = std::move(*maybePredicateOps); |
| 269 | |
| 270 | return IdBuilderResult{/*errorMsg=*/std::move(errorMsg), |
| 271 | /*mappingIdOps=*/std::move(ids), |
| 272 | /*predicateOps=*/std::move(predicateOps)}; |
| 273 | }; |
| 274 | |
| 275 | return res; |
| 276 | } |
| 277 | |
| 278 | namespace mlir { |
| 279 | namespace transform { |
| 280 | namespace gpu { |
| 281 | |
| 282 | GpuIdBuilder::GpuIdBuilder(MLIRContext *ctx, bool useLinearMapping, |
| 283 | const MappingIdBuilderFnType &fn) |
| 284 | : mappingAttributes(), idBuilder() { |
| 285 | if (useLinearMapping) { |
| 286 | for (uint64_t d = static_cast<uint64_t>(MappingId::LinearDim0), |
| 287 | e = getMaxEnumValForMappingId(); |
| 288 | d <= e; ++d) |
| 289 | mappingAttributes.push_back(Elt: fn(ctx, symbolizeMappingId(d).value())); |
| 290 | } else { |
| 291 | for (uint64_t d = static_cast<uint64_t>(MappingId::DimX), |
| 292 | e = static_cast<uint64_t>(MappingId::DimZ); |
| 293 | d <= e; ++d) |
| 294 | mappingAttributes.push_back(Elt: fn(ctx, symbolizeMappingId(d).value())); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | GpuBlockIdBuilder::GpuBlockIdBuilder(MLIRContext *ctx, bool useLinearMapping, |
| 299 | DeviceMaskingAttrInterface mask) |
| 300 | : GpuIdBuilder(ctx, useLinearMapping, [](MLIRContext *ctx, MappingId id) { |
| 301 | return GPUBlockMappingAttr::get(context: ctx, block: id); |
| 302 | }) { |
| 303 | assert((!mask || useLinearMapping) && "mask requires linear mapping" ); |
| 304 | idBuilder = useLinearMapping |
| 305 | ? commonLinearIdBuilderFn<BlockIdOp>(/*multiplicity=*/1, mask) |
| 306 | : common3DIdBuilderFn<BlockIdOp>(/*multiplicity=*/1); |
| 307 | } |
| 308 | |
| 309 | GpuWarpgroupIdBuilder::GpuWarpgroupIdBuilder(MLIRContext *ctx, int64_t warpSize, |
| 310 | bool useLinearMapping, |
| 311 | DeviceMaskingAttrInterface mask) |
| 312 | : GpuIdBuilder(ctx, useLinearMapping, |
| 313 | [](MLIRContext *ctx, MappingId id) { |
| 314 | return GPUWarpgroupMappingAttr::get(context: ctx, warpgroup: id); |
| 315 | }), |
| 316 | warpSize(warpSize) { |
| 317 | assert((!mask || useLinearMapping) && "mask requires linear mapping" ); |
| 318 | idBuilder = useLinearMapping |
| 319 | ? commonLinearIdBuilderFn<ThreadIdOp>( |
| 320 | /*multiplicity=*/kNumWarpsPerGroup * warpSize, mask) |
| 321 | : common3DIdBuilderFn<ThreadIdOp>( |
| 322 | /*multiplicity=*/kNumWarpsPerGroup * warpSize); |
| 323 | } |
| 324 | |
| 325 | GpuWarpIdBuilder::GpuWarpIdBuilder(MLIRContext *ctx, int64_t warpSize, |
| 326 | bool useLinearMapping, |
| 327 | DeviceMaskingAttrInterface mask) |
| 328 | : GpuIdBuilder(ctx, useLinearMapping, |
| 329 | [](MLIRContext *ctx, MappingId id) { |
| 330 | return GPUWarpMappingAttr::get(context: ctx, warp: id); |
| 331 | }), |
| 332 | warpSize(warpSize) { |
| 333 | assert((!mask || useLinearMapping) && "mask requires linear mapping" ); |
| 334 | idBuilder = useLinearMapping |
| 335 | ? commonLinearIdBuilderFn<ThreadIdOp>( |
| 336 | /*multiplicity=*/warpSize, mask) |
| 337 | : common3DIdBuilderFn<ThreadIdOp>(/*multiplicity=*/warpSize); |
| 338 | } |
| 339 | |
| 340 | GpuThreadIdBuilder::GpuThreadIdBuilder(MLIRContext *ctx, bool useLinearMapping, |
| 341 | DeviceMaskingAttrInterface mask) |
| 342 | : GpuIdBuilder(ctx, useLinearMapping, [](MLIRContext *ctx, MappingId id) { |
| 343 | return GPUThreadMappingAttr::get(context: ctx, thread: id); |
| 344 | }) { |
| 345 | idBuilder = |
| 346 | useLinearMapping |
| 347 | ? commonLinearIdBuilderFn<ThreadIdOp>(/*multiplicity=*/1, mask) |
| 348 | : common3DIdBuilderFn<ThreadIdOp>(/*multiplicity=*/1); |
| 349 | } |
| 350 | |
| 351 | GpuLaneIdBuilder::GpuLaneIdBuilder(MLIRContext *ctx, int64_t warpSize, |
| 352 | bool unused, DeviceMaskingAttrInterface mask) |
| 353 | : GpuIdBuilder(ctx, /*useLinearMapping=*/true, |
| 354 | [](MLIRContext *ctx, MappingId id) { |
| 355 | return GPULaneMappingAttr::get(context: ctx, lane: id); |
| 356 | }), |
| 357 | warpSize(warpSize) { |
| 358 | assert(!mask && "mask NYI for lanes, unclear it should be at all" ); |
| 359 | idBuilder = laneIdBuilderFn(/*periodicity=*/warpSize); |
| 360 | } |
| 361 | |
| 362 | DiagnosedSilenceableFailure checkGpuLimits(TransformOpInterface transformOp, |
| 363 | std::optional<int64_t> gridDimX, |
| 364 | std::optional<int64_t> gridDimY, |
| 365 | std::optional<int64_t> gridDimZ, |
| 366 | std::optional<int64_t> blockDimX, |
| 367 | std::optional<int64_t> blockDimY, |
| 368 | std::optional<int64_t> blockDimZ) { |
| 369 | |
| 370 | // TODO: pass a configuration object to set the limits properly. |
| 371 | |
| 372 | if ((blockDimX.value_or(u: 1) * blockDimY.value_or(u: 1) * blockDimZ.value_or(u: 1)) > |
| 373 | kMaxTotalBlockdim || |
| 374 | (gridDimX.value_or(u: 1) * gridDimY.value_or(u: 1) * gridDimZ.value_or(u: 1)) > |
| 375 | kMaxTotalGriddim || |
| 376 | blockDimX.value_or(u: 1) > kMaxBlockdimx || |
| 377 | blockDimY.value_or(u: 1) > kMaxBlockdimy || |
| 378 | blockDimZ.value_or(u: 1) > kMaxBlockdimz || |
| 379 | gridDimY.value_or(u: 1) > kMaxGriddimy || |
| 380 | gridDimZ.value_or(u: 1) > kMaxGriddimz || |
| 381 | gridDimX.value_or(u: 1) > kMaxGriddimx) { |
| 382 | return transformOp.emitSilenceableError() |
| 383 | << "Trying to launch a GPU kernel with grid_dims = (" |
| 384 | << gridDimX.value_or(u: 1) << ", " << gridDimY.value_or(u: 1) << ", " |
| 385 | << gridDimZ.value_or(u: 1) << ") block_dims = (" |
| 386 | << blockDimX.value_or(u: 1) << ", " << blockDimY.value_or(u: 1) << ", " |
| 387 | << blockDimZ.value_or(u: 1) << "). It is larger than the limits." ; |
| 388 | } |
| 389 | return DiagnosedSilenceableFailure::success(); |
| 390 | } |
| 391 | |
| 392 | DiagnosedSilenceableFailure createGpuLaunch( |
| 393 | RewriterBase &rewriter, Location loc, TransformOpInterface transformOp, |
| 394 | LaunchOp &launchOp, std::optional<int64_t> gridDimX, |
| 395 | std::optional<int64_t> gridDimY, std::optional<int64_t> gridDimZ, |
| 396 | std::optional<int64_t> blockDimX, std::optional<int64_t> blockDimY, |
| 397 | std::optional<int64_t> blockDimZ) { |
| 398 | DiagnosedSilenceableFailure diag = |
| 399 | checkGpuLimits(transformOp, gridDimX, gridDimY, gridDimZ, blockDimX, |
| 400 | blockDimY, blockDimZ); |
| 401 | if (!diag.succeeded()) |
| 402 | return diag; |
| 403 | |
| 404 | auto createConst = [&](int dim) { |
| 405 | return arith::ConstantIndexOp::create(builder&: rewriter, location: loc, value: dim); |
| 406 | }; |
| 407 | OpBuilder::InsertionGuard guard(rewriter); |
| 408 | Value one = createConst(1); |
| 409 | Value gridSizeX = gridDimX.has_value() ? createConst(gridDimX.value()) : one; |
| 410 | Value gridSizeY = gridDimY.has_value() ? createConst(gridDimY.value()) : one; |
| 411 | Value gridSizeZ = gridDimZ.has_value() ? createConst(gridDimZ.value()) : one; |
| 412 | Value blkSizeX = blockDimX.has_value() ? createConst(blockDimX.value()) : one; |
| 413 | Value blkSizeY = blockDimY.has_value() ? createConst(blockDimY.value()) : one; |
| 414 | Value blkSizeZ = blockDimZ.has_value() ? createConst(blockDimZ.value()) : one; |
| 415 | launchOp = LaunchOp::create(builder&: rewriter, location: loc, gridSizeX, gridSizeY, gridSizeZ, |
| 416 | blockSizeX: blkSizeX, blockSizeY: blkSizeY, blockSizeZ: blkSizeZ); |
| 417 | rewriter.setInsertionPointToEnd(&launchOp.getBody().front()); |
| 418 | TerminatorOp::create(builder&: rewriter, location: loc); |
| 419 | return DiagnosedSilenceableFailure::success(); |
| 420 | } |
| 421 | |
| 422 | /// Alter kernel configuration of the given kernel. |
| 423 | DiagnosedSilenceableFailure alterGpuLaunch( |
| 424 | RewriterBase &rewriter, LaunchOp gpuLaunch, |
| 425 | TransformOpInterface transformOp, std::optional<int64_t> gridDimX, |
| 426 | std::optional<int64_t> gridDimY, std::optional<int64_t> gridDimZ, |
| 427 | std::optional<int64_t> blockDimX, std::optional<int64_t> blockDimY, |
| 428 | std::optional<int64_t> blockDimZ) { |
| 429 | DiagnosedSilenceableFailure diag = |
| 430 | checkGpuLimits(transformOp, gridDimX, gridDimY, gridDimZ, blockDimX, |
| 431 | blockDimY, blockDimZ); |
| 432 | if (!diag.succeeded()) |
| 433 | return diag; |
| 434 | |
| 435 | KernelDim3 currentBlockdim = gpuLaunch.getBlockSizeOperandValues(); |
| 436 | OpBuilder::InsertionGuard guard(rewriter); |
| 437 | rewriter.setInsertionPointAfterValue(currentBlockdim.x); |
| 438 | auto createConstValue = [&](int dim) { |
| 439 | return arith::ConstantIndexOp::create(builder&: rewriter, location: currentBlockdim.x.getLoc(), |
| 440 | value: dim); |
| 441 | }; |
| 442 | |
| 443 | if (gridDimX.has_value()) |
| 444 | gpuLaunch.getGridSizeXMutable().assign(value: createConstValue(gridDimX.value())); |
| 445 | if (gridDimY.has_value()) |
| 446 | gpuLaunch.getGridSizeYMutable().assign(value: createConstValue(gridDimY.value())); |
| 447 | if (gridDimZ.has_value()) |
| 448 | gpuLaunch.getGridSizeZMutable().assign(value: createConstValue(gridDimZ.value())); |
| 449 | if (blockDimX.has_value()) |
| 450 | gpuLaunch.getBlockSizeXMutable().assign( |
| 451 | value: createConstValue(blockDimX.value())); |
| 452 | if (blockDimY.has_value()) |
| 453 | gpuLaunch.getBlockSizeYMutable().assign( |
| 454 | value: createConstValue(blockDimY.value())); |
| 455 | if (blockDimZ.has_value()) |
| 456 | gpuLaunch.getBlockSizeZMutable().assign( |
| 457 | value: createConstValue(blockDimZ.value())); |
| 458 | return DiagnosedSilenceableFailure::success(); |
| 459 | } |
| 460 | |
| 461 | } // namespace gpu |
| 462 | } // namespace transform |
| 463 | } // namespace mlir |
| 464 | |