Skip to content

Commit d0dc379

Browse files
committed
[OpenACC][NFCI] Add AST Infrastructure for reduction recipes
This patch does the bare minimum to start setting up the reduction recipe support, including adding a type to the AST to store it. No real additional work is done, and a bunch of static_asserts are left around to allow us to do this properly.
1 parent 50a3368 commit d0dc379

File tree

9 files changed

+102
-20
lines changed

9 files changed

+102
-20
lines changed

clang/include/clang/AST/OpenACCClause.h

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,32 +1250,59 @@ class OpenACCCreateClause final
12501250
SourceLocation EndLoc);
12511251
};
12521252

1253+
// A structure to stand in for the recipe on a reduction. RecipeDecl is the
1254+
// 'main' declaration used for initializaiton, which is fixed.
1255+
struct OpenACCReductionRecipe {
1256+
VarDecl *RecipeDecl;
1257+
// TODO: OpenACC: this should eventually have the operations here too.
1258+
};
1259+
12531260
class OpenACCReductionClause final
12541261
: public OpenACCClauseWithVarList,
1255-
private llvm::TrailingObjects<OpenACCReductionClause, Expr *> {
1262+
private llvm::TrailingObjects<OpenACCReductionClause, Expr *,
1263+
OpenACCReductionRecipe> {
12561264
friend TrailingObjects;
12571265
OpenACCReductionOperator Op;
12581266

12591267
OpenACCReductionClause(SourceLocation BeginLoc, SourceLocation LParenLoc,
12601268
OpenACCReductionOperator Operator,
1261-
ArrayRef<Expr *> VarList, SourceLocation EndLoc)
1269+
ArrayRef<Expr *> VarList,
1270+
ArrayRef<OpenACCReductionRecipe> Recipes,
1271+
SourceLocation EndLoc)
12621272
: OpenACCClauseWithVarList(OpenACCClauseKind::Reduction, BeginLoc,
12631273
LParenLoc, EndLoc),
12641274
Op(Operator) {
1265-
setExprs(getTrailingObjects(VarList.size()), VarList);
1275+
assert(VarList.size() == Recipes.size());
1276+
setExprs(getTrailingObjects<Expr *>(VarList.size()), VarList);
1277+
llvm::uninitialized_copy(Recipes, getTrailingObjects<
1278+
OpenACCReductionRecipe > ());
12661279
}
12671280

12681281
public:
12691282
static bool classof(const OpenACCClause *C) {
12701283
return C->getClauseKind() == OpenACCClauseKind::Reduction;
12711284
}
12721285

1286+
ArrayRef<OpenACCReductionRecipe> getRecipes() {
1287+
return ArrayRef<OpenACCReductionRecipe>{
1288+
getTrailingObjects<OpenACCReductionRecipe>(), getExprs().size()};
1289+
}
1290+
1291+
ArrayRef<OpenACCReductionRecipe> getRecipes() const {
1292+
return ArrayRef<OpenACCReductionRecipe>{
1293+
getTrailingObjects<OpenACCReductionRecipe>(), getExprs().size()};
1294+
}
1295+
12731296
static OpenACCReductionClause *
12741297
Create(const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
12751298
OpenACCReductionOperator Operator, ArrayRef<Expr *> VarList,
1276-
SourceLocation EndLoc);
1299+
ArrayRef<OpenACCReductionRecipe> Recipes, SourceLocation EndLoc);
12771300

12781301
OpenACCReductionOperator getReductionOp() const { return Op; }
1302+
1303+
size_t numTrailingObjects(OverloadToken<Expr *>) const {
1304+
return getExprs().size();
1305+
}
12791306
};
12801307

12811308
class OpenACCLinkClause final

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -947,12 +947,12 @@ class SemaOpenACC : public SemaBase {
947947
ArrayRef<Expr *> IntExprs, SourceLocation EndLoc);
948948
// Does the checking for a 'reduction ' clause that needs to be done in
949949
// dependent and not dependent cases.
950-
OpenACCClause *
951-
CheckReductionClause(ArrayRef<const OpenACCClause *> ExistingClauses,
952-
OpenACCDirectiveKind DirectiveKind,
953-
SourceLocation BeginLoc, SourceLocation LParenLoc,
954-
OpenACCReductionOperator ReductionOp,
955-
ArrayRef<Expr *> Vars, SourceLocation EndLoc);
950+
OpenACCClause *CheckReductionClause(
951+
ArrayRef<const OpenACCClause *> ExistingClauses,
952+
OpenACCDirectiveKind DirectiveKind, SourceLocation BeginLoc,
953+
SourceLocation LParenLoc, OpenACCReductionOperator ReductionOp,
954+
ArrayRef<Expr *> Vars, ArrayRef<OpenACCReductionRecipe> Recipes,
955+
SourceLocation EndLoc);
956956

