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