clang 22.0.0git
CIRGenStmtOpenACC.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// Emit OpenACC Stmt nodes as CIR code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenBuilder.h"
14#include "CIRGenFunction.h"
15#include "mlir/Dialect/OpenACC/OpenACC.h"
18
19using namespace clang;
20using namespace clang::CIRGen;
21using namespace cir;
22using namespace mlir::acc;
23
24template <typename Op, typename TermOp>
25mlir::LogicalResult CIRGenFunction::emitOpenACCOpAssociatedStmt(
26 mlir::Location start, mlir::Location end, OpenACCDirectiveKind dirKind,
28 const Stmt *associatedStmt) {
29 mlir::LogicalResult res = mlir::success();
30
33 auto op = builder.create<Op>(start, retTy, operands);
34
35 emitOpenACCClauses(op, dirKind, dirLoc, clauses);
36
37 {
38 mlir::Block &block = op.getRegion().emplaceBlock();
39 mlir::OpBuilder::InsertionGuard guardCase(builder);
40 builder.setInsertionPointToEnd(&block);
41
42 LexicalScope ls{*this, start, builder.getInsertionBlock()};
43 res = emitStmt(associatedStmt, /*useCurrentScope=*/true);
44
45 builder.create<TermOp>(end);
46 }
47 return res;
48}
49
50namespace {
51template <typename Op> struct CombinedType;
52template <> struct CombinedType<ParallelOp> {
53 static constexpr mlir::acc::CombinedConstructsType value =
54 mlir::acc::CombinedConstructsType::ParallelLoop;
55};
56template <> struct CombinedType<SerialOp> {
57 static constexpr mlir::acc::CombinedConstructsType value =
58 mlir::acc::CombinedConstructsType::SerialLoop;
59};
60template <> struct CombinedType<KernelsOp> {
61 static constexpr mlir::acc::CombinedConstructsType value =
62 mlir::acc::CombinedConstructsType::KernelsLoop;
63};
64} // namespace
65
66template <typename Op, typename TermOp>
67mlir::LogicalResult CIRGenFunction::emitOpenACCOpCombinedConstruct(
68 mlir::Location start, mlir::Location end, OpenACCDirectiveKind dirKind,
70 const Stmt *loopStmt) {
71 mlir::LogicalResult res = mlir::success();
72
75
76 auto computeOp = builder.create<Op>(start, retTy, operands);
77 computeOp.setCombinedAttr(builder.getUnitAttr());
78 mlir::acc::LoopOp loopOp;
79
80 // First, emit the bodies of both operations, with the loop inside the body of
81 // the combined construct.
82 {
83 mlir::Block &block = computeOp.getRegion().emplaceBlock();
84 mlir::OpBuilder::InsertionGuard guardCase(builder);
85 builder.setInsertionPointToEnd(&block);
86
87 LexicalScope ls{*this, start, builder.getInsertionBlock()};
88 auto loopOp = builder.create<LoopOp>(start, retTy, operands);
89 loopOp.setCombinedAttr(mlir::acc::CombinedConstructsTypeAttr::get(
90 builder.getContext(), CombinedType<Op>::value));
91
92 {
93 mlir::Block &innerBlock = loopOp.getRegion().emplaceBlock();
94 mlir::OpBuilder::InsertionGuard guardCase(builder);
95 builder.setInsertionPointToEnd(&innerBlock);
96
97 LexicalScope ls{*this, start, builder.getInsertionBlock()};
98 ActiveOpenACCLoopRAII activeLoop{*this, &loopOp};
99
100 res = emitStmt(loopStmt, /*useCurrentScope=*/true);
101
102 builder.create<mlir::acc::YieldOp>(end);
103 }
104
105 emitOpenACCClauses(computeOp, loopOp, dirKind, dirLoc, clauses);
106
107 updateLoopOpParallelism(loopOp, /*isOrphan=*/false, dirKind);
108
109 builder.create<TermOp>(end);
110 }
111
112 return res;
113}
114
115template <typename Op>
116Op CIRGenFunction::emitOpenACCOp(
117 mlir::Location start, OpenACCDirectiveKind dirKind, SourceLocation dirLoc,
121 auto op = builder.create<Op>(start, retTy, operands);
122
123 emitOpenACCClauses(op, dirKind, dirLoc, clauses);
124 return op;
125}
126
127mlir::LogicalResult
129 mlir::Location start = getLoc(s.getSourceRange().getBegin());
130 mlir::Location end = getLoc(s.getSourceRange().getEnd());
131
132 switch (s.getDirectiveKind()) {
134 return emitOpenACCOpAssociatedStmt<ParallelOp, mlir::acc::YieldOp>(
135 start, end, s.getDirectiveKind(), s.getDirectiveLoc(), s.clauses(),
136 s.getStructuredBlock());
138 return emitOpenACCOpAssociatedStmt<SerialOp, mlir::acc::YieldOp>(
139 start, end, s.getDirectiveKind(), s.getDirectiveLoc(), s.clauses(),
140 s.getStructuredBlock());
142 return emitOpenACCOpAssociatedStmt<KernelsOp, mlir::acc::TerminatorOp>(
143 start, end, s.getDirectiveKind(), s.getDirectiveLoc(), s.clauses(),
144 s.getStructuredBlock());
145 default:
146 llvm_unreachable("invalid compute construct kind");
147 }
148}
149
150mlir::LogicalResult
152 mlir::Location start = getLoc(s.getSourceRange().getBegin());
153 mlir::Location end = getLoc(s.getSourceRange().getEnd());
154
155 return emitOpenACCOpAssociatedStmt<DataOp, mlir::acc::TerminatorOp>(
156 start, end, s.getDirectiveKind(), s.getDirectiveLoc(), s.clauses(),
157 s.getStructuredBlock());
158}
159
160mlir::LogicalResult
162 mlir::Location start = getLoc(s.getSourceRange().getBegin());
163 emitOpenACCOp<InitOp>(start, s.getDirectiveKind(), s.getDirectiveLoc(),
164 s.clauses());
165 return mlir::success();
166}
167
168mlir::LogicalResult
170 mlir::Location start = getLoc(s.getSourceRange().getBegin());
171 emitOpenACCOp<SetOp>(start, s.getDirectiveKind(), s.getDirectiveLoc(),
172 s.clauses());
173 return mlir::success();
174}
175
178 mlir::Location start = getLoc(s.getSourceRange().getBegin());
179 emitOpenACCOp<ShutdownOp>(start, s.getDirectiveKind(),
180 s.getDirectiveLoc(), s.clauses());
181 return mlir::success();
182}
183
184mlir::LogicalResult
186 mlir::Location start = getLoc(s.getSourceRange().getBegin());
187 auto waitOp = emitOpenACCOp<WaitOp>(start, s.getDirectiveKind(),
188 s.getDirectiveLoc(), s.clauses());
189
190 auto createIntExpr = [this](const Expr *intExpr) {
191 mlir::Value expr = emitScalarExpr(intExpr);
192 mlir::Location exprLoc = cgm.getLoc(intExpr->getBeginLoc());
193
194 mlir::IntegerType targetType = mlir::IntegerType::get(
195 &getMLIRContext(), getContext().getIntWidth(intExpr->getType()),
196 intExpr->getType()->isSignedIntegerOrEnumerationType()
197 ? mlir::IntegerType::SignednessSemantics::Signed
198 : mlir::IntegerType::SignednessSemantics::Unsigned);
199
200 auto conversionOp = builder.create<mlir::UnrealizedConversionCastOp>(
201 exprLoc, targetType, expr);
202 return conversionOp.getResult(0);
203 };
204
205 // Emit the correct 'wait' clauses.
206 {
207 mlir::OpBuilder::InsertionGuard guardCase(builder);
208 builder.setInsertionPoint(waitOp);
209
210 if (s.hasDevNumExpr())
211 waitOp.getWaitDevnumMutable().append(createIntExpr(s.getDevNumExpr()));
212
213 for (Expr *QueueExpr : s.getQueueIdExprs())
214 waitOp.getWaitOperandsMutable().append(createIntExpr(QueueExpr));
215 }
216
217 return mlir::success();
218}
219
222 mlir::Location start = getLoc(s.getSourceRange().getBegin());
223 mlir::Location end = getLoc(s.getSourceRange().getEnd());
224
225 switch (s.getDirectiveKind()) {
227 return emitOpenACCOpCombinedConstruct<ParallelOp, mlir::acc::YieldOp>(
228 start, end, s.getDirectiveKind(), s.getDirectiveLoc(), s.clauses(),
229 s.getLoop());
231 return emitOpenACCOpCombinedConstruct<SerialOp, mlir::acc::YieldOp>(
232 start, end, s.getDirectiveKind(), s.getDirectiveLoc(), s.clauses(),
233 s.getLoop());
235 return emitOpenACCOpCombinedConstruct<KernelsOp, mlir::acc::TerminatorOp>(
236 start, end, s.getDirectiveKind(), s.getDirectiveLoc(), s.clauses(),
237 s.getLoop());
238 default:
239 llvm_unreachable("invalid compute construct kind");
240 }
241}
242
245 mlir::Location start = getLoc(s.getSourceRange().getBegin());
246 mlir::Location end = getLoc(s.getSourceRange().getEnd());
247
248 return emitOpenACCOpAssociatedStmt<HostDataOp, mlir::acc::TerminatorOp>(
249 start, end, s.getDirectiveKind(), s.getDirectiveLoc(), s.clauses(),
250 s.getStructuredBlock());
251}
252
255 mlir::Location start = getLoc(s.getSourceRange().getBegin());
256 emitOpenACCOp<EnterDataOp>(start, s.getDirectiveKind(), s.getDirectiveLoc(),
257 s.clauses());
258 return mlir::success();
259}
260
263 mlir::Location start = getLoc(s.getSourceRange().getBegin());
264 emitOpenACCOp<ExitDataOp>(start, s.getDirectiveKind(), s.getDirectiveLoc(),
265 s.clauses());
266 return mlir::success();
267}
268
269mlir::LogicalResult
271 mlir::Location start = getLoc(s.getSourceRange().getBegin());
272 emitOpenACCOp<UpdateOp>(start, s.getDirectiveKind(), s.getDirectiveLoc(),
273 s.clauses());
274 return mlir::success();
275}
276
277mlir::LogicalResult
279 // The 'cache' directive 'may' be at the top of a loop by standard, but
280 // doesn't have to be. Additionally, there is nothing that requires this be a
281 // loop affected by an OpenACC pragma. Sema doesn't do any level of
282 // enforcement here, since it isn't particularly valuable to do so thanks to
283 // that. Instead, we treat cache as a 'noop' if there is no acc.loop to apply
284 // it to.
285 if (!activeLoopOp)
286 return mlir::success();
287
288 mlir::acc::LoopOp loopOp = *activeLoopOp;
289
290 mlir::OpBuilder::InsertionGuard guard(builder);
291 builder.setInsertionPoint(loopOp);
292
293 for (const Expr *var : s.getVarList()) {
296
297 auto cacheOp = builder.create<CacheOp>(
298 opInfo.beginLoc, opInfo.varValue,
299 /*structured=*/false, /*implicit=*/false, opInfo.name, opInfo.bounds);
300
301 loopOp.getCacheOperandsMutable().append(cacheOp.getResult());
302 }
303
304 return mlir::success();
305}
306
307mlir::LogicalResult
309 cgm.errorNYI(s.getSourceRange(), "OpenACC Atomic Construct");
310 return mlir::failure();
311}
This file defines OpenACC AST classes for statement-level contructs.
__device__ __2f16 float __ockl_bool s
mlir::LogicalResult emitOpenACCDataConstruct(const OpenACCDataConstruct &s)
mlir::LogicalResult emitOpenACCCombinedConstruct(const OpenACCCombinedConstruct &s)
mlir::LogicalResult emitOpenACCWaitConstruct(const OpenACCWaitConstruct &s)
mlir::LogicalResult emitOpenACCUpdateConstruct(const OpenACCUpdateConstruct &s)
mlir::LogicalResult emitOpenACCCacheConstruct(const OpenACCCacheConstruct &s)
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
mlir::LogicalResult emitOpenACCInitConstruct(const OpenACCInitConstruct &s)
mlir::LogicalResult emitOpenACCSetConstruct(const OpenACCSetConstruct &s)
OpenACCDataOperandInfo getOpenACCDataOperandInfo(const Expr *e)
mlir::LogicalResult emitOpenACCComputeConstruct(const OpenACCComputeConstruct &s)
mlir::LogicalResult emitOpenACCShutdownConstruct(const OpenACCShutdownConstruct &s)
mlir::Value emitScalarExpr(const clang::Expr *e)
Emit the computation of the specified expression of scalar type.
mlir::LogicalResult emitOpenACCHostDataConstruct(const OpenACCHostDataConstruct &s)
mlir::MLIRContext & getMLIRContext()
mlir::LogicalResult emitOpenACCEnterDataConstruct(const OpenACCEnterDataConstruct &s)
clang::ASTContext & getContext() const
mlir::LogicalResult emitStmt(const clang::Stmt *s, bool useCurrentScope, llvm::ArrayRef< const Attr * > attrs={})
Definition: CIRGenStmt.cpp:110
mlir::LogicalResult emitOpenACCExitDataConstruct(const OpenACCExitDataConstruct &s)
mlir::LogicalResult emitOpenACCAtomicConstruct(const OpenACCAtomicConstruct &s)
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
mlir::Location getLoc(clang::SourceLocation cLoc)
Helpers to convert the presumed location of Clang's SourceLocation to an MLIR Location.
This represents one expression.
Definition: Expr.h:112
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
Definition: StmtOpenACC.h:132
Encodes a location in the source.
Stmt - This represents one statement.
Definition: Stmt.h:85
Definition: ABIArgInfo.h:22
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
The JSON file list parser is used to communicate input to InstallAPI.
OpenACCDirectiveKind
Definition: OpenACCKinds.h:28