957957
ExprResult BuildOpenACCAsteriskSizeExpr(SourceLocation AsteriskLoc);
958958
ExprResult ActOnOpenACCAsteriskSizeExpr(SourceLocation AsteriskLoc);

clang/lib/AST/OpenACCClause.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,13 @@ OpenACCDeviceTypeClause *OpenACCDeviceTypeClause::Create(
506506
OpenACCReductionClause *OpenACCReductionClause::Create(
507507
const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
508508
OpenACCReductionOperator Operator, ArrayRef<Expr *> VarList,
509+
ArrayRef<OpenACCReductionRecipe> Recipes,
509510
SourceLocation EndLoc) {
510511
void *Mem = C.Allocate(
511-
OpenACCReductionClause::totalSizeToAlloc<Expr *>(VarList.size()));
512-
return new (Mem)
513-
OpenACCReductionClause(BeginLoc, LParenLoc, Operator, VarList, EndLoc);
512+
OpenACCReductionClause::totalSizeToAlloc<Expr *, OpenACCReductionRecipe>(
513+
VarList.size(), Recipes.size()));
514+
return new (Mem) OpenACCReductionClause(BeginLoc, LParenLoc, Operator,
515+
VarList, Recipes, EndLoc);
514516
}
515517

516518
OpenACCAutoClause *OpenACCAutoClause::Create(const ASTContext &C,

clang/lib/AST/StmtProfile.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,6 +2748,14 @@ void OpenACCClauseProfiler::VisitGangClause(const OpenACCGangClause &Clause) {
27482748
void OpenACCClauseProfiler::VisitReductionClause(
27492749
const OpenACCReductionClause &Clause) {
27502750
VisitClauseWithVarList(Clause);
2751+
2752+
for (auto &Recipe : Clause.getRecipes()) {
2753+
Profiler.VisitDecl(Recipe.RecipeDecl);
2754+
// TODO: OpenACC: Make sure we remember to update this when we figure out
2755+
// what we're adding for the operation recipe, in the meantime, a static
2756+
// assert will make sure we don't add something.
2757+
static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
2758+
}
27512759
}
27522760

27532761
void OpenACCClauseProfiler::VisitBindClause(const OpenACCBindClause &Clause) {

clang/lib/Sema/SemaOpenACCClause.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,18 +1774,27 @@ OpenACCClause *SemaOpenACCClauseVisitor::VisitReductionClause(
17741774
}
17751775

17761776
SmallVector<Expr *> ValidVars;
1777+
SmallVector<OpenACCReductionRecipe> Recipes;
17771778

17781779
for (Expr *Var : Clause.getVarList()) {
17791780
ExprResult Res = SemaRef.CheckReductionVar(Clause.getDirectiveKind(),
17801781
Clause.getReductionOp(), Var);
17811782

1782-
if (Res.isUsable())
1783+
if (Res.isUsable()) {
17831784
ValidVars.push_back(Res.get());
1785+
1786+
VarDecl *InitRecipe =
1787+
SemaRef.CreateInitRecipe(OpenACCClauseKind::Reduction, Res.get())
1788+
.first;
1789+
// TODO: OpenACC: Create the reduction operation recipe here too.
1790+
Recipes.push_back({InitRecipe});
1791+
}
17841792
}
17851793

17861794
return SemaRef.CheckReductionClause(
17871795
ExistingClauses, Clause.getDirectiveKind(), Clause.getBeginLoc(),
17881796
Clause.getLParenLoc(), Clause.getReductionOp(), ValidVars,
1797+
Recipes,
17891798
Clause.getEndLoc());
17901799
}
17911800

@@ -2158,7 +2167,8 @@ OpenACCClause *SemaOpenACC::CheckReductionClause(
21582167
ArrayRef<const OpenACCClause *> ExistingClauses,
21592168
OpenACCDirectiveKind DirectiveKind, SourceLocation BeginLoc,
21602169
SourceLocation LParenLoc, OpenACCReductionOperator ReductionOp,
2161-
ArrayRef<Expr *> Vars, SourceLocation EndLoc) {
2170+
ArrayRef<Expr *> Vars, ArrayRef<OpenACCReductionRecipe> Recipes,
2171+
SourceLocation EndLoc) {
21622172
if (DirectiveKind == OpenACCDirectiveKind::Loop ||
21632173
isOpenACCCombinedDirectiveKind(DirectiveKind)) {
21642174
// OpenACC 3.3 2.9.11: A reduction clause may not appear on a loop directive
@@ -2187,7 +2197,7 @@ OpenACCClause *SemaOpenACC::CheckReductionClause(
21872197
}
21882198

21892199
auto *Ret = OpenACCReductionClause::Create(
2190-
getASTContext(), BeginLoc, LParenLoc, ReductionOp, Vars, EndLoc);
2200+
getASTContext(), BeginLoc, LParenLoc, ReductionOp, Vars, Recipes, EndLoc);
21912201
return Ret;
21922202
}
21932203

clang/lib/Sema/TreeTransform.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12295,18 +12295,36 @@ void OpenACCClauseTransform<Derived>::VisitReductionClause(
1229512295
const OpenACCReductionClause &C) {
1229612296
SmallVector<Expr *> TransformedVars = VisitVarList(C.getVarList());
1229712297
SmallVector<Expr *> ValidVars;
12298+
llvm::SmallVector<OpenACCReductionRecipe> Recipes;
1229812299

12299-
for (Expr *Var : TransformedVars) {
12300+
for (const auto [Var, OrigRecipes] :
12301+
llvm::zip(TransformedVars, C.getRecipes())) {
1230012302
ExprResult Res = Self.getSema().OpenACC().CheckReductionVar(
1230112303
ParsedClause.getDirectiveKind(), C.getReductionOp(), Var);
12302-
if (Res.isUsable())
12304+
if (Res.isUsable()) {
1230312305
ValidVars.push_back(Res.get());
12306+
12307+
// TODO OpenACC: When the recipe changes, make sure we get these right
12308+
// too. We probably need something similar for the operation.
12309+
static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int*));
12310+
VarDecl *InitRecipe = nullptr;
12311+
if (OrigRecipes.RecipeDecl)
12312+
InitRecipe = OrigRecipes.RecipeDecl;
12313+
else
12314+
InitRecipe =
12315+
Self.getSema()
12316+
.OpenACC()
12317+
.CreateInitRecipe(OpenACCClauseKind::Reduction, Res.get())
12318+
.first;
12319+
12320+
Recipes.push_back({InitRecipe});
12321+
}
1230412322
}
1230512323

1230612324
NewClause = Self.getSema().OpenACC().CheckReductionClause(
1230712325
ExistingClauses, ParsedClause.getDirectiveKind(),
1230812326
ParsedClause.getBeginLoc(), ParsedClause.getLParenLoc(),
12309-
C.getReductionOp(), ValidVars, ParsedClause.getEndLoc());
12327+
C.getReductionOp(), ValidVars, Recipes, ParsedClause.getEndLoc());
1231012328
}
1231112329

