|
| 1 | +//===- OpenACCUtils.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 | +#include "mlir/Dialect/OpenACC/OpenACCUtils.h" |
| 10 | + |
| 11 | +#include "mlir/Dialect/OpenACC/OpenACC.h" |
| 12 | +#include "llvm/ADT/TypeSwitch.h" |
| 13 | + |
| 14 | +mlir::Operation *mlir::acc::getEnclosingComputeOp(mlir::Region ®ion) { |
| 15 | + mlir::Operation *parentOp = region.getParentOp(); |
| 16 | + while (parentOp) { |
| 17 | + if (mlir::isa<ACC_COMPUTE_CONSTRUCT_OPS>(parentOp)) |
| 18 | + return parentOp; |
| 19 | + parentOp = parentOp->getParentOp(); |
| 20 | + } |
| 21 | + return nullptr; |
| 22 | +} |
| 23 | + |
| 24 | +template <typename OpTy> |
| 25 | +static bool isOnlyUsedByOpClauses(mlir::Value val, mlir::Region ®ion) { |
| 26 | + auto checkIfUsedOnlyByOpInside = [&](mlir::Operation *user) { |
| 27 | + // For any users which are not in the current acc region, we can ignore. |
| 28 | + // Return true so that it can be used in a `all_of` check. |
| 29 | + if (!region.isAncestor(user->getParentRegion())) |
| 30 | + return true; |
| 31 | + return mlir::isa<OpTy>(user); |
| 32 | + }; |
| 33 | + |
| 34 | + return llvm::all_of(val.getUsers(), checkIfUsedOnlyByOpInside); |
| 35 | +} |
| 36 | + |
| 37 | +bool mlir::acc::isOnlyUsedByPrivateClauses(mlir::Value val, |
| 38 | + mlir::Region ®ion) { |
| 39 | + return isOnlyUsedByOpClauses<mlir::acc::PrivateOp>(val, region); |
| 40 | +} |
| 41 | + |
| 42 | +bool mlir::acc::isOnlyUsedByReductionClauses(mlir::Value val, |
| 43 | + mlir::Region ®ion) { |
| 44 | + return isOnlyUsedByOpClauses<mlir::acc::ReductionOp>(val, region); |
| 45 | +} |
| 46 | + |
| 47 | +std::optional<mlir::acc::ClauseDefaultValue> |
| 48 | +mlir::acc::getDefaultAttr(Operation *op) { |
| 49 | + std::optional<mlir::acc::ClauseDefaultValue> defaultAttr; |
| 50 | + Operation *currOp = op; |
| 51 | + |
| 52 | + // Iterate outwards until a default clause is found (since OpenACC |
| 53 | + // specification notes that a visible default clause is the nearest default |
| 54 | + // clause appearing on the compute construct or a lexically containing data |
| 55 | + // construct. |
| 56 | + while (!defaultAttr.has_value() && currOp) { |
| 57 | + defaultAttr = |
| 58 | + llvm::TypeSwitch<mlir::Operation *, |
| 59 | + std::optional<mlir::acc::ClauseDefaultValue>>(currOp) |
| 60 | + .Case<ACC_COMPUTE_CONSTRUCT_OPS, mlir::acc::DataOp>( |
| 61 | + [&](auto op) { return op.getDefaultAttr(); }) |
| 62 | + .Default([&](Operation *) { return std::nullopt; }); |
| 63 | + currOp = currOp->getParentOp(); |
| 64 | + } |
| 65 | + |
| 66 | + return defaultAttr; |
| 67 | +} |
| 68 | + |
| 69 | +mlir::acc::VariableTypeCategory mlir::acc::getTypeCategory(mlir::Value var) { |
| 70 | + mlir::acc::VariableTypeCategory typeCategory = |
| 71 | + mlir::acc::VariableTypeCategory::uncategorized; |
| 72 | + if (auto mappableTy = dyn_cast<mlir::acc::MappableType>(var.getType())) |
| 73 | + typeCategory = mappableTy.getTypeCategory(var); |
| 74 | + else if (auto pointerLikeTy = |
| 75 | + dyn_cast<mlir::acc::PointerLikeType>(var.getType())) |
| 76 | + typeCategory = pointerLikeTy.getPointeeTypeCategory( |
| 77 | + cast<TypedValue<mlir::acc::PointerLikeType>>(var), |
| 78 | + pointerLikeTy.getElementType()); |
| 79 | + return typeCategory; |
| 80 | +} |
0 commit comments