Skip to content

Commit 819f34a

Browse files
authored
[NFC][OpenACC] Remove 'initExpr' from AST/etc. (#161674)
I originally expected that we were going to need the initExpr stored separately from the allocaDecl when doing arrays/pointers, however after implementing it, we found that the idea of having the allocaDecl just store its init directly still works perfectly. This patch removes the extra field from the AST.
1 parent 847e1e1 commit 819f34a

File tree

9 files changed

+62
-78
lines changed

9 files changed

+62
-78
lines changed

clang/include/clang/AST/OpenACCClause.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -840,14 +840,13 @@ class OpenACCClauseWithVarList : public OpenACCClauseWithExprs {
840840
// alloca at the level of the base, and the init at the element level.
841841
struct OpenACCPrivateRecipe {
842842
VarDecl *AllocaDecl;
843-
Expr *InitExpr;
844843

845-
OpenACCPrivateRecipe(VarDecl *A, Expr *I) : AllocaDecl(A), InitExpr(I) {}
844+
OpenACCPrivateRecipe(VarDecl *A) : AllocaDecl(A) {}
846845

847846
bool isSet() const { return AllocaDecl; }
848847

849848
static OpenACCPrivateRecipe Empty() {
850-
return OpenACCPrivateRecipe(nullptr, nullptr);
849+
return OpenACCPrivateRecipe(/*AllocaDecl=*/nullptr);
851850
}
852851
};
853852

@@ -899,18 +898,17 @@ class OpenACCPrivateClause final
899898
// InitFromTemporary is the 'temp' declaration we put in to be 'copied from'.
900899
struct OpenACCFirstPrivateRecipe {
901900
VarDecl *AllocaDecl;
902-
Expr *InitExpr;
903901
VarDecl *InitFromTemporary;
904-
OpenACCFirstPrivateRecipe(VarDecl *A, Expr *I, VarDecl *T)
905-
: AllocaDecl(A), InitExpr(I), InitFromTemporary(T) {
906-
assert(!AllocaDecl || AllocaDecl->getInit() == nullptr);
902+
OpenACCFirstPrivateRecipe(VarDecl *A, VarDecl *T)
903+
: AllocaDecl(A), InitFromTemporary(T) {
907904
assert(!InitFromTemporary || InitFromTemporary->getInit() == nullptr);
908905
}
909906

910907
bool isSet() const { return AllocaDecl; }
911908

912909
static OpenACCFirstPrivateRecipe Empty() {
913-
return OpenACCFirstPrivateRecipe(nullptr, nullptr, nullptr);
910+
return OpenACCFirstPrivateRecipe(/*AllocaDecl=*/nullptr,
911+
/*InitFromTemporary=*/nullptr);
914912
}
915913
};
916914

@@ -1282,16 +1280,13 @@ class OpenACCCreateClause final
12821280
// 'main' declaration used for initializaiton, which is fixed.
12831281
struct OpenACCReductionRecipe {
12841282
VarDecl *AllocaDecl;
1285-
Expr *InitExpr;
12861283
// TODO: OpenACC: this should eventually have the operations here too.
12871284

1288-
OpenACCReductionRecipe(VarDecl *A, Expr *I) : AllocaDecl(A), InitExpr(I) {
1289-
assert(!AllocaDecl || AllocaDecl->getInit() == nullptr);
1290-
}
1285+
OpenACCReductionRecipe(VarDecl *A) : AllocaDecl(A) {}
12911286

12921287
bool isSet() const { return AllocaDecl; }
12931288
static OpenACCReductionRecipe Empty() {
1294-
return OpenACCReductionRecipe(nullptr, nullptr);
1289+
return OpenACCReductionRecipe(/*AllocaDecl=*/nullptr);
12951290
}
12961291
};
12971292

clang/lib/AST/StmtProfile.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,8 +2655,6 @@ void OpenACCClauseProfiler::VisitPrivateClause(
26552655

26562656
for (auto &Recipe : Clause.getInitRecipes()) {
26572657
Profiler.VisitDecl(Recipe.AllocaDecl);
2658-
if (Recipe.InitExpr)
2659-
Profiler.VisitExpr(Recipe.InitExpr);
26602658
}
26612659
}
26622660

@@ -2666,8 +2664,6 @@ void OpenACCClauseProfiler::VisitFirstPrivateClause(
26662664

26672665
for (auto &Recipe : Clause.getInitRecipes()) {
26682666
Profiler.VisitDecl(Recipe.AllocaDecl);
2669-
if (Recipe.InitExpr)
2670-
Profiler.VisitExpr(Recipe.InitExpr);
26712667
Profiler.VisitDecl(Recipe.InitFromTemporary);
26722668
}
26732669
}
@@ -2773,12 +2769,10 @@ void OpenACCClauseProfiler::VisitReductionClause(
27732769

27742770
for (auto &Recipe : Clause.getRecipes()) {
27752771
Profiler.VisitDecl(Recipe.AllocaDecl);
2776-
if (Recipe.InitExpr)
2777-
Profiler.VisitExpr(Recipe.InitExpr);
27782772
// TODO: OpenACC: Make sure we remember to update this when we figure out
27792773
// what we're adding for the operation recipe, in the meantime, a static
27802774
// assert will make sure we don't add something.
2781-
static_assert(sizeof(OpenACCReductionRecipe) == 2 * sizeof(int *));
2775+
static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
27822776
}
27832777
}
27842778

clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ class OpenACCClauseCIREmitter final
10011001
OpenACCRecipeBuilder<mlir::acc::PrivateRecipeOp>(cgf, builder)
10021002
.getOrCreateRecipe(
10031003
cgf.getContext(), recipeInsertLocation, varExpr,
1004-
varRecipe.AllocaDecl, varRecipe.InitExpr,
1004+
varRecipe.AllocaDecl,
10051005
/*temporary=*/nullptr, OpenACCReductionOperator::Invalid,
10061006
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
10071007
opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,
@@ -1036,20 +1036,13 @@ class OpenACCClauseCIREmitter final
10361036

10371037
{
10381038
mlir::OpBuilder::InsertionGuard guardCase(builder);
1039-
// TODO: OpenACC: At the moment this is a bit of a hacky way of doing
1040-
// this, and won't work when we get to bounds/etc. Do this for now to
1041-
// limit the scope of this refactor.
1042-
VarDecl *allocaDecl = varRecipe.AllocaDecl;
1043-
allocaDecl->setInit(varRecipe.InitExpr);
1044-
allocaDecl->setInitStyle(VarDecl::CallInit);
10451039

10461040
auto recipe =
10471041
OpenACCRecipeBuilder<mlir::acc::FirstprivateRecipeOp>(cgf,
10481042
builder)
10491043
.getOrCreateRecipe(
10501044
cgf.getContext(), recipeInsertLocation, varExpr,
1051-
varRecipe.AllocaDecl, varRecipe.InitExpr,
1052-
varRecipe.InitFromTemporary,
1045+
varRecipe.AllocaDecl, varRecipe.InitFromTemporary,
10531046
OpenACCReductionOperator::Invalid,
10541047
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
10551048
opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,
@@ -1086,18 +1079,12 @@ class OpenACCClauseCIREmitter final
10861079

10871080
{
10881081
mlir::OpBuilder::InsertionGuard guardCase(builder);
1089-
// TODO: OpenACC: At the moment this is a bit of a hacky way of doing
1090-
// this, and won't work when we get to bounds/etc. Do this for now to
1091-
// limit the scope of this refactor.
1092-
VarDecl *allocaDecl = varRecipe.AllocaDecl;
1093-
allocaDecl->setInit(varRecipe.InitExpr);
1094-
allocaDecl->setInitStyle(VarDecl::CallInit);
10951082

10961083
auto recipe =
10971084
OpenACCRecipeBuilder<mlir::acc::ReductionRecipeOp>(cgf, builder)
10981085
.getOrCreateRecipe(
10991086
cgf.getContext(), recipeInsertLocation, varExpr,
1100-
varRecipe.AllocaDecl, varRecipe.InitExpr,
1087+
varRecipe.AllocaDecl,
11011088
/*temporary=*/nullptr, clause.getReductionOp(),
11021089
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
11031090
opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,

clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ void OpenACCRecipeBuilderBase::createPrivateInitRecipe(
435435
mlir::Location loc, mlir::Location locEnd, SourceRange exprRange,
436436
mlir::Value mainOp, mlir::acc::PrivateRecipeOp recipe, size_t numBounds,
437437
llvm::ArrayRef<QualType> boundTypes, const VarDecl *allocaDecl,
438-
QualType origType, const Expr *initExpr) {
438+
QualType origType) {
439439
assert(allocaDecl && "Required recipe variable not set?");
440440
CIRGenFunction::DeclMapRevertingRAII declMapRAII{cgf, allocaDecl};
441441

@@ -473,9 +473,10 @@ void OpenACCRecipeBuilderBase::createPrivateInitRecipe(
473473

474474
// If the initializer is trivial, there is nothing to do here, so save
475475
// ourselves some effort.
476-
if (initExpr && (!cgf.isTrivialInitializer(initExpr) ||
477-
cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
478-
LangOptions::TrivialAutoVarInitKind::Uninitialized))
476+
if (allocaDecl->getInit() &&
477+
(!cgf.isTrivialInitializer(allocaDecl->getInit()) ||
478+
cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
479+
LangOptions::TrivialAutoVarInitKind::Uninitialized))
479480
makeBoundsInit(alloca, loc, block, allocaDecl, origType,
480481
/*isInitSection=*/true);
481482
}

clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ class OpenACCRecipeBuilderBase {
7070
mlir::acc::PrivateRecipeOp recipe,
7171
size_t numBounds,
7272
llvm::ArrayRef<QualType> boundTypes,
73-
const VarDecl *allocaDecl, QualType origType,
74-
const Expr *initExpr);
73+
const VarDecl *allocaDecl, QualType origType);
7574

7675
void createRecipeDestroySection(mlir::Location loc, mlir::Location locEnd,
7776
mlir::Value mainOp, CharUnits alignment,
@@ -212,15 +211,12 @@ class OpenACCRecipeBuilder : OpenACCRecipeBuilderBase {
212211
OpenACCRecipeBuilder(CIRGen::CIRGenFunction &cgf,
213212
CIRGen::CIRGenBuilderTy &builder)
214213
: OpenACCRecipeBuilderBase(cgf, builder) {}
215-
RecipeTy getOrCreateRecipe(ASTContext &astCtx,
216-
mlir::OpBuilder::InsertPoint &insertLocation,
217-
const Expr *varRef, const VarDecl *varRecipe,
218-
const Expr *initExpr, const VarDecl *temporary,
219-
OpenACCReductionOperator reductionOp,
220-
DeclContext *dc, QualType origType,
221-
size_t numBounds,
222-
llvm::ArrayRef<QualType> boundTypes,
223-
QualType baseType, mlir::Value mainOp) {
214+
RecipeTy getOrCreateRecipe(
215+
ASTContext &astCtx, mlir::OpBuilder::InsertPoint &insertLocation,
216+
const Expr *varRef, const VarDecl *varRecipe, const VarDecl *temporary,
217+
OpenACCReductionOperator reductionOp, DeclContext *dc, QualType origType,
218+
size_t numBounds, llvm::ArrayRef<QualType> boundTypes, QualType baseType,
219+
mlir::Value mainOp) {
224220
assert(!varRecipe->getType()->isSpecificBuiltinType(
225221
BuiltinType::ArraySection) &&
226222
"array section shouldn't make it to recipe creation");
@@ -266,7 +262,7 @@ class OpenACCRecipeBuilder : OpenACCRecipeBuilderBase {
266262
if constexpr (std::is_same_v<RecipeTy, mlir::acc::PrivateRecipeOp>) {
267263
createPrivateInitRecipe(loc, locEnd, varRef->getSourceRange(), mainOp,
268264
recipe, numBounds, boundTypes, varRecipe,
269-
origType, initExpr);
265+
origType);
270266
} else {
271267
createRecipeInitCopy(loc, locEnd, varRef->getSourceRange(), mainOp,
272268
recipe, varRecipe, temporary);

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,7 +2789,7 @@ OpenACCPrivateRecipe SemaOpenACC::CreatePrivateInitRecipe(const Expr *VarExpr) {
27892789
AllocaDecl->setInitStyle(VarDecl::CallInit);
27902790
}
27912791

2792-
return OpenACCPrivateRecipe(AllocaDecl, Init.get());
2792+
return OpenACCPrivateRecipe(AllocaDecl);
27932793
}
27942794

27952795
OpenACCFirstPrivateRecipe
@@ -2828,7 +2828,14 @@ SemaOpenACC::CreateFirstPrivateInitRecipe(const Expr *VarExpr) {
28282828
if (!ArrTy) {
28292829
ExprResult Init = FinishValueInit(
28302830
SemaRef.SemaRef, Entity, VarExpr->getBeginLoc(), VarTy, TemporaryDRE);
2831-
return OpenACCFirstPrivateRecipe(AllocaDecl, Init.get(), Temporary);
2831+
2832+
// For 'no bounds' version, we can use this as a shortcut, so set the init
2833+
// anyway.
2834+
if (Init.isUsable()) {
2835+
AllocaDecl->setInit(Init.get());
2836+
AllocaDecl->setInitStyle(VarDecl::CallInit);
2837+
}
2838+
return OpenACCFirstPrivateRecipe(AllocaDecl, Temporary);
28322839
}
28332840

28342841
// Arrays need to have each individual element initialized as there
@@ -2875,8 +2882,16 @@ SemaOpenACC::CreateFirstPrivateInitRecipe(const Expr *VarExpr) {
28752882
ExprResult Init = FinishValueInit(SemaRef.SemaRef, Entity,
28762883
VarExpr->getBeginLoc(), VarTy, InitExpr);
28772884

2878-
return OpenACCFirstPrivateRecipe(AllocaDecl, Init.get(), Temporary);
2885+
// For 'no bounds' version, we can use this as a shortcut, so set the init
2886+
// anyway.
2887+
if (Init.isUsable()) {
2888+
AllocaDecl->setInit(Init.get());
2889+
AllocaDecl->setInitStyle(VarDecl::CallInit);
2890+
}
2891+
2892+
return OpenACCFirstPrivateRecipe(AllocaDecl, Temporary);
28792893
}
2894+
28802895
OpenACCReductionRecipe SemaOpenACC::CreateReductionInitRecipe(
28812896
OpenACCReductionOperator ReductionOperator, const Expr *VarExpr) {
28822897
// TODO: OpenACC: This shouldn't be necessary, see PrivateInitRecipe
@@ -2932,5 +2947,12 @@ OpenACCReductionRecipe SemaOpenACC::CreateReductionInitRecipe(
29322947

29332948
ExprResult Init = FinishValueInit(SemaRef.SemaRef, Entity,
29342949
VarExpr->getBeginLoc(), VarTy, InitExpr);
2935-
return OpenACCReductionRecipe(AllocaDecl, Init.get());
2950+
2951+
// For 'no bounds' version, we can use this as a shortcut, so set the init
2952+
// anyway.
2953+
if (Init.isUsable()) {
2954+
AllocaDecl->setInit(Init.get());
2955+
AllocaDecl->setInitStyle(VarDecl::CallInit);
2956+
}
2957+
return OpenACCReductionRecipe(AllocaDecl);
29362958
}

clang/lib/Serialization/ASTReader.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12860,10 +12860,9 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1286012860

1286112861
llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
1286212862
for (unsigned I = 0; I < VarList.size(); ++I) {
12863-
static_assert(sizeof(OpenACCPrivateRecipe) == 2 * sizeof(int *));
12863+
static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
1286412864
VarDecl *Alloca = readDeclAs<VarDecl>();
12865-
Expr *InitExpr = readSubExpr();
12866-
RecipeList.push_back({Alloca, InitExpr});
12865+
RecipeList.push_back({Alloca});
1286712866
}
1286812867

1286912868
return OpenACCPrivateClause::Create(getContext(), BeginLoc, LParenLoc,
@@ -12886,11 +12885,10 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1288612885
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
1288712886
llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
1288812887
for (unsigned I = 0; I < VarList.size(); ++I) {
12889-
static_assert(sizeof(OpenACCFirstPrivateRecipe) == 3 * sizeof(int *));
12888+
static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
1289012889
VarDecl *Recipe = readDeclAs<VarDecl>();
12891-
Expr *InitExpr = readSubExpr();
1289212890
VarDecl *RecipeTemp = readDeclAs<VarDecl>();
12893-
RecipeList.push_back({Recipe, InitExpr, RecipeTemp});
12891+
RecipeList.push_back({Recipe, RecipeTemp});
1289412892
}
1289512893

1289612894
return OpenACCFirstPrivateClause::Create(getContext(), BeginLoc, LParenLoc,
@@ -13011,10 +13009,9 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1301113009
llvm::SmallVector<OpenACCReductionRecipe> RecipeList;
1301213010

1301313011
for (unsigned I = 0; I < VarList.size(); ++I) {
13014-
static_assert(sizeof(OpenACCReductionRecipe) == 2 * sizeof(int *));
13012+
static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
1301513013
VarDecl *Recipe = readDeclAs<VarDecl>();
13016-
Expr *InitExpr = readSubExpr();
13017-
RecipeList.push_back({Recipe, InitExpr});
13014+
RecipeList.push_back({Recipe});
1301813015
}
1301913016

1302013017
return OpenACCReductionClause::Create(getContext(), BeginLoc, LParenLoc, Op,

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8779,9 +8779,8 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
87798779
writeOpenACCVarList(PC);
87808780

87818781
for (const OpenACCPrivateRecipe &R : PC->getInitRecipes()) {
8782-
static_assert(sizeof(R) == 2 * sizeof(int *));
8782+
static_assert(sizeof(R) == 1 * sizeof(int *));
87838783
AddDeclRef(R.AllocaDecl);
8784-
AddStmt(const_cast<Expr *>(R.InitExpr));
87858784
}
87868785
return;
87878786
}
@@ -8803,9 +8802,8 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
88038802
writeOpenACCVarList(FPC);
88048803

88058804
for (const OpenACCFirstPrivateRecipe &R : FPC->getInitRecipes()) {
8806-
static_assert(sizeof(R) == 3 * sizeof(int *));
8805+
static_assert(sizeof(R) == 2 * sizeof(int *));
88078806
AddDeclRef(R.AllocaDecl);
8808-
AddStmt(const_cast<Expr *>(R.InitExpr));
88098807
AddDeclRef(R.InitFromTemporary);
88108808
}
88118809
return;
@@ -8927,9 +8925,8 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
89278925
writeOpenACCVarList(RC);
89288926

89298927
for (const OpenACCReductionRecipe &R : RC->getRecipes()) {
8930-
static_assert(sizeof(OpenACCReductionRecipe) == 2 * sizeof(int *));
8928+
static_assert(sizeof(OpenACCReductionRecipe) == 1 * sizeof(int *));
89318929
AddDeclRef(R.AllocaDecl);
8932-
AddStmt(const_cast<Expr *>(R.InitExpr));
89338930
}
89348931
return;
89358932
}

clang/tools/libclang/CIndex.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,10 +2832,8 @@ void OpenACCClauseEnqueue::VisitTileClause(const OpenACCTileClause &C) {
28322832

28332833
void OpenACCClauseEnqueue::VisitPrivateClause(const OpenACCPrivateClause &C) {
28342834
VisitVarList(C);
2835-
for (const OpenACCPrivateRecipe &R : C.getInitRecipes()) {
2835+
for (const OpenACCPrivateRecipe &R : C.getInitRecipes())
28362836
Visitor.AddDecl(R.AllocaDecl);
2837-
Visitor.AddStmt(R.InitExpr);
2838-
}
28392837
}
28402838

28412839
void OpenACCClauseEnqueue::VisitHostClause(const OpenACCHostClause &C) {
@@ -2851,7 +2849,6 @@ void OpenACCClauseEnqueue::VisitFirstPrivateClause(
28512849
VisitVarList(C);
28522850
for (const OpenACCFirstPrivateRecipe &R : C.getInitRecipes()) {
28532851
Visitor.AddDecl(R.AllocaDecl);
2854-
Visitor.AddStmt(R.InitExpr);
28552852
Visitor.AddDecl(R.InitFromTemporary);
28562853
}
28572854
}
@@ -2927,10 +2924,8 @@ void OpenACCClauseEnqueue::VisitDeviceTypeClause(
29272924
void OpenACCClauseEnqueue::VisitReductionClause(
29282925
const OpenACCReductionClause &C) {
29292926
VisitVarList(C);
2930-
for (const OpenACCReductionRecipe &R : C.getRecipes()) {
2927+
for (const OpenACCReductionRecipe &R : C.getRecipes())
29312928
Visitor.AddDecl(R.AllocaDecl);
2932-
Visitor.AddStmt(R.InitExpr);
2933-
}
29342929
}
29352930
void OpenACCClauseEnqueue::VisitAutoClause(const OpenACCAutoClause &C) {}
29362931
void OpenACCClauseEnqueue::VisitIndependentClause(

0 commit comments

Comments
 (0)