MLIR 23.0.0git
ACCImplicitData.cpp
Go to the documentation of this file.
1//===- ACCImplicitData.cpp ------------------------------------------------===//
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 pass implements the OpenACC specification for "Variables with
10// Implicitly Determined Data Attributes" (OpenACC 3.4 spec, section 2.6.2).
11//
12// Overview:
13// ---------
14// The pass automatically generates data clause operations for variables used
15// within OpenACC compute constructs (parallel, kernels, serial) that do not
16// already have explicit data clauses. The semantics follow these rules:
17//
18// 1. If there is a default(none) clause visible, no implicit data actions
19// apply.
20//
21// 2. An aggregate variable (arrays, derived types, etc.) will be treated as:
22// - In a present clause when default(present) is visible.
23// - In a copy clause otherwise.
24//
25// 3. A scalar variable will be treated as if it appears in:
26// - A copy clause if the compute construct is a kernels construct.
27// - A firstprivate clause otherwise (parallel, serial).
28//
29// Requirements:
30// -------------
31// To use this pass in a pipeline, the following requirements must be met:
32//
33// 1. Type Interface Implementation: Variables from the dialect being used
34// must implement one or both of the following MLIR interfaces:
35// `acc::MappableType` and/or `acc::PointerLikeType`
36//
37// These interfaces provide the necessary methods for the pass to:
38// - Determine variable type categories (scalar vs. aggregate)
39// - Generate appropriate bounds information
40// - Generate privatization recipes
41//
42// 2. Operation Interface Implementation: Operations that access partial
43// entities or create views should implement the following MLIR
44// interfaces: `acc::PartialEntityAccess` and/or
45// `mlir::ViewLikeOpInterface`
46//
47// These interfaces are used for proper data clause ordering, ensuring
48// that base entities are mapped before derived entities (e.g., a
49// struct is mapped before its fields, an array is mapped before
50// subarray views).
51//
52// 3. Analysis Registration (Optional): If custom behavior is needed for
53// variable name extraction or alias analysis, the dialect should
54// pre-register the `acc::OpenACCSupport` and `mlir::AliasAnalysis` analyses.
55//
56// If not registered, default behavior will be used.
57//
58// Implementation Details:
59// -----------------------
60// The pass performs the following operations:
61//
62// 1. Finds candidate variables which are live-in to the compute region and
63// are not already in a data clause or private clause.
64//
65// 2. Generates both data "entry" and "exit" clause operations that match
66// the intended action depending on variable type:
67// - copy -> acc.copyin (entry) + acc.copyout (exit)
68// - present -> acc.present (entry) + acc.delete (exit)
69// - firstprivate -> acc.firstprivate (entry only, no exit)
70//
71// 3. Ensures that default clause is taken into consideration by looking
72// through current construct and parent constructs to find the "visible
73// default clause".
74//
75// 4. Fixes up SSA value links so that uses in the acc region reference the
76// result of the newly created data clause operations.
77//
78// 5. When generating implicit data clause operations, it also adds variable
79// name information and marks them with the implicit flag.
80//
81// 6. Recipes are generated by calling the appropriate entrypoints in the
82// MappableType and PointerLikeType interfaces.
83//
84// 7. AliasAnalysis is used to determine if a variable is already covered by
85// an existing data clause (e.g., an interior pointer covered by its parent).
86//
87// Examples:
88// ---------
89//
90// Example 1: Scalar in parallel construct (implicit firstprivate)
91//
92// Before:
93// func.func @test() {
94// %scalar = memref.alloca() {acc.var_name = "x"} : memref<f32>
95// acc.parallel {
96// %val = memref.load %scalar[] : memref<f32>
97// acc.yield
98// }
99// }
100//
101// After:
102// func.func @test() {
103// %scalar = memref.alloca() {acc.var_name = "x"} : memref<f32>
104// %firstpriv = acc.firstprivate varPtr(%scalar : memref<f32>)
105// -> memref<f32> {implicit = true, name = "x"}
106// acc.parallel firstprivate(@recipe -> %firstpriv : memref<f32>) {
107// %val = memref.load %firstpriv[] : memref<f32>
108// acc.yield
109// }
110// }
111//
112// Example 2: Scalar in kernels construct (implicit copy)
113//
114// Before:
115// func.func @test() {
116// %scalar = memref.alloca() {acc.var_name = "n"} : memref<i32>
117// acc.kernels {
118// %val = memref.load %scalar[] : memref<i32>
119// acc.terminator
120// }
121// }
122//
123// After:
124// func.func @test() {
125// %scalar = memref.alloca() {acc.var_name = "n"} : memref<i32>
126// %copyin = acc.copyin varPtr(%scalar : memref<i32>) -> memref<i32>
127// {dataClause = #acc<data_clause acc_copy>,
128// implicit = true, name = "n"}
129// acc.kernels dataOperands(%copyin : memref<i32>) {
130// %val = memref.load %copyin[] : memref<i32>
131// acc.terminator
132// }
133// acc.copyout accPtr(%copyin : memref<i32>)
134// to varPtr(%scalar : memref<i32>)
135// {dataClause = #acc<data_clause acc_copy>,
136// implicit = true, name = "n"}
137// }
138//
139// Example 3: Array (aggregate) in parallel (implicit copy)
140//
141// Before:
142// func.func @test() {
143// %array = memref.alloca() {acc.var_name = "arr"} : memref<100xf32>
144// acc.parallel {
145// %c0 = arith.constant 0 : index
146// %val = memref.load %array[%c0] : memref<100xf32>
147// acc.yield
148// }
149// }
150//
151// After:
152// func.func @test() {
153// %array = memref.alloca() {acc.var_name = "arr"} : memref<100xf32>
154// %copyin = acc.copyin varPtr(%array : memref<100xf32>)
155// -> memref<100xf32>
156// {dataClause = #acc<data_clause acc_copy>,
157// implicit = true, name = "arr"}
158// acc.parallel dataOperands(%copyin : memref<100xf32>) {
159// %c0 = arith.constant 0 : index
160// %val = memref.load %copyin[%c0] : memref<100xf32>
161// acc.yield
162// }
163// acc.copyout accPtr(%copyin : memref<100xf32>)
164// to varPtr(%array : memref<100xf32>)
165// {dataClause = #acc<data_clause acc_copy>,
166// implicit = true, name = "arr"}
167// }
168//
169// Example 4: Array with default(present)
170//
171// Before:
172// func.func @test() {
173// %array = memref.alloca() {acc.var_name = "arr"} : memref<100xf32>
174// acc.parallel {
175// %c0 = arith.constant 0 : index
176// %val = memref.load %array[%c0] : memref<100xf32>
177// acc.yield
178// } attributes {defaultAttr = #acc<defaultvalue present>}
179// }
180//
181// After:
182// func.func @test() {
183// %array = memref.alloca() {acc.var_name = "arr"} : memref<100xf32>
184// %present = acc.present varPtr(%array : memref<100xf32>)
185// -> memref<100xf32>
186// {implicit = true, name = "arr"}
187// acc.parallel dataOperands(%present : memref<100xf32>)
188// attributes {defaultAttr = #acc<defaultvalue present>} {
189// %c0 = arith.constant 0 : index
190// %val = memref.load %present[%c0] : memref<100xf32>
191// acc.yield
192// }
193// acc.delete accPtr(%present : memref<100xf32>)
194// {dataClause = #acc<data_clause acc_present>,
195// implicit = true, name = "arr"}
196// }
197//
198//===----------------------------------------------------------------------===//
199
201#include "llvm/ADT/SmallVectorExtras.h"
202
207#include "mlir/IR/Builders.h"
208#include "mlir/IR/BuiltinOps.h"
209#include "mlir/IR/Dominance.h"
210#include "mlir/IR/Operation.h"
211#include "mlir/IR/Value.h"
215#include "llvm/ADT/STLExtras.h"
216#include "llvm/ADT/SmallVector.h"
217#include "llvm/ADT/TypeSwitch.h"
218#include "llvm/Support/ErrorHandling.h"
219#include <type_traits>
220
221namespace mlir {
222namespace acc {
223#define GEN_PASS_DEF_ACCIMPLICITDATA
224#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
225} // namespace acc
226} // namespace mlir
227
228#define DEBUG_TYPE "acc-implicit-data"
229
230using namespace mlir;
231
232namespace {
233
234class ACCImplicitData : public acc::impl::ACCImplicitDataBase<ACCImplicitData> {
235public:
236 using acc::impl::ACCImplicitDataBase<ACCImplicitData>::ACCImplicitDataBase;
237
238 void runOnOperation() override;
239
240private:
241 /// Looks through the `dominatingDataClauses` to find the original data clause
242 /// op for an alias. Returns nullptr if no original data clause op is found.
243 template <typename OpT>
244 Operation *getOriginalDataClauseOpForAlias(
245 Value var, OpBuilder &builder, OpT computeConstructOp,
246 const SmallVector<Value> &dominatingDataClauses);
247
248 /// Generates the appropriate `acc.copyin`, `acc.present`,`acc.firstprivate`,
249 /// etc. data clause op for a candidate variable.
250 template <typename OpT>
251 Operation *generateDataClauseOpForCandidate(
252 Value var, ModuleOp &module, OpBuilder &builder, OpT computeConstructOp,
253 const SmallVector<Value> &dominatingDataClauses,
254 const std::optional<acc::ClauseDefaultValue> &defaultClause);
255
256 /// Generates the implicit data ops for a compute construct.
257 template <typename OpT>
258 void
259 generateImplicitDataOps(ModuleOp &module, OpT computeConstructOp,
260 std::optional<acc::ClauseDefaultValue> &defaultClause,
261 acc::OpenACCSupport &accSupport);
262
263 /// Generates a private recipe for a variable.
264 acc::PrivateRecipeOp generatePrivateRecipe(ModuleOp &module, Value var,
265 Location loc, OpBuilder &builder,
266 acc::OpenACCSupport &accSupport);
267
268 /// Generates a firstprivate recipe for a variable.
269 acc::FirstprivateRecipeOp
270 generateFirstprivateRecipe(ModuleOp &module, Value var, Location loc,
271 OpBuilder &builder,
272 acc::OpenACCSupport &accSupport);
273
274 /// Generates recipes for a list of variables.
275 void generateRecipes(ModuleOp &module, OpBuilder &builder,
276 Operation *computeConstructOp,
277 const SmallVector<Value> &newOperands);
278};
279
280/// Determines if a variable is a candidate for implicit data mapping.
281/// Returns true if the variable is a candidate, false otherwise.
282static bool isCandidateForImplicitData(Value val, Region &accRegion,
283 acc::OpenACCSupport &accSupport) {
284 // Ensure the variable is an allowed type for data clause.
285 if (!acc::isPointerLikeType(val.getType()) &&
287 return false;
288
289 // If this is already coming from a data clause, we do not need to generate
290 // another.
291 if (isa_and_nonnull<ACC_DATA_ENTRY_OPS>(val.getDefiningOp()))
292 return false;
293
294 // Device data is a candidate - it will get a deviceptr clause.
295 if (acc::isDeviceValue(val))
296 return true;
297
298 // If it is otherwise valid, skip it.
299 if (accSupport.isValidValueUse(val, accRegion))
300 return false;
301
302 return true;
303}
304
305template <typename OpT>
306Operation *ACCImplicitData::getOriginalDataClauseOpForAlias(
307 Value var, OpBuilder &builder, OpT computeConstructOp,
308 const SmallVector<Value> &dominatingDataClauses) {
309 auto &aliasAnalysis = this->getAnalysis<AliasAnalysis>();
310 for (auto dataClause : dominatingDataClauses) {
311 if (auto *dataClauseOp = dataClause.getDefiningOp()) {
312 // Only accept clauses that guarantee that the alias is present.
313 if (isa<acc::CopyinOp, acc::CreateOp, acc::PresentOp, acc::NoCreateOp,
314 acc::DevicePtrOp>(dataClauseOp))
315 if (aliasAnalysis.alias(acc::getVar(dataClauseOp), var).isMust()) {
316 LLVM_DEBUG(llvm::dbgs()
317 << "Using existing data clause:\n\t" << *dataClauseOp
318 << "\n\tas reference when processing var:\n\t" << var
319 << "\n";);
320 return dataClauseOp;
321 }
322 }
323 }
324 return nullptr;
325}
326
327// Generates bounds for variables that have unknown dimensions
328static void fillInBoundsForUnknownDimensions(Operation *dataClauseOp,
329 OpBuilder &builder) {
330
331 if (!acc::getBounds(dataClauseOp).empty())
332 // If bounds are already present, do not overwrite them.
333 return;
334
335 // For types that have unknown dimensions, attempt to generate bounds by
336 // relying on MappableType being able to extract it from the IR.
337 auto var = acc::getVar(dataClauseOp);
338 auto type = var.getType();
339 if (auto mappableTy = dyn_cast<acc::MappableType>(type)) {
340 if (mappableTy.hasUnknownDimensions()) {
341 TypeSwitch<Operation *>(dataClauseOp)
342 .Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>([&](auto dataClauseOp) {
343 if (std::is_same_v<decltype(dataClauseOp), acc::DevicePtrOp>)
344 return;
345 OpBuilder::InsertionGuard guard(builder);
346 builder.setInsertionPoint(dataClauseOp);
347 auto bounds = mappableTy.generateAccBounds(var, builder);
348 if (!bounds.empty())
349 dataClauseOp.getBoundsMutable().assign(bounds);
350 });
351 }
352 }
353}
354
355acc::PrivateRecipeOp
356ACCImplicitData::generatePrivateRecipe(ModuleOp &module, Value var,
357 Location loc, OpBuilder &builder,
358 acc::OpenACCSupport &accSupport) {
359 auto type = var.getType();
360 std::string recipeName =
361 accSupport.getRecipeName(acc::RecipeKind::private_recipe, type, var);
362
363 // Check if recipe already exists
364 auto existingRecipe = module.lookupSymbol<acc::PrivateRecipeOp>(recipeName);
365 if (existingRecipe)
366 return existingRecipe;
367
368 // Set insertion point to module body in a scoped way
369 OpBuilder::InsertionGuard guard(builder);
370 builder.setInsertionPointToStart(module.getBody());
371
372 auto recipe =
373 acc::PrivateRecipeOp::createAndPopulate(builder, loc, recipeName, var);
374 if (!recipe.has_value())
375 return accSupport.emitNYI(loc, "implicit private"), nullptr;
376 return recipe.value();
377}
378
379acc::FirstprivateRecipeOp
380ACCImplicitData::generateFirstprivateRecipe(ModuleOp &module, Value var,
381 Location loc, OpBuilder &builder,
382 acc::OpenACCSupport &accSupport) {
383 auto type = var.getType();
384 std::string recipeName =
385 accSupport.getRecipeName(acc::RecipeKind::firstprivate_recipe, type, var);
386
387 // Check if recipe already exists
388 auto existingRecipe =
389 module.lookupSymbol<acc::FirstprivateRecipeOp>(recipeName);
390 if (existingRecipe)
391 return existingRecipe;
392
393 // Set insertion point to module body in a scoped way
394 OpBuilder::InsertionGuard guard(builder);
395 builder.setInsertionPointToStart(module.getBody());
396
397 auto recipe = acc::FirstprivateRecipeOp::createAndPopulate(builder, loc,
398 recipeName, var);
399 if (!recipe.has_value())
400 return accSupport.emitNYI(loc, "implicit firstprivate"), nullptr;
401 return recipe.value();
402}
403
404void ACCImplicitData::generateRecipes(ModuleOp &module, OpBuilder &builder,
405 Operation *computeConstructOp,
406 const SmallVector<Value> &newOperands) {
407 auto &accSupport = this->getAnalysis<acc::OpenACCSupport>();
408 for (auto var : newOperands) {
409 auto loc{var.getLoc()};
410 if (auto privateOp = var.getDefiningOp<acc::PrivateOp>()) {
411 auto recipe = generatePrivateRecipe(
412 module, acc::getVar(var.getDefiningOp()), loc, builder, accSupport);
413 if (recipe)
414 privateOp.setRecipeAttr(
415 SymbolRefAttr::get(module->getContext(), recipe.getSymName()));
416 } else if (auto firstprivateOp = var.getDefiningOp<acc::FirstprivateOp>()) {
417 auto recipe = generateFirstprivateRecipe(
418 module, acc::getVar(var.getDefiningOp()), loc, builder, accSupport);
419 if (recipe)
420 firstprivateOp.setRecipeAttr(SymbolRefAttr::get(
421 module->getContext(), recipe.getSymName().str()));
422 } else {
423 accSupport.emitNYI(var.getLoc(), "implicit reduction");
424 }
425 }
426}
427
428// Generates the data entry data op clause so that it adheres to OpenACC
429// rules as follows (line numbers and specification from OpenACC 3.4):
430// 1388 An aggregate variable will be treated as if it appears either:
431// 1389 - In a present clause if there is a default(present) clause visible at
432// the compute construct.
433// 1391 - In a copy clause otherwise.
434// 1392 A scalar variable will be treated as if it appears either:
435// 1393 - In a copy clause if the compute construct is a kernels construct.
436// 1394 - In a firstprivate clause otherwise.
437template <typename OpT>
438Operation *ACCImplicitData::generateDataClauseOpForCandidate(
439 Value var, ModuleOp &module, OpBuilder &builder, OpT computeConstructOp,
440 const SmallVector<Value> &dominatingDataClauses,
441 const std::optional<acc::ClauseDefaultValue> &defaultClause) {
442 auto &accSupport = this->getAnalysis<acc::OpenACCSupport>();
443 acc::VariableTypeCategory typeCategory =
444 acc::VariableTypeCategory::uncategorized;
445 if (auto mappableTy = dyn_cast<acc::MappableType>(var.getType())) {
446 typeCategory = mappableTy.getTypeCategory(var);
447 } else if (auto pointerLikeTy =
448 dyn_cast<acc::PointerLikeType>(var.getType())) {
449 typeCategory = pointerLikeTy.getPointeeTypeCategory(
451 pointerLikeTy.getElementType());
452 }
453
454 bool isScalar =
455 acc::bitEnumContainsAny(typeCategory, acc::VariableTypeCategory::scalar);
456 bool isAnyAggregate = acc::bitEnumContainsAny(
457 typeCategory, acc::VariableTypeCategory::aggregate);
458 Location loc = computeConstructOp->getLoc();
459
460 if (acc::isDeviceValue(var)) {
461 // If the variable is device data, use deviceptr clause.
462 LLVM_DEBUG(llvm::dbgs() << "Using deviceptr clause because variable is "
463 "device data\n");
464 return acc::DevicePtrOp::create(builder, loc, var,
465 /*structured=*/true, /*implicit=*/true,
466 accSupport.getVariableName(var));
467 }
468
469 Operation *op = nullptr;
470 op = getOriginalDataClauseOpForAlias(var, builder, computeConstructOp,
471 dominatingDataClauses);
472 if (op) {
473 if (isa<acc::NoCreateOp>(op))
474 return acc::NoCreateOp::create(builder, loc, var,
475 /*structured=*/true, /*implicit=*/true,
476 accSupport.getVariableName(var),
477 acc::getBounds(op));
478
479 if (isa<acc::DevicePtrOp>(op))
480 return acc::DevicePtrOp::create(builder, loc, var,
481 /*structured=*/true, /*implicit=*/true,
482 accSupport.getVariableName(var),
483 acc::getBounds(op));
484
485 // The original data clause op is a PresentOp, CopyinOp, or CreateOp,
486 // hence guaranteed to be present.
487 return acc::PresentOp::create(builder, loc, var,
488 /*structured=*/true, /*implicit=*/true,
489 accSupport.getVariableName(var),
490 acc::getBounds(op));
491 }
492
493 if (isScalar) {
494 if (enableImplicitReductionCopy &&
496 computeConstructOp->getRegion(0))) {
497 auto copyinOp =
498 acc::CopyinOp::create(builder, loc, var,
499 /*structured=*/true, /*implicit=*/true,
500 accSupport.getVariableName(var));
501 copyinOp.setDataClause(acc::DataClause::acc_reduction);
502 return copyinOp.getOperation();
503 }
504 if constexpr (std::is_same_v<OpT, acc::KernelsOp> ||
505 std::is_same_v<OpT, acc::KernelEnvironmentOp>) {
506 // Scalars are implicit copyin in kernels construct.
507 // We also do the same for acc.kernel_environment because semantics
508 // of user variable mappings should be applied while ACC construct exists
509 // and at this point we should only be dealing with unmapped variables
510 // that were made live-in by the compiler.
511 // TODO: This may be revisited.
512 auto copyinOp =
513 acc::CopyinOp::create(builder, loc, var,
514 /*structured=*/true, /*implicit=*/true,
515 accSupport.getVariableName(var));
516 copyinOp.setDataClause(acc::DataClause::acc_copy);
517 return copyinOp.getOperation();
518 } else {
519 // Scalars are implicit firstprivate in parallel and serial construct.
520 return acc::FirstprivateOp::create(builder, loc, var,
521 /*structured=*/true, /*implicit=*/true,
522 accSupport.getVariableName(var));
523 }
524 } else if (isAnyAggregate) {
525 Operation *newDataOp = nullptr;
526
527 // When default(present) is true, the implicit behavior is present.
528 if (defaultClause.has_value() &&
529 defaultClause.value() == acc::ClauseDefaultValue::Present) {
530 newDataOp = acc::PresentOp::create(builder, loc, var,
531 /*structured=*/true, /*implicit=*/true,
532 accSupport.getVariableName(var));
534 builder.getUnitAttr());
535 } else {
536 auto copyinOp =
537 acc::CopyinOp::create(builder, loc, var,
538 /*structured=*/true, /*implicit=*/true,
539 accSupport.getVariableName(var));
540 copyinOp.setDataClause(acc::DataClause::acc_copy);
541 newDataOp = copyinOp.getOperation();
542 }
543
544 return newDataOp;
545 } else {
546 // This is not a fatal error - for example when the element type is
547 // pointer type (aka we have a pointer of pointer), it is potentially a
548 // deep copy scenario which is not being handled here.
549 // Other types need to be canonicalized. Thus just log unhandled cases.
550 LLVM_DEBUG(llvm::dbgs()
551 << "Unhandled case for implicit data mapping " << var << "\n");
552 }
553 return nullptr;
554}
555
556// Ensures that result values from the acc data clause ops are used inside the
557// acc region. ie:
558// acc.kernels {
559// use %val
560// }
561// =>
562// %dev = acc.dataop %val
563// acc.kernels {
564// use %dev
565// }
566static void legalizeValuesInRegion(Region &accRegion,
567 SmallVector<Value> &newPrivateOperands,
568 SmallVector<Value> &newDataClauseOperands) {
569 for (Value dataClause :
570 llvm::concat<Value>(newDataClauseOperands, newPrivateOperands)) {
571 Value var = acc::getVar(dataClause.getDefiningOp());
572 replaceAllUsesInRegionWith(var, dataClause, accRegion);
573 }
574}
575
576// Adds the private operands to the compute construct operation.
577template <typename OpT>
578static void addNewPrivateOperands(OpT &accOp,
579 const SmallVector<Value> &privateOperands) {
580 if (privateOperands.empty())
581 return;
582
583 for (auto priv : privateOperands) {
584 if (isa<acc::PrivateOp>(priv.getDefiningOp())) {
585 accOp.getPrivateOperandsMutable().append(priv);
586 } else if (isa<acc::FirstprivateOp>(priv.getDefiningOp())) {
587 accOp.getFirstprivateOperandsMutable().append(priv);
588 } else {
589 llvm_unreachable("unhandled reduction operand");
590 }
591 }
592}
593
594static Operation *findDataExitOp(Operation *dataEntryOp) {
595 auto res = acc::getAccVar(dataEntryOp);
596 for (auto *user : res.getUsers())
597 if (isa<ACC_DATA_EXIT_OPS>(user))
598 return user;
599 return nullptr;
600}
601
602// Generates matching data exit operation as described in the acc dialect
603// for how data clauses are decomposed:
604// https://mlir.llvm.org/docs/Dialects/OpenACCDialect/#operation-categories
605// Key ones used here:
606// * acc {construct} copy -> acc.copyin (before region) + acc.copyout (after
607// region)
608// * acc {construct} present -> acc.present (before region) + acc.delete
609// (after region)
610static void
611generateDataExitOperations(OpBuilder &builder, Operation *accOp,
612 const SmallVector<Value> &newDataClauseOperands,
613 const SmallVector<Value> &sortedDataClauseOperands) {
614 builder.setInsertionPointAfter(accOp);
615 Value lastDataClause = nullptr;
616 for (auto dataEntry : llvm::reverse(sortedDataClauseOperands)) {
617 if (llvm::find(newDataClauseOperands, dataEntry) ==
618 newDataClauseOperands.end()) {
619 // If this is not a new data clause operand, we should not generate an
620 // exit operation for it.
621 lastDataClause = dataEntry;
622 continue;
623 }
624 if (lastDataClause)
625 if (auto *dataExitOp = findDataExitOp(lastDataClause.getDefiningOp()))
626 builder.setInsertionPointAfter(dataExitOp);
627 Operation *dataEntryOp = dataEntry.getDefiningOp();
628 if (isa<acc::CopyinOp>(dataEntryOp)) {
629 auto copyoutOp = acc::CopyoutOp::create(
630 builder, dataEntryOp->getLoc(), dataEntry, acc::getVar(dataEntryOp),
631 /*structured=*/true, /*implicit=*/true,
632 acc::getVarName(dataEntryOp).value(), acc::getBounds(dataEntryOp));
633 copyoutOp.setDataClause(acc::DataClause::acc_copy);
634 } else if (isa<acc::PresentOp, acc::NoCreateOp>(dataEntryOp)) {
635 auto deleteOp = acc::DeleteOp::create(
636 builder, dataEntryOp->getLoc(), dataEntry,
637 /*structured=*/true, /*implicit=*/true,
638 acc::getVarName(dataEntryOp).value(), acc::getBounds(dataEntryOp));
639 deleteOp.setDataClause(acc::getDataClause(dataEntryOp).value());
640 } else if (isa<acc::DevicePtrOp>(dataEntryOp)) {
641 // Do nothing.
642 } else {
643 llvm_unreachable("unhandled data exit");
644 }
645 lastDataClause = dataEntry;
646 }
647}
648
649/// Returns all base references of a value in order.
650/// So for example, if we have a reference to a struct field like
651/// s.f1.f2.f3, this will return <s, s.f1, s.f1.f2, s.f1.f2.f3>.
652/// Any intermediate casts/view-like operations are included in the
653/// chain as well.
654static SmallVector<Value> getBaseRefsChain(Value val) {
655 SmallVector<Value> baseRefs;
656 baseRefs.push_back(val);
657 while (true) {
658 Value prevVal = val;
659
660 val = acc::getBaseEntity(val);
661 if (val != baseRefs.front())
662 baseRefs.insert(baseRefs.begin(), val);
663
664 // If this is a view-like operation, it is effectively another
665 // view of the same entity so we should add it to the chain also.
666 if (auto viewLikeOp = val.getDefiningOp<ViewLikeOpInterface>()) {
667 val = viewLikeOp.getViewSource();
668 baseRefs.insert(baseRefs.begin(), val);
669 }
670
671 // Continue loop if we made any progress
672 if (val == prevVal)
673 break;
674 }
675
676 return baseRefs;
677}
678
679static void insertInSortedOrder(SmallVector<Value> &sortedDataClauseOperands,
680 Operation *newClause) {
681 auto *insertPos =
682 std::find_if(sortedDataClauseOperands.begin(),
683 sortedDataClauseOperands.end(), [&](Value dataClauseVal) {
684 // Get the base refs for the current clause we are looking
685 // at.
686 auto var = acc::getVar(dataClauseVal.getDefiningOp());
687 auto baseRefs = getBaseRefsChain(var);
688
689 // If the newClause is of a base ref of an existing clause,
690 // we should insert it right before the current clause.
691 // Thus return true to stop iteration when this is the
692 // case.
693 return std::find(baseRefs.begin(), baseRefs.end(),
694 acc::getVar(newClause)) != baseRefs.end();
695 });
696
697 if (insertPos != sortedDataClauseOperands.end()) {
698 newClause->moveBefore(insertPos->getDefiningOp());
699 sortedDataClauseOperands.insert(insertPos, acc::getAccVar(newClause));
700 } else {
701 sortedDataClauseOperands.push_back(acc::getAccVar(newClause));
702 }
703}
704
705template <typename OpT>
706void ACCImplicitData::generateImplicitDataOps(
707 ModuleOp &module, OpT computeConstructOp,
708 std::optional<acc::ClauseDefaultValue> &defaultClause,
709 acc::OpenACCSupport &accSupport) {
710 // Implicit data attributes are only applied if "[t]here is no default(none)
711 // clause visible at the compute construct."
712 if (defaultClause.has_value() &&
713 defaultClause.value() == acc::ClauseDefaultValue::None)
714 return;
715 assert(!defaultClause.has_value() ||
716 defaultClause.value() == acc::ClauseDefaultValue::Present);
717
718 // 1) Collect live-in values.
719 Region &accRegion = computeConstructOp->getRegion(0);
720 SetVector<Value> liveInValues;
721 getUsedValuesDefinedAbove(accRegion, liveInValues);
722
723 // 2) Run the filtering to find relevant pointers that need copied.
724 auto isCandidate{[&](Value val) -> bool {
725 return isCandidateForImplicitData(val, accRegion, accSupport);
726 }};
727 auto candidateVars(llvm::filter_to_vector(liveInValues, isCandidate));
728 if (candidateVars.empty())
729 return;
730
731 // 3) Generate data clauses for the variables.
732 SmallVector<Value> newPrivateOperands;
733 SmallVector<Value> newDataClauseOperands;
734 OpBuilder builder(computeConstructOp);
735 if (!candidateVars.empty()) {
736 LLVM_DEBUG(llvm::dbgs() << "== Generating clauses for ==\n"
737 << computeConstructOp << "\n");
738 }
739 auto &domInfo = this->getAnalysis<DominanceInfo>();
740 auto &postDomInfo = this->getAnalysis<PostDominanceInfo>();
741 auto dominatingDataClauses =
742 acc::getDominatingDataClauses(computeConstructOp, domInfo, postDomInfo);
743 for (auto var : candidateVars) {
744 auto newDataClauseOp = generateDataClauseOpForCandidate(
745 var, module, builder, computeConstructOp, dominatingDataClauses,
746 defaultClause);
747 fillInBoundsForUnknownDimensions(newDataClauseOp, builder);
748 LLVM_DEBUG(llvm::dbgs() << "Generated data clause for " << var << ":\n"
749 << "\t" << *newDataClauseOp << "\n");
750 if (isa_and_nonnull<acc::PrivateOp, acc::FirstprivateOp, acc::ReductionOp>(
751 newDataClauseOp)) {
752 newPrivateOperands.push_back(acc::getAccVar(newDataClauseOp));
753 } else if (isa_and_nonnull<ACC_DATA_CLAUSE_OPS>(newDataClauseOp)) {
754 newDataClauseOperands.push_back(acc::getAccVar(newDataClauseOp));
755 dominatingDataClauses.push_back(acc::getAccVar(newDataClauseOp));
756 }
757 }
758
759 // 4) Legalize values in region (aka the uses in the region are the result
760 // of the data clause ops)
761 legalizeValuesInRegion(accRegion, newPrivateOperands, newDataClauseOperands);
762
763 // 5) Generate private recipes which are required for properly attaching
764 // private operands.
765 if constexpr (!std::is_same_v<OpT, acc::KernelsOp> &&
766 !std::is_same_v<OpT, acc::KernelEnvironmentOp>)
767 generateRecipes(module, builder, computeConstructOp, newPrivateOperands);
768
769 // 6) Figure out insertion order for the new data clause operands.
770 SmallVector<Value> sortedDataClauseOperands(
771 computeConstructOp.getDataClauseOperands());
772 for (auto newClause : newDataClauseOperands)
773 insertInSortedOrder(sortedDataClauseOperands, newClause.getDefiningOp());
774
775 // 7) Generate the data exit operations.
776 generateDataExitOperations(builder, computeConstructOp, newDataClauseOperands,
777 sortedDataClauseOperands);
778 // 8) Add all of the new operands to the compute construct op.
779 if constexpr (!std::is_same_v<OpT, acc::KernelsOp> &&
780 !std::is_same_v<OpT, acc::KernelEnvironmentOp>)
781 addNewPrivateOperands(computeConstructOp, newPrivateOperands);
782 computeConstructOp.getDataClauseOperandsMutable().assign(
783 sortedDataClauseOperands);
784}
785
786void ACCImplicitData::runOnOperation() {
787 ModuleOp module = this->getOperation();
788
789 acc::OpenACCSupport &accSupport = getAnalysis<acc::OpenACCSupport>();
790
791 module.walk([&](Operation *op) {
792 if (isa<ACC_COMPUTE_CONSTRUCT_OPS, acc::KernelEnvironmentOp>(op)) {
793 assert(op->getNumRegions() == 1 && "must have 1 region");
794
795 auto defaultClause = acc::getDefaultAttr(op);
797 .Case<ACC_COMPUTE_CONSTRUCT_OPS, acc::KernelEnvironmentOp>(
798 [&](auto op) {
799 generateImplicitDataOps(module, op, defaultClause, accSupport);
800 })
801 .Default([&](Operation *) {});
802 }
803 });
804}
805
806} // namespace
UnitAttr getUnitAttr()
Definition Builders.cpp:102
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
RAII guard to reset the insertion point of the builder when destroyed.
Definition Builders.h:350
This class helps build Operations.
Definition Builders.h:209
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition Builders.h:433
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:400
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition Builders.h:414
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:241
void setAttr(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
Definition Operation.h:608
void moveBefore(Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
Location getLoc() const
Return the location of this value.
Definition Value.cpp:24
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition Value.cpp:18
InFlightDiagnostic emitNYI(Location loc, const Twine &message)
Report a case that is not yet supported by the implementation.
bool isValidValueUse(Value v, Region &region)
Check if a value use is legal in an OpenACC region.
std::string getVariableName(Value v)
Get the variable name for a given value.
std::string getRecipeName(RecipeKind kind, Type type, Value var)
Get the recipe name for a given type and value.
#define ACC_COMPUTE_CONSTRUCT_OPS
Definition OpenACC.h:62
#define ACC_DATA_ENTRY_OPS
Definition OpenACC.h:48
#define ACC_DATA_EXIT_OPS
Definition OpenACC.h:58
static constexpr StringLiteral getFromDefaultClauseAttrName()
Definition OpenACC.h:204
mlir::Value getAccVar(mlir::Operation *accDataClauseOp)
Used to obtain the accVar from a data clause operation.
Definition OpenACC.cpp:5174
mlir::Value getVar(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation.
Definition OpenACC.cpp:5143
std::optional< mlir::acc::DataClause > getDataClause(mlir::Operation *accDataEntryOp)
Used to obtain the dataClause from a data entry operation.
Definition OpenACC.cpp:5247
bool isPointerLikeType(mlir::Type type)
Used to check whether the provided type implements the PointerLikeType interface.
Definition OpenACC.h:165
mlir::SmallVector< mlir::Value > getBounds(mlir::Operation *accDataClauseOp)
Used to obtain bounds from an acc data clause operation.
Definition OpenACC.cpp:5192
std::optional< ClauseDefaultValue > getDefaultAttr(mlir::Operation *op)
Looks for an OpenACC default attribute on the current operation op or in a parent operation which enc...
bool isOnlyUsedByReductionClauses(mlir::Value val, mlir::Region &region)
Returns true if this value is only used by acc.reduction operations in the region.
std::optional< llvm::StringRef > getVarName(mlir::Operation *accOp)
Used to obtain the name from an acc operation.
Definition OpenACC.cpp:5236
llvm::SmallVector< mlir::Value > getDominatingDataClauses(mlir::Operation *computeConstructOp, mlir::DominanceInfo &domInfo, mlir::PostDominanceInfo &postDomInfo)
Collects all data clauses that dominate the compute construct.
bool isMappableType(mlir::Type type)
Used to check whether the provided type implements the MappableType interface.
Definition OpenACC.h:171
bool isDeviceValue(mlir::Value val)
Check if a value represents device data.
mlir::Value getBaseEntity(mlir::Value val)
Include the generated interface declarations.
void replaceAllUsesInRegionWith(Value orig, Value replacement, Region &region)
Replace all uses of orig within the given region with replacement.
llvm::SetVector< T, Vector, Set, N > SetVector
Definition LLVM.h:125
std::conditional_t< std::is_same_v< Ty, mlir::Type >, mlir::Value, detail::TypedValue< Ty > > TypedValue
If Ty is mlir::Type this will select Value instead of having a wrapper around it.
Definition Value.h:494
void getUsedValuesDefinedAbove(Region &region, Region &limit, SetVector< Value > &values)
Fill values with a list of values defined at the ancestors of the limit region and used within region...
llvm::TypeSwitch< T, ResultT > TypeSwitch
Definition LLVM.h:139