Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "flang/Semantics/scope.h"
#include "flang/Semantics/tools.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/OpenACC/OpenACCUtils.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Support/LLVM.h"
Expand Down
6 changes: 0 additions & 6 deletions mlir/include/mlir/Dialect/OpenACC/OpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,6 @@ mlir::ValueRange getDataOperands(mlir::Operation *accOp);
/// Used to get a mutable range iterating over the data operands.
mlir::MutableOperandRange getMutableDataOperands(mlir::Operation *accOp);

/// Used to obtain the enclosing compute construct operation that contains
/// the provided `region`. Returns nullptr if no compute construct operation
/// is found. The returns operation is one of types defined by
///`ACC_COMPUTE_CONSTRUCT_OPS`.
mlir::Operation *getEnclosingComputeOp(mlir::Region &region);

/// Used to check whether the provided `type` implements the `PointerLikeType`
/// interface.
inline bool isPointerLikeType(mlir::Type type) {
Expand Down
44 changes: 44 additions & 0 deletions mlir/include/mlir/Dialect/OpenACC/OpenACCUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===- OpenACCUtils.h - OpenACC Utilities -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_DIALECT_OPENACC_OPENACCUTILS_H_
#define MLIR_DIALECT_OPENACC_OPENACCUTILS_H_

#include "mlir/Dialect/OpenACC/OpenACC.h"

namespace mlir {
namespace acc {

/// Used to obtain the enclosing compute construct operation that contains
/// the provided `region`. Returns nullptr if no compute construct operation
/// is found. The returned operation is one of types defined by
/// `ACC_COMPUTE_CONSTRUCT_OPS`.
mlir::Operation *getEnclosingComputeOp(mlir::Region &region);

/// Returns true if this value is only used by `acc.private` operations in the
/// `region`.
bool isOnlyUsedByPrivateClauses(mlir::Value val, mlir::Region &region);

/// Returns true if this value is only used by `acc.reduction` operations in
/// the `region`.
bool isOnlyUsedByReductionClauses(mlir::Value val, mlir::Region &region);

/// Looks for an OpenACC default attribute on the current operation `op` or in
/// a parent operation which encloses `op`. This is useful because OpenACC
/// specification notes that a visible default clause is the nearest default
/// clause appearing on the compute construct or a lexically containing data
/// construct.
std::optional<ClauseDefaultValue> getDefaultAttr(mlir::Operation *op);

/// Get the type category of an OpenACC variable.
mlir::acc::VariableTypeCategory getTypeCategory(mlir::Value var);

} // namespace acc
} // namespace mlir

#endif // MLIR_DIALECT_OPENACC_OPENACCUTILS_H_
1 change: 1 addition & 0 deletions mlir/lib/Dialect/OpenACC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(IR)
add_subdirectory(Utils)
add_subdirectory(Transforms)
11 changes: 0 additions & 11 deletions mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4649,14 +4649,3 @@ mlir::acc::getMutableDataOperands(mlir::Operation *accOp) {
.Default([&](mlir::Operation *) { return nullptr; })};
return dataOperands;
}

mlir::Operation *mlir::acc::getEnclosingComputeOp(mlir::Region &region) {
mlir::Operation *parentOp = region.getParentOp();
while (parentOp) {
if (mlir::isa<ACC_COMPUTE_CONSTRUCT_OPS>(parentOp)) {
return parentOp;
}
parentOp = parentOp->getParentOp();
}
return nullptr;
}
20 changes: 20 additions & 0 deletions mlir/lib/Dialect/OpenACC/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
add_mlir_dialect_library(MLIROpenACCUtils
OpenACCUtils.cpp

ADDITIONAL_HEADER_DIRS
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/OpenACC

DEPENDS
MLIROpenACCPassIncGen
MLIROpenACCOpsIncGen
MLIROpenACCEnumsIncGen
MLIROpenACCAttributesIncGen
MLIROpenACCMPOpsInterfacesIncGen
MLIROpenACCOpsInterfacesIncGen
MLIROpenACCTypeInterfacesIncGen

LINK_LIBS PUBLIC
MLIROpenACCDialect
MLIRIR
MLIRSupport
)
80 changes: 80 additions & 0 deletions mlir/lib/Dialect/OpenACC/Utils/OpenACCUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//===- OpenACCUtils.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/OpenACC/OpenACCUtils.h"

