Skip to content

Commit 231aa30

Browse files
authored
[OpenACC][CIR] Basic infrastructure for OpenACC lowering (#134717)
This is the first of a few patches that will do infrastructure work to enable the OpenACC lowering via the OpenACC dialect. At the moment this just gets the various function calls that will end up generating OpenACC, plus some tests to validate that we're doing the diagnostics in OpenACC specific locations. Additionally, this adds Stmt and Decl files for CIRGen.
1 parent edcbd4a commit 231aa30

16 files changed

+303
-15
lines changed

clang/include/clang/AST/DeclOpenACC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class OpenACCConstructDecl : public Decl {
5959
}
6060

6161
ArrayRef<const OpenACCClause *> clauses() const { return Clauses; }
62+
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
63+
static bool classofKind(Kind K);
6264
};
6365

6466
class OpenACCDeclareDecl final

clang/include/clang/AST/GlobalDecl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/AST/Attr.h"
1818
#include "clang/AST/DeclCXX.h"
1919
#include "clang/AST/DeclObjC.h"
20+
#include "clang/AST/DeclOpenACC.h"
2021
#include "clang/AST/DeclOpenMP.h"
2122
#include "clang/AST/DeclTemplate.h"
2223
#include "clang/Basic/ABI.h"
@@ -86,6 +87,8 @@ class GlobalDecl {
8687
GlobalDecl(const ObjCMethodDecl *D) { Init(D); }
8788
GlobalDecl(const OMPDeclareReductionDecl *D) { Init(D); }
8889
GlobalDecl(const OMPDeclareMapperDecl *D) { Init(D); }
90+
GlobalDecl(const OpenACCRoutineDecl *D) { Init(D); }
91+
GlobalDecl(const OpenACCDeclareDecl *D) { Init(D); }
8992
GlobalDecl(const CXXConstructorDecl *D, CXXCtorType Type) : Value(D, Type) {}
9093
GlobalDecl(const CXXDestructorDecl *D, CXXDtorType Type) : Value(D, Type) {}
9194
GlobalDecl(const VarDecl *D, DynamicInitKind StubKind)

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,4 +850,9 @@ def warn_missing_include_dirs : Warning<
850850

851851
def err_drv_malformed_warning_suppression_mapping : Error<
852852
"failed to process suppression mapping file '%0': %1">;
853+
854+
def warn_drv_openacc_without_cir
855+
: Warning<"OpenACC directives will result in no runtime behavior; use "
856+
"-fclangir to enable runtime effect">,
857+
InGroup<SourceUsesOpenACC>;
853858
}

clang/lib/AST/DeclOpenACC.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
using namespace clang;
1919

