Skip to content

Commit 39c443b

Browse files
committed
Correct /simplify
The gang/worker/vector check was insufficient based on review, so this fixes it. It also moves the check for the ParallelismFlag and gang/worker/vector check to LoopOp so that it can be used elsewhere. We also can simplify the Clang version here for the same reasons.
1 parent 644612d commit 39c443b

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

clang/lib/CIR/CodeGen/CIRGenStmtOpenACCLoop.cpp

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,56 +27,29 @@ void CIRGenFunction::updateLoopOpParallelism(mlir::acc::LoopOp &op,
2727
OpenACCDirectiveKind dk) {
2828
// Check that at least one of auto, independent, or seq is present
2929
// for the device-independent default clauses.
30-
auto hasDeviceNone = [](mlir::acc::DeviceTypeAttr attr) -> bool {
31-
return attr.getValue() == mlir::acc::DeviceType::None;
32-
};
33-
bool hasDefaultSeq =
34-
op.getSeqAttr()
35-
? llvm::any_of(
36-
op.getSeqAttr().getAsRange<mlir::acc::DeviceTypeAttr>(),
37-
hasDeviceNone)
38-
: false;
39-
bool hasDefaultIndependent =
40-
op.getIndependentAttr()
41-
? llvm::any_of(
42-
op.getIndependentAttr().getAsRange<mlir::acc::DeviceTypeAttr>(),
43-
hasDeviceNone)
44-
: false;
45-
bool hasDefaultAuto =
46-
op.getAuto_Attr()
47-
? llvm::any_of(
48-
op.getAuto_Attr().getAsRange<mlir::acc::DeviceTypeAttr>(),
49-
hasDeviceNone)
50-
: false;
51-
52-
if (hasDefaultSeq || hasDefaultIndependent || hasDefaultAuto)
30+
if (op.hasParallelismFlag(mlir::acc::DeviceType::None))
5331
return;
5432

55-
// Orphan or parallel results in 'independent'.
56-
if (isOrphan || dk == OpenACCDirectiveKind::Parallel ||
57-
dk == OpenACCDirectiveKind::ParallelLoop) {
33+
switch (dk) {
34+
default:
35+
llvm_unreachable("Invalid parent directive kind");
36+
case OpenACCDirectiveKind::Invalid:
37+
case OpenACCDirectiveKind::Parallel:
38+
case OpenACCDirectiveKind::ParallelLoop:
5839
op.addIndependent(builder.getContext(), {});
5940
return;
60-
}
61-
62-
// Kernels always results in 'auto'.
63-
if (dk == OpenACCDirectiveKind::Kernels ||
64-
dk == OpenACCDirectiveKind::KernelsLoop) {
41+
case OpenACCDirectiveKind::Kernels:
42+
case OpenACCDirectiveKind::KernelsLoop:
6543
op.addAuto(builder.getContext(), {});
6644
return;
67-
}
68-
69-
// Serial should use 'seq' unless there is a gang, worker, or vector clause,
70-
// in which case, it should use 'auto'.
71-
assert(dk == OpenACCDirectiveKind::Serial ||
72-
dk == OpenACCDirectiveKind::SerialLoop);
73-
74-
if (op.getWorkerAttr() || op.getVectorAttr() || op.getGangAttr()) {
75-
op.addAuto(builder.getContext(), {});
45+
case OpenACCDirectiveKind::Serial:
46+
case OpenACCDirectiveKind::SerialLoop:
47+
if (op.hasDefaultGangWorkerVector())
48+
op.addAuto(builder.getContext(), {});
49+
else
50+
op.addSeq(builder.getContext(), {});
7651
return;
77-
}
78-
79-
op.addSeq(builder.getContext(), {});
52+
};
8053
}
8154

8255
mlir::LogicalResult

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,14 @@ def OpenACC_LoopOp : OpenACC_Op<"loop",
22462246
// device_types. This is for the case where there is no expression specified
22472247
// in a 'gang'.
22482248
void addEmptyGang(MLIRContext *, llvm::ArrayRef<DeviceType>);
2249+
2250+
// Return whether this LoopOp has an auto, seq, or independent for the
2251+
// specified device-type.
2252+
bool hasParallelismFlag(DeviceType);
2253+
2254+
// Return whether this LoopOp has a gang, worker, or vector applying to the
2255+
// 'default'/None device-type.
2256+
bool hasDefaultGangWorkerVector();
22492257
}];
22502258

22512259
let hasCustomAssemblyFormat = 1;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,6 +2839,30 @@ void acc::LoopOp::addEmptyGang(
28392839
effectiveDeviceTypes));
28402840
}
28412841

2842+
bool acc::LoopOp::hasParallelismFlag(DeviceType dt) {
2843+
auto hasDevice = [=](DeviceTypeAttr attr) -> bool {
2844+
return attr.getValue() == dt;
2845+
};
2846+
auto testFromArr = [=](ArrayAttr arr) -> bool {
2847+
return llvm::any_of(arr.getAsRange<DeviceTypeAttr>(), hasDevice);
2848+
};
2849+
2850+
if (ArrayAttr arr = getSeqAttr(); arr && testFromArr(arr))
2851+
return true;
2852+
if (ArrayAttr arr = getIndependentAttr(); arr && testFromArr(arr))
2853+
return true;
2854+
if (ArrayAttr arr = getAuto_Attr(); arr && testFromArr(arr))
2855+
return true;
2856+
2857+
return false;
2858+
}
2859+
2860+
bool acc::LoopOp::hasDefaultGangWorkerVector() {
2861+
return hasVector() || getVectorValue() || hasWorker() || getWorkerValue() ||
2862+
hasGang() || getGangValue(GangArgType::Num) ||
2863+
getGangValue(GangArgType::Dim) || getGangValue(GangArgType::Static);
2864+
}
2865+
28422866
void acc::LoopOp::addGangOperands(
28432867
MLIRContext *context, llvm::ArrayRef<DeviceType> effectiveDeviceTypes,
28442868
llvm::ArrayRef<GangArgType> argTypes, mlir::ValueRange values) {

0 commit comments

Comments
 (0)