Skip to content

[mlir][acc] Refactor and expand OpenACC utils #115119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 0 additions & 53 deletions mlir/include/mlir/Dialect/OpenACC/OpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,59 +83,6 @@ namespace acc {
/// combined and the final mapping value would be 5 (4 | 1).
enum OpenACCExecMapping { NONE = 0, VECTOR = 1, WORKER = 2, GANG = 4 };

/// Used to obtain the `varPtr` from a data clause operation.
/// Returns empty value if not a data clause operation or is a data exit
/// operation with no `varPtr`.
mlir::Value getVarPtr(mlir::Operation *accDataClauseOp);

/// Used to obtain the `accPtr` from a data clause operation.
/// When a data entry operation, it obtains its result `accPtr` value.
/// If a data exit operation, it obtains its operand `accPtr` value.
/// Returns empty value if not a data clause operation.
mlir::Value getAccPtr(mlir::Operation *accDataClauseOp);

/// Used to obtain the `varPtrPtr` from a data clause operation.
/// Returns empty value if not a data clause operation.
mlir::Value getVarPtrPtr(mlir::Operation *accDataClauseOp);

/// Used to obtain `bounds` from an acc data clause operation.
/// Returns an empty vector if there are no bounds.
mlir::SmallVector<mlir::Value> getBounds(mlir::Operation *accDataClauseOp);

/// Used to obtain `async` operands from an acc data clause operation.
/// Returns an empty vector if there are no such operands.
mlir::SmallVector<mlir::Value>
getAsyncOperands(mlir::Operation *accDataClauseOp);

/// Returns an array of acc:DeviceTypeAttr attributes attached to
/// an acc data clause operation, that correspond to the device types
/// associated with the async clauses with an async-value.
mlir::ArrayAttr getAsyncOperandsDeviceType(mlir::Operation *accDataClauseOp);

/// Returns an array of acc:DeviceTypeAttr attributes attached to
/// an acc data clause operation, that correspond to the device types
/// associated with the async clauses without an async-value.
mlir::ArrayAttr getAsyncOnly(mlir::Operation *accDataClauseOp);

/// Used to obtain the `name` from an acc operation.
std::optional<llvm::StringRef> getVarName(mlir::Operation *accOp);

/// Used to obtain the `dataClause` from a data entry operation.
/// Returns empty optional if not a data entry operation.
std::optional<mlir::acc::DataClause>
getDataClause(mlir::Operation *accDataEntryOp);

/// Used to find out whether data operation is implicit.
/// Returns false if not a data operation or if it is a data operation without
/// implicit flag.
bool getImplicitFlag(mlir::Operation *accDataEntryOp);

/// Used to get an immutable range iterating over the data operands.
mlir::ValueRange getDataOperands(mlir::Operation *accOp);

/// Used to get a mutable range iterating over the data operands.
mlir::MutableOperandRange getMutableDataOperands(mlir::Operation *accOp);

Comment on lines -86 to -138
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nicer to refactor this into proper interfaces for these operations instead of just creating a new lib.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this. There are a couple of reasons I decided to do this MR this way:

  • The existing utilities work with opaque Operation. So no need to have any explicit casts in source code.
  • The interfaces, as I am imagining, are being "DataEntryOpInterface" and "DataExitOpInterface". So from the user side, it is still more steps than just calling one of these utilities. Some operations don't have varPtr, some cannot redefine accPtr, and other inconsistencies still require more logic from use side.
  • This MR makes the improvement so that we do have a place for new utilities for acc in a way that matches MLIR conventions. I did not want to add new utilities in the dialect itself.

I see this MR as orthogonal to your recommendation. The utilities can use the interfaces that we define.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MR -> PR

I'm not sure I see the motivation to move this away from the ops. Now you need to duplicate the code in the dialect part and have an extra library to link with. These utility functions are pretty much tied to the operations or do I miss something? Ultimately I'm not against it but it feels odd to duplicate some code. Maybe these could be inlined function defined in header?

