• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ParallelLoopMapper.h - Utilities for mapping parallel loops to GPU ====//
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 // This header file declares the utilities to generate mappings for parallel
10 // loops to GPU devices.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_DIALECT_GPU_PARALLELLOOPMAPPER_H
15 #define MLIR_DIALECT_GPU_PARALLELLOOPMAPPER_H
16 
17 #include "mlir/IR/BuiltinAttributes.h"
18 #include "mlir/Support/LLVM.h"
19 #include "llvm/ADT/DenseMap.h"
20 
21 #include "mlir/Dialect/GPU/ParallelLoopMapperEnums.h.inc"
22 
23 namespace mlir {
24 
25 class AffineMap;
26 struct LogicalResult;
27 class Operation;
28 class Region;
29 
30 } // namespace mlir
31 
32 #include "mlir/Dialect/GPU/ParallelLoopMapperAttr.h.inc"
33 
34 namespace mlir {
35 namespace scf {
36 class ParallelOp;
37 }
38 
39 namespace gpu {
40 
41 /// Name of the mapping attribute produced by loop mappers.
42 StringRef getMappingAttrName();
43 
44 /// Get the value of the processor in the ParallelLoopDimMapping attribute.
getProcessor(ParallelLoopDimMapping attr)45 inline Processor getProcessor(ParallelLoopDimMapping attr) {
46   return static_cast<Processor>(attr.processor().getInt());
47 }
48 
49 /// Helper function to create a ParallelDimMapperAttr.
50 /// TODO: Replace its uses with an auto-gened method.
51 ParallelLoopDimMapping getParallelLoopDimMappingAttr(Processor processor,
52                                                      AffineMap map,
53                                                      AffineMap bound);
54 
55 /// Sets the mapping attribute of a scf.parallel operation. Verifies that the
56 /// mapping passed is valid.
57 /// - the number of DimMapperAttr provided is same as the number of loops of
58 ///   the `ploopOp`.
59 /// - the mapping does not map multiple loops to the same processor.
60 LogicalResult setMappingAttr(scf::ParallelOp ploopOp,
61                              ArrayRef<ParallelLoopDimMapping> mapping);
62 } // end namespace gpu
63 
64 /// Maps the parallel loops found in the given function to workgroups. The first
65 /// loop encountered will be mapped to the global workgroup and the second loop
66 /// encountered to the local workgroup. Within each mapping, the first three
67 /// dimensions are mapped to x/y/z hardware ids and all following dimensions are
68 /// mapped to sequential loops.
69 void greedilyMapParallelSCFToGPU(Region &region);
70 
71 } // end namespace mlir
72 #endif // MLIR_DIALECT_GPU_PARALLELLOOPMAPPER_H
73