Skip to content

Commit e45f6e5

Browse files
authored
[clang] Factor out OpenACC part of Sema (#84184)
This patch moves OpenACC parts of `Sema` into a separate class `SemaOpenACC` that is placed in a separate header `Sema/SemaOpenACC.h`. This patch is intended to be a model of factoring things out of `Sema`, so I picked a small OpenACC part. Goals are the following: 1) Split `Sema` into manageable parts. 2) Make dependencies between parts visible. 3) Improve Clang development cycle by avoiding recompiling unrelated parts of the compiler. 4) Avoid compile-time regressions. 5) Avoid notational regressions in the code that uses Sema.
1 parent b8ead21 commit e45f6e5

File tree

7 files changed

+125
-83
lines changed

7 files changed

+125
-83
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "clang/AST/NSAPI.h"
3434
#include "clang/AST/PrettyPrinter.h"
3535
#include "clang/AST/StmtCXX.h"
36-
#include "clang/AST/StmtOpenACC.h"
3736
#include "clang/AST/StmtOpenMP.h"
3837
#include "clang/AST/TypeLoc.h"
3938
#include "clang/AST/TypeOrdering.h"
@@ -42,7 +41,6 @@
4241
#include "clang/Basic/DarwinSDKInfo.h"
4342
#include "clang/Basic/ExpressionTraits.h"
4443
#include "clang/Basic/Module.h"
45-
#include "clang/Basic/OpenACCKinds.h"
4644
#include "clang/Basic/OpenCLOptions.h"
4745
#include "clang/Basic/OpenMPKinds.h"
4846
#include "clang/Basic/PragmaKinds.h"
@@ -183,6 +181,7 @@ class Preprocessor;
183181
class PseudoDestructorTypeStorage;
184182
class PseudoObjectExpr;
185183
class QualType;
184+
class SemaOpenACC;
186185
class StandardConversionSequence;
187186
class Stmt;
188187
class StringLiteral;
@@ -466,9 +465,8 @@ class Sema final {
466465
// 37. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)
467466
// 38. CUDA (SemaCUDA.cpp)
468467
// 39. HLSL Constructs (SemaHLSL.cpp)
469-
// 40. OpenACC Constructs (SemaOpenACC.cpp)
470-
// 41. OpenMP Directives and Clauses (SemaOpenMP.cpp)
471-
// 42. SYCL Constructs (SemaSYCL.cpp)
468+
// 40. OpenMP Directives and Clauses (SemaOpenMP.cpp)
469+
// 41. SYCL Constructs (SemaSYCL.cpp)
472470

473471
/// \name Semantic Analysis
474472
/// Implementations are in Sema.cpp
@@ -1162,6 +1160,11 @@ class Sema final {
11621160
/// CurContext - This is the current declaration context of parsing.
11631161
DeclContext *CurContext;
11641162

1163+
SemaOpenACC &OpenACC() {
1164+
assert(OpenACCPtr);
1165+
return *OpenACCPtr;
1166+
}
1167+
11651168
protected:
11661169
friend class Parser;
11671170
friend class InitializationSequence;
@@ -1192,6 +1195,8 @@ class Sema final {
11921195

11931196
mutable IdentifierInfo *Ident_super;
11941197

1198+
std::unique_ptr<SemaOpenACC> OpenACCPtr;
1199+
11951200
///@}
11961201

11971202
//
@@ -13351,56 +13356,6 @@ class Sema final {
1335113356
//
1335213357
//
1335313358

13354-
/// \name OpenACC Constructs
13355-
/// Implementations are in SemaOpenACC.cpp
13356-
///@{
13357-
13358-
public:
13359-
/// Called after parsing an OpenACC Clause so that it can be checked.
13360-
bool ActOnOpenACCClause(OpenACCClauseKind ClauseKind,
13361-
SourceLocation StartLoc);
13362-
13363-
/// Called after the construct has been parsed, but clauses haven't been
13364-
/// parsed. This allows us to diagnose not-implemented, as well as set up any
13365-
/// state required for parsing the clauses.
13366-
void ActOnOpenACCConstruct(OpenACCDirectiveKind K, SourceLocation StartLoc);
13367-
13368-
/// Called after the directive, including its clauses, have been parsed and
13369-
/// parsing has consumed the 'annot_pragma_openacc_end' token. This DOES
13370-
/// happen before any associated declarations or statements have been parsed.
13371-
/// This function is only called when we are parsing a 'statement' context.
13372-
bool ActOnStartOpenACCStmtDirective(OpenACCDirectiveKind K,
13373-
SourceLocation StartLoc);
13374-
13375-
/// Called after the directive, including its clauses, have been parsed and
13376-
/// parsing has consumed the 'annot_pragma_openacc_end' token. This DOES
13377-
/// happen before any associated declarations or statements have been parsed.
13378-
/// This function is only called when we are parsing a 'Decl' context.
13379-
bool ActOnStartOpenACCDeclDirective(OpenACCDirectiveKind K,
13380-
SourceLocation StartLoc);
13381-
/// Called when we encounter an associated statement for our construct, this
13382-
/// should check legality of the statement as it appertains to this Construct.
13383-
StmtResult ActOnOpenACCAssociatedStmt(OpenACCDirectiveKind K,
13384-
StmtResult AssocStmt);
13385-
13386-
/// Called after the directive has been completely parsed, including the
13387-
/// declaration group or associated statement.
13388-
StmtResult ActOnEndOpenACCStmtDirective(OpenACCDirectiveKind K,
13389-
SourceLocation StartLoc,
13390-
SourceLocation EndLoc,
13391-
StmtResult AssocStmt);
13392-
/// Called after the directive has been completely parsed, including the
13393-
/// declaration group or associated statement.
13394-
DeclGroupRef ActOnEndOpenACCDeclDirective();
13395-
13396-
///@}
13397-
13398-
//
13399-
//
13400-
// -------------------------------------------------------------------------
13401-
//
13402-
//
13403-
1340413359
/// \name OpenMP Directives and Clauses
1340513360
/// Implementations are in SemaOpenMP.cpp
1340613361
///@{
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===----- SemaOpenACC.h - Semantic Analysis for OpenACC constructs -------===//
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+
/// \file
9+
/// This file declares semantic analysis for OpenACC constructs and
10+
/// clauses.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CLANG_SEMA_SEMAOPENACC_H
15+
#define LLVM_CLANG_SEMA_SEMAOPENACC_H
16+
17+
#include "clang/AST/DeclGroup.h"
18+
#include "clang/Basic/OpenACCKinds.h"
19+
#include "clang/Basic/SourceLocation.h"
20+
#include "clang/Sema/Ownership.h"
21+
22+
namespace clang {
23+
24+
class ASTContext;
25+
class DiagnosticEngine;
26+
class LangOptions;
27+
class Sema;
28+
29+
class SemaOpenACC {
30+
public:
31+
SemaOpenACC(Sema &S);
32+
33+
ASTContext &getASTContext() const;
34+
DiagnosticsEngine &getDiagnostics() const;
35+
const LangOptions &getLangOpts() const;
36+
37+
Sema &SemaRef;
38+
39+
/// Called after parsing an OpenACC Clause so that it can be checked.
40+
bool ActOnClause(OpenACCClauseKind ClauseKind, SourceLocation StartLoc);
41+
42+
/// Called after the construct has been parsed, but clauses haven't been
43+
/// parsed. This allows us to diagnose not-implemented, as well as set up any
44+
/// state required for parsing the clauses.
45+
void ActOnConstruct(OpenACCDirectiveKind K, SourceLocation StartLoc);
46+
47+
/// Called after the directive, including its clauses, have been parsed and
48+
/// parsing has consumed the 'annot_pragma_openacc_end' token. This DOES
49+
/// happen before any associated declarations or statements have been parsed.
50+
/// This function is only called when we are parsing a 'statement' context.
51+
bool ActOnStartStmtDirective(OpenACCDirectiveKind K, SourceLocation StartLoc);
52+
53+
/// Called after the directive, including its clauses, have been parsed and
54+
/// parsing has consumed the 'annot_pragma_openacc_end' token. This DOES
55+
/// happen before any associated declarations or statements have been parsed.
56+
/// This function is only called when we are parsing a 'Decl' context.
57+
bool ActOnStartDeclDirective(OpenACCDirectiveKind K, SourceLocation StartLoc);
58+
/// Called when we encounter an associated statement for our construct, this
59+
/// should check legality of the statement as it appertains to this Construct.
60+
StmtResult ActOnAssociatedStmt(OpenACCDirectiveKind K, StmtResult AssocStmt);
61+
62+
/// Called after the directive has been completely parsed, including the
63+
/// declaration group or associated statement.
64+
StmtResult ActOnEndStmtDirective(OpenACCDirectiveKind K,
65+
SourceLocation StartLoc,
66+
SourceLocation EndLoc, StmtResult AssocStmt);
67+
/// Called after the directive has been completely parsed, including the
68+
/// declaration group or associated statement.
69+
DeclGroupRef ActOnEndDeclDirective();
70+
};
71+
72+
} // namespace clang
73+
74+
#endif // LLVM_CLANG_SEMA_SEMAOPENACC_H

clang/lib/Parse/ParseOpenACC.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/Parse/ParseDiagnostic.h"
1515
#include "clang/Parse/Parser.h"
1616
#include "clang/Parse/RAIIObjectsForParser.h"
17+
#include "clang/Sema/SemaOpenACC.h"
1718
#include "llvm/ADT/StringRef.h"
1819
#include "llvm/ADT/StringSwitch.h"
1920

@@ -777,7 +778,7 @@ bool Parser::ParseOpenACCClause(OpenACCDirectiveKind DirKind) {
777778
SourceLocation ClauseLoc = ConsumeToken();
778779

779780
bool Result = ParseOpenACCClauseParams(DirKind, Kind);
780-
getActions().ActOnOpenACCClause(Kind, ClauseLoc);
781+
getActions().OpenACC().ActOnClause(Kind, ClauseLoc);
781782
return Result;
782783
}
783784

@@ -1151,7 +1152,7 @@ Parser::OpenACCDirectiveParseInfo Parser::ParseOpenACCDirective() {
11511152
SourceLocation StartLoc = getCurToken().getLocation();
11521153
OpenACCDirectiveKind DirKind = ParseOpenACCDirectiveKind(*this);
11531154

1154-
getActions().ActOnOpenACCConstruct(DirKind, StartLoc);
1155+
getActions().OpenACC().ActOnConstruct(DirKind, StartLoc);
11551156

11561157
// Once we've parsed the construct/directive name, some have additional
11571158
// specifiers that need to be taken care of. Atomic has an 'atomic-clause'
@@ -1223,12 +1224,12 @@ Parser::DeclGroupPtrTy Parser::ParseOpenACCDirectiveDecl() {
12231224

12241225
OpenACCDirectiveParseInfo DirInfo = ParseOpenACCDirective();
12251226

1226-
if (getActions().ActOnStartOpenACCDeclDirective(DirInfo.DirKind,
1227-
DirInfo.StartLoc))
1227+
if (getActions().OpenACC().ActOnStartDeclDirective(DirInfo.DirKind,
1228+
DirInfo.StartLoc))
12281229
return nullptr;
12291230

12301231
// TODO OpenACC: Do whatever decl parsing is required here.
1231-
return DeclGroupPtrTy::make(getActions().ActOnEndOpenACCDeclDirective());
1232+
return DeclGroupPtrTy::make(getActions().OpenACC().ActOnEndDeclDirective());
12321233
}
12331234

12341235
// Parse OpenACC Directive on a Statement.
@@ -1239,8 +1240,8 @@ StmtResult Parser::ParseOpenACCDirectiveStmt() {
12391240
ConsumeAnnotationToken();
12401241

12411242
OpenACCDirectiveParseInfo DirInfo = ParseOpenACCDirective();
1242-
if (getActions().ActOnStartOpenACCStmtDirective(DirInfo.DirKind,
1243-
DirInfo.StartLoc))
1243+
if (getActions().OpenACC().ActOnStartStmtDirective(DirInfo.DirKind,
1244+
DirInfo.StartLoc))
12441245
return StmtError();
12451246

12461247
StmtResult AssocStmt;
@@ -1249,10 +1250,10 @@ StmtResult Parser::ParseOpenACCDirectiveStmt() {
12491250
ParsingOpenACCDirectiveRAII DirScope(*this, /*Value=*/false);
12501251
ParseScope ACCScope(this, getOpenACCScopeFlags(DirInfo.DirKind));
12511252

1252-
AssocStmt = getActions().ActOnOpenACCAssociatedStmt(DirInfo.DirKind,
1253-
ParseStatement());
1253+
AssocStmt = getActions().OpenACC().ActOnAssociatedStmt(DirInfo.DirKind,
1254+
ParseStatement());
12541255
}
12551256

1256-
return getActions().ActOnEndOpenACCStmtDirective(
1257+
return getActions().OpenACC().ActOnEndStmtDirective(
12571258
DirInfo.DirKind, DirInfo.StartLoc, DirInfo.EndLoc, AssocStmt);
12581259
}

clang/lib/Sema/JumpDiagnostics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "clang/AST/ExprCXX.h"
1717
#include "clang/AST/StmtCXX.h"
1818
#include "clang/AST/StmtObjC.h"
19+
#include "clang/AST/StmtOpenACC.h"
1920
#include "clang/AST/StmtOpenMP.h"
2021
#include "clang/Basic/SourceLocation.h"
2122
#include "clang/Sema/SemaInternal.h"

clang/lib/Sema/Sema.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "clang/Sema/ScopeInfo.h"
4444
#include "clang/Sema/SemaConsumer.h"
4545
#include "clang/Sema/SemaInternal.h"
46+
#include "clang/Sema/SemaOpenACC.h"
4647
#include "clang/Sema/TemplateDeduction.h"
4748
#include "clang/Sema/TemplateInstCallback.h"
4849
#include "clang/Sema/TypoCorrection.h"
@@ -196,7 +197,7 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
196197
ThreadSafetyDeclCache(nullptr), LateTemplateParser(nullptr),
197198
LateTemplateParserCleanup(nullptr), OpaqueParser(nullptr),
198199
CurContext(nullptr), ExternalSource(nullptr), CurScope(nullptr),
199-
Ident_super(nullptr),
200+
Ident_super(nullptr), OpenACCPtr(std::make_unique<SemaOpenACC>(*this)),
200201
MSPointerToMemberRepresentationMethod(
201202
LangOpts.getMSPointerToMemberRepresentationMethod()),
202203
MSStructPragmaOn(false), VtorDispStack(LangOpts.getVtorDispMode()),

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
///
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "clang/AST/StmtOpenACC.h"
15+
#include "clang/Sema/SemaOpenACC.h"
1416
#include "clang/Basic/DiagnosticSema.h"
15-
#include "clang/Basic/OpenACCKinds.h"
1617
#include "clang/Sema/Sema.h"
1718

1819
using namespace clang;
1920

2021
namespace {
21-
bool diagnoseConstructAppertainment(Sema &S, OpenACCDirectiveKind K,
22+
bool diagnoseConstructAppertainment(SemaOpenACC &S, OpenACCDirectiveKind K,
2223
SourceLocation StartLoc, bool IsStmt) {
2324
switch (K) {
2425
default:
@@ -30,24 +31,32 @@ bool diagnoseConstructAppertainment(Sema &S, OpenACCDirectiveKind K,
3031
case OpenACCDirectiveKind::Serial:
3132
case OpenACCDirectiveKind::Kernels:
3233
if (!IsStmt)
33-
return S.Diag(StartLoc, diag::err_acc_construct_appertainment) << K;
34+
return S.SemaRef.Diag(StartLoc, diag::err_acc_construct_appertainment)
35+
<< K;
3436
break;
3537
}
3638
return false;
3739
}
3840
} // namespace
3941

40-
bool Sema::ActOnOpenACCClause(OpenACCClauseKind ClauseKind,
42+
SemaOpenACC::SemaOpenACC(Sema &S) : SemaRef(S) {}
43+
44+
ASTContext &SemaOpenACC::getASTContext() const { return SemaRef.Context; }
45+
DiagnosticsEngine &SemaOpenACC::getDiagnostics() const { return SemaRef.Diags; }
46+
const LangOptions &SemaOpenACC::getLangOpts() const { return SemaRef.LangOpts; }
47+
48+
bool SemaOpenACC::ActOnClause(OpenACCClauseKind ClauseKind,
4149
SourceLocation StartLoc) {
4250
if (ClauseKind == OpenACCClauseKind::Invalid)
4351
return false;
4452
// For now just diagnose that it is unsupported and leave the parsing to do
4553
// whatever it can do. This function will eventually need to start returning
4654
// some sort of Clause AST type, but for now just return true/false based on
4755
// success.
48-
return Diag(StartLoc, diag::warn_acc_clause_unimplemented) << ClauseKind;
56+
return SemaRef.Diag(StartLoc, diag::warn_acc_clause_unimplemented)
57+
<< ClauseKind;
4958
}
50-
void Sema::ActOnOpenACCConstruct(OpenACCDirectiveKind K,
59+
void SemaOpenACC::ActOnConstruct(OpenACCDirectiveKind K,
5160
SourceLocation StartLoc) {
5261
switch (K) {
5362
case OpenACCDirectiveKind::Invalid:
@@ -63,17 +72,17 @@ void Sema::ActOnOpenACCConstruct(OpenACCDirectiveKind K,
6372
// here as these constructs do not take any arguments.
6473
break;
6574
default:
66-
Diag(StartLoc, diag::warn_acc_construct_unimplemented) << K;
75+
SemaRef.Diag(StartLoc, diag::warn_acc_construct_unimplemented) << K;
6776
break;
6877
}
6978
}
7079

71-
bool Sema::ActOnStartOpenACCStmtDirective(OpenACCDirectiveKind K,
80+
bool SemaOpenACC::ActOnStartStmtDirective(OpenACCDirectiveKind K,
7281
SourceLocation StartLoc) {
7382
return diagnoseConstructAppertainment(*this, K, StartLoc, /*IsStmt=*/true);
7483
}
7584

76-
StmtResult Sema::ActOnEndOpenACCStmtDirective(OpenACCDirectiveKind K,
85+
StmtResult SemaOpenACC::ActOnEndStmtDirective(OpenACCDirectiveKind K,
7786
SourceLocation StartLoc,
7887
SourceLocation EndLoc,
7988
StmtResult AssocStmt) {
@@ -92,7 +101,7 @@ StmtResult Sema::ActOnEndOpenACCStmtDirective(OpenACCDirectiveKind K,
92101
llvm_unreachable("Unhandled case in directive handling?");
93102
}
94103

95-
StmtResult Sema::ActOnOpenACCAssociatedStmt(OpenACCDirectiveKind K,
104+
StmtResult SemaOpenACC::ActOnAssociatedStmt(OpenACCDirectiveKind K,
96105
StmtResult AssocStmt) {
97106
switch (K) {
98107
default:
@@ -114,9 +123,9 @@ StmtResult Sema::ActOnOpenACCAssociatedStmt(OpenACCDirectiveKind K,
114123
llvm_unreachable("Invalid associated statement application");
115124
}
116125

117-
bool Sema::ActOnStartOpenACCDeclDirective(OpenACCDirectiveKind K,
126+
bool SemaOpenACC::ActOnStartDeclDirective(OpenACCDirectiveKind K,
118127
SourceLocation StartLoc) {
119128
return diagnoseConstructAppertainment(*this, K, StartLoc, /*IsStmt=*/false);
120129
}
121130

122-
DeclGroupRef Sema::ActOnEndOpenACCDeclDirective() { return DeclGroupRef{}; }
131+
DeclGroupRef SemaOpenACC::ActOnEndDeclDirective() { return DeclGroupRef{}; }

0 commit comments

Comments
 (0)