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,
27 llvm::ArrayRef<const OpenACCClause *> clauses, const Stmt *associatedStmt) {
28 mlir::LogicalResult res = mlir::success();
29
30 llvm::SmallVector<mlir::Type> retTy;
31 llvm::SmallVector<mlir::Value> operands;
32 auto op = Op::create(builder, start, retTy, operands);
33
34 emitOpenACCClauses(op, dirKind, clauses);
35
36 {
37 mlir::Block &block = op.getRegion().emplaceBlock();
38 mlir::OpBuilder::InsertionGuard guardCase(builder);
39 builder.setInsertionPointToEnd(&block);
40
41 LexicalScope ls{*this, start, builder.getInsertionBlock()};
42 res = emitStmt(associatedStmt, /*useCurrentScope=*/true);
43
44 TermOp::create(builder, end);
45 }
46 return res;
47}
48
49namespace {
50template <typename Op> struct CombinedType;
51template <> struct CombinedType<ParallelOp> {
52 static constexpr mlir::acc::CombinedConstructsType value =
53 mlir::acc::CombinedConstructsType::ParallelLoop;
54};
55template <> struct CombinedType<SerialOp> {
56 static constexpr mlir::acc::CombinedConstructsType value =
57 mlir::acc::CombinedConstructsType::SerialLoop;
58};
59template <> struct CombinedType<KernelsOp> {
60 static constexpr mlir::acc::CombinedConstructsType value =
61 mlir::acc::CombinedConstructsType::KernelsLoop;
62};
63} // namespace
64
65template <typename Op, typename TermOp>
66mlir::LogicalResult CIRGenFunction::emitOpenACCOpCombinedConstruct(
67 mlir::Location start, mlir::Location end, OpenACCDirectiveKind dirKind,
68 llvm::ArrayRef<const OpenACCClause *> clauses, const Stmt *loopStmt) {
69 mlir::LogicalResult res = mlir::success();
70
71 llvm::SmallVector<mlir::Type> retTy;
72 llvm::SmallVector<mlir::Value> operands;
73
74 auto computeOp = Op::create(builder, start, retTy, operands);
75 computeOp.setCombinedAttr(builder.getUnitAttr());
76 mlir::acc::LoopOp loopOp;
77
78 // First, emit the bodies of both operations, with the loop inside the body of
79 // the combined construct.
80 {
81 mlir::Block &block = computeOp.getRegion().emplaceBlock();
82 mlir::OpBuilder::InsertionGuard guardCase(builder);
83 builder.setInsertionPointToEnd(&block);
84
85 LexicalScope ls{*this, start, builder.getInsertionBlock()};
86 auto loopOp = LoopOp::create(builder, start, retTy, operands);
87 loopOp.setCombinedAttr(mlir::acc::CombinedConstructsTypeAttr::get(
88 builder.getContext(), CombinedType<Op>::value));
89
90 {
91 mlir::Block &innerBlock = loopOp.getRegion().emplaceBlock();
92 mlir::OpBuilder::InsertionGuard guardCase(builder);
93 builder.setInsertionPointToEnd(&innerBlock);
94
95 LexicalScope ls{*this, start, builder.getInsertionBlock()};
96 ActiveOpenACCLoopRAII activeLoop{*this, &loopOp};
97
98 res = emitStmt(loopStmt, /*useCurrentScope=*/true);
99
100 mlir::acc::YieldOp::create(builder, end);
101 }
102
103 emitOpenACCClauses(computeOp, loopOp, dirKind, clauses);
104
105 updateLoopOpParallelism(loopOp, /*isOrphan=*/false, dirKind);
106
107 TermOp::create(builder, end);
108 }
109
110 return res;
111}
112
113template <typename Op>
114Op CIRGenFunction::emitOpenACCOp(
115 mlir::Location start, OpenACCDirectiveKind dirKind,
116 llvm::ArrayRef<const OpenACCClause *> clauses) {
117 llvm::SmallVector<mlir::Type> retTy;
118 llvm::SmallVector<mlir::Value> operands;
119 auto op = Op::create(builder, start, retTy, operands);
120
121 emitOpenACCClauses(op, dirKind, clauses);
122 return op;
123}
124
125mlir::LogicalResult
127 mlir::Location start = getLoc(s.getSourceRange().getBegin());
128 mlir::Location end = getLoc(s.getSourceRange().getEnd());
129
130 switch (s.getDirectiveKind()) {
132 return emitOpenACCOpAssociatedStmt<ParallelOp, mlir::acc::YieldOp>(
133 start, end, s.getDirectiveKind(), s.clauses(), s.getStructuredBlock());
135 return emitOpenACCOpAssociatedStmt<SerialOp, mlir::acc::YieldOp>(
136 start, end, s.getDirectiveKind(), s.clauses(), s.getStructuredBlock());
138 return emitOpenACCOpAssociatedStmt<KernelsOp, mlir::acc::TerminatorOp>(
139 start, end, s.getDirectiveKind(), s.clauses(), s.getStructuredBlock());
140 default:
141 llvm_unreachable("invalid compute construct kind");
142 }
143}
144
145mlir::LogicalResult
147 mlir::Location start = getLoc(s.getSourceRange().getBegin());
148 mlir::Location end = getLoc(s.getSourceRange().getEnd());
149
150 return emitOpenACCOpAssociatedStmt<DataOp, mlir::acc::TerminatorOp>(
151 start, end, s.getDirectiveKind(), s.clauses(), s.getStructuredBlock());
152}
153
154mlir::LogicalResult
156 mlir::Location start = getLoc(s.getSourceRange().getBegin());
157 emitOpenACCOp<InitOp>(start, s.getDirectiveKind(), s.clauses());
158 return mlir::success();
159}
160
161mlir::LogicalResult
163 mlir::Location start = getLoc(s.getSourceRange().getBegin());
164 emitOpenACCOp<SetOp>(start, s.getDirectiveKind(), s.clauses());
165 return mlir::success();
166}
167
170 mlir::Location start = getLoc(s.getSourceRange().getBegin());
171 emitOpenACCOp<ShutdownOp>(start, s.getDirectiveKind(), s.clauses());
172 return mlir::success();
173}
174
175mlir::LogicalResult
177 mlir::Location start = getLoc(s.getSourceRange().getBegin());
178 auto waitOp = emitOpenACCOp<WaitOp>(start, s.getDirectiveKind(), s.clauses());
179
180 auto createIntExpr = [this](const Expr *intExpr) {
181 mlir::Value expr = emitScalarExpr(intExpr);
182 mlir::Location exprLoc = cgm.getLoc(intExpr->getBeginLoc());
183
184 mlir::IntegerType targetType = mlir::IntegerType::get(
185 &getMLIRContext(), getContext().getIntWidth(intExpr->getType()),
186 intExpr->getType()->isSignedIntegerOrEnumerationType()
187 ? mlir::IntegerType::SignednessSemantics::Signed
188 : mlir::IntegerType::SignednessSemantics::Unsigned);
189
190 auto conversionOp = mlir::UnrealizedConversionCastOp::create(
191 builder, exprLoc, targetType, expr);
192 return conversionOp.getResult(0);
193 };
194
195 // Emit the correct 'wait' clauses.
196 {
197 mlir::OpBuilder::InsertionGuard guardCase(builder);
198 builder.setInsertionPoint(waitOp);
199
200 if (s.hasDevNumExpr())
201 waitOp.getWaitDevnumMutable().append(createIntExpr(s.getDevNumExpr()));
202
203 for (Expr *QueueExpr : s.getQueueIdExprs())
204 waitOp.getWaitOperandsMutable().append(createIntExpr(QueueExpr));
205 }
206
207 return mlir::success();
208}
209
212 mlir::Location start = getLoc(s.getSourceRange().getBegin());
213 mlir::Location end = getLoc(s.getSourceRange().getEnd());
214
215 switch (s.getDirectiveKind()) {
217 return emitOpenACCOpCombinedConstruct<ParallelOp, mlir::acc::YieldOp>(
218 start, end, s.getDirectiveKind(), s.clauses(), s.getLoop());
220 return emitOpenACCOpCombinedConstruct<SerialOp, mlir::acc::YieldOp>(
221 start, end, s.getDirectiveKind(), s.clauses(), s.getLoop());
223 return emitOpenACCOpCombinedConstruct<KernelsOp, mlir::acc::TerminatorOp>(
224 start, end, s.getDirectiveKind(), s.clauses(), s.getLoop());
225 default:
226 llvm_unreachable("invalid compute construct kind");
227 }
228}
229
232 mlir::Location start = getLoc(s.getSourceRange().getBegin());
233 mlir::Location end = getLoc(s.getSourceRange().getEnd());
234
235 return emitOpenACCOpAssociatedStmt<HostDataOp, mlir::acc::TerminatorOp>(
236 start, end, s.getDirectiveKind(), s.clauses(), s.getStructuredBlock());
237}
238
241 mlir::Location start = getLoc(s.getSourceRange().getBegin());
242 emitOpenACCOp<EnterDataOp>(start, s.getDirectiveKind(), s.clauses());
243 return mlir::success();
244}
245
248 mlir::Location start = getLoc(s.getSourceRange().getBegin());
249 emitOpenACCOp<ExitDataOp>(start, s.getDirectiveKind(), s.clauses());
250 return mlir::success();
251}
252
253mlir::LogicalResult
255 mlir::Location start = getLoc(s.getSourceRange().getBegin());
256 emitOpenACCOp<UpdateOp>(start, s.getDirectiveKind(), s.clauses());
257 return mlir::success();
258}
259
260mlir::LogicalResult
262 // The 'cache' directive 'may' be at the top of a loop by standard, but
263 // doesn't have to be. Additionally, there is nothing that requires this be a
264 // loop affected by an OpenACC pragma. Sema doesn't do any level of
265 // enforcement here, since it isn't particularly valuable to do so thanks to
266 // that. Instead, we treat cache as a 'noop' if there is no acc.loop to apply
267 // it to.
268 if (!activeLoopOp)
269 return mlir::success();
270
271 mlir::acc::LoopOp loopOp = *activeLoopOp;
272
273 mlir::OpBuilder::InsertionGuard guard(builder);
274 builder.setInsertionPoint(loopOp);
275
276 for (const Expr *var : s.getVarList()) {
279
280 auto cacheOp = CacheOp::create(builder, opInfo.beginLoc, opInfo.varValue,
281 /*structured=*/false, /*implicit=*/false,
282 opInfo.name, opInfo.bounds);
283
284 loopOp.getCacheOperandsMutable().append(cacheOp.getResult());
285 }
286
287 return mlir::success();
288}
289
290const VarDecl *getLValueDecl(const Expr *e) {
291 // We are going to assume that after stripping implicit casts, that the LValue
292 // is just a DRE around the var-decl.
293
294 e = e->IgnoreImpCasts();
295
296 const auto *dre = cast<DeclRefExpr>(e);
297 return cast<VarDecl>(dre->getDecl());
298}
299
300static mlir::acc::AtomicReadOp
302 mlir::Location start,
304 // Atomic 'read' only permits 'v = x', where v and x are both scalar L
305 // values. The getAssociatedStmtInfo strips off implicit casts, which
306 // includes implicit conversions and L-to-R-Value conversions, so we can
307 // just emit it as an L value. The Flang implementation has no problem with
308 // different types, so it appears that the dialect can handle the
309 // conversions.
310 mlir::Value v = cgf.emitLValue(inf.V).getPointer();
311 mlir::Value x = cgf.emitLValue(inf.X).getPointer();
312 mlir::Type resTy = cgf.convertType(inf.V->getType());
313 return mlir::acc::AtomicReadOp::create(builder, start, x, v, resTy,
314 /*ifCond=*/{});
315}
316
317static mlir::acc::AtomicWriteOp
319 mlir::Location start,
321 mlir::Value x = cgf.emitLValue(inf.X).getPointer();
322 mlir::Value expr = cgf.emitAnyExpr(inf.RefExpr).getValue();
323 return mlir::acc::AtomicWriteOp::create(builder, start, x, expr,
324 /*ifCond=*/{});
325}
326
327static std::pair<mlir::LogicalResult, mlir::acc::AtomicUpdateOp>
329 mlir::Location start, mlir::Location end,
331 mlir::Value x = cgf.emitLValue(inf.X).getPointer();
332 auto op = mlir::acc::AtomicUpdateOp::create(builder, start, x, /*ifCond=*/{});
333
334 mlir::LogicalResult res = mlir::success();
335 {
336 mlir::OpBuilder::InsertionGuard guardCase(builder);
337 mlir::Type argTy = cast<cir::PointerType>(x.getType()).getPointee();
338 std::array<mlir::Type, 1> recipeType{argTy};
339 std::array<mlir::Location, 1> recipeLoc{start};
340 auto *recipeBlock = builder.createBlock(
341 &op.getRegion(), op.getRegion().end(), recipeType, recipeLoc);
342 builder.setInsertionPointToEnd(recipeBlock);
343 // Since we have an initial value that we know is a scalar type, we can
344 // just emit the entire statement here after sneaking-in our 'alloca' in
345 // the right place, then loading out of it. Flang does a lot less work
346 // (probably does its own emitting!), but we have more complicated AST
347 // nodes to worry about, so we can just count on opt to remove the extra
348 // alloca/load/store set.
349 auto alloca = cir::AllocaOp::create(
350 builder, start, x.getType(), argTy, "x_var",
351 cgf.cgm.getSize(
352 cgf.getContext().getTypeAlignInChars(inf.X->getType())));
353
354 alloca.setInitAttr(builder.getUnitAttr());
355 builder.CIRBaseBuilderTy::createStore(start, recipeBlock->getArgument(0),
356 alloca);
357
358 const VarDecl *xval = getLValueDecl(inf.X);
359 CIRGenFunction::DeclMapRevertingRAII declMapRAII{cgf, xval};
361 xval, Address{alloca, argTy, cgf.getContext().getDeclAlign(xval)});
362
363 res = cgf.emitStmt(inf.WholeExpr, /*useCurrentScope=*/true);
364
365 auto load = cir::LoadOp::create(builder, start, {alloca});
366 mlir::acc::YieldOp::create(builder, end, {load});
367 }
368
369 return {res, op};
370}
371
372mlir::LogicalResult
374 // While Atomic is an 'associated statement' construct, it 'steals' the
375 // expression it is associated with rather than emitting it inside of it. So
376 // it has custom emit logic.
377 mlir::Location start = getLoc(s.getSourceRange().getBegin());
378 mlir::Location end = getLoc(s.getSourceRange().getEnd());
379 OpenACCAtomicConstruct::StmtInfo inf = s.getAssociatedStmtInfo();
380
381 switch (s.getAtomicKind()) {
384 mlir::acc::AtomicReadOp op =
385 emitAtomicRead(*this, builder, start, inf.First);
386 emitOpenACCClauses(op, s.getDirectiveKind(), s.clauses());
387 return mlir::success();
388 }
391 auto op = emitAtomicWrite(*this, builder, start, inf.First);
392 emitOpenACCClauses(op, s.getDirectiveKind(), s.clauses());
393 return mlir::success();
394 }
398 auto [res, op] = emitAtomicUpdate(*this, builder, start, end, inf.First);
399 emitOpenACCClauses(op, s.getDirectiveKind(), s.clauses());
400 return res;
401 }
403 // Atomic-capture is made up of two statements, either an update = read,
404 // read + update, or read + write. As a result, the IR represents the
405 // capture region as having those two 'inside' of it.
406 auto op = mlir::acc::AtomicCaptureOp::create(builder, start, /*ifCond=*/{});
407 emitOpenACCClauses(op, s.getDirectiveKind(), s.clauses());
408 mlir::LogicalResult res = mlir::success();
409 {
410 mlir::OpBuilder::InsertionGuard guardCase(builder);
411
412 mlir::Block *block =
413 builder.createBlock(&op.getRegion(), op.getRegion().end(), {}, {});
414
415 builder.setInsertionPointToStart(block);
416
417 auto terminator = mlir::acc::TerminatorOp::create(builder, end);
418
419 // The AtomicCaptureOp only permits the two acc.atomic.* operations inside
420 // of it, so all other parts of the expression need to be emitted before
421 // the AtomicCaptureOp, then moved into place.
422 builder.setInsertionPoint(op);
423
424 switch (inf.Form) {
425 default:
426 llvm_unreachable("invalid form for Capture");
428 mlir::acc::AtomicReadOp first =
429 emitAtomicRead(*this, builder, start, inf.First);
430 mlir::acc::AtomicWriteOp second =
431 emitAtomicWrite(*this, builder, start, inf.Second);
432
433 first->moveBefore(terminator);
434 second->moveBefore(terminator);
435 break;
436 }
438 mlir::acc::AtomicReadOp first =
439 emitAtomicRead(*this, builder, start, inf.First);
440 auto [this_res, second] =
441 emitAtomicUpdate(*this, builder, start, end, inf.Second);
442 res = this_res;
443
444 first->moveBefore(terminator);
445 second->moveBefore(terminator);
446 break;
447 }
449 auto [this_res, first] =
450 emitAtomicUpdate(*this, builder, start, end, inf.First);
451 res = this_res;
452 mlir::acc::AtomicReadOp second =
453 emitAtomicRead(*this, builder, start, inf.Second);
454
455 first->moveBefore(terminator);
456 second->moveBefore(terminator);
457 break;
458 }
459 }
460 }
461 return res;
462 }
463 }
464
465 llvm_unreachable("unknown OpenACC atomic kind");
466}
static mlir::acc::AtomicReadOp emitAtomicRead(CIRGenFunction &cgf, CIRGenBuilderTy &builder, mlir::Location start, const OpenACCAtomicConstruct::SingleStmtInfo &inf)
static std::pair< mlir::LogicalResult, mlir::acc::AtomicUpdateOp > emitAtomicUpdate(CIRGenFunction &cgf, CIRGenBuilderTy &builder, mlir::Location start, mlir::Location end, const OpenACCAtomicConstruct::SingleStmtInfo &inf)
static mlir::acc::AtomicWriteOp emitAtomicWrite(CIRGenFunction &cgf, CIRGenBuilderTy &builder, mlir::Location start, const OpenACCAtomicConstruct::SingleStmtInfo &inf)
const VarDecl * getLValueDecl(const Expr *e)
This file defines OpenACC AST classes for statement-level contructs.
__device__ __2f16 float __ockl_bool s
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
mlir::Type convertType(clang::QualType t)
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)
void replaceAddrOfLocalVar(const clang::VarDecl *vd, Address addr)
LValue emitLValue(const clang::Expr *e)
Emit code to compute a designator that specifies the location of the expression.
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::LogicalResult emitOpenACCHostDataConstruct(const OpenACCHostDataConstruct &s)
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
mlir::MLIRContext & getMLIRContext()
mlir::LogicalResult emitOpenACCEnterDataConstruct(const OpenACCEnterDataConstruct &s)
RValue emitAnyExpr(const clang::Expr *e, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
Emit code to compute the specified expression which can have any type.
clang::ASTContext & getContext() const
mlir::LogicalResult emitStmt(const clang::Stmt *s, bool useCurrentScope, llvm::ArrayRef< const Attr * > attrs={})
mlir::LogicalResult emitOpenACCExitDataConstruct(const OpenACCExitDataConstruct &s)
mlir::LogicalResult emitOpenACCAtomicConstruct(const OpenACCAtomicConstruct &s)
mlir::IntegerAttr getSize(CharUnits size)
mlir::Value getPointer() const
mlir::Value getValue() const
Return the value of this scalar value.
Definition CIRGenValue.h:57
This represents one expression.
Definition Expr.h:112
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3065
QualType getType() const
Definition Expr.h:144
Stmt - This represents one statement.
Definition Stmt.h:85
Represents a variable declaration or definition.
Definition Decl.h:926
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
The JSON file list parser is used to communicate input to InstallAPI.
OpenACCDirectiveKind
OpenACCComputeConstruct(OpenACCDirectiveKind K, SourceLocation Start, SourceLocation DirectiveLoc, SourceLocation End, ArrayRef< const OpenACCClause * > Clauses, Stmt *StructuredBlock)
U cast(CodeGen::Address addr)
Definition Address.h:327
enum OpenACCAtomicConstruct::StmtInfo::StmtForm Form
Represents a scope, including function bodies, compound statements, and the substatements of if/while...