20+
bool OpenACCConstructDecl::classofKind(Kind K) {
21+
return OpenACCDeclareDecl::classofKind(K) ||
22+
OpenACCRoutineDecl::classofKind(K);
23+
}
24+
2025
OpenACCDeclareDecl *
2126
OpenACCDeclareDecl::Create(ASTContext &Ctx, DeclContext *DC,
2227
SourceLocation StartLoc, SourceLocation DirLoc,

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mlir/IR/Location.h"
1616
#include "clang/AST/Attr.h"
1717
#include "clang/AST/Decl.h"
18+
#include "clang/AST/DeclOpenACC.h"
1819
#include "clang/AST/Expr.h"
1920
#include "clang/AST/ExprCXX.h"
2021
#include "clang/CIR/MissingFeatures.h"
@@ -266,6 +267,12 @@ void CIRGenFunction::emitDecl(const Decl &d) {
266267
emitVarDecl(vd);
267268
return;
268269
}
270+
case Decl::OpenACCDeclare:
271+
emitOpenACCDeclare(cast<OpenACCDeclareDecl>(d));
272+
return;
273+
case Decl::OpenACCRoutine:
274+
emitOpenACCRoutine(cast<OpenACCRoutineDecl>(d));
275+
return;
269276
default:
270277
cgm.errorNYI(d.getSourceRange(), "emitDecl: unhandled decl type");
271278
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This contains code to emit Decl nodes as CIR code.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CIRGenFunction.h"
14+
#include "clang/AST/DeclOpenACC.h"
15+
16+
using namespace clang;
17+
using namespace clang::CIRGen;
18+
19+
void CIRGenFunction::emitOpenACCDeclare(const OpenACCDeclareDecl &d) {
20+
getCIRGenModule().errorNYI(d.getSourceRange(), "OpenACC Declare Construct");
21+
}
22+
23+
void CIRGenFunction::emitOpenACCRoutine(const OpenACCRoutineDecl &d) {
24+
getCIRGenModule().errorNYI(d.getSourceRange(), "OpenACC Routine Construct");
25+
}
26+
27+
void CIRGenModule::emitGlobalOpenACCDecl(const OpenACCConstructDecl *d) {
28+
if (isa<OpenACCRoutineDecl>(d))
29+
errorNYI(d->getSourceRange(), "OpenACC Routine Construct");
30+
else if (isa<OpenACCDeclareDecl>(d))
31+
errorNYI(d->getSourceRange(), "OpenACC Declare Construct");
32+
else
33+
llvm_unreachable("unknown OpenACC declaration kind?");
34+
}

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,36 @@ class CIRGenFunction : public CIRGenTypeCache {
509509
public:
510510
Address createTempAlloca(mlir::Type ty, CharUnits align, mlir::Location loc,
511511
const Twine &name, bool insertIntoFnEntryBlock);
512+
513+
//===--------------------------------------------------------------------===//
514+
// OpenACC Emission
515+
//===--------------------------------------------------------------------===//
516+
public:
517+
mlir::LogicalResult
518+
emitOpenACCComputeConstruct(const OpenACCComputeConstruct &s);
519+
mlir::LogicalResult emitOpenACCLoopConstruct(const OpenACCLoopConstruct &s);
520+
mlir::LogicalResult
521+
emitOpenACCCombinedConstruct(const OpenACCCombinedConstruct &s);
522+
mlir::LogicalResult emitOpenACCDataConstruct(const OpenACCDataConstruct &s);
523+
mlir::LogicalResult
524+
emitOpenACCEnterDataConstruct(const OpenACCEnterDataConstruct &s);
525+
mlir::LogicalResult
526+
emitOpenACCExitDataConstruct(const OpenACCExitDataConstruct &s);
527+
mlir::LogicalResult
528+
emitOpenACCHostDataConstruct(const OpenACCHostDataConstruct &s);
529+
mlir::LogicalResult emitOpenACCWaitConstruct(const OpenACCWaitConstruct &s);
530+
mlir::LogicalResult emitOpenACCInitConstruct(const OpenACCInitConstruct &s);
531+
mlir::LogicalResult
532+
emitOpenACCShutdownConstruct(const OpenACCShutdownConstruct &s);
533+
mlir::LogicalResult emitOpenACCSetConstruct(const OpenACCSetConstruct &s);
534+
mlir::LogicalResult
535+
emitOpenACCUpdateConstruct(const OpenACCUpdateConstruct &s);
536+
mlir::LogicalResult
537+
emitOpenACCAtomicConstruct(const OpenACCAtomicConstruct &s);
538+
mlir::LogicalResult emitOpenACCCacheConstruct(const OpenACCCacheConstruct &s);
539+
540+
void emitOpenACCDeclare(const OpenACCDeclareDecl &d);
541+
void emitOpenACCRoutine(const OpenACCRoutineDecl &d);
512542
};
513543

514544
} // namespace clang::CIRGen

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "clang/AST/ASTContext.h"
1818
#include "clang/AST/DeclBase.h"
19+
#include "clang/AST/DeclOpenACC.h"
1920
#include "clang/AST/GlobalDecl.h"
2021
#include "clang/Basic/SourceManager.h"
2122
#include "clang/CIR/Dialect/IR/CIRDialect.h"
@@ -91,6 +92,11 @@ mlir::Location CIRGenModule::getLoc(SourceRange cRange) {
9192
}
9293