1231212330
template <typename Derived>

clang/lib/Serialization/ASTReader.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12996,8 +12996,16 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1299612996
SourceLocation LParenLoc = readSourceLocation();
1299712997
OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
1299812998
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12999+
llvm::SmallVector<OpenACCReductionRecipe> RecipeList;
13000+
13001+
for (unsigned I = 0; I < VarList.size(); ++I) {
13002+
VarDecl *Recipe = readDeclAs<VarDecl>();
13003+
static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
13004+
RecipeList.push_back({Recipe});
13005+
}
13006+
1299913007
return OpenACCReductionClause::Create(getContext(), BeginLoc, LParenLoc, Op,
13000-
VarList, EndLoc);
13008+
VarList, RecipeList, EndLoc);
1300113009
}
1300213010
case OpenACCClauseKind::Seq:
1300313011
return OpenACCSeqClause::Create(getContext(), BeginLoc, EndLoc);

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8886,6 +8886,11 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
88868886
writeSourceLocation(RC->getLParenLoc());
88878887
writeEnum(RC->getReductionOp());
88888888
writeOpenACCVarList(RC);
8889+
8890+
for (const OpenACCReductionRecipe &R : RC->getRecipes()) {
8891+
static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
8892+
AddDeclRef(R.RecipeDecl);
8893+
}
88898894
return;
88908895
}
88918896
case OpenACCClauseKind::Seq:

clang/tools/libclang/CIndex.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2928,6 +2928,10 @@ void OpenACCClauseEnqueue::VisitDeviceTypeClause(
29282928
void OpenACCClauseEnqueue::VisitReductionClause(
29292929
const OpenACCReductionClause &C) {
29302930
VisitVarList(C);
2931+
for (const OpenACCReductionRecipe &R : C.getRecipes()) {
2932+
static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
2933+
Visitor.AddDecl(R.RecipeDecl);
2934+
}
29312935
}
29322936
void OpenACCClauseEnqueue::VisitAutoClause(const OpenACCAutoClause &C) {}
29332937
void OpenACCClauseEnqueue::VisitIndependentClause(

0 commit comments

Comments
 (0)