clang 24.0.0git
CIRGenOpenACCClause.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 clause nodes as CIR code.
10//
11//===----------------------------------------------------------------------===//
12
13#include <type_traits>
14
15#include "CIRGenCXXABI.h"
16#include "CIRGenFunction.h"
18#include "CIRGenOpenACCRecipe.h"
19
20#include "clang/AST/ExprCXX.h"
21
22#include "mlir/Dialect/Arith/IR/Arith.h"
23#include "mlir/Dialect/OpenACC/OpenACC.h"
24#include "llvm/ADT/TypeSwitch.h"
25
26using namespace clang;
27using namespace clang::CIRGen;
28
29namespace {
30// Simple type-trait to see if the first template arg is one of the list, so we
31// can tell whether to `if-constexpr` a bunch of stuff.
32template <typename ToTest, typename T, typename... Tys>
33constexpr bool isOneOfTypes =
34 std::is_same_v<ToTest, T> || isOneOfTypes<ToTest, Tys...>;
35template <typename ToTest, typename T>
36constexpr bool isOneOfTypes<ToTest, T> = std::is_same_v<ToTest, T>;
37
38// Holds information for emitting clauses for a combined construct. We
39// instantiate the clause emitter with this type so that it can use
40// if-constexpr to specially handle these.
41template <typename CompOpTy> struct CombinedConstructClauseInfo {
42 using ComputeOpTy = CompOpTy;
43 ComputeOpTy computeOp;
44 mlir::acc::LoopOp loopOp;
45};
46template <typename ToTest> constexpr bool isCombinedType = false;
47template <typename T>
48constexpr bool isCombinedType<CombinedConstructClauseInfo<T>> = true;
49
50template <typename OpTy>
51class OpenACCClauseCIREmitter final
52 : public OpenACCClauseVisitor<OpenACCClauseCIREmitter<OpTy>> {
53 // Necessary for combined constructs.
54 template <typename FriendOpTy> friend class OpenACCClauseCIREmitter;
55
56 OpTy &operation;
57 mlir::OpBuilder::InsertPoint &recipeInsertLocation;
58 CIRGen::CIRGenFunction &cgf;
59 CIRGen::CIRGenBuilderTy &builder;
60
61 // This is necessary since a few of the clauses emit differently based on the
62 // directive kind they are attached to.
64
65 llvm::SmallVector<mlir::acc::DeviceType> lastDeviceTypeValues;
66 // Keep track of the async-clause so that we can shortcut updating the data
67 // operands async clauses.
68 bool hasAsyncClause = false;
69 // Keep track of the data operands so that we can update their async clauses.
70 llvm::SmallVector<mlir::Operation *> dataOperands;
71
72 void setLastDeviceTypeClause(const OpenACCDeviceTypeClause &clause) {
73 lastDeviceTypeValues.clear();
74
75 for (const DeviceTypeArgument &arg : clause.getArchitectures())
76 lastDeviceTypeValues.push_back(decodeDeviceType(arg.getIdentifierInfo()));
77 }
78
79 mlir::Value emitIntExpr(const Expr *intExpr) {
80 return cgf.emitOpenACCIntExpr(intExpr);
81 }
82
83 mlir::Value emitSignlessIntExpr(const Expr *intExpr) {
84 return cgf.emitOpenACCSignlessIntExpr(intExpr);
85 }
86
87 // 'condition' as an OpenACC grammar production is used for 'if' and (some
88 // variants of) 'self'. It needs to be emitted as a signless-1-bit value, so
89 // this function emits the expression, then sets the unrealized conversion
90 // cast correctly, and returns the completed value.
91 mlir::Value createCondition(const Expr *condExpr) {
92 mlir::Value condition = cgf.evaluateExprAsBool(condExpr);
93 mlir::Location exprLoc = cgf.cgm.getLoc(condExpr->getBeginLoc());
94 mlir::IntegerType targetType = mlir::IntegerType::get(
95 &cgf.getMLIRContext(), /*width=*/1,
96 mlir::IntegerType::SignednessSemantics::Signless);
97 auto conversionOp = mlir::UnrealizedConversionCastOp::create(
98 builder, exprLoc, targetType, condition);
99 return conversionOp.getResult(0);
100 }
101
102 mlir::Value createConstantInt(mlir::Location loc, unsigned width,
103 int64_t value) {
104 return cgf.createOpenACCConstantInt(loc, width, value);
105 mlir::IntegerType ty = mlir::IntegerType::get(
106 &cgf.getMLIRContext(), width,
107 mlir::IntegerType::SignednessSemantics::Signless);
108 auto constOp = mlir::arith::ConstantOp::create(
109 builder, loc, builder.getIntegerAttr(ty, value));
110
111 return constOp;
112 }
113
114 mlir::Value createConstantInt(SourceLocation loc, unsigned width,
115 int64_t value) {
116 return createConstantInt(cgf.cgm.getLoc(loc), width, value);
117 }
118
119 mlir::acc::GangArgType decodeGangType(OpenACCGangKind gk) {
120 switch (gk) {
121 case OpenACCGangKind::Num:
122 return mlir::acc::GangArgType::Num;
123 case OpenACCGangKind::Dim:
124 return mlir::acc::GangArgType::Dim;
125 case OpenACCGangKind::Static:
126 return mlir::acc::GangArgType::Static;
127 }
128 llvm_unreachable("unknown gang kind");
129 }
130
131 template <typename U = void,
132 typename = std::enable_if_t<isCombinedType<OpTy>, U>>
133 void applyToLoopOp(const OpenACCClause &c) {
134 mlir::OpBuilder::InsertionGuard guardCase(builder);
135 builder.setInsertionPoint(operation.loopOp);
136 OpenACCClauseCIREmitter<mlir::acc::LoopOp> loopEmitter{
137 operation.loopOp, recipeInsertLocation, cgf, builder, dirKind};
138 loopEmitter.lastDeviceTypeValues = lastDeviceTypeValues;
139 loopEmitter.Visit(&c);
140 }
141
142 template <typename U = void,
143 typename = std::enable_if_t<isCombinedType<OpTy>, U>>
144 void applyToComputeOp(const OpenACCClause &c) {
145 mlir::OpBuilder::InsertionGuard guardCase(builder);
146 builder.setInsertionPoint(operation.computeOp);
147 OpenACCClauseCIREmitter<typename OpTy::ComputeOpTy> computeEmitter{
148 operation.computeOp, recipeInsertLocation, cgf, builder, dirKind};
149
150 computeEmitter.lastDeviceTypeValues = lastDeviceTypeValues;
151
152 // Async handler uses the first data operand to figure out where to insert
153 // its information if it is present. This ensures that the new handler will
154 // correctly set the insertion point for async.
155 if (!dataOperands.empty())
156 computeEmitter.dataOperands.push_back(dataOperands.front());
157 computeEmitter.Visit(&c);
158
159 // Make sure all of the new data operands are kept track of here. The
160 // combined constructs always apply 'async' to only the compute component,
161 // so we need to collect these.
162 dataOperands.append(computeEmitter.dataOperands);
163 }
164
165 template <typename BeforeOpTy, typename AfterOpTy>
166 void addDataOperand(const Expr *varOperand, mlir::acc::DataClause dataClause,
167 OpenACCModifierKind modifiers, bool structured,
168 bool implicit) {
169 CIRGenFunction::OpenACCDataOperandInfo opInfo =
170 cgf.getOpenACCDataOperandInfo(varOperand);
171
172 auto beforeOp =
173 BeforeOpTy::create(builder, opInfo.beginLoc, opInfo.varValue,
174 structured, implicit, opInfo.name, opInfo.bounds);
175 operation.getDataClauseOperandsMutable().append(beforeOp.getResult());
176
177 AfterOpTy afterOp;
178 {
179 mlir::OpBuilder::InsertionGuard guardCase(builder);
180 builder.setInsertionPointAfter(operation);
181
182 if constexpr (std::is_same_v<AfterOpTy, mlir::acc::DeleteOp> ||
183 std::is_same_v<AfterOpTy, mlir::acc::DetachOp>) {
184 // Detach/Delete ops don't have the variable reference here, so they
185 // take 1 fewer argument to their build function.
186 afterOp =
187 AfterOpTy::create(builder, opInfo.beginLoc, beforeOp, structured,
188 implicit, opInfo.name, opInfo.bounds);
189 } else {
190 afterOp = AfterOpTy::create(builder, opInfo.beginLoc, beforeOp,
191 opInfo.varValue, structured, implicit,
192 opInfo.name, opInfo.bounds);
193 }
194 }
195
196 // Set the 'rest' of the info for both operations.
197 beforeOp.setDataClause(dataClause);
198 afterOp.setDataClause(dataClause);
199 beforeOp.setModifiers(convertOpenACCModifiers(modifiers));
200 afterOp.setModifiers(convertOpenACCModifiers(modifiers));
201
202 // Make sure we record these, so 'async' values can be updated later.
203 dataOperands.push_back(beforeOp.getOperation());
204 dataOperands.push_back(afterOp.getOperation());
205 }
206
207 template <typename BeforeOpTy>
208 void addDataOperand(const Expr *varOperand, mlir::acc::DataClause dataClause,
209 OpenACCModifierKind modifiers, bool structured,
210 bool implicit) {
211 CIRGenFunction::OpenACCDataOperandInfo opInfo =
212 cgf.getOpenACCDataOperandInfo(varOperand);
213 auto beforeOp =
214 BeforeOpTy::create(builder, opInfo.beginLoc, opInfo.varValue,
215 structured, implicit, opInfo.name, opInfo.bounds);
216 operation.getDataClauseOperandsMutable().append(beforeOp.getResult());
217
218 // Set the 'rest' of the info for the operation.
219 beforeOp.setDataClause(dataClause);
220 beforeOp.setModifiers(convertOpenACCModifiers(modifiers));
221
222 // Make sure we record these, so 'async' values can be updated later.
223 dataOperands.push_back(beforeOp.getOperation());
224 }
225
226 // Helper function that covers for the fact that we don't have this function
227 // on all operation types.
228 mlir::ArrayAttr getAsyncOnlyAttr() {
229 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
230 mlir::acc::KernelsOp, mlir::acc::DataOp,
231 mlir::acc::UpdateOp>) {
232 return operation.getAsyncOnlyAttr();
233 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::EnterDataOp,
234 mlir::acc::ExitDataOp>) {
235 if (!operation.getAsyncAttr())
236 return mlir::ArrayAttr{};
237
238 llvm::SmallVector<mlir::Attribute> devTysTemp;
239 devTysTemp.push_back(mlir::acc::DeviceTypeAttr::get(
240 builder.getContext(), mlir::acc::DeviceType::None));
241 return mlir::ArrayAttr::get(builder.getContext(), devTysTemp);
242 } else if constexpr (isCombinedType<OpTy>) {
243 return operation.computeOp.getAsyncOnlyAttr();
244 }
245
246 // Note: 'wait' has async as well, but it cannot have data clauses, so we
247 // don't have to handle them here.
248
249 llvm_unreachable("getting asyncOnly when clause not valid on operation?");
250 }
251
252 // Helper function that covers for the fact that we don't have this function
253 // on all operation types.
254 mlir::ArrayAttr getAsyncOperandsDeviceTypeAttr() {
255 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
256 mlir::acc::KernelsOp, mlir::acc::DataOp,
257 mlir::acc::UpdateOp>) {
258 return operation.getAsyncOperandsDeviceTypeAttr();
259 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::EnterDataOp,
260 mlir::acc::ExitDataOp>) {
261 if (!operation.getAsyncOperand())
262 return mlir::ArrayAttr{};
263
264 llvm::SmallVector<mlir::Attribute> devTysTemp;
265 devTysTemp.push_back(mlir::acc::DeviceTypeAttr::get(
266 builder.getContext(), mlir::acc::DeviceType::None));
267 return mlir::ArrayAttr::get(builder.getContext(), devTysTemp);
268 } else if constexpr (isCombinedType<OpTy>) {
269 return operation.computeOp.getAsyncOperandsDeviceTypeAttr();
270 }
271
272 // Note: 'wait' has async as well, but it cannot have data clauses, so we
273 // don't have to handle them here.
274
275 llvm_unreachable(
276 "getting asyncOperandsDeviceType when clause not valid on operation?");
277 }
278
279 // Helper function that covers for the fact that we don't have this function
280 // on all operation types.
281 mlir::OperandRange getAsyncOperands() {
282 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
283 mlir::acc::KernelsOp, mlir::acc::DataOp,
284 mlir::acc::UpdateOp>)
285 return operation.getAsyncOperands();
286 else if constexpr (isOneOfTypes<OpTy, mlir::acc::EnterDataOp,
287 mlir::acc::ExitDataOp>)
288 return operation.getAsyncOperandMutable();
289 else if constexpr (isCombinedType<OpTy>)
290 return operation.computeOp.getAsyncOperands();
291
292 // Note: 'wait' has async as well, but it cannot have data clauses, so we
293 // don't have to handle them here.
294
295 llvm_unreachable(
296 "getting asyncOperandsDeviceType when clause not valid on operation?");
297 }
298
299 // The 'data' clauses all require that we add the 'async' values from the
300 // operation to them. We've collected the data operands along the way, so use
301 // that list to get the current 'async' values.
302 void updateDataOperandAsyncValues() {
303 if (!hasAsyncClause || dataOperands.empty())
304 return;
305
306 for (mlir::Operation *dataOp : dataOperands) {
307 llvm::TypeSwitch<mlir::Operation *, void>(dataOp)
308 .Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>([&](auto op) {
309 op.setAsyncOnlyAttr(getAsyncOnlyAttr());
310 op.setAsyncOperandsDeviceTypeAttr(getAsyncOperandsDeviceTypeAttr());
311 op.getAsyncOperandsMutable().assign(getAsyncOperands());
312 })
313 .Default([&](mlir::Operation *) {
314 llvm_unreachable("Not a data operation?");
315 });
316 }
317 }
318
319public:
320 OpenACCClauseCIREmitter(OpTy &operation,
321 mlir::OpBuilder::InsertPoint &recipeInsertLocation,
322 CIRGen::CIRGenFunction &cgf,
323 CIRGen::CIRGenBuilderTy &builder,
324 OpenACCDirectiveKind dirKind)
325 : operation(operation), recipeInsertLocation(recipeInsertLocation),
326 cgf(cgf), builder(builder), dirKind(dirKind) {}
327
328 void VisitClause(const OpenACCClause &clause) {
329 llvm_unreachable("Unknown/unhandled clause kind");
330 }
331
332 // The entry point for the CIR emitter. All users should use this rather than
333 // 'visitClauseList', as this also handles the things that have to happen
334 // 'after' the clauses are all visited.
335 void emitClauses(ArrayRef<const OpenACCClause *> clauses) {
336 this->VisitClauseList(clauses);
337 updateDataOperandAsyncValues();
338 }
339
340 void VisitDefaultClause(const OpenACCDefaultClause &clause) {
341 // This type-trait checks if 'op'(the first arg) is one of the mlir::acc
342 // operations listed in the rest of the arguments.
343 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
344 mlir::acc::KernelsOp, mlir::acc::DataOp>) {
345 switch (clause.getDefaultClauseKind()) {
346 case OpenACCDefaultClauseKind::None:
347 operation.setDefaultAttr(mlir::acc::ClauseDefaultValue::None);
348 break;
349 case OpenACCDefaultClauseKind::Present:
350 operation.setDefaultAttr(mlir::acc::ClauseDefaultValue::Present);
351 break;
352 case OpenACCDefaultClauseKind::Invalid:
353 break;
354 }
355 } else if constexpr (isCombinedType<OpTy>) {
356 applyToComputeOp(clause);
357 } else {
358 llvm_unreachable("Unknown construct kind in VisitDefaultClause");
359 }
360 }
361
362 void VisitDeviceTypeClause(const OpenACCDeviceTypeClause &clause) {
363 setLastDeviceTypeClause(clause);
364
365 if constexpr (isOneOfTypes<OpTy, mlir::acc::InitOp,
366 mlir::acc::ShutdownOp>) {
367 for (const DeviceTypeArgument &arg : clause.getArchitectures())
368 operation.addDeviceType(builder.getContext(),
369 decodeDeviceType(arg.getIdentifierInfo()));
370 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::SetOp>) {
371 assert(!operation.getDeviceTypeAttr() && "already have device-type?");
372 assert(clause.getArchitectures().size() <= 1);
373
374 if (!clause.getArchitectures().empty())
375 operation.setDeviceType(
376 decodeDeviceType(clause.getArchitectures()[0].getIdentifierInfo()));
377 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp,
378 mlir::acc::SerialOp, mlir::acc::KernelsOp,
379 mlir::acc::DataOp, mlir::acc::LoopOp,
380 mlir::acc::UpdateOp>) {
381 // Nothing to do here, these constructs don't have any IR for these, as
382 // they just modify the other clauses IR. So setting of
383 // `lastDeviceTypeValues` (done above) is all we need.
384 } else if constexpr (isCombinedType<OpTy>) {
385 // Nothing to do here either, combined constructs are just going to use
386 // 'lastDeviceTypeValues' to set the value for the child visitor.
387 } else {
388 llvm_unreachable("Unknown construct kind in VisitDeviceTypeClause");
389 }
390 }
391
392 void VisitNumWorkersClause(const OpenACCNumWorkersClause &clause) {
393 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp,
394 mlir::acc::KernelsOp>) {
395 operation.addNumWorkersOperand(builder.getContext(),
396 emitSignlessIntExpr(clause.getIntExpr()),
397 lastDeviceTypeValues);
398 } else if constexpr (isCombinedType<OpTy>) {
399 applyToComputeOp(clause);
400 } else {
401 llvm_unreachable("Unknown construct kind in VisitNumGangsClause");
402 }
403 }
404
405 void VisitVectorLengthClause(const OpenACCVectorLengthClause &clause) {
406 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp,
407 mlir::acc::KernelsOp>) {
408 operation.addVectorLengthOperand(builder.getContext(),
409 emitSignlessIntExpr(clause.getIntExpr()),
410 lastDeviceTypeValues);
411 } else if constexpr (isCombinedType<OpTy>) {
412 applyToComputeOp(clause);
413 } else {
414 llvm_unreachable("Unknown construct kind in VisitVectorLengthClause");
415 }
416 }
417
418 void VisitAsyncClause(const OpenACCAsyncClause &clause) {
419 hasAsyncClause = true;
420 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
421 mlir::acc::KernelsOp, mlir::acc::DataOp,
422 mlir::acc::EnterDataOp, mlir::acc::ExitDataOp,
423 mlir::acc::UpdateOp>) {
424 if (!clause.hasIntExpr()) {
425 operation.addAsyncOnly(builder.getContext(), lastDeviceTypeValues);
426 } else {
427
428 mlir::Value intExpr;
429 {
430 // Async int exprs can be referenced by the data operands, which means
431 // that the int-exprs have to appear before them. IF there is a data
432 // operand already, set the insertion point to 'before' it.
433 mlir::OpBuilder::InsertionGuard guardCase(builder);
434 if (!dataOperands.empty())
435 builder.setInsertionPoint(dataOperands.front());
436 intExpr = emitIntExpr(clause.getIntExpr());
437 }
438 operation.addAsyncOperand(builder.getContext(), intExpr,
439 lastDeviceTypeValues);
440 }
441 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::WaitOp>) {
442 // Wait doesn't have a device_type, so its handling here is slightly
443 // different.
444 if (!clause.hasIntExpr())
445 operation.setAsync(true);
446 else
447 operation.getAsyncOperandMutable().append(
448 emitIntExpr(clause.getIntExpr()));
449 } else if constexpr (isCombinedType<OpTy>) {
450 applyToComputeOp(clause);
451 } else {
452 llvm_unreachable("Unknown construct kind in VisitAsyncClause");
453 }
454 }
455
456 void VisitSelfClause(const OpenACCSelfClause &clause) {
457 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
458 mlir::acc::KernelsOp>) {
459 if (clause.isEmptySelfClause()) {
460 operation.setSelfAttr(true);
461 } else if (clause.isConditionExprClause()) {
462 assert(clause.hasConditionExpr());
463 operation.getSelfCondMutable().append(
464 createCondition(clause.getConditionExpr()));
465 } else {
466 llvm_unreachable("var-list version of self shouldn't get here");
467 }
468 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::UpdateOp>) {
469 assert(!clause.isEmptySelfClause() && !clause.isConditionExprClause() &&
470 "var-list version of self required for update");
471 for (const Expr *var : clause.getVarList())
472 addDataOperand<mlir::acc::GetDevicePtrOp, mlir::acc::UpdateHostOp>(
473 var, mlir::acc::DataClause::acc_update_self, {},
474 /*structured=*/false, /*implicit=*/false);
475 } else if constexpr (isCombinedType<OpTy>) {
476 applyToComputeOp(clause);
477 } else {
478 llvm_unreachable("Unknown construct kind in VisitSelfClause");
479 }
480 }
481
482 void VisitHostClause(const OpenACCHostClause &clause) {
483 if constexpr (isOneOfTypes<OpTy, mlir::acc::UpdateOp>) {
484 for (const Expr *var : clause.getVarList())
485 addDataOperand<mlir::acc::GetDevicePtrOp, mlir::acc::UpdateHostOp>(
486 var, mlir::acc::DataClause::acc_update_host, {},
487 /*structured=*/false, /*implicit=*/false);
488 } else {
489 llvm_unreachable("Unknown construct kind in VisitHostClause");
490 }
491 }
492
493 void VisitDeviceClause(const OpenACCDeviceClause &clause) {
494 if constexpr (isOneOfTypes<OpTy, mlir::acc::UpdateOp>) {
495 for (const Expr *var : clause.getVarList())
496 addDataOperand<mlir::acc::UpdateDeviceOp>(
497 var, mlir::acc::DataClause::acc_update_device, {},
498 /*structured=*/false, /*implicit=*/false);
499 } else {
500 llvm_unreachable("Unknown construct kind in VisitDeviceClause");
501 }
502 }
503
504 void VisitIfClause(const OpenACCIfClause &clause) {
505 if constexpr (isOneOfTypes<
506 OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
507 mlir::acc::KernelsOp, mlir::acc::InitOp,
508 mlir::acc::ShutdownOp, mlir::acc::SetOp,
509 mlir::acc::DataOp, mlir::acc::WaitOp,
510 mlir::acc::HostDataOp, mlir::acc::EnterDataOp,
511 mlir::acc::ExitDataOp, mlir::acc::UpdateOp,
512 mlir::acc::AtomicReadOp, mlir::acc::AtomicWriteOp,
513 mlir::acc::AtomicUpdateOp, mlir::acc::AtomicCaptureOp>) {
514 operation.getIfCondMutable().append(
515 createCondition(clause.getConditionExpr()));
516 } else if constexpr (isCombinedType<OpTy>) {
517 applyToComputeOp(clause);
518 } else {
519 llvm_unreachable("Unknown construct kind in VisitIfClause");
520 }
521 }
522
523 void VisitIfPresentClause(const OpenACCIfPresentClause &clause) {
524 if constexpr (isOneOfTypes<OpTy, mlir::acc::HostDataOp,
525 mlir::acc::UpdateOp>) {
526 operation.setIfPresent(true);
527 } else {
528 llvm_unreachable("unknown construct kind in VisitIfPresentClause");
529 }
530 }
531
532 void VisitDeviceNumClause(const OpenACCDeviceNumClause &clause) {
533 if constexpr (isOneOfTypes<OpTy, mlir::acc::InitOp, mlir::acc::ShutdownOp,
534 mlir::acc::SetOp>) {
535 operation.getDeviceNumMutable().append(emitIntExpr(clause.getIntExpr()));
536 } else {
537 llvm_unreachable(
538 "init, shutdown, set, are only valid device_num constructs");
539 }
540 }
541
542 void VisitNumGangsClause(const OpenACCNumGangsClause &clause) {
543 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp,
544 mlir::acc::KernelsOp>) {
545 llvm::SmallVector<mlir::Value> values;
546 for (const Expr *E : clause.getIntExprs())
547 values.push_back(emitSignlessIntExpr(E));
548
549 operation.addNumGangsOperands(builder.getContext(), values,
550 lastDeviceTypeValues);
551 } else if constexpr (isCombinedType<OpTy>) {
552 applyToComputeOp(clause);
553 } else {
554 llvm_unreachable("Unknown construct kind in VisitNumGangsClause");
555 }
556 }
557
558 void VisitWaitClause(const OpenACCWaitClause &clause) {
559 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
560 mlir::acc::KernelsOp, mlir::acc::DataOp,
561 mlir::acc::EnterDataOp, mlir::acc::ExitDataOp,
562 mlir::acc::UpdateOp>) {
563 if (!clause.hasExprs()) {
564 operation.addWaitOnly(builder.getContext(), lastDeviceTypeValues);
565 } else {
566 llvm::SmallVector<mlir::Value> values;
567 if (clause.hasDevNumExpr())
568 values.push_back(emitIntExpr(clause.getDevNumExpr()));
569 for (const Expr *E : clause.getQueueIdExprs())
570 values.push_back(emitIntExpr(E));
571 operation.addWaitOperands(builder.getContext(), clause.hasDevNumExpr(),
572 values, lastDeviceTypeValues);
573 }
574 } else if constexpr (isCombinedType<OpTy>) {
575 applyToComputeOp(clause);
576 } else {
577 // TODO: When we've implemented this for everything, switch this to an
578 // unreachable. update construct remains.
579 llvm_unreachable("Unknown construct kind in VisitWaitClause");
580 }
581 }
582
583 void VisitDefaultAsyncClause(const OpenACCDefaultAsyncClause &clause) {
584 if constexpr (isOneOfTypes<OpTy, mlir::acc::SetOp>) {
585 operation.getDefaultAsyncMutable().append(
586 emitIntExpr(clause.getIntExpr()));
587 } else {
588 llvm_unreachable("set, is only valid device_num constructs");
589 }
590 }
591
592 void VisitSeqClause(const OpenACCSeqClause &clause) {
593 if constexpr (isOneOfTypes<OpTy, mlir::acc::LoopOp>) {
594 operation.addSeq(builder.getContext(), lastDeviceTypeValues);
595 } else if constexpr (isCombinedType<OpTy>) {
596 applyToLoopOp(clause);
597 } else {
598 llvm_unreachable("Unknown construct kind in VisitSeqClause");
599 }
600 }
601
602 void VisitAutoClause(const OpenACCAutoClause &clause) {
603 if constexpr (isOneOfTypes<OpTy, mlir::acc::LoopOp>) {
604 operation.addAuto(builder.getContext(), lastDeviceTypeValues);
605 } else if constexpr (isCombinedType<OpTy>) {
606 applyToLoopOp(clause);
607 } else {
608 llvm_unreachable("Unknown construct kind in VisitAutoClause");
609 }
610 }
611
612 void VisitIndependentClause(const OpenACCIndependentClause &clause) {
613 if constexpr (isOneOfTypes<OpTy, mlir::acc::LoopOp>) {
614 operation.addIndependent(builder.getContext(), lastDeviceTypeValues);
615 } else if constexpr (isCombinedType<OpTy>) {
616 applyToLoopOp(clause);
617 } else {
618 llvm_unreachable("Unknown construct kind in VisitIndependentClause");
619 }
620 }
621
622 void VisitCollapseClause(const OpenACCCollapseClause &clause) {
623 if constexpr (isOneOfTypes<OpTy, mlir::acc::LoopOp>) {
624 llvm::APInt value =
625 clause.getIntExpr()->EvaluateKnownConstInt(cgf.cgm.getASTContext());
626
627 value = value.sextOrTrunc(64);
628 operation.setCollapseForDeviceTypes(builder.getContext(),
629 lastDeviceTypeValues, value);
630 } else if constexpr (isCombinedType<OpTy>) {
631 applyToLoopOp(clause);
632 } else {
633 llvm_unreachable("Unknown construct kind in VisitCollapseClause");
634 }
635 }
636
637 void VisitTileClause(const OpenACCTileClause &clause) {
638 if constexpr (isOneOfTypes<OpTy, mlir::acc::LoopOp>) {
639 llvm::SmallVector<mlir::Value> values;
640
641 for (const Expr *e : clause.getSizeExprs()) {
642 mlir::Location exprLoc = cgf.cgm.getLoc(e->getBeginLoc());
643
644 // We represent the * as -1. Additionally, this is a constant, so we
645 // can always just emit it as 64 bits to avoid having to do any more
646 // work to determine signedness or size.
648 values.push_back(createConstantInt(exprLoc, 64, -1));
649 } else {
650 llvm::APInt curValue =
651 e->EvaluateKnownConstInt(cgf.cgm.getASTContext());
652 values.push_back(createConstantInt(
653 exprLoc, 64, curValue.sextOrTrunc(64).getSExtValue()));
654 }
655 }
656
657 operation.setTileForDeviceTypes(builder.getContext(),
658 lastDeviceTypeValues, values);
659 } else if constexpr (isCombinedType<OpTy>) {
660 applyToLoopOp(clause);
661 } else {
662 llvm_unreachable("Unknown construct kind in VisitTileClause");
663 }
664 }
665
666 void VisitWorkerClause(const OpenACCWorkerClause &clause) {
667 if constexpr (isOneOfTypes<OpTy, mlir::acc::LoopOp>) {
668 if (clause.hasIntExpr())
669 operation.addWorkerNumOperand(builder.getContext(),
670 emitSignlessIntExpr(clause.getIntExpr()),
671 lastDeviceTypeValues);
672 else
673 operation.addEmptyWorker(builder.getContext(), lastDeviceTypeValues);
674
675 } else if constexpr (isCombinedType<OpTy>) {
676 applyToLoopOp(clause);
677 } else {
678 llvm_unreachable("Unknown construct kind in VisitWorkerClause");
679 }
680 }
681
682 void VisitVectorClause(const OpenACCVectorClause &clause) {
683 if constexpr (isOneOfTypes<OpTy, mlir::acc::LoopOp>) {
684 if (clause.hasIntExpr())
685 operation.addVectorOperand(builder.getContext(),
686 emitSignlessIntExpr(clause.getIntExpr()),
687 lastDeviceTypeValues);
688 else
689 operation.addEmptyVector(builder.getContext(), lastDeviceTypeValues);
690
691 } else if constexpr (isCombinedType<OpTy>) {
692 applyToLoopOp(clause);
693 } else {
694 llvm_unreachable("Unknown construct kind in VisitVectorClause");
695 }
696 }
697
698 void VisitGangClause(const OpenACCGangClause &clause) {
699 if constexpr (isOneOfTypes<OpTy, mlir::acc::LoopOp>) {
700 if (clause.getNumExprs() == 0) {
701 operation.addEmptyGang(builder.getContext(), lastDeviceTypeValues);
702 } else {
703 llvm::SmallVector<mlir::Value> values;
704 llvm::SmallVector<mlir::acc::GangArgType> argTypes;
705 for (unsigned i : llvm::index_range(0u, clause.getNumExprs())) {
706 auto [kind, expr] = clause.getExpr(i);
707 mlir::Location exprLoc = cgf.cgm.getLoc(expr->getBeginLoc());
708 argTypes.push_back(decodeGangType(kind));
709 if (kind == OpenACCGangKind::Dim) {
710 llvm::APInt curValue =
711 expr->EvaluateKnownConstInt(cgf.cgm.getASTContext());
712 // The value is 1, 2, or 3, but the type isn't necessarily smaller
713 // than 64.
714 curValue = curValue.sextOrTrunc(64);
715 values.push_back(
716 createConstantInt(exprLoc, 64, curValue.getSExtValue()));
718 values.push_back(createConstantInt(exprLoc, 64, -1));
719 } else {
720 values.push_back(emitSignlessIntExpr(expr));
721 }
722 }
723
724 operation.addGangOperands(builder.getContext(), lastDeviceTypeValues,
725 argTypes, values);
726 }
727 } else if constexpr (isCombinedType<OpTy>) {
728 applyToLoopOp(clause);
729 } else {
730 llvm_unreachable("Unknown construct kind in VisitGangClause");
731 }
732 }
733
734 void VisitCopyClause(const OpenACCCopyClause &clause) {
735 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
736 mlir::acc::KernelsOp, mlir::acc::DataOp>) {
737 for (const Expr *var : clause.getVarList())
738 addDataOperand<mlir::acc::CopyinOp, mlir::acc::CopyoutOp>(
739 var, mlir::acc::DataClause::acc_copy, clause.getModifierList(),
740 /*structured=*/true,
741 /*implicit=*/false);
742 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
743 for (const Expr *var : clause.getVarList())
744 addDataOperand<mlir::acc::CopyinOp>(
745 var, mlir::acc::DataClause::acc_copy, clause.getModifierList(),
746 /*structured=*/true,
747 /*implicit=*/false);
748 } else if constexpr (isCombinedType<OpTy>) {
749 applyToComputeOp(clause);
750 } else {
751 llvm_unreachable("Unknown construct kind in VisitCopyClause");
752 }
753 }
754
755 void VisitCopyInClause(const OpenACCCopyInClause &clause) {
756 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
757 mlir::acc::KernelsOp, mlir::acc::DataOp>) {
758 for (const Expr *var : clause.getVarList())
759 addDataOperand<mlir::acc::CopyinOp, mlir::acc::DeleteOp>(
760 var, mlir::acc::DataClause::acc_copyin, clause.getModifierList(),
761 /*structured=*/true,
762 /*implicit=*/false);
763 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::EnterDataOp>) {
764 for (const Expr *var : clause.getVarList())
765 addDataOperand<mlir::acc::CopyinOp>(
766 var, mlir::acc::DataClause::acc_copyin, clause.getModifierList(),
767 /*structured=*/false, /*implicit=*/false);
768 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
769 for (const Expr *var : clause.getVarList())
770 addDataOperand<mlir::acc::CopyinOp>(
771 var, mlir::acc::DataClause::acc_copyin, clause.getModifierList(),
772 /*structured=*/true,
773 /*implicit=*/false);
774 } else if constexpr (isCombinedType<OpTy>) {
775 applyToComputeOp(clause);
776 } else {
777 llvm_unreachable("Unknown construct kind in VisitCopyInClause");
778 }
779 }
780
781 void VisitCopyOutClause(const OpenACCCopyOutClause &clause) {
782 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
783 mlir::acc::KernelsOp, mlir::acc::DataOp>) {
784 for (const Expr *var : clause.getVarList())
785 addDataOperand<mlir::acc::CreateOp, mlir::acc::CopyoutOp>(
786 var, mlir::acc::DataClause::acc_copyout, clause.getModifierList(),
787 /*structured=*/true,
788 /*implicit=*/false);
789 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::ExitDataOp>) {
790 for (const Expr *var : clause.getVarList())
791 addDataOperand<mlir::acc::GetDevicePtrOp, mlir::acc::CopyoutOp>(
792 var, mlir::acc::DataClause::acc_copyout, clause.getModifierList(),
793 /*structured=*/false,
794 /*implicit=*/false);
795 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
796 for (const Expr *var : clause.getVarList())
797 addDataOperand<mlir::acc::CreateOp>(
798 var, mlir::acc::DataClause::acc_copyout, clause.getModifierList(),
799 /*structured=*/true,
800 /*implicit=*/false);
801 } else if constexpr (isCombinedType<OpTy>) {
802 applyToComputeOp(clause);
803 } else {
804 llvm_unreachable("Unknown construct kind in VisitCopyOutClause");
805 }
806 }
807
808 void VisitCreateClause(const OpenACCCreateClause &clause) {
809 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
810 mlir::acc::KernelsOp, mlir::acc::DataOp>) {
811 for (const Expr *var : clause.getVarList())
812 addDataOperand<mlir::acc::CreateOp, mlir::acc::DeleteOp>(
813 var, mlir::acc::DataClause::acc_create, clause.getModifierList(),
814 /*structured=*/true,
815 /*implicit=*/false);
816 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::EnterDataOp>) {
817 for (const Expr *var : clause.getVarList())
818 addDataOperand<mlir::acc::CreateOp>(
819 var, mlir::acc::DataClause::acc_create, clause.getModifierList(),
820 /*structured=*/false, /*implicit=*/false);
821 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
822 for (const Expr *var : clause.getVarList())
823 addDataOperand<mlir::acc::CreateOp>(
824 var, mlir::acc::DataClause::acc_create, clause.getModifierList(),
825 /*structured=*/true,
826 /*implicit=*/false);
827 } else if constexpr (isCombinedType<OpTy>) {
828 applyToComputeOp(clause);
829 } else {
830 llvm_unreachable("Unknown construct kind in VisitCreateClause");
831 }
832 }
833
834 void VisitLinkClause(const OpenACCLinkClause &clause) {
835 if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
836 for (const Expr *var : clause.getVarList())
837 addDataOperand<mlir::acc::DeclareLinkOp>(
838 var, mlir::acc::DataClause::acc_declare_link, {},
839 /*structured=*/true,
840 /*implicit=*/false);
841 } else {
842 llvm_unreachable("Unknown construct kind in VisitLinkClause");
843 }
844 }
845
846 void VisitDeleteClause(const OpenACCDeleteClause &clause) {
847 if constexpr (isOneOfTypes<OpTy, mlir::acc::ExitDataOp>) {
848 for (const Expr *var : clause.getVarList())
849 addDataOperand<mlir::acc::GetDevicePtrOp, mlir::acc::DeleteOp>(
850 var, mlir::acc::DataClause::acc_delete, {},
851 /*structured=*/false,
852 /*implicit=*/false);
853 } else {
854 llvm_unreachable("Unknown construct kind in VisitDeleteClause");
855 }
856 }
857
858 void VisitDetachClause(const OpenACCDetachClause &clause) {
859 if constexpr (isOneOfTypes<OpTy, mlir::acc::ExitDataOp>) {
860 for (const Expr *var : clause.getVarList())
861 addDataOperand<mlir::acc::GetDevicePtrOp, mlir::acc::DetachOp>(
862 var, mlir::acc::DataClause::acc_detach, {},
863 /*structured=*/false,
864 /*implicit=*/false);
865 } else {
866 llvm_unreachable("Unknown construct kind in VisitDetachClause");
867 }
868 }
869
870 void VisitFinalizeClause(const OpenACCFinalizeClause &clause) {
871 if constexpr (isOneOfTypes<OpTy, mlir::acc::ExitDataOp>) {
872 operation.setFinalize(true);
873 } else {
874 llvm_unreachable("Unknown construct kind in VisitFinalizeClause");
875 }
876 }
877
878 void VisitUseDeviceClause(const OpenACCUseDeviceClause &clause) {
879 if constexpr (isOneOfTypes<OpTy, mlir::acc::HostDataOp>) {
880 for (const Expr *var : clause.getVarList())
881 addDataOperand<mlir::acc::UseDeviceOp>(
882 var, mlir::acc::DataClause::acc_use_device, {}, /*structured=*/true,
883 /*implicit=*/false);
884 } else {
885 llvm_unreachable("Unknown construct kind in VisitUseDeviceClause");
886 }
887 }
888
889 void VisitDevicePtrClause(const OpenACCDevicePtrClause &clause) {
890 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
891 mlir::acc::KernelsOp, mlir::acc::DataOp,
892 mlir::acc::DeclareEnterOp>) {
893 for (const Expr *var : clause.getVarList())
894 addDataOperand<mlir::acc::DevicePtrOp>(
895 var, mlir::acc::DataClause::acc_deviceptr, {},
896 /*structured=*/true,
897 /*implicit=*/false);
898 } else if constexpr (isCombinedType<OpTy>) {
899 applyToComputeOp(clause);
900 } else {
901 llvm_unreachable("Unknown construct kind in VisitDevicePtrClause");
902 }
903 }
904
905 void VisitNoCreateClause(const OpenACCNoCreateClause &clause) {
906 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
907 mlir::acc::KernelsOp, mlir::acc::DataOp>) {
908 for (const Expr *var : clause.getVarList())
909 addDataOperand<mlir::acc::NoCreateOp, mlir::acc::DeleteOp>(
910 var, mlir::acc::DataClause::acc_no_create, {}, /*structured=*/true,
911 /*implicit=*/false);
912 } else if constexpr (isCombinedType<OpTy>) {
913 applyToComputeOp(clause);
914 } else {
915 llvm_unreachable("Unknown construct kind in VisitNoCreateClause");
916 }
917 }
918
919 void VisitPresentClause(const OpenACCPresentClause &clause) {
920 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
921 mlir::acc::KernelsOp, mlir::acc::DataOp>) {
922 for (const Expr *var : clause.getVarList())
923 addDataOperand<mlir::acc::PresentOp, mlir::acc::DeleteOp>(
924 var, mlir::acc::DataClause::acc_present, {}, /*structured=*/true,
925 /*implicit=*/false);
926 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
927 for (const Expr *var : clause.getVarList())
928 addDataOperand<mlir::acc::PresentOp>(
929 var, mlir::acc::DataClause::acc_present, {},
930 /*structured=*/true,
931 /*implicit=*/false);
932 } else if constexpr (isCombinedType<OpTy>) {
933 applyToComputeOp(clause);
934 } else {
935 llvm_unreachable("Unknown construct kind in VisitPresentClause");
936 }
937 }
938
939 void VisitAttachClause(const OpenACCAttachClause &clause) {
940 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
941 mlir::acc::KernelsOp, mlir::acc::DataOp>) {
942 for (const Expr *var : clause.getVarList())
943 addDataOperand<mlir::acc::AttachOp, mlir::acc::DetachOp>(
944 var, mlir::acc::DataClause::acc_attach, {}, /*structured=*/true,
945 /*implicit=*/false);
946 } else if constexpr (isOneOfTypes<OpTy, mlir::acc::EnterDataOp>) {
947 for (const Expr *var : clause.getVarList())
948 addDataOperand<mlir::acc::AttachOp>(
949 var, mlir::acc::DataClause::acc_attach, {},
950 /*structured=*/false, /*implicit=*/false);
951 } else if constexpr (isCombinedType<OpTy>) {
952 applyToComputeOp(clause);
953 } else {
954 llvm_unreachable("Unknown construct kind in VisitAttachClause");
955 }
956 }
957
958 void VisitPrivateClause(const OpenACCPrivateClause &clause) {
959 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
960 mlir::acc::LoopOp>) {
961 for (const auto [varExpr, varRecipe] :
962 llvm::zip_equal(clause.getVarList(), clause.getInitRecipes())) {
963 CIRGenFunction::OpenACCDataOperandInfo opInfo =
964 cgf.getOpenACCDataOperandInfo(varExpr);
965 auto privateOp = mlir::acc::PrivateOp::create(
966 builder, opInfo.beginLoc, opInfo.varValue, /*structured=*/true,
967 /*implicit=*/false, opInfo.name, opInfo.bounds);
968 privateOp.setDataClause(mlir::acc::DataClause::acc_private);
969
970 {
971 mlir::OpBuilder::InsertionGuard guardCase(builder);
972
973 auto recipe =
974 OpenACCRecipeBuilder<mlir::acc::PrivateRecipeOp>(cgf, builder)
975 .getOrCreateRecipe(
976 cgf.getContext(), recipeInsertLocation, varExpr,
977 varRecipe.AllocaDecl,
978 /*temporary=*/nullptr, OpenACCReductionOperator::Invalid,
979 Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
980 opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,
981 privateOp, /*reductionCombinerRecipes=*/{});
982 // TODO: OpenACC: The dialect is going to change in the near future to
983 // have these be on a different operation, so when that changes, we
984 // probably need to change these here.
985 operation.addPrivatization(builder.getContext(), privateOp, recipe);
986 }
987 }
988 } else if constexpr (isCombinedType<OpTy>) {
989 // Despite this being valid on ParallelOp or SerialOp, combined type
990 // applies to the 'loop'.
991 applyToLoopOp(clause);
992 } else {
993 llvm_unreachable("Unknown construct kind in VisitPrivateClause");
994 }
995 }
996
997 void VisitFirstPrivateClause(const OpenACCFirstPrivateClause &clause) {
998 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp,
999 mlir::acc::SerialOp>) {
1000 for (const auto [varExpr, varRecipe] :
1001 llvm::zip_equal(clause.getVarList(), clause.getInitRecipes())) {
1002 CIRGenFunction::OpenACCDataOperandInfo opInfo =
1003 cgf.getOpenACCDataOperandInfo(varExpr);
1004 auto firstPrivateOp = mlir::acc::FirstprivateOp::create(
1005 builder, opInfo.beginLoc, opInfo.varValue, /*structured=*/true,
1006 /*implicit=*/false, opInfo.name, opInfo.bounds);
1007
1008 firstPrivateOp.setDataClause(mlir::acc::DataClause::acc_firstprivate);
1009
1010 {
1011 mlir::OpBuilder::InsertionGuard guardCase(builder);
1012
1013 auto recipe =
1014 OpenACCRecipeBuilder<mlir::acc::FirstprivateRecipeOp>(cgf,
1015 builder)
1016 .getOrCreateRecipe(
1017 cgf.getContext(), recipeInsertLocation, varExpr,
1018 varRecipe.AllocaDecl, varRecipe.InitFromTemporary,
1019 OpenACCReductionOperator::Invalid,
1020 Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
1021 opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,
1022 firstPrivateOp, /*reductionCombinerRecipe=*/{});
1023
1024 // TODO: OpenACC: The dialect is going to change in the near future to
1025 // have these be on a different operation, so when that changes, we
1026 // probably need to change these here.
1027 operation.addFirstPrivatization(builder.getContext(), firstPrivateOp,
1028 recipe);
1029 }
1030 }
1031 } else if constexpr (isCombinedType<OpTy>) {
1032 // Unlike 'private', 'firstprivate' applies to the compute op, not the
1033 // loop op.
1034 applyToComputeOp(clause);
1035 } else {
1036 llvm_unreachable("Unknown construct kind in VisitFirstPrivateClause");
1037 }
1038 }
1039
1040 void VisitReductionClause(const OpenACCReductionClause &clause) {
1041 if constexpr (isOneOfTypes<OpTy, mlir::acc::ParallelOp, mlir::acc::SerialOp,
1042 mlir::acc::LoopOp>) {
1043 for (const auto [varExpr, varRecipe] :
1044 llvm::zip_equal(clause.getVarList(), clause.getRecipes())) {
1045 CIRGenFunction::OpenACCDataOperandInfo opInfo =
1046 cgf.getOpenACCDataOperandInfo(varExpr);
1047
1048 auto reductionOp = mlir::acc::ReductionOp::create(
1049 builder, opInfo.beginLoc, opInfo.varValue, /*structured=*/true,
1050 /*implicit=*/false, opInfo.name, opInfo.bounds);
1051 reductionOp.setDataClause(mlir::acc::DataClause::acc_reduction);
1052
1053 {
1054 mlir::OpBuilder::InsertionGuard guardCase(builder);
1055
1056 auto recipe =
1057 OpenACCRecipeBuilder<mlir::acc::ReductionRecipeOp>(cgf, builder)
1058 .getOrCreateRecipe(
1059 cgf.getContext(), recipeInsertLocation, varExpr,
1060 varRecipe.AllocaDecl,
1061 /*temporary=*/nullptr, clause.getReductionOp(),
1062 Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
1063 opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,
1064 reductionOp, varRecipe.CombinerRecipes);
1065
1066 operation.addReduction(builder.getContext(), reductionOp, recipe);
1067 }
1068 }
1069 } else if constexpr (isCombinedType<OpTy>) {
1070 // Despite this being valid on ParallelOp or SerialOp, combined type
1071 // applies to the 'loop'.
1072 applyToLoopOp(clause);
1073 } else {
1074 llvm_unreachable("Unknown construct kind in VisitReductionClause");
1075 }
1076 }
1077
1078 void VisitDeviceResidentClause(const OpenACCDeviceResidentClause &clause) {
1079 if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
1080 for (const Expr *var : clause.getVarList())
1081 addDataOperand<mlir::acc::DeclareDeviceResidentOp>(
1082 var, mlir::acc::DataClause::acc_declare_device_resident, {},
1083 /*structured=*/true,
1084 /*implicit=*/false);
1085 } else {
1086 llvm_unreachable("Unknown construct kind in VisitDeviceResidentClause");
1087 }
1088 }
1089};
1090
1091template <typename OpTy>
1092auto makeClauseEmitter(OpTy &op,
1093 mlir::OpBuilder::InsertPoint &recipeInsertLocation,
1095 CIRGen::CIRGenBuilderTy &builder,
1096 OpenACCDirectiveKind dirKind) {
1097 return OpenACCClauseCIREmitter<OpTy>(op, recipeInsertLocation, cgf, builder,
1098 dirKind);
1099}
1100} // namespace
1101
1102template <typename Op>
1103void CIRGenFunction::emitOpenACCClauses(
1104 Op &op, OpenACCDirectiveKind dirKind,
1106 mlir::OpBuilder::InsertionGuard guardCase(builder);
1107
1108 // Sets insertion point before the 'op', since every new expression needs to
1109 // be before the operation.
1110 builder.setInsertionPoint(op);
1111 makeClauseEmitter(op, lastRecipeLocation, *this, builder, dirKind)
1112 .emitClauses(clauses);
1113}
1114
1115#define EXPL_SPEC(N) \
1116 template void CIRGenFunction::emitOpenACCClauses<N>( \
1117 N &, OpenACCDirectiveKind, ArrayRef<const OpenACCClause *>);
1118EXPL_SPEC(mlir::acc::ParallelOp)
1119EXPL_SPEC(mlir::acc::SerialOp)
1120EXPL_SPEC(mlir::acc::KernelsOp)
1121EXPL_SPEC(mlir::acc::LoopOp)
1122EXPL_SPEC(mlir::acc::DataOp)
1123EXPL_SPEC(mlir::acc::InitOp)
1124EXPL_SPEC(mlir::acc::ShutdownOp)
1125EXPL_SPEC(mlir::acc::SetOp)
1126EXPL_SPEC(mlir::acc::WaitOp)
1127EXPL_SPEC(mlir::acc::HostDataOp)
1128EXPL_SPEC(mlir::acc::EnterDataOp)
1129EXPL_SPEC(mlir::acc::ExitDataOp)
1130EXPL_SPEC(mlir::acc::UpdateOp)
1131EXPL_SPEC(mlir::acc::AtomicReadOp)
1132EXPL_SPEC(mlir::acc::AtomicWriteOp)
1133EXPL_SPEC(mlir::acc::AtomicCaptureOp)
1134EXPL_SPEC(mlir::acc::AtomicUpdateOp)
1135EXPL_SPEC(mlir::acc::DeclareEnterOp)
1136#undef EXPL_SPEC
1137
1138template <typename ComputeOp, typename LoopOp>
1139void CIRGenFunction::emitOpenACCClauses(
1140 ComputeOp &op, LoopOp &loopOp, OpenACCDirectiveKind dirKind,
1142 static_assert(std::is_same_v<mlir::acc::LoopOp, LoopOp>);
1143
1144 CombinedConstructClauseInfo<ComputeOp> inf{op, loopOp};
1145 // We cannot set the insertion point here and do so in the emitter, but make
1146 // sure we reset it with the 'guard' anyway.
1147 mlir::OpBuilder::InsertionGuard guardCase(builder);
1148 makeClauseEmitter(inf, lastRecipeLocation, *this, builder, dirKind)
1149 .emitClauses(clauses);
1150}
1151
1152#define EXPL_SPEC(N) \
1153 template void CIRGenFunction::emitOpenACCClauses<N, mlir::acc::LoopOp>( \
1154 N &, mlir::acc::LoopOp &, OpenACCDirectiveKind, \
1155 ArrayRef<const OpenACCClause *>);
1156
1157EXPL_SPEC(mlir::acc::ParallelOp)
1158EXPL_SPEC(mlir::acc::SerialOp)
1159EXPL_SPEC(mlir::acc::KernelsOp)
1160#undef EXPL_SPEC
#define EXPL_SPEC(N)
Defines the clang::Expr interface and subclasses for C++ expressions.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a an optional score condition
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
static DeclContext * castToDeclContext(const Decl *)
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
const Expr * getConditionExpr() const
ArrayRef< Expr * > getVarList() const
OpenACCModifierKind getModifierList() const
OpenACCModifierKind getModifierList() const
OpenACCModifierKind getModifierList() const
OpenACCModifierKind getModifierList() const
OpenACCDefaultClauseKind getDefaultClauseKind() const
ArrayRef< DeviceTypeArgument > getArchitectures() const
ArrayRef< OpenACCFirstPrivateRecipe > getInitRecipes() const
unsigned getNumExprs() const
std::pair< OpenACCGangKind, const Expr * > getExpr(unsigned I) const
ArrayRef< Expr * > getIntExprs() const
ArrayRef< OpenACCPrivateRecipe > getInitRecipes() const
ArrayRef< OpenACCReductionRecipe > getRecipes() const
OpenACCReductionOperator getReductionOp() const
const Expr * getConditionExpr() const
bool isConditionExprClause() const
ArrayRef< Expr * > getVarList() const
ArrayRef< Expr * > getSizeExprs() const
Expr * getDevNumExpr() const
ArrayRef< Expr * > getQueueIdExprs() const
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
mlir::acc::DeviceType decodeDeviceType(const IdentifierInfo *ii)
mlir::acc::DataClauseModifier convertOpenACCModifiers(OpenACCModifierKind modifiers)
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Top level wrappers for InstallAPI frontend operations.
OpenACCDirectiveKind
bool isa(CodeGen::Address addr)
Definition Address.h:330
OpenACCModifierKind
IdentifierLoc DeviceTypeArgument
@ Default
Set to the current date and time.
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t