9394
void CIRGenModule::emitGlobal(clang::GlobalDecl gd) {
95+
if (const auto *cd = dyn_cast<clang::OpenACCConstructDecl>(gd.getDecl())) {
96+
emitGlobalOpenACCDecl(cd);
97+
return;
98+
}
99+
94100
const auto *global = cast<ValueDecl>(gd.getDecl());
95101

96102
if (const auto *fd = dyn_cast<FunctionDecl>(global)) {
@@ -423,6 +429,12 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) {
423429
emitGlobal(vd);
424430
break;
425431
}
432+
case Decl::OpenACCRoutine:
433+
emitGlobalOpenACCDecl(cast<OpenACCRoutineDecl>(decl));
434+
break;
435+
case Decl::OpenACCDeclare:
436+
emitGlobalOpenACCDecl(cast<OpenACCDeclareDecl>(decl));
437+
break;
426438
}
427439
}
428440

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class CIRGenModule : public CIRGenTypeCache {
113113
void emitGlobalVarDefinition(const clang::VarDecl *vd,
114114
bool isTentative = false);
115115

116+
void emitGlobalOpenACCDecl(const clang::OpenACCConstructDecl *cd);
117+
116118
/// Return the result of value-initializing the given type, i.e. a null
117119
/// expression of the given type.
118120
mlir::Value emitNullConstant(QualType t, mlir::Location loc);

clang/lib/CIR/CodeGen/CIRGenStmt.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mlir/IR/Builders.h"
1717
#include "clang/AST/ExprCXX.h"
1818
#include "clang/AST/Stmt.h"
19+
#include "clang/AST/StmtOpenACC.h"
1920

2021
using namespace clang;
2122
using namespace clang::CIRGen;
@@ -85,7 +86,34 @@ mlir::LogicalResult CIRGenFunction::emitStmt(const Stmt *s,
8586
return emitWhileStmt(cast<WhileStmt>(*s));
8687
case Stmt::DoStmtClass:
8788
return emitDoStmt(cast<DoStmt>(*s));
88-
89+
case Stmt::OpenACCComputeConstructClass:
90+
return emitOpenACCComputeConstruct(cast<OpenACCComputeConstruct>(*s));
91+
case Stmt::OpenACCLoopConstructClass:
92+
return emitOpenACCLoopConstruct(cast<OpenACCLoopConstruct>(*s));
93+
case Stmt::OpenACCCombinedConstructClass:
94+
return emitOpenACCCombinedConstruct(cast<OpenACCCombinedConstruct>(*s));
95+
case Stmt::OpenACCDataConstructClass:
96+
return emitOpenACCDataConstruct(cast<OpenACCDataConstruct>(*s));
97+
case Stmt::OpenACCEnterDataConstructClass:
98+
return emitOpenACCEnterDataConstruct(cast<OpenACCEnterDataConstruct>(*s));
99+
case Stmt::OpenACCExitDataConstructClass:
100+
return emitOpenACCExitDataConstruct(cast<OpenACCExitDataConstruct>(*s));
101+
case Stmt::OpenACCHostDataConstructClass:
102+
return emitOpenACCHostDataConstruct(cast<OpenACCHostDataConstruct>(*s));
103+
case Stmt::OpenACCWaitConstructClass:
104+
return emitOpenACCWaitConstruct(cast<OpenACCWaitConstruct>(*s));
105+
case Stmt::OpenACCInitConstructClass:
106+
return emitOpenACCInitConstruct(cast<OpenACCInitConstruct>(*s));
107+
case Stmt::OpenACCShutdownConstructClass:
108+
return emitOpenACCShutdownConstruct(cast<OpenACCShutdownConstruct>(*s));
109+
case Stmt::OpenACCSetConstructClass:
110+
return emitOpenACCSetConstruct(cast<OpenACCSetConstruct>(*s));
111+
case Stmt::OpenACCUpdateConstructClass:
112+
return emitOpenACCUpdateConstruct(cast<OpenACCUpdateConstruct>(*s));
113+
case Stmt::OpenACCCacheConstructClass:
114+
return emitOpenACCCacheConstruct(cast<OpenACCCacheConstruct>(*s));
115+
case Stmt::OpenACCAtomicConstructClass:
116+
return emitOpenACCAtomicConstruct(cast<OpenACCAtomicConstruct>(*s));
89117
case Stmt::OMPScopeDirectiveClass:
90118
case Stmt::OMPErrorDirectiveClass:
91119
case Stmt::NoStmtClass:
@@ -192,20 +220,6 @@ mlir::LogicalResult CIRGenFunction::emitStmt(const Stmt *s,
192220
case Stmt::OMPAssumeDirectiveClass:
193221
case Stmt::OMPMaskedDirectiveClass:
194222
case Stmt::OMPStripeDirectiveClass:
195-
case Stmt::OpenACCComputeConstructClass:
196-
case Stmt::OpenACCLoopConstructClass:
197-
case Stmt::OpenACCCombinedConstructClass:
198-
case Stmt::OpenACCDataConstructClass:
199-
case Stmt::OpenACCEnterDataConstructClass:
200-
case Stmt::OpenACCExitDataConstructClass:
201-
case Stmt::OpenACCHostDataConstructClass:
202-
case Stmt::OpenACCWaitConstructClass:
203-
case Stmt::OpenACCInitConstructClass:
204-
case Stmt::OpenACCShutdownConstructClass:
205-
case Stmt::OpenACCSetConstructClass:
206-
case Stmt::OpenACCUpdateConstructClass:
207-
case Stmt::OpenACCCacheConstructClass:
208-
case Stmt::OpenACCAtomicConstructClass:
209223
case Stmt::ObjCAtCatchStmtClass:
210224
case Stmt::ObjCAtFinallyStmtClass:
211225
cgm.errorNYI(s->getSourceRange(),
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Emit OpenACC Stmt nodes as CIR code.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CIRGenBuilder.h"
14+
#include "CIRGenFunction.h"
15+
#include "clang/AST/StmtOpenACC.h"
16+
17+
using namespace clang;
18+
using namespace clang::CIRGen;
19+
using namespace cir;
20+
21+
mlir::LogicalResult
22+
CIRGenFunction::emitOpenACCComputeConstruct(const OpenACCComputeConstruct &s) {
23+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Compute Construct");
24+
return mlir::failure();
25+
}
26+
27+
mlir::LogicalResult
28+
CIRGenFunction::emitOpenACCLoopConstruct(const OpenACCLoopConstruct &s) {
29+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Loop Construct");
30+
return mlir::failure();
31+
}
32+
mlir::LogicalResult CIRGenFunction::emitOpenACCCombinedConstruct(
33+
const OpenACCCombinedConstruct &s) {
34+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Combined Construct");
35+
return mlir::failure();
36+
}
37+
mlir::LogicalResult
38+
CIRGenFunction::emitOpenACCDataConstruct(const OpenACCDataConstruct &s) {
39+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Data Construct");
40+
return mlir::failure();
41+
}
42+
mlir::LogicalResult CIRGenFunction::emitOpenACCEnterDataConstruct(
43+
const OpenACCEnterDataConstruct &s) {
44+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC EnterData Construct");
45+
return mlir::failure();
46+
}
47+
mlir::LogicalResult CIRGenFunction::emitOpenACCExitDataConstruct(
48+
const OpenACCExitDataConstruct &s) {
49+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC ExitData Construct");
50+
return mlir::failure();
51+
}
52+
mlir::LogicalResult CIRGenFunction::emitOpenACCHostDataConstruct(
53+
const OpenACCHostDataConstruct &s) {
54+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC HostData Construct");
55+
return mlir::failure();
56+
}
57+
mlir::LogicalResult
58+
CIRGenFunction::emitOpenACCWaitConstruct(const OpenACCWaitConstruct &s) {
59+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Wait Construct");
60+
return mlir::failure();
61+
}
62+
mlir::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
73+
CIRGenFunction::emitOpenACCSetConstruct(const OpenACCSetConstruct &s) {
74+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Set Construct");
75+
return mlir::failure();
76+
}
77+
mlir::LogicalResult
78+
CIRGenFunction::emitOpenACCUpdateConstruct(const OpenACCUpdateConstruct &s) {
79+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Update Construct");
80+
return mlir::failure();
81+
}
82+
mlir::LogicalResult
83+
CIRGenFunction::emitOpenACCAtomicConstruct(const OpenACCAtomicConstruct &s) {
84+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Atomic Construct");
85+
return mlir::failure();
86+
}
87+
mlir::LogicalResult
88+
CIRGenFunction::emitOpenACCCacheConstruct(const OpenACCCacheConstruct &s) {
89+
getCIRGenModule().errorNYI(s.getSourceRange(), "OpenACC Cache Construct");
90+
return mlir::failure();
91+
}

clang/lib/CIR/CodeGen/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
99
add_clang_library(clangCIR
1010
CIRGenerator.cpp
1111
CIRGenDecl.cpp
12+
CIRGenDeclOpenACC.cpp
1213
CIRGenExpr.cpp
1314
CIRGenExprAggregate.cpp
1415
CIRGenExprConstant.cpp
1516
CIRGenExprScalar.cpp
1617
CIRGenFunction.cpp
1718
CIRGenModule.cpp
1819
CIRGenStmt.cpp
20+
CIRGenStmtOpenACC.cpp
1921
CIRGenTypes.cpp
2022

2123
DEPENDS

0 commit comments

Comments
 (0)