Skip to content

Commit 1a16bc1

Browse files
authored
[OpenACC][NFCI] Split up the init and decl from OpenACC recipes (llvm#156938)
Expressions/references with 'bounds' are going to need to do initialization significantly differently, so we need to have the initializer and the declaration 'separate' in the future. This patch splits the AST node into two, and normalizes them a bit. Additionally, since this required significant work on the recipe generation, this patch also does a bit of a refactor to improve readability and future expansion, now that we have a good understanding of how these are going to look.
1 parent 9337344 commit 1a16bc1

File tree

11 files changed

+338
-287
lines changed

11 files changed

+338
-287
lines changed

clang/include/clang/AST/OpenACCClause.h

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -835,19 +835,40 @@ class OpenACCClauseWithVarList : public OpenACCClauseWithExprs {
835835
ArrayRef<Expr *> getVarList() const { return getExprs(); }
836836
};
837837

838+
// Represents all the data needed for recipe generation. The declaration and
839+
// init are stored separately, because in the case of subscripts, we do the
840+
// alloca at the level of the base, and the init at the element level.
841+
struct OpenACCPrivateRecipe {
842+
VarDecl *AllocaDecl;
843+
Expr *InitExpr;
844+
845+
OpenACCPrivateRecipe(VarDecl *A, Expr *I) : AllocaDecl(A), InitExpr(I) {
846+
assert(!AllocaDecl || AllocaDecl->getInit() == nullptr);
847+
}
848+
849+
bool isSet() const { return AllocaDecl; }
850+
851+
static OpenACCPrivateRecipe Empty() {
852+
return OpenACCPrivateRecipe(nullptr, nullptr);
853+
}
854+
};
855+
838856
class OpenACCPrivateClause final
839857
: public OpenACCClauseWithVarList,
840-
private llvm::TrailingObjects<OpenACCPrivateClause, Expr *, VarDecl *> {
858+
private llvm::TrailingObjects<OpenACCPrivateClause, Expr *,
859+
OpenACCPrivateRecipe> {
841860
friend TrailingObjects;
842861

843862
OpenACCPrivateClause(SourceLocation BeginLoc, SourceLocation LParenLoc,
844863
ArrayRef<Expr *> VarList,
845-
ArrayRef<VarDecl *> InitRecipes, SourceLocation EndLoc)
864+
ArrayRef<OpenACCPrivateRecipe> InitRecipes,
865+
SourceLocation EndLoc)
846866
: OpenACCClauseWithVarList(OpenACCClauseKind::Private, BeginLoc,
847867
LParenLoc, EndLoc) {
848868
assert(VarList.size() == InitRecipes.size());
849869
setExprs(getTrailingObjects<Expr *>(VarList.size()), VarList);
850-
llvm::uninitialized_copy(InitRecipes, getTrailingObjects<VarDecl *>());
870+
llvm::uninitialized_copy(InitRecipes,
871+
getTrailingObjects<OpenACCPrivateRecipe>());
851872
}
852873

853874
public:
@@ -856,19 +877,19 @@ class OpenACCPrivateClause final
856877
}
857878
// Gets a list of 'made up' `VarDecl` objects that can be used by codegen to
858879
// ensure that we properly initialize each of these variables.
859-
ArrayRef<VarDecl *> getInitRecipes() {
860-
return ArrayRef<VarDecl *>{getTrailingObjects<VarDecl *>(),
861-
getExprs().size()};
880+
ArrayRef<OpenACCPrivateRecipe> getInitRecipes() {
881+
return ArrayRef<OpenACCPrivateRecipe>{
882+
getTrailingObjects<OpenACCPrivateRecipe>(), getExprs().size()};
862883
}
863884

864-
ArrayRef<VarDecl *> getInitRecipes() const {
865-
return ArrayRef<VarDecl *>{getTrailingObjects<VarDecl *>(),
866-
getExprs().size()};
885+
ArrayRef<OpenACCPrivateRecipe> getInitRecipes() const {
886+
return ArrayRef<OpenACCPrivateRecipe>{
887+
getTrailingObjects<OpenACCPrivateRecipe>(), getExprs().size()};
867888
}
868889

869890
static OpenACCPrivateClause *
870891
Create(const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
871-
ArrayRef<Expr *> VarList, ArrayRef<VarDecl *> InitRecipes,
892+
ArrayRef<Expr *> VarList, ArrayRef<OpenACCPrivateRecipe> InitRecipes,
872893
SourceLocation EndLoc);
873894

874895
size_t numTrailingObjects(OverloadToken<Expr *>) const {
@@ -879,11 +900,20 @@ class OpenACCPrivateClause final
879900
// A 'pair' to stand in for the recipe. RecipeDecl is the main declaration, and
880901
// InitFromTemporary is the 'temp' declaration we put in to be 'copied from'.
881902
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) {}
903+
VarDecl *AllocaDecl;
904+
Expr *InitExpr;
905+
VarDecl *InitFromTemporary;
906+
OpenACCFirstPrivateRecipe(VarDecl *A, Expr *I, VarDecl *T)
907+
: AllocaDecl(A), InitExpr(I), InitFromTemporary(T) {
908+
assert(!AllocaDecl || AllocaDecl->getInit() == nullptr);
909+
assert(!InitFromTemporary || InitFromTemporary->getInit() == nullptr);
910+
}
911+
912+
bool isSet() const { return AllocaDecl; }
913+
914+
static OpenACCFirstPrivateRecipe Empty() {
915+
return OpenACCFirstPrivateRecipe(nullptr, nullptr, nullptr);
916+
}
887917
};
888918

889919
class OpenACCFirstPrivateClause final
@@ -1253,8 +1283,18 @@ class OpenACCCreateClause final
12531283
// A structure to stand in for the recipe on a reduction. RecipeDecl is the
12541284
// 'main' declaration used for initializaiton, which is fixed.
12551285
struct OpenACCReductionRecipe {
1256-
VarDecl *RecipeDecl;
1286+
VarDecl *AllocaDecl;
1287+
Expr *InitExpr;
12571288
// TODO: OpenACC: this should eventually have the operations here too.
1289+
1290+
OpenACCReductionRecipe(VarDecl *A, Expr *I) : AllocaDecl(A), InitExpr(I) {
1291+
assert(!AllocaDecl || AllocaDecl->getInit() == nullptr);
1292+
}
1293+
1294+
bool isSet() const { return AllocaDecl; }
1295+
static OpenACCReductionRecipe Empty() {
1296+
return OpenACCReductionRecipe(nullptr, nullptr);
1297+
}
12581298
};
12591299

12601300
class OpenACCReductionClause final

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define LLVM_CLANG_SEMA_SEMAOPENACC_H
1616

1717
#include "clang/AST/DeclGroup.h"
18+
#include "clang/AST/OpenACCClause.h"
1819
#include "clang/AST/StmtOpenACC.h"
1920
#include "clang/Basic/LLVM.h"
2021
#include "clang/Basic/OpenACCKinds.h"
@@ -237,21 +238,11 @@ class SemaOpenACC : public SemaBase {
237238
SourceLocation ClauseLoc,
238239
ArrayRef<const OpenACCClause *> Clauses);
239240

240-
// Creates a VarDecl with a proper default init for the purposes of a
241-
// `private`/'firstprivate'/'reduction' clause, so it can be used to generate
242-
// a recipe later.
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) {
248-
assert(CK != OpenACCClauseKind::Reduction);
249-
return CreateInitRecipe(CK, OpenACCReductionOperator::Invalid, VarExpr);
250-
}
251-
std::pair<VarDecl *, VarDecl *>
252-
CreateInitRecipe(OpenACCClauseKind CK,
253-
OpenACCReductionOperator ReductionOperator,
254-
const Expr *VarExpr);
241+
OpenACCPrivateRecipe CreatePrivateInitRecipe(const Expr *VarExpr);
242+
OpenACCFirstPrivateRecipe CreateFirstPrivateInitRecipe(const Expr *VarExpr);
243+
OpenACCReductionRecipe
244+
CreateReductionInitRecipe(OpenACCReductionOperator ReductionOperator,
245+
const Expr *VarExpr);
255246

256247
public:
257248
ComputeConstructInfo &getActiveComputeConstructInfo() {

clang/lib/AST/OpenACCClause.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,11 @@ OpenACCTileClause *OpenACCTileClause::Create(const ASTContext &C,
317317
OpenACCPrivateClause *
318318
OpenACCPrivateClause::Create(const ASTContext &C, SourceLocation BeginLoc,
319319
SourceLocation LParenLoc, ArrayRef<Expr *> VarList,
320-
ArrayRef<VarDecl *> InitRecipes,
320+
ArrayRef<OpenACCPrivateRecipe> InitRecipes,
321321
SourceLocation EndLoc) {
322322
assert(VarList.size() == InitRecipes.size());
323-
void *Mem =
324-
C.Allocate(OpenACCPrivateClause::totalSizeToAlloc<Expr *, VarDecl *>(
323+
void *Mem = C.Allocate(
324+
OpenACCPrivateClause::totalSizeToAlloc<Expr *, OpenACCPrivateRecipe>(
325325
VarList.size(), InitRecipes.size()));
326326
return new (Mem)
327327
OpenACCPrivateClause(BeginLoc, LParenLoc, VarList, InitRecipes, EndLoc);

clang/lib/AST/StmtProfile.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,16 +2636,21 @@ void OpenACCClauseProfiler::VisitPrivateClause(
26362636
const OpenACCPrivateClause &Clause) {
26372637
VisitClauseWithVarList(Clause);
26382638

2639-
for (auto *VD : Clause.getInitRecipes())
2640-
Profiler.VisitDecl(VD);
2639+
for (auto &Recipe : Clause.getInitRecipes()) {
2640+
Profiler.VisitDecl(Recipe.AllocaDecl);
2641+
if (Recipe.InitExpr)
2642+
Profiler.VisitExpr(Recipe.InitExpr);
2643+
}
26412644
}
26422645

26432646
void OpenACCClauseProfiler::VisitFirstPrivateClause(
26442647
const OpenACCFirstPrivateClause &Clause) {
26452648
VisitClauseWithVarList(Clause);
26462649

26472650
for (auto &Recipe : Clause.getInitRecipes()) {
2648-
Profiler.VisitDecl(Recipe.RecipeDecl);
2651+
Profiler.VisitDecl(Recipe.AllocaDecl);
2652+
if (Recipe.InitExpr)
2653+
Profiler.VisitExpr(Recipe.InitExpr);
26492654
Profiler.VisitDecl(Recipe.InitFromTemporary);
26502655
}
26512656
}
@@ -2750,11 +2755,13 @@ void OpenACCClauseProfiler::VisitReductionClause(
27502755
VisitClauseWithVarList(Clause);
27512756

27522757
for (auto &Recipe : Clause.getRecipes()) {
2753-
Profiler.VisitDecl(Recipe.RecipeDecl);
2758+
Profiler.VisitDecl(Recipe.AllocaDecl);
2759+
if (Recipe.InitExpr)
2760+
Profiler.VisitExpr(Recipe.InitExpr);
27542761
// TODO: OpenACC: Make sure we remember to update this when we figure out
27552762
// what we're adding for the operation recipe, in the meantime, a static
27562763
// assert will make sure we don't add something.
2757-
static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
2764+
static_assert(sizeof(OpenACCReductionRecipe) == 2 * sizeof(int *));
27582765
}
27592766
}
27602767

clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,10 +1280,16 @@ class OpenACCClauseCIREmitter final
12801280

12811281
{
12821282
mlir::OpBuilder::InsertionGuard guardCase(builder);
1283+
// TODO: OpenACC: At the moment this is a bit of a hacky way of doing
1284+
// this, and won't work when we get to bounds/etc. Do this for now to
1285+
// limit the scope of this refactor.
1286+
VarDecl *allocaDecl = varRecipe.AllocaDecl;
1287+
allocaDecl->setInit(varRecipe.InitExpr);
1288+
allocaDecl->setInitStyle(VarDecl::CallInit);
1289+
12831290
auto recipe = getOrCreateRecipe<mlir::acc::PrivateRecipeOp>(
1284-
cgf.getContext(), varExpr, varRecipe, /*temporary=*/nullptr,
1291+
cgf.getContext(), varExpr, allocaDecl, /*temporary=*/nullptr,
12851292
OpenACCReductionOperator::Invalid,
1286-
12871293
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.baseType,
12881294
privateOp.getResult());
12891295
// TODO: OpenACC: The dialect is going to change in the near future to
@@ -1316,8 +1322,15 @@ class OpenACCClauseCIREmitter final
13161322

13171323
{
13181324
mlir::OpBuilder::InsertionGuard guardCase(builder);
1325+
// TODO: OpenACC: At the moment this is a bit of a hacky way of doing
1326+
// this, and won't work when we get to bounds/etc. Do this for now to
1327+
// limit the scope of this refactor.
1328+
VarDecl *allocaDecl = varRecipe.AllocaDecl;
1329+
allocaDecl->setInit(varRecipe.InitExpr);
1330+
allocaDecl->setInitStyle(VarDecl::CallInit);
1331+
13191332
auto recipe = getOrCreateRecipe<mlir::acc::FirstprivateRecipeOp>(
1320-
cgf.getContext(), varExpr, varRecipe.RecipeDecl,
1333+
cgf.getContext(), varExpr, allocaDecl,
13211334
varRecipe.InitFromTemporary, OpenACCReductionOperator::Invalid,
13221335
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.baseType,
13231336
firstPrivateOp.getResult());
@@ -1353,9 +1366,15 @@ class OpenACCClauseCIREmitter final
13531366

13541367
{
13551368
mlir::OpBuilder::InsertionGuard guardCase(builder);
1369+
// TODO: OpenACC: At the moment this is a bit of a hacky way of doing
1370+
// this, and won't work when we get to bounds/etc. Do this for now to
1371+
// limit the scope of this refactor.
1372+
VarDecl *allocaDecl = varRecipe.AllocaDecl;
1373+
allocaDecl->setInit(varRecipe.InitExpr);
1374+
allocaDecl->setInitStyle(VarDecl::CallInit);
13561375

13571376
auto recipe = getOrCreateRecipe<mlir::acc::ReductionRecipeOp>(
1358-
cgf.getContext(), varExpr, varRecipe.RecipeDecl,
1377+
cgf.getContext(), varExpr, allocaDecl,
13591378
/*temporary=*/nullptr, clause.getReductionOp(),
13601379
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.baseType,
13611380
reductionOp.getResult());

0 commit comments

Comments
 (0)