| 1 | //===- SCFToGPUPass.cpp - Convert a loop nest to a GPU kernel -----------===// |
|---|---|
| 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/Conversion/SCFToGPU/SCFToGPUPass.h" |
| 10 | |
| 11 | #include "mlir/Conversion/SCFToGPU/SCFToGPU.h" |
| 12 | #include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 13 | #include "mlir/Dialect/GPU/IR/GPUDialect.h" |
| 14 | #include "mlir/Transforms/DialectConversion.h" |
| 15 | |
| 16 | namespace mlir { |
| 17 | #define GEN_PASS_DEF_CONVERTAFFINEFORTOGPUPASS |
| 18 | #define GEN_PASS_DEF_CONVERTPARALLELLOOPTOGPUPASS |
| 19 | #include "mlir/Conversion/Passes.h.inc" |
| 20 | } // namespace mlir |
| 21 | |
| 22 | using namespace mlir; |
| 23 | using namespace mlir::scf; |
| 24 | |
| 25 | namespace { |
| 26 | // A pass that traverses top-level loops in the function and converts them to |
| 27 | // GPU launch operations. Nested launches are not allowed, so this does not |
| 28 | // walk the function recursively to avoid considering nested loops. |
| 29 | struct ForLoopMapper |
| 30 | : public impl::ConvertAffineForToGPUPassBase<ForLoopMapper> { |
| 31 | using Base::Base; |
| 32 | |
| 33 | void runOnOperation() override { |
| 34 | for (Operation &op : llvm::make_early_inc_range( |
| 35 | Range: getOperation().getFunctionBody().getOps())) { |
| 36 | if (auto forOp = dyn_cast<affine::AffineForOp>(Val: &op)) { |
| 37 | if (failed(Result: convertAffineLoopNestToGPULaunch(forOp, numBlockDims, |
| 38 | numThreadDims))) |
| 39 | signalPassFailure(); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | struct ParallelLoopToGpuPass |
| 46 | : public impl::ConvertParallelLoopToGpuPassBase<ParallelLoopToGpuPass> { |
| 47 | void runOnOperation() override { |
| 48 | RewritePatternSet patterns(&getContext()); |
| 49 | populateParallelLoopToGPUPatterns(patterns); |
| 50 | ConversionTarget target(getContext()); |
| 51 | target.markUnknownOpDynamicallyLegal(fn: [](Operation *) { return true; }); |
| 52 | configureParallelLoopToGPULegality(target); |
| 53 | if (failed(Result: applyPartialConversion(op: getOperation(), target, |
| 54 | patterns: std::move(patterns)))) |
| 55 | signalPassFailure(); |
| 56 | finalizeParallelLoopToGPUConversion(op: getOperation()); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | } // namespace |
| 61 |
