MLIR  19.0.0git
SCFToGPUPass.cpp
Go to the documentation of this file.
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 
10 
17 #include "mlir/Pass/Pass.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/Support/CommandLine.h"
21 
22 namespace mlir {
23 #define GEN_PASS_DEF_CONVERTAFFINEFORTOGPU
24 #define GEN_PASS_DEF_CONVERTPARALLELLOOPTOGPU
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 : public impl::ConvertAffineForToGPUBase<ForLoopMapper> {
36  ForLoopMapper() = default;
37  ForLoopMapper(unsigned numBlockDims, unsigned numThreadDims) {
38  this->numBlockDims = numBlockDims;
39  this->numThreadDims = numThreadDims;
40  }
41 
42  void runOnOperation() override {
43  for (Operation &op : llvm::make_early_inc_range(
44  getOperation().getFunctionBody().getOps())) {
45  if (auto forOp = dyn_cast<affine::AffineForOp>(&op)) {
46  if (failed(convertAffineLoopNestToGPULaunch(forOp, numBlockDims,
47  numThreadDims)))
48  signalPassFailure();
49  }
50  }
51  }
52 };
53 
54 struct ParallelLoopToGpuPass
55  : public impl::ConvertParallelLoopToGpuBase<ParallelLoopToGpuPass> {
56  void runOnOperation() override {
57  RewritePatternSet patterns(&getContext());
59  ConversionTarget target(getContext());
60  target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
62  if (failed(applyPartialConversion(getOperation(), target,
63  std::move(patterns))))
64  signalPassFailure();
66  }
67 };
68 
69 } // namespace
70 
71 std::unique_ptr<InterfacePass<FunctionOpInterface>>
72 mlir::createAffineForToGPUPass(unsigned numBlockDims, unsigned numThreadDims) {
73  return std::make_unique<ForLoopMapper>(numBlockDims, numThreadDims);
74 }
75 std::unique_ptr<InterfacePass<FunctionOpInterface>>
77  return std::make_unique<ForLoopMapper>();
78 }
79 
80 std::unique_ptr<Pass> mlir::createParallelLoopToGpuPass() {
81  return std::make_unique<ParallelLoopToGpuPass>();
82 }
static MLIRContext * getContext(OpFoldResult val)
This class describes a specific conversion target.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Include the generated interface declarations.
void finalizeParallelLoopToGPUConversion(Operation *op)
Clean up after applyPartialConversion/applyFullConversion call.
Definition: SCFToGPU.cpp:688
std::unique_ptr< InterfacePass< FunctionOpInterface > > createAffineForToGPUPass(unsigned numBlockDims, unsigned numThreadDims)
Create a pass that converts loop nests into GPU kernels.
void populateParallelLoopToGPUPatterns(RewritePatternSet &patterns)
Adds the conversion pattern from scf.parallel to gpu.launch to the provided pattern list.
Definition: SCFToGPU.cpp:676
LogicalResult convertAffineLoopNestToGPULaunch(affine::AffineForOp forOp, unsigned numBlockDims, unsigned numThreadDims)
Convert a perfect affine loop nest with the outermost loop identified by forOp into a gpu::Launch ope...
std::unique_ptr< Pass > createParallelLoopToGpuPass()
Creates a pass that converts scf.parallel operations into a gpu.launch operation.
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
void configureParallelLoopToGPULegality(ConversionTarget &target)
Configures the rewrite target such that only scf.parallel operations that are not rewritten by the pr...
Definition: SCFToGPU.cpp:680