Skip to content

Commit d06af45

Browse files
erichkeaneaadeshps-mcw
authored andcommitted
[OpenAC][CIR] func-local-declare 'copy' clause lowering (llvm#169115)
This patch implements the lowering for the 'copy' clause for a function-local declare directive. This is the first of the clauses that requires a 'cleanup' step, so it also includes some basic infrastructure for that. Fortunately there are only 8 clauses (only 6 of which require cleanup), so the if/else chain won't get too long. Also fortunately, we don't have to include any of the AST components, as it is possible to tell all the required details from the entry operation itself.
1 parent abc38f2 commit d06af45

File tree

3 files changed

+248
-11
lines changed

3 files changed

+248
-11
lines changed

clang/lib/CIR/CodeGen/CIRGenDeclOpenACC.cpp

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,52 @@ using namespace clang::CIRGen;
1919

2020
namespace {
2121
struct OpenACCDeclareCleanup final : EHScopeStack::Cleanup {
22+
SourceRange declareRange;
2223
mlir::acc::DeclareEnterOp enterOp;
2324

24-
OpenACCDeclareCleanup(mlir::acc::DeclareEnterOp enterOp) : enterOp(enterOp) {}
25+
OpenACCDeclareCleanup(SourceRange declareRange,
26+
mlir::acc::DeclareEnterOp enterOp)
27+
: declareRange(declareRange), enterOp(enterOp) {}
28+
29+
template <typename OutTy, typename InTy>
30+
void createOutOp(CIRGenFunction &cgf, InTy inOp) {
31+
auto outOp =
32+
OutTy::create(cgf.getBuilder(), inOp.getLoc(), inOp, inOp.getVarPtr(),
33+
inOp.getStructured(), inOp.getImplicit(),
34+
llvm::Twine(inOp.getNameAttr()), inOp.getBounds());
35+
outOp.setDataClause(inOp.getDataClause());
36+
outOp.setModifiers(inOp.getModifiers());
37+
}
2538

2639
void emit(CIRGenFunction &cgf) override {
27-
mlir::acc::DeclareExitOp::create(cgf.getBuilder(), enterOp.getLoc(),
28-
enterOp, {});
40+
auto exitOp = mlir::acc::DeclareExitOp::create(
41+
cgf.getBuilder(), enterOp.getLoc(), enterOp, {});
2942

30-
// TODO(OpenACC): Some clauses require that we add info about them to the
31-
// DeclareExitOp. However, we don't have any of those implemented yet, so
32-
// we should add infrastructure here to do that once we have one
33-
// implemented.
43+
// Some data clauses need to be referenced in 'exit', AND need to have an
44+
// operation after the exit. Copy these from the enter operation.
45+
for (mlir::Value val : enterOp.getDataClauseOperands()) {
46+
if (auto copyin = val.getDefiningOp<mlir::acc::CopyinOp>()) {
47+
switch (copyin.getDataClause()) {
48+
default:
49+
cgf.cgm.errorNYI(declareRange,
50+
"OpenACC local declare clause copyin cleanup");
51+
break;
52+
case mlir::acc::DataClause::acc_copy:
53+
createOutOp<mlir::acc::CopyoutOp>(cgf, copyin);
54+
break;
55+
}
56+
} else if (val.getDefiningOp<mlir::acc::DeclareLinkOp>()) {
57+
// Link has no exit clauses, and shouldn't be copied.
58+
continue;
59+
} else if (val.getDefiningOp<mlir::acc::DevicePtrOp>()) {
60+
// DevicePtr has no exit clauses, and shouldn't be copied.
61+
continue;
62+
} else {
63+
cgf.cgm.errorNYI(declareRange, "OpenACC local declare clause cleanup");
64+
continue;
65+
}
66+
exitOp.getDataClauseOperandsMutable().append(val);
67+
}
3468
}
3569
};
3670
} // namespace
@@ -45,7 +79,7 @@ void CIRGenFunction::emitOpenACCDeclare(const OpenACCDeclareDecl &d) {
4579
d.clauses());
4680

4781
ehStack.pushCleanup<OpenACCDeclareCleanup>(CleanupKind::NormalCleanup,
48-
enterOp);
82+
d.getSourceRange(), enterOp);
4983
}
5084

5185
void CIRGenFunction::emitOpenACCRoutine(const OpenACCRoutineDecl &d) {

clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,16 @@ class OpenACCClauseCIREmitter final
800800
var, mlir::acc::DataClause::acc_copy, clause.getModifierList(),
801801
/*structured=*/true,
802802
/*implicit=*/false);
803+
} else if constexpr (isOneOfTypes<OpTy, mlir::acc::DeclareEnterOp>) {
804+
for (const Expr *var : clause.getVarList())
805+
addDataOperand<mlir::acc::CopyinOp>(
806+
var, mlir::acc::DataClause::acc_copy, clause.getModifierList(),
807+
/*structured=*/true,
808+
/*implicit=*/false);
803809
} else if constexpr (isCombinedType<OpTy>) {
804810
applyToComputeOp(clause);
805811
} else {
806-
// TODO: When we've implemented this for everything, switch this to an
807-
// unreachable. declare construct remains.
808-
return clauseNotImplemented(clause);
812+
llvm_unreachable("Unknown construct kind in VisitCopyClause");
809813
}
810814
}
811815

0 commit comments

Comments
 (0)