1212
1313#include " CIRGenBuilder.h"
1414#include " CIRGenFunction.h"
15+ #include " clang/AST/OpenACCClause.h"
1516#include " clang/AST/StmtOpenACC.h"
1617
18+ #include " mlir/Dialect/OpenACC/OpenACC.h"
19+
1720using namespace clang ;
1821using namespace clang ::CIRGen;
1922using namespace cir ;
23+ using namespace mlir ::acc;
24+
25+ namespace {
26+ class OpenACCClauseCIREmitter final
27+ : public OpenACCClauseVisitor<OpenACCClauseCIREmitter> {
28+ CIRGenModule &cgm;
29+
30+ struct AttributeData {
31+ // Value of the 'default' attribute, added on 'data' and 'compute'/etc
32+ // constructs as a 'default-attr'.
33+ std::optional<ClauseDefaultValue> defaultVal = std::nullopt ;
34+ } attrData;
35+
36+ void clauseNotImplemented (const OpenACCClause &c) {
37+ cgm.errorNYI (c.getSourceRange (), " OpenACC Clause" , c.getClauseKind ());
38+ }
39+
40+ public:
41+ OpenACCClauseCIREmitter (CIRGenModule &cgm) : cgm(cgm) {}
42+
43+ void VisitClause (const OpenACCClause &clause) {
44+ clauseNotImplemented (clause);
45+ }
46+
47+ void VisitDefaultClause (const OpenACCDefaultClause &clause) {
48+ switch (clause.getDefaultClauseKind ()) {
49+ case OpenACCDefaultClauseKind::None:
50+ attrData.defaultVal = ClauseDefaultValue::None;
51+ break ;
52+ case OpenACCDefaultClauseKind::Present:
53+ attrData.defaultVal = ClauseDefaultValue::Present;
54+ break ;
55+ case OpenACCDefaultClauseKind::Invalid:
56+ break ;
57+ }
58+ }
59+
60+ // Apply any of the clauses that resulted in an 'attribute'.
61+ template <typename Op> void applyAttributes (Op &op) {
62+ if (attrData.defaultVal .has_value ())
63+ op.setDefaultAttr (*attrData.defaultVal );
64+ }
65+ };
66+ } // namespace
67+
68+ template <typename Op, typename TermOp>
69+ mlir::LogicalResult CIRGenFunction::emitOpenACCOpAssociatedStmt (
70+ mlir::Location start, mlir::Location end,
71+ llvm::ArrayRef<const OpenACCClause *> clauses, const Stmt *associatedStmt) {
72+ mlir::LogicalResult res = mlir::success ();
73+
74+ llvm::SmallVector<mlir::Type> retTy;
75+ llvm::SmallVector<mlir::Value> operands;
76+
77+ // Clause-emitter must be here because it might modify operands.
78+ OpenACCClauseCIREmitter clauseEmitter (getCIRGenModule ());
79+ clauseEmitter.VisitClauseList (clauses);
80+
81+ auto op = builder.create <Op>(start, retTy, operands);
82+
83+ // Apply the attributes derived from the clauses.
84+ clauseEmitter.applyAttributes (op);
85+
86+ mlir::Block &block = op.getRegion ().emplaceBlock ();
87+ mlir::OpBuilder::InsertionGuard guardCase (builder);
88+ builder.setInsertionPointToEnd (&block);
89+
90+ LexicalScope ls{*this , start, builder.getInsertionBlock ()};
91+ res = emitStmt (associatedStmt, /* useCurrentScope=*/ true );
92+
93+ builder.create <TermOp>(end);
94+ return res;
95+ }
96+
97+ template <typename Op>
98+ mlir::LogicalResult
99+ CIRGenFunction::emitOpenACCOp (mlir::Location start,
100+ llvm::ArrayRef<const OpenACCClause *> clauses) {
101+ mlir::LogicalResult res = mlir::success ();
102+
103+ llvm::SmallVector<mlir::Type> retTy;
104+ llvm::SmallVector<mlir::Value> operands;
105+
106+ // Clause-emitter must be here because it might modify operands.
107+ OpenACCClauseCIREmitter clauseEmitter (getCIRGenModule ());
108+ clauseEmitter.VisitClauseList (clauses);
109+
110+ builder.create <Op>(start, retTy, operands);
111+ return res;
112+ }
20113
21114mlir::LogicalResult
22115CIRGenFunction::emitOpenACCComputeConstruct (const OpenACCComputeConstruct &s) {
23- getCIRGenModule ().errorNYI (s.getSourceRange (), " OpenACC Compute Construct" );
24- return mlir::failure ();
116+ mlir::Location start = getLoc (s.getSourceRange ().getEnd ());
117+ mlir::Location end = getLoc (s.getSourceRange ().getEnd ());
118+
119+ switch (s.getDirectiveKind ()) {
120+ case OpenACCDirectiveKind::Parallel:
121+ return emitOpenACCOpAssociatedStmt<ParallelOp, mlir::acc::YieldOp>(
122+ start, end, s.clauses (), s.getStructuredBlock ());
123+ case OpenACCDirectiveKind::Serial:
124+ return emitOpenACCOpAssociatedStmt<SerialOp, mlir::acc::YieldOp>(
125+ start, end, s.clauses (), s.getStructuredBlock ());
126+ case OpenACCDirectiveKind::Kernels:
127+ return emitOpenACCOpAssociatedStmt<KernelsOp, mlir::acc::TerminatorOp>(
128+ start, end, s.clauses (), s.getStructuredBlock ());
129+ default :
130+ llvm_unreachable (" invalid compute construct kind" );
131+ }
132+ }
133+
134+ mlir::LogicalResult
135+ CIRGenFunction::emitOpenACCDataConstruct (const OpenACCDataConstruct &s) {
136+ mlir::Location start = getLoc (s.getSourceRange ().getEnd ());
137+ mlir::Location end = getLoc (s.getSourceRange ().getEnd ());
138+
139+ return emitOpenACCOpAssociatedStmt<DataOp, mlir::acc::TerminatorOp>(
140+ start, end, s.clauses (), s.getStructuredBlock ());
141+ }
142+
143+ mlir::LogicalResult
144+ CIRGenFunction::emitOpenACCInitConstruct (const OpenACCInitConstruct &s) {
145+ mlir::Location start = getLoc (s.getSourceRange ().getEnd ());
146+ return emitOpenACCOp<InitOp>(start, s.clauses ());
147+ }
148+ mlir::LogicalResult CIRGenFunction::emitOpenACCShutdownConstruct (
149+ const OpenACCShutdownConstruct &s) {
150+ mlir::Location start = getLoc (s.getSourceRange ().getEnd ());
151+ return emitOpenACCOp<ShutdownOp>(start, s.clauses ());
25152}
26153
27154mlir::LogicalResult
@@ -34,11 +161,6 @@ mlir::LogicalResult CIRGenFunction::emitOpenACCCombinedConstruct(
34161 getCIRGenModule ().errorNYI (s.getSourceRange (), " OpenACC Combined Construct" );
35162 return mlir::failure ();
36163}
37- mlir::LogicalResult
38- CIRGenFunction::emitOpenACCDataConstruct (const OpenACCDataConstruct &s) {
39- getCIRGenModule ().errorNYI (s.getSourceRange (), " OpenACC Data Construct" );
40- return mlir::failure ();
41- }
42164mlir::LogicalResult CIRGenFunction::emitOpenACCEnterDataConstruct (
43165 const OpenACCEnterDataConstruct &s) {
44166 getCIRGenModule ().errorNYI (s.getSourceRange (), " OpenACC EnterData Construct" );
@@ -60,16 +182,6 @@ CIRGenFunction::emitOpenACCWaitConstruct(const OpenACCWaitConstruct &s) {
60182 return mlir::failure ();
61183}
62184mlir::LogicalResult
63- CIRGenFunction::emitOpenACCInitConstruct (const OpenACCInitConstruct &s) {
64- getCIRGenModule ().errorNYI (s.getSourceRange (), " OpenACC Init Construct" );
65- return mlir::failure ();
66- }
67- mlir::LogicalResult CIRGenFunction::emitOpenACCShutdownConstruct (
68- const OpenACCShutdownConstruct &s) {
69- getCIRGenModule ().errorNYI (s.getSourceRange (), " OpenACC Shutdown Construct" );
70- return mlir::failure ();
71- }
72- mlir::LogicalResult
73185CIRGenFunction::emitOpenACCSetConstruct (const OpenACCSetConstruct &s) {
74186 getCIRGenModule ().errorNYI (s.getSourceRange (), " OpenACC Set Construct" );
75187 return mlir::failure ();
0 commit comments