/// Used to obtain the attribute name for declare.
static constexpr StringLiteral getDeclareAttrName() {
return StringLiteral("acc.declare");
Expand Down
116 changes: 116 additions & 0 deletions mlir/include/mlir/Dialect/OpenACC/Utils/OpenACCUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//===- 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_UTILS_OPENACCUTILS_H_
#define MLIR_DIALECT_OPENACC_UTILS_OPENACCUTILS_H_

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

namespace mlir {
namespace acc {
/// Used to obtain the `varPtr` from a data clause operation.
/// Returns empty value if not a data clause operation or is a data exit
/// operation with no `varPtr`.
mlir::Value getVarPtr(mlir::Operation *accDataClauseOp);

/// Used to set the `varPtr` of a data clause operation.
/// Returns true if it was set successfully and false if this is not a data
/// clause operation.
bool setVarPtr(mlir::Operation *accDataClauseOp, mlir::Value varPtr);

/// Used to obtain the `accPtr` from a data clause operation.
/// When a data entry operation, it obtains its result `accPtr` value.
/// If a data exit operation, it obtains its operand `accPtr` value.
/// Returns empty value if not a data clause operation.
mlir::Value getAccPtr(mlir::Operation *accDataClauseOp);

/// Used to set the `accPtr` for a data exit operation.
/// Returns true if it was set successfully and false if is not a data exit
/// operation (data entry operations have their result as `accPtr` which
/// cannot be changed).
bool setAccPtr(mlir::Operation *accDataClauseOp, mlir::Value accPtr);

/// Used to obtain the `varPtrPtr` from a data clause operation.
/// Returns empty value if not a data clause operation.
mlir::Value getVarPtrPtr(mlir::Operation *accDataClauseOp);

/// Used to set the `varPtrPtr` for a data clause operation.
/// Returns false if the operation does not have varPtrPtr or is not a data
/// clause op.
bool setVarPtrPtr(mlir::Operation *accDataClauseOp, mlir::Value varPtrPtr);

/// Used to obtain `bounds` from an acc data clause operation.
/// Returns an empty vector if there are no bounds.
mlir::SmallVector<mlir::Value> getBounds(mlir::Operation *accDataClauseOp);

/// Used to set `bounds` for an acc data clause operation. It completely
/// replaces all bounds operands with the new list.
/// Returns false if new bounds were not set (such as when argument is not
/// an acc data clause operation).
bool setBounds(mlir::Operation *accDataClauseOp,
mlir::SmallVector<mlir::Value> &bounds);
bool setBounds(mlir::Operation *accDataClauseOp, mlir::Value bound);

/// Used to obtain the `dataClause` from a data clause operation.
/// Returns empty optional if not a data operation.
std::optional<mlir::acc::DataClause>
getDataClause(mlir::Operation *accDataClauseOp);

/// Used to set the `dataClause` on a data clause operation.
/// Returns true if successfully set and false otherwise.
bool setDataClause(mlir::Operation *accDataClauseOp,
mlir::acc::DataClause dataClause);

/// Used to find out whether this data operation uses structured runtime
/// counters. Returns false if not a data operation or if it is a data operation
/// without the structured flag set.
bool getStructuredFlag(mlir::Operation *accDataClauseOp);

/// Used to update the data clause operation whether it represents structured
/// or dynamic (value of `structured` is passed as false).
/// Returns true if successfully set and false otherwise.
bool setStructuredFlag(mlir::Operation *accDataClauseOp, bool structured);

/// Used to find out whether data operation is implicit.
/// Returns false if not a data operation or if it is a data operation without
/// implicit flag.
bool getImplicitFlag(mlir::Operation *accDataClauseOp);

/// Used to update the data clause operation whether this operation is
/// implicit or explicit (`implicit` set as false).
/// Returns true if successfully set and false otherwise.
bool setImplicitFlag(mlir::Operation *accDataClauseOp, bool implicit);

/// Used to obtain the `name` from an acc operation.
std::optional<llvm::StringRef> getVarName(mlir::Operation *accDataClauseOp);

/// Used to obtain `async` operands from an acc data clause operation.
/// Returns an empty vector if there are no such operands.
mlir::SmallVector<mlir::Value>
getAsyncOperands(mlir::Operation *accDataClauseOp);

/// Returns an array of acc:DeviceTypeAttr attributes attached to
/// an acc data clause operation, that correspond to the device types
/// associated with the async clauses with an async-value.
mlir::ArrayAttr getAsyncOperandsDeviceType(mlir::Operation *accDataClauseOp);

/// Returns an array of acc:DeviceTypeAttr attributes attached to
/// an acc data clause operation, that correspond to the device types
/// associated with the async clauses without an async-value.
mlir::ArrayAttr getAsyncOnly(mlir::Operation *accDataClauseOp);

/// Used to get an immutable range iterating over the data operands.
mlir::ValueRange getDataOperands(mlir::Operation *accOp);

/// Used to get a mutable range iterating over the data operands.
mlir::MutableOperandRange getMutableDataOperands(mlir::Operation *accOp);

} // namespace acc
} // namespace mlir

#endif // MLIR_DIALECT_OPENACC_UTILS_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(Transforms)
add_subdirectory(Utils)
145 changes: 18 additions & 127 deletions mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2379,11 +2379,21 @@ checkDeclareOperands(Op &op, const mlir::ValueRange &operands,
"expect valid declare data entry operation or acc.getdeviceptr "
"as defining op");

mlir::Value varPtr{getVarPtr(operand.getDefiningOp())};
mlir::Value varPtr{
llvm::TypeSwitch<mlir::Operation *, mlir::Value>(
operand.getDefiningOp())
.Case<ACC_DATA_ENTRY_OPS>(
[&](auto entry) { return entry.getVarPtr(); })
.Default([&](mlir::Operation *) { return mlir::Value(); })};
assert(varPtr && "declare operands can only be data entry operations which "
"must have varPtr");
std::optional<mlir::acc::DataClause> dataClauseOptional{
getDataClause(operand.getDefiningOp())};
llvm::TypeSwitch<mlir::Operation *,
std::optional<mlir::acc::DataClause>>(
operand.getDefiningOp())
.Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>(
[&](auto entry) { return entry.getDataClause(); })
.Default([&](mlir::Operation *) { return std::nullopt; })};
assert(dataClauseOptional.has_value() &&
"declare operands can only be data entry operations which must have "
"dataClause");
Expand All @@ -2409,8 +2419,13 @@ checkDeclareOperands(Op &op, const mlir::ValueRange &operands,
// since implicit data action may be inserted to do actions like updating
// device copy, in which case the variable is not necessarily implicitly
// declare'd.
bool operandOpImplicitFlag{
llvm::TypeSwitch<mlir::Operation *, bool>(operand.getDefiningOp())
.Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>(
[&](auto entry) { return entry.getImplicit(); })
.Default([&](mlir::Operation *) { return false; })};
if (declAttr.getImplicit() &&
declAttr.getImplicit() != acc::getImplicitFlag(operand.getDefiningOp()))
declAttr.getImplicit() != operandOpImplicitFlag)
return op.emitError(
"implicitness must match between declare op and flag on variable");
}
Expand Down Expand Up @@ -2868,127 +2883,3 @@ LogicalResult acc::WaitOp::verify() {

#define GET_TYPEDEF_CLASSES
#include "mlir/Dialect/OpenACC/OpenACCOpsTypes.cpp.inc"

//===----------------------------------------------------------------------===//
// acc dialect utilities
//===----------------------------------------------------------------------===//

mlir::Value mlir::acc::getVarPtr(mlir::Operation *accDataClauseOp) {
auto varPtr{llvm::TypeSwitch<mlir::Operation *, mlir::Value>(accDataClauseOp)
.Case<ACC_DATA_ENTRY_OPS>(
[&](auto entry) { return entry.getVarPtr(); })
.Case<mlir::acc::CopyoutOp, mlir::acc::UpdateHostOp>(
[&](auto exit) { return exit.getVarPtr(); })
.Default([&](mlir::Operation *) { return mlir::Value(); })};
return varPtr;
}

mlir::Value mlir::acc::getAccPtr(mlir::Operation *accDataClauseOp) {
auto accPtr{llvm::TypeSwitch<mlir::Operation *, mlir::Value>(accDataClauseOp)
.Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>(
[&](auto dataClause) { return dataClause.getAccPtr(); })
.Default([&](mlir::Operation *) { return mlir::Value(); })};
return accPtr;
}

mlir::Value mlir::acc::getVarPtrPtr(mlir::Operation *accDataClauseOp) {
auto varPtrPtr{
llvm::TypeSwitch<mlir::Operation *, mlir::Value>(accDataClauseOp)
.Case<ACC_DATA_ENTRY_OPS>(
[&](auto dataClause) { return dataClause.getVarPtrPtr(); })
.Default([&](mlir::Operation *) { return mlir::Value(); })};
return varPtrPtr;
}

mlir::SmallVector<mlir::Value>
mlir::acc::getBounds(mlir::Operation *accDataClauseOp) {
mlir::SmallVector<mlir::Value> bounds{
llvm::TypeSwitch<mlir::Operation *, mlir::SmallVector<mlir::Value>>(
accDataClauseOp)
.Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>([&](auto dataClause) {
return mlir::SmallVector<mlir::Value>(
dataClause.getBounds().begin(), dataClause.getBounds().end());
})
.Default([&](mlir::Operation *) {
return mlir::SmallVector<mlir::Value, 0>();
})};
return bounds;
}

mlir::SmallVector<mlir::Value>
mlir::acc::getAsyncOperands(mlir::Operation *accDataClauseOp) {
return llvm::TypeSwitch<mlir::Operation *, mlir::SmallVector<mlir::Value>>(
accDataClauseOp)
.Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>([&](auto dataClause) {
return mlir::SmallVector<mlir::Value>(
dataClause.getAsyncOperands().begin(),
dataClause.getAsyncOperands().end());
})
.Default([&](mlir::Operation *) {
return mlir::SmallVector<mlir::Value, 0>();
});
}

mlir::ArrayAttr
mlir::acc::getAsyncOperandsDeviceType(mlir::Operation *accDataClauseOp) {
return llvm::TypeSwitch<mlir::Operation *, mlir::ArrayAttr>(accDataClauseOp)
.Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>([&](auto dataClause) {
return dataClause.getAsyncOperandsDeviceTypeAttr();
})
.Default([&](mlir::Operation *) { return mlir::ArrayAttr{}; });
}

mlir::ArrayAttr mlir::acc::getAsyncOnly(mlir::Operation *accDataClauseOp) {
return llvm::TypeSwitch<mlir::Operation *, mlir::ArrayAttr>(accDataClauseOp)
.Case<ACC_DATA_ENTRY_OPS, ACC_DATA_EXIT_OPS>(
[&](auto dataClause) { return dataClause.getAsyncOnlyAttr(); })
.Default([&](mlir::Operation *) { return mlir::ArrayAttr{}; });
}

std::optional<llvm::StringRef> mlir::acc::getVarName(mlir::Operation *accOp) {
auto name{
llvm::TypeSwitch<mlir::Operation *, std::optional<llvm::StringRef>>(accOp)
.Case<ACC_DATA_ENTRY_OPS>([&](auto entry) { return entry.getName(); })
.Default([&](mlir::Operation *) -> std::optional<llvm::StringRef> {
return {};
})};
return name;
}

std::optional<mlir::acc::DataClause>
mlir::acc::getDataClause(mlir::Operation *accDataEntryOp) {
auto dataClause{
llvm::TypeSwitch<mlir::Operation *, std::optional<mlir::acc::DataClause>>(
accDataEntryOp)
.Case<ACC_DATA_ENTRY_OPS>(
[&](auto entry) { return entry.getDataClause(); })
.Default([&](mlir::Operation *) { return std::nullopt; })};
return dataClause;
}

bool mlir::acc::getImplicitFlag(mlir::Operation *accDataEntryOp) {
auto implicit{llvm::TypeSwitch<mlir::Operation *, bool>(accDataEntryOp)
.Case<ACC_DATA_ENTRY_OPS>(
[&](auto entry) { return entry.getImplicit(); })
.Default([&](mlir::Operation *) { return false; })};
return implicit;
}

mlir::ValueRange mlir::acc::getDataOperands(mlir::Operation *accOp) {
auto dataOperands{
llvm::TypeSwitch<mlir::Operation *, mlir::ValueRange>(accOp)
.Case<ACC_COMPUTE_AND_DATA_CONSTRUCT_OPS>(
[&](auto entry) { return entry.getDataClauseOperands(); })
.Default([&](mlir::Operation *) { return mlir::ValueRange(); })};
return dataOperands;
}

mlir::MutableOperandRange
mlir::acc::getMutableDataOperands(mlir::Operation *accOp) {
auto dataOperands{
llvm::TypeSwitch<mlir::Operation *, mlir::MutableOperandRange>(accOp)
.Case<ACC_COMPUTE_AND_DATA_CONSTRUCT_OPS>(
[&](auto entry) { return entry.getDataClauseOperandsMutable(); })
.Default([&](mlir::Operation *) { return nullptr; })};
return dataOperands;
}
1 change: 1 addition & 0 deletions mlir/lib/Dialect/OpenACC/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_mlir_dialect_library(MLIROpenACCTransforms

LINK_LIBS PUBLIC
MLIROpenACCDialect
MLIROpenACCUtils
MLIRFuncDialect
MLIRIR
MLIRPass
Expand Down
5 changes: 2 additions & 3 deletions mlir/lib/Dialect/OpenACC/Transforms/LegalizeDataValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
//
//===----------------------------------------------------------------------===//

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

#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/OpenACC/OpenACC.h"
#include "mlir/Dialect/OpenACC/Transforms/Passes.h"
#include "mlir/Dialect/OpenACC/Utils/OpenACCUtils.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/RegionUtils.h"
#include "llvm/Support/ErrorHandling.h"

namespace mlir {
Expand Down
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
MLIRIR
MLIROpenACCDialect
MLIRSupport
)
Loading
Loading