#include "mlir/Dialect/OpenACC/OpenACC.h"
#include "llvm/ADT/TypeSwitch.h"

mlir::Operation *mlir::acc::getEnclosingComputeOp(mlir::Region &region) {
mlir::Operation *parentOp = region.getParentOp();
while (parentOp) {
if (mlir::isa<ACC_COMPUTE_CONSTRUCT_OPS>(parentOp))
return parentOp;
parentOp = parentOp->getParentOp();
}
return nullptr;
}

template <typename OpTy>
static bool isOnlyUsedByOpClauses(mlir::Value val, mlir::Region &region) {
auto checkIfUsedOnlyByOpInside = [&](mlir::Operation *user) {
// For any users which are not in the current acc region, we can ignore.
// Return true so that it can be used in a `all_of` check.
if (!region.isAncestor(user->getParentRegion()))
return true;
return mlir::isa<OpTy>(user);
};

return llvm::all_of(val.getUsers(), checkIfUsedOnlyByOpInside);
}

bool mlir::acc::isOnlyUsedByPrivateClauses(mlir::Value val,
mlir::Region &region) {
return isOnlyUsedByOpClauses<mlir::acc::PrivateOp>(val, region);
}

bool mlir::acc::isOnlyUsedByReductionClauses(mlir::Value val,
mlir::Region &region) {
return isOnlyUsedByOpClauses<mlir::acc::ReductionOp>(val, region);
}

std::optional<mlir::acc::ClauseDefaultValue>
mlir::acc::getDefaultAttr(Operation *op) {
std::optional<mlir::acc::ClauseDefaultValue> defaultAttr;
Operation *currOp = op;

// Iterate outwards until a default clause is found (since OpenACC
// specification notes that a visible default clause is the nearest default
// clause appearing on the compute construct or a lexically containing data
// construct.
while (!defaultAttr.has_value() && currOp) {
defaultAttr =
llvm::TypeSwitch<mlir::Operation *,
std::optional<mlir::acc::ClauseDefaultValue>>(currOp)
.Case<ACC_COMPUTE_CONSTRUCT_OPS, mlir::acc::DataOp>(
[&](auto op) { return op.getDefaultAttr(); })
.Default([&](Operation *) { return std::nullopt; });
currOp = currOp->getParentOp();
}

return defaultAttr;
}

mlir::acc::VariableTypeCategory mlir::acc::getTypeCategory(mlir::Value var) {
mlir::acc::VariableTypeCategory typeCategory =
mlir::acc::VariableTypeCategory::uncategorized;
if (auto mappableTy = dyn_cast<mlir::acc::MappableType>(var.getType()))
typeCategory = mappableTy.getTypeCategory(var);
else if (auto pointerLikeTy =
dyn_cast<mlir::acc::PointerLikeType>(var.getType()))
typeCategory = pointerLikeTy.getPointeeTypeCategory(
cast<TypedValue<mlir::acc::PointerLikeType>>(var),
pointerLikeTy.getElementType());
return typeCategory;
}
5 changes: 5 additions & 0 deletions mlir/unittests/Dialect/OpenACC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
add_mlir_unittest(MLIROpenACCTests
OpenACCOpsTest.cpp
OpenACCUtilsTest.cpp
)
mlir_target_link_libraries(MLIROpenACCTests
PRIVATE
MLIRIR
MLIRFuncDialect
MLIRMemRefDialect
MLIRArithDialect
MLIROpenACCDialect
MLIROpenACCUtils
)
Loading