Skip to content

Commit 3c4dff3

Browse files
authored
[NFC][OpenACC] addDeviceType to init/shutdown ops (#137372)
As a first step of attempting to make for a 'better' interface to lowering to the OpenACC dialect, this patch adds a helper function to InitOp and ShutdownOp to make adding a device-type clause easier.
1 parent 61d78d0 commit 3c4dff3

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

clang/lib/CIR/CodeGen/CIRGenStmtOpenACC.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,11 @@ class OpenACCClauseCIREmitter final
238238
void VisitDeviceTypeClause(const OpenACCDeviceTypeClause &clause) {
239239
lastDeviceTypeClause = &clause;
240240
if constexpr (isOneOfTypes<OpTy, InitOp, ShutdownOp>) {
241-
llvm::SmallVector<mlir::Attribute> deviceTypes;
242-
std::optional<mlir::ArrayAttr> existingDeviceTypes =
243-
operation.getDeviceTypes();
244-
245-
// Ensure we keep the existing ones, and in the correct 'new' order.
246-
if (existingDeviceTypes) {
247-
for (mlir::Attribute attr : *existingDeviceTypes)
248-
deviceTypes.push_back(mlir::acc::DeviceTypeAttr::get(
249-
builder.getContext(),
250-
cast<mlir::acc::DeviceTypeAttr>(attr).getValue()));
251-
}
252-
253-
for (const DeviceTypeArgument &arg : clause.getArchitectures()) {
254-
deviceTypes.push_back(mlir::acc::DeviceTypeAttr::get(
255-
builder.getContext(), decodeDeviceType(arg.getIdentifierInfo())));
256-
}
257-
operation.removeDeviceTypesAttr();
258-
operation.setDeviceTypesAttr(
259-
mlir::ArrayAttr::get(builder.getContext(), deviceTypes));
241+
llvm::for_each(
242+
clause.getArchitectures(), [this](const DeviceTypeArgument &arg) {
243+
operation.addDeviceType(builder.getContext(),
244+
decodeDeviceType(arg.getIdentifierInfo()));
245+
});
260246
} else if constexpr (isOneOfTypes<OpTy, SetOp>) {
261247
assert(!operation.getDeviceTypeAttr() && "already have device-type?");
262248
assert(clause.getArchitectures().size() <= 1);

mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,6 +2614,11 @@ def OpenACC_InitOp : OpenACC_Op<"init", [AttrSizedOperandSegments]> {
26142614
Optional<IntOrIndex>:$deviceNum,
26152615
Optional<I1>:$ifCond);
26162616

2617+
let extraClassDeclaration = [{
2618+
/// Adds a device type to the list of device types for this directive.
2619+
void addDeviceType(MLIRContext *, mlir::acc::DeviceType);
2620+
}];
2621+
26172622
let assemblyFormat = [{
26182623
oilist(`device_num` `(` $deviceNum `:` type($deviceNum) `)`
26192624
| `if` `(` $ifCond `)`
@@ -2645,6 +2650,11 @@ def OpenACC_ShutdownOp : OpenACC_Op<"shutdown", [AttrSizedOperandSegments]> {
26452650
Optional<IntOrIndex>:$deviceNum,
26462651
Optional<I1>:$ifCond);
26472652

2653+
let extraClassDeclaration = [{
2654+
/// Adds a device type to the list of device types for this directive.
2655+
void addDeviceType(MLIRContext *, mlir::acc::DeviceType);
2656+
}];
2657+
26482658
let assemblyFormat = [{
26492659
oilist(`device_num` `(` $deviceNum `:` type($deviceNum) `)`
26502660
|`if` `(` $ifCond `)`

mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,6 +2954,16 @@ LogicalResult acc::InitOp::verify() {
29542954
return success();
29552955
}
29562956

2957+
void acc::InitOp::addDeviceType(MLIRContext *context,
2958+
mlir::acc::DeviceType deviceType) {
2959+
llvm::SmallVector<mlir::Attribute> deviceTypes;
2960+
if (getDeviceTypesAttr())
2961+
llvm::copy(getDeviceTypesAttr(), std::back_inserter(deviceTypes));
2962+
2963+
deviceTypes.push_back(acc::DeviceTypeAttr::get(context, deviceType));
2964+
setDeviceTypesAttr(mlir::ArrayAttr::get(context, deviceTypes));
2965+
}
2966+
29572967
//===----------------------------------------------------------------------===//
29582968
// ShutdownOp
29592969
//===----------------------------------------------------------------------===//
@@ -2966,6 +2976,16 @@ LogicalResult acc::ShutdownOp::verify() {
29662976
return success();
29672977
}
29682978

2979+
void acc::ShutdownOp::addDeviceType(MLIRContext *context,
2980+
mlir::acc::DeviceType deviceType) {
2981+
llvm::SmallVector<mlir::Attribute> deviceTypes;
2982+
if (getDeviceTypesAttr())
2983+
llvm::copy(getDeviceTypesAttr(), std::back_inserter(deviceTypes));
2984+
2985+
deviceTypes.push_back(acc::DeviceTypeAttr::get(context, deviceType));
2986+
setDeviceTypesAttr(mlir::ArrayAttr::get(context, deviceTypes));
2987+
}
2988+
29692989
//===----------------------------------------------------------------------===//
29702990
// SetOp
29712991
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)