1//===- OpenACCParMapping.h - OpenACC Parallelism Mapping -------*- C++ -*-===//
2//
3// Part of the MLIR 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 file defines the interface for mapping OpenACC parallelism levels
10// (gang, worker, vector) to target-specific parallel dimension attributes.
11//
12// Users can provide custom implementations of ACCParMappingPolicy to
13// support different mapping strategies and target attributes.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef MLIR_DIALECT_OPENACC_OPENACCPARMAPPING_H_
18#define MLIR_DIALECT_OPENACC_OPENACCPARMAPPING_H_
19
20#include "mlir/Dialect/OpenACC/OpenACC.h"
21#include "mlir/IR/MLIRContext.h"
22#include "llvm/Support/ErrorHandling.h"
23
24namespace mlir {
25namespace acc {
26
27//===----------------------------------------------------------------------===//
28// Utility functions
29//===----------------------------------------------------------------------===//
30
31/// Convert a gang dimension value (1, 2, or 3) to the corresponding ParLevel.
32/// Asserts if the value is not a valid gang dimension.
33inline ParLevel getGangParLevel(int64_t gangDimValue) {
34 assert((gangDimValue >= 1 && gangDimValue <= 3) &&
35 "gang dimension must be 1, 2, or 3");
36 switch (gangDimValue) {
37 case 1:
38 return ParLevel::gang_dim1;
39 case 2:
40 return ParLevel::gang_dim2;
41 case 3:
42 return ParLevel::gang_dim3;
43 }
44 llvm_unreachable("validated gang dimension");
45}
46
47//===----------------------------------------------------------------------===//
48// ACCParMappingPolicy
49//===----------------------------------------------------------------------===//
50
51/// Policy class that defines how OpenACC parallelism levels map to
52/// target-specific parallel dimension attributes. Implementations provide the
53/// actual mapping.
54///
55/// Template parameter ParDimAttrT specifies the attribute type returned by
56/// the mapping functions (e.g., mlir::acc::GPUParallelDimAttr for GPU targets).
57///
58/// This policy allows different mapping strategies:
59/// - Standard GPU mapping (gang->block, worker->threadY, vector->threadX)
60/// - Custom mappings for specific targets or optimization strategies
61///
62/// Pass an implementation to functions that need to perform the mapping.
63template <typename ParDimAttrT>
64class ACCParMappingPolicy {
65public:
66 virtual ~ACCParMappingPolicy() = default;
67
68 /// Map an OpenACC parallelism level to target dimension.
69 /// @param ctx The MLIR context
70 /// @param level The OpenACC parallelism level (gang_dim1, gang_dim2,
71 /// gang_dim3, worker, vector, or seq)
72 /// @return The corresponding parallel dimension attribute
73 virtual ParDimAttrT map(MLIRContext *ctx, ParLevel level) const = 0;
74
75 /// Convenience methods for specific parallelism levels.
76 ParDimAttrT gangDim(MLIRContext *ctx, ParLevel level) const {
77 assert((level == ParLevel::gang_dim1 || level == ParLevel::gang_dim2 ||
78 level == ParLevel::gang_dim3) &&
79 "gangDim requires a gang parallelism level");
80 return map(ctx, level);
81 }
82 ParDimAttrT workerDim(MLIRContext *ctx) const {
83 return map(ctx, level: ParLevel::worker);
84 }
85 ParDimAttrT vectorDim(MLIRContext *ctx) const {
86 return map(ctx, level: ParLevel::vector);
87 }
88 ParDimAttrT seqDim(MLIRContext *ctx) const { return map(ctx, level: ParLevel::seq); }
89
90 //===--------------------------------------------------------------------===//
91 // Predicate methods - check if an attribute matches a parallelism level
92 //===--------------------------------------------------------------------===//
93
94 /// Check if the attribute represents vector parallelism.
95 virtual bool isVector(ParDimAttrT attr) const = 0;
96
97 /// Check if the attribute represents worker parallelism.
98 virtual bool isWorker(ParDimAttrT attr) const = 0;
99
100 /// Check if the attribute represents gang parallelism (any gang dimension).
101 virtual bool isGang(ParDimAttrT attr) const = 0;
102
103 /// Check if the attribute represents sequential execution.
104 virtual bool isSeq(ParDimAttrT attr) const = 0;
105};
106
107//===----------------------------------------------------------------------===//
108// DefaultACCToGPUMappingPolicy
109//===----------------------------------------------------------------------===//
110
111/// Default policy that provides the standard GPU mapping:
112/// gang(dim:1) -> BlockX (gridDim.x / blockIdx.x)
113/// gang(dim:2) -> BlockY (gridDim.y / blockIdx.y)
114/// gang(dim:3) -> BlockZ (gridDim.z / blockIdx.z)
115/// worker -> ThreadY (blockDim.y / threadIdx.y)
116/// vector -> ThreadX (blockDim.x / threadIdx.x)
117/// seq -> Sequential
118class DefaultACCToGPUMappingPolicy
119 : public ACCParMappingPolicy<mlir::acc::GPUParallelDimAttr> {
120public:
121 mlir::acc::GPUParallelDimAttr map(MLIRContext *ctx,
122 ParLevel level) const override {
123 switch (level) {
124 case ParLevel::gang_dim1:
125 return mlir::acc::GPUParallelDimAttr::blockXDim(ctx);
126 case ParLevel::gang_dim2:
127 return mlir::acc::GPUParallelDimAttr::blockYDim(ctx);
128 case ParLevel::gang_dim3:
129 return mlir::acc::GPUParallelDimAttr::blockZDim(ctx);
130 case ParLevel::worker:
131 return mlir::acc::GPUParallelDimAttr::threadYDim(ctx);
132 case ParLevel::vector:
133 return mlir::acc::GPUParallelDimAttr::threadXDim(ctx);
134 case ParLevel::seq:
135 return mlir::acc::GPUParallelDimAttr::seqDim(ctx);
136 }
137 llvm_unreachable("Unknown ParLevel");
138 }
139
140 bool isVector(mlir::acc::GPUParallelDimAttr attr) const override {
141 return attr.isThreadX();
142 }
143
144 bool isWorker(mlir::acc::GPUParallelDimAttr attr) const override {
145 return attr.isThreadY();
146 }
147
148 bool isGang(mlir::acc::GPUParallelDimAttr attr) const override {
149 return attr.isAnyBlock();
150 }
151
152 bool isSeq(mlir::acc::GPUParallelDimAttr attr) const override {
153 return attr.isSeq();
154 }
155};
156
157/// Type alias for the GPU-specific mapping policy
158using ACCToGPUMappingPolicy =
159 ACCParMappingPolicy<mlir::acc::GPUParallelDimAttr>;
160
161} // namespace acc
162} // namespace mlir
163
164#endif // MLIR_DIALECT_OPENACC_OPENACCPARMAPPING_H_
165