Skip to content

Commit b291d02

Browse files
committed
[OpenACC][NFCI] Add extra data to firstprivate recipe AST node
During implementation I found that I need some additional data in the AST node for codegen, so this patch adds the second declaration reference.
1 parent 25bf86f commit b291d02

File tree

10 files changed

+63
-33
lines changed

10 files changed

+63
-33
lines changed

clang/include/clang/AST/OpenACCClause.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -876,21 +876,32 @@ class OpenACCPrivateClause final
876876
}
877877
};
878878

879+
// A 'pair' to stand in for the recipe. RecipeDecl is the main declaration, and
880+
// InitFromTemporary is the 'temp' declaration we put in to be 'copied from'.
881+
struct OpenACCFirstPrivateRecipe {
882+
VarDecl *RecipeDecl, *InitFromTemporary;
883+
OpenACCFirstPrivateRecipe(VarDecl *R, VarDecl *T)
884+
: RecipeDecl(R), InitFromTemporary(T) {}
885+
OpenACCFirstPrivateRecipe(std::pair<VarDecl *, VarDecl *> p)
886+
: RecipeDecl(p.first), InitFromTemporary(p.second) {}
887+
};
888+
879889
class OpenACCFirstPrivateClause final
880890
: public OpenACCClauseWithVarList,
881891
private llvm::TrailingObjects<OpenACCFirstPrivateClause, Expr *,
882-
VarDecl *> {
892+
OpenACCFirstPrivateRecipe> {
883893
friend TrailingObjects;
884894

885895
OpenACCFirstPrivateClause(SourceLocation BeginLoc, SourceLocation LParenLoc,
886896
ArrayRef<Expr *> VarList,
887-
ArrayRef<VarDecl *> InitRecipes,
897+
ArrayRef<OpenACCFirstPrivateRecipe> InitRecipes,
888898
SourceLocation EndLoc)
889899
: OpenACCClauseWithVarList(OpenACCClauseKind::FirstPrivate, BeginLoc,
890900
LParenLoc, EndLoc) {
891901
assert(VarList.size() == InitRecipes.size());
892902
setExprs(getTrailingObjects<Expr *>(VarList.size()), VarList);
893-
llvm::uninitialized_copy(InitRecipes, getTrailingObjects<VarDecl *>());
903+
llvm::uninitialized_copy(InitRecipes,
904+
getTrailingObjects<OpenACCFirstPrivateRecipe>());
894905
}
895906

896907
public:
@@ -900,19 +911,20 @@ class OpenACCFirstPrivateClause final
900911

901912
// Gets a list of 'made up' `VarDecl` objects that can be used by codegen to
902913
// ensure that we properly initialize each of these variables.
903-
ArrayRef<VarDecl *> getInitRecipes() {
904-
return ArrayRef<VarDecl *>{getTrailingObjects<VarDecl *>(),
905-
getExprs().size()};
914+
ArrayRef<OpenACCFirstPrivateRecipe> getInitRecipes() {
915+
return ArrayRef<OpenACCFirstPrivateRecipe>{
916+
getTrailingObjects<OpenACCFirstPrivateRecipe>(), getExprs().size()};
906917
}
907918

908-
ArrayRef<VarDecl *> getInitRecipes() const {
909-
return ArrayRef<VarDecl *>{getTrailingObjects<VarDecl *>(),
910-
getExprs().size()};
919+
ArrayRef<OpenACCFirstPrivateRecipe> getInitRecipes() const {
920+
return ArrayRef<OpenACCFirstPrivateRecipe>{
921+
getTrailingObjects<OpenACCFirstPrivateRecipe>(), getExprs().size()};
911922
}
912923

913924
static OpenACCFirstPrivateClause *
914925
Create(const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
915-
ArrayRef<Expr *> VarList, ArrayRef<VarDecl *> InitRecipes,
926+
ArrayRef<Expr *> VarList,
927+
ArrayRef<OpenACCFirstPrivateRecipe> InitRecipes,
916928
SourceLocation EndLoc);
917929

918930
size_t numTrailingObjects(OverloadToken<Expr *>) const {

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ class SemaOpenACC : public SemaBase {
240240
// Creates a VarDecl with a proper default init for the purposes of a
241241
// `private`/'firstprivate'/'reduction' clause, so it can be used to generate
242242
// a recipe later.
243-
VarDecl *CreateInitRecipe(OpenACCClauseKind CK, const Expr *VarExpr);
243+
// The first entry is the recipe itself, the second is any required
244+
// 'temporary' created for the init (in the case of a copy), such as with
245+
// firstprivate.
246+
std::pair<VarDecl *, VarDecl *> CreateInitRecipe(OpenACCClauseKind CK,
247+
const Expr *VarExpr);
244248

245249
public:
246250
ComputeConstructInfo &getActiveComputeConstructInfo() {

clang/lib/AST/OpenACCClause.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,11 @@ OpenACCPrivateClause::Create(const ASTContext &C, SourceLocation BeginLoc,
329329

330330
OpenACCFirstPrivateClause *OpenACCFirstPrivateClause::Create(
331331
const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
332-
ArrayRef<Expr *> VarList, ArrayRef<VarDecl *> InitRecipes,
332+
ArrayRef<Expr *> VarList, ArrayRef<OpenACCFirstPrivateRecipe> InitRecipes,
333333
SourceLocation EndLoc) {
334-
void *Mem =
335-
C.Allocate(OpenACCFirstPrivateClause::totalSizeToAlloc<Expr *, VarDecl *>(
334+
void *Mem = C.Allocate(
335+
OpenACCFirstPrivateClause::totalSizeToAlloc<Expr *,
336+
OpenACCFirstPrivateRecipe>(
336337
VarList.size(), InitRecipes.size()));
337338
return new (Mem) OpenACCFirstPrivateClause(BeginLoc, LParenLoc, VarList,
338339
InitRecipes, EndLoc);

clang/lib/AST/StmtProfile.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,8 +2645,10 @@ void OpenACCClauseProfiler::VisitFirstPrivateClause(
26452645
const OpenACCFirstPrivateClause &Clause) {
26462646
VisitClauseWithVarList(Clause);
26472647

2648-
for (auto *VD : Clause.getInitRecipes())
2649-
Profiler.VisitDecl(VD);
2648+
for (auto &Recipe : Clause.getInitRecipes()) {
2649+
Profiler.VisitDecl(Recipe.RecipeDecl);
2650+
Profiler.VisitDecl(Recipe.InitFromTemporary);
2651+
}
26502652
}
26512653

26522654
void OpenACCClauseProfiler::VisitAttachClause(

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,8 +2575,8 @@ SemaOpenACC::ActOnOpenACCAsteriskSizeExpr(SourceLocation AsteriskLoc) {
25752575
return BuildOpenACCAsteriskSizeExpr(AsteriskLoc);
25762576
}
25772577

2578-
VarDecl *SemaOpenACC::CreateInitRecipe(OpenACCClauseKind CK,
2579-
const Expr *VarExpr) {
2578+
std::pair<VarDecl *, VarDecl *>
2579+
SemaOpenACC::CreateInitRecipe(OpenACCClauseKind CK, const Expr *VarExpr) {
25802580
// Strip off any array subscripts/array section exprs to get to the type of
25812581
// the variable.
25822582
while (isa_and_present<ArraySectionExpr, ArraySubscriptExpr>(VarExpr)) {
@@ -2590,7 +2590,7 @@ VarDecl *SemaOpenACC::CreateInitRecipe(OpenACCClauseKind CK,
25902590
// fill in with nullptr. We'll count on TreeTransform to make this if
25912591
// necessary.
25922592
if (!VarExpr || VarExpr->getType()->isDependentType())
2593-
return nullptr;
2593+
return {nullptr, nullptr};
25942594

25952595
QualType VarTy =
25962596
VarExpr->getType().getNonReferenceType().getUnqualifiedType();
@@ -2602,6 +2602,7 @@ VarDecl *SemaOpenACC::CreateInitRecipe(OpenACCClauseKind CK,
26022602
getASTContext().getTrivialTypeSourceInfo(VarTy), SC_Auto);
26032603

26042604
ExprResult Init;
2605+
VarDecl *Temporary = nullptr;
26052606

26062607
if (CK == OpenACCClauseKind::Private) {
26072608
// Trap errors so we don't get weird ones here. If we can't init, we'll just
@@ -2626,5 +2627,5 @@ VarDecl *SemaOpenACC::CreateInitRecipe(OpenACCClauseKind CK,
26262627
Recipe->setInitStyle(VarDecl::CallInit);
26272628
}
26282629

2629-
return Recipe;
2630+
return {Recipe, Temporary};
26302631
}

clang/lib/Sema/SemaOpenACCClause.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ OpenACCClause *SemaOpenACCClauseVisitor::VisitPrivateClause(
800800
// Assemble the recipes list.
801801
for (const Expr *VarExpr : Clause.getVarList())
802802
InitRecipes.push_back(
803-
SemaRef.CreateInitRecipe(OpenACCClauseKind::Private, VarExpr));
803+
SemaRef.CreateInitRecipe(OpenACCClauseKind::Private, VarExpr).first);
804804

805805
return OpenACCPrivateClause::Create(
806806
Ctx, Clause.getBeginLoc(), Clause.getLParenLoc(), Clause.getVarList(),
@@ -813,7 +813,7 @@ OpenACCClause *SemaOpenACCClauseVisitor::VisitFirstPrivateClause(
813813
// really isn't anything to do here. GCC does some duplicate-finding, though
814814
// it isn't apparent in the standard where this is justified.
815815

816-
llvm::SmallVector<VarDecl *> InitRecipes;
816+
llvm::SmallVector<OpenACCFirstPrivateRecipe> InitRecipes;
817817

818818
// Assemble the recipes list.
819819
for (const Expr *VarExpr : Clause.getVarList())

clang/lib/Sema/TreeTransform.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11901,8 +11901,11 @@ void OpenACCClauseTransform<Derived>::VisitPrivateClause(
1190111901
if (InitRecipe)
1190211902
InitRecipes.push_back(InitRecipe);
1190311903
else
11904-
InitRecipes.push_back(Self.getSema().OpenACC().CreateInitRecipe(
11905-
OpenACCClauseKind::Private, VarRef.get()));
11904+
InitRecipes.push_back(
11905+
Self.getSema()
11906+
.OpenACC()
11907+
.CreateInitRecipe(OpenACCClauseKind::Private, VarRef.get())
11908+
.first);
1190611909
}
1190711910
}
1190811911
ParsedClause.setVarListDetails(InstantiatedVarList,
@@ -11942,7 +11945,7 @@ template <typename Derived>
1194211945
void OpenACCClauseTransform<Derived>::VisitFirstPrivateClause(
1194311946
const OpenACCFirstPrivateClause &C) {
1194411947
llvm::SmallVector<Expr *> InstantiatedVarList;
11945-
llvm::SmallVector<VarDecl *> InitRecipes;
11948+
llvm::SmallVector<OpenACCFirstPrivateRecipe> InitRecipes;
1194611949

1194711950
for (const auto [RefExpr, InitRecipe] :
1194811951
llvm::zip(C.getVarList(), C.getInitRecipes())) {
@@ -11953,7 +11956,7 @@ void OpenACCClauseTransform<Derived>::VisitFirstPrivateClause(
1195311956

1195411957
// We only have to create a new one if it is dependent, and Sema won't
1195511958
// make one of these unless the type is non-dependent.
11956-
if (InitRecipe)
11959+
if (InitRecipe.RecipeDecl)
1195711960
InitRecipes.push_back(InitRecipe);
1195811961
else
1195911962
InitRecipes.push_back(Self.getSema().OpenACC().CreateInitRecipe(

clang/lib/Serialization/ASTReader.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12877,9 +12877,12 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1287712877
case OpenACCClauseKind::FirstPrivate: {
1287812878
SourceLocation LParenLoc = readSourceLocation();
1287912879
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
12880-
llvm::SmallVector<VarDecl *> RecipeList;
12881-
for (unsigned I = 0; I < VarList.size(); ++I)
12882-
RecipeList.push_back(readDeclAs<VarDecl>());
12880+
llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
12881+
for (unsigned I = 0; I < VarList.size(); ++I) {
12882+
VarDecl *Recipe = readDeclAs<VarDecl>();
12883+
VarDecl *RecipeTemp = readDeclAs<VarDecl>();
12884+
RecipeList.push_back({Recipe, RecipeTemp});
12885+
}
1288312886

1288412887
return OpenACCFirstPrivateClause::Create(getContext(), BeginLoc, LParenLoc,
1288512888
VarList, RecipeList, EndLoc);

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8762,8 +8762,10 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
87628762
writeSourceLocation(FPC->getLParenLoc());
87638763
writeOpenACCVarList(FPC);
87648764

8765-
for (VarDecl *VD : FPC->getInitRecipes())
8766-
AddDeclRef(VD);
8765+
for (const OpenACCFirstPrivateRecipe &R : FPC->getInitRecipes()) {
8766+
AddDeclRef(R.RecipeDecl);
8767+
AddDeclRef(R.InitFromTemporary);
8768+
}
87678769
return;
87688770
}
87698771
case OpenACCClauseKind::Attach: {

clang/tools/libclang/CIndex.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,8 +2895,10 @@ void OpenACCClauseEnqueue::VisitDeviceClause(const OpenACCDeviceClause &C) {
28952895
void OpenACCClauseEnqueue::VisitFirstPrivateClause(
28962896
const OpenACCFirstPrivateClause &C) {
28972897
VisitVarList(C);
2898-
for (VarDecl *V : C.getInitRecipes())
2899-
Visitor.AddDecl(V);
2898+
for (const OpenACCFirstPrivateRecipe &R : C.getInitRecipes()) {
2899+
Visitor.AddDecl(R.RecipeDecl);
2900+
Visitor.AddDecl(R.InitFromTemporary);
2901+
}
29002902
}
29012903

29022904
void OpenACCClauseEnqueue::VisitPresentClause(const OpenACCPresentClause &C) {

0 commit comments

Comments
 (0)