[clang] [NFC][OpenACC] Remove 'initExpr' from AST/etc. (PR #161674)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 2 07:21:18 PDT 2025
https://github.com/erichkeane created https://github.com/llvm/llvm-project/pull/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.
>From 5e319dad0174af97e7809c39bdf99b094923b362 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Wed, 1 Oct 2025 14:17:17 -0700
Subject: [PATCH] [NFC][OpenACC] Remove 'initExpr' from AST/etc.
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.
---
clang/include/clang/AST/OpenACCClause.h | 20 +++++--------
clang/lib/AST/StmtProfile.cpp | 8 +----
clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp | 19 ++----------
clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.cpp | 9 +++---
clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.h | 20 +++++--------
clang/lib/Sema/SemaOpenACC.cpp | 30 ++++++++++++++++---
clang/lib/Serialization/ASTReader.cpp | 15 ++++------
clang/lib/Serialization/ASTWriter.cpp | 9 ++----
clang/tools/libclang/CIndex.cpp | 9 ++----
9 files changed, 61 insertions(+), 78 deletions(-)
diff --git a/clang/include/clang/AST/OpenACCClause.h b/clang/include/clang/AST/OpenACCClause.h
index 5f06117d65a47..c67685d14981d 100644
--- a/clang/include/clang/AST/OpenACCClause.h
+++ b/clang/include/clang/AST/OpenACCClause.h
@@ -840,14 +840,13 @@ class OpenACCClauseWithVarList : public OpenACCClauseWithExprs {
// alloca at the level of the base, and the init at the element level.
struct OpenACCPrivateRecipe {
VarDecl *AllocaDecl;
- Expr *InitExpr;
- OpenACCPrivateRecipe(VarDecl *A, Expr *I) : AllocaDecl(A), InitExpr(I) {}
+ OpenACCPrivateRecipe(VarDecl *A) : AllocaDecl(A) {}
bool isSet() const { return AllocaDecl; }
static OpenACCPrivateRecipe Empty() {
- return OpenACCPrivateRecipe(nullptr, nullptr);
+ return OpenACCPrivateRecipe(nullptr);
}
};
@@ -899,18 +898,16 @@ class OpenACCPrivateClause final
// InitFromTemporary is the 'temp' declaration we put in to be 'copied from'.
struct OpenACCFirstPrivateRecipe {
VarDecl *AllocaDecl;
- Expr *InitExpr;
VarDecl *InitFromTemporary;
- OpenACCFirstPrivateRecipe(VarDecl *A, Expr *I, VarDecl *T)
- : AllocaDecl(A), InitExpr(I), InitFromTemporary(T) {
- assert(!AllocaDecl || AllocaDecl->getInit() == nullptr);
+ OpenACCFirstPrivateRecipe(VarDecl *A, VarDecl *T)
+ : AllocaDecl(A), InitFromTemporary(T) {
assert(!InitFromTemporary || InitFromTemporary->getInit() == nullptr);
}
bool isSet() const { return AllocaDecl; }
static OpenACCFirstPrivateRecipe Empty() {
- return OpenACCFirstPrivateRecipe(nullptr, nullptr, nullptr);
+ return OpenACCFirstPrivateRecipe(nullptr, nullptr);
}
};
@@ -1282,16 +1279,13 @@ class OpenACCCreateClause final
// 'main' declaration used for initializaiton, which is fixed.
struct OpenACCReductionRecipe {
VarDecl *AllocaDecl;
- Expr *InitExpr;
// TODO: OpenACC: this should eventually have the operations here too.
- OpenACCReductionRecipe(VarDecl *A, Expr *I) : AllocaDecl(A), InitExpr(I) {
- assert(!AllocaDecl || AllocaDecl->getInit() == nullptr);
- }
+ OpenACCReductionRecipe(VarDecl *A) : AllocaDecl(A) {}
bool isSet() const { return AllocaDecl; }
static OpenACCReductionRecipe Empty() {
- return OpenACCReductionRecipe(nullptr, nullptr);
+ return OpenACCReductionRecipe(nullptr);
}
};
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 589a156a2b6ea..f3b5478222488 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -2655,8 +2655,6 @@ void OpenACCClauseProfiler::VisitPrivateClause(
for (auto &Recipe : Clause.getInitRecipes()) {
Profiler.VisitDecl(Recipe.AllocaDecl);
- if (Recipe.InitExpr)
- Profiler.VisitExpr(Recipe.InitExpr);
}
}
@@ -2666,8 +2664,6 @@ void OpenACCClauseProfiler::VisitFirstPrivateClause(
for (auto &Recipe : Clause.getInitRecipes()) {
Profiler.VisitDecl(Recipe.AllocaDecl);
- if (Recipe.InitExpr)
- Profiler.VisitExpr(Recipe.InitExpr);
Profiler.VisitDecl(Recipe.InitFromTemporary);
}
}
@@ -2773,12 +2769,10 @@ void OpenACCClauseProfiler::VisitReductionClause(
for (auto &Recipe : Clause.getRecipes()) {
Profiler.VisitDecl(Recipe.AllocaDecl);
- if (Recipe.InitExpr)
- Profiler.VisitExpr(Recipe.InitExpr);
// TODO: OpenACC: Make sure we remember to update this when we figure out
// what we're adding for the operation recipe, in the meantime, a static
// assert will make sure we don't add something.
- static_assert(sizeof(OpenACCReductionRecipe) == 2 * sizeof(int *));
+ static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
}
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp
index f22f3e8845f8a..3d86f71b077e2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenACCClause.cpp
@@ -1001,7 +1001,7 @@ class OpenACCClauseCIREmitter final
OpenACCRecipeBuilder<mlir::acc::PrivateRecipeOp>(cgf, builder)
.getOrCreateRecipe(
cgf.getContext(), recipeInsertLocation, varExpr,
- varRecipe.AllocaDecl, varRecipe.InitExpr,
+ varRecipe.AllocaDecl,
/*temporary=*/nullptr, OpenACCReductionOperator::Invalid,
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,
@@ -1036,20 +1036,13 @@ class OpenACCClauseCIREmitter final
{
mlir::OpBuilder::InsertionGuard guardCase(builder);
- // TODO: OpenACC: At the moment this is a bit of a hacky way of doing
- // this, and won't work when we get to bounds/etc. Do this for now to
- // limit the scope of this refactor.
- VarDecl *allocaDecl = varRecipe.AllocaDecl;
- allocaDecl->setInit(varRecipe.InitExpr);
- allocaDecl->setInitStyle(VarDecl::CallInit);
auto recipe =
OpenACCRecipeBuilder<mlir::acc::FirstprivateRecipeOp>(cgf,
builder)
.getOrCreateRecipe(
cgf.getContext(), recipeInsertLocation, varExpr,
- varRecipe.AllocaDecl, varRecipe.InitExpr,
- varRecipe.InitFromTemporary,
+ varRecipe.AllocaDecl, varRecipe.InitFromTemporary,
OpenACCReductionOperator::Invalid,
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,
@@ -1086,18 +1079,12 @@ class OpenACCClauseCIREmitter final
{
mlir::OpBuilder::InsertionGuard guardCase(builder);
- // TODO: OpenACC: At the moment this is a bit of a hacky way of doing
- // this, and won't work when we get to bounds/etc. Do this for now to
- // limit the scope of this refactor.
- VarDecl *allocaDecl = varRecipe.AllocaDecl;
- allocaDecl->setInit(varRecipe.InitExpr);
- allocaDecl->setInitStyle(VarDecl::CallInit);
auto recipe =
OpenACCRecipeBuilder<mlir::acc::ReductionRecipeOp>(cgf, builder)
.getOrCreateRecipe(
cgf.getContext(), recipeInsertLocation, varExpr,
- varRecipe.AllocaDecl, varRecipe.InitExpr,
+ varRecipe.AllocaDecl,
/*temporary=*/nullptr, clause.getReductionOp(),
Decl::castToDeclContext(cgf.curFuncDecl), opInfo.origType,
opInfo.bounds.size(), opInfo.boundTypes, opInfo.baseType,
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.cpp b/clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.cpp
index f8e511e28faed..30b04a27ee050 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.cpp
@@ -435,7 +435,7 @@ void OpenACCRecipeBuilderBase::createPrivateInitRecipe(
mlir::Location loc, mlir::Location locEnd, SourceRange exprRange,
mlir::Value mainOp, mlir::acc::PrivateRecipeOp recipe, size_t numBounds,
llvm::ArrayRef<QualType> boundTypes, const VarDecl *allocaDecl,
- QualType origType, const Expr *initExpr) {
+ QualType origType) {
assert(allocaDecl && "Required recipe variable not set?");
CIRGenFunction::DeclMapRevertingRAII declMapRAII{cgf, allocaDecl};
@@ -473,9 +473,10 @@ void OpenACCRecipeBuilderBase::createPrivateInitRecipe(
// If the initializer is trivial, there is nothing to do here, so save
// ourselves some effort.
- if (initExpr && (!cgf.isTrivialInitializer(initExpr) ||
- cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
- LangOptions::TrivialAutoVarInitKind::Uninitialized))
+ if (allocaDecl->getInit() &&
+ (!cgf.isTrivialInitializer(allocaDecl->getInit()) ||
+ cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
+ LangOptions::TrivialAutoVarInitKind::Uninitialized))
makeBoundsInit(alloca, loc, block, allocaDecl, origType,
/*isInitSection=*/true);
}
diff --git a/clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.h b/clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.h
index 203eaffc96427..a05b0bdaf6774 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.h
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenACCRecipe.h
@@ -70,8 +70,7 @@ class OpenACCRecipeBuilderBase {
mlir::acc::PrivateRecipeOp recipe,
size_t numBounds,
llvm::ArrayRef<QualType> boundTypes,
- const VarDecl *allocaDecl, QualType origType,
- const Expr *initExpr);
+ const VarDecl *allocaDecl, QualType origType);
void createRecipeDestroySection(mlir::Location loc, mlir::Location locEnd,
mlir::Value mainOp, CharUnits alignment,
@@ -212,15 +211,12 @@ class OpenACCRecipeBuilder : OpenACCRecipeBuilderBase {
OpenACCRecipeBuilder(CIRGen::CIRGenFunction &cgf,
CIRGen::CIRGenBuilderTy &builder)
: OpenACCRecipeBuilderBase(cgf, builder) {}
- RecipeTy getOrCreateRecipe(ASTContext &astCtx,
- mlir::OpBuilder::InsertPoint &insertLocation,
- const Expr *varRef, const VarDecl *varRecipe,
- const Expr *initExpr, const VarDecl *temporary,
- OpenACCReductionOperator reductionOp,
- DeclContext *dc, QualType origType,
- size_t numBounds,
- llvm::ArrayRef<QualType> boundTypes,
- QualType baseType, mlir::Value mainOp) {
+ RecipeTy getOrCreateRecipe(
+ ASTContext &astCtx, mlir::OpBuilder::InsertPoint &insertLocation,
+ const Expr *varRef, const VarDecl *varRecipe, const VarDecl *temporary,
+ OpenACCReductionOperator reductionOp, DeclContext *dc, QualType origType,
+ size_t numBounds, llvm::ArrayRef<QualType> boundTypes, QualType baseType,
+ mlir::Value mainOp) {
assert(!varRecipe->getType()->isSpecificBuiltinType(
BuiltinType::ArraySection) &&
"array section shouldn't make it to recipe creation");
@@ -266,7 +262,7 @@ class OpenACCRecipeBuilder : OpenACCRecipeBuilderBase {
if constexpr (std::is_same_v<RecipeTy, mlir::acc::PrivateRecipeOp>) {
createPrivateInitRecipe(loc, locEnd, varRef->getSourceRange(), mainOp,
recipe, numBounds, boundTypes, varRecipe,
- origType, initExpr);
+ origType);
} else {
createRecipeInitCopy(loc, locEnd, varRef->getSourceRange(), mainOp,
recipe, varRecipe, temporary);
diff --git a/clang/lib/Sema/SemaOpenACC.cpp b/clang/lib/Sema/SemaOpenACC.cpp
index a64f207ca0231..9aaf7f463403d 100644
--- a/clang/lib/Sema/SemaOpenACC.cpp
+++ b/clang/lib/Sema/SemaOpenACC.cpp
@@ -2789,7 +2789,7 @@ OpenACCPrivateRecipe SemaOpenACC::CreatePrivateInitRecipe(const Expr *VarExpr) {
AllocaDecl->setInitStyle(VarDecl::CallInit);
}
- return OpenACCPrivateRecipe(AllocaDecl, Init.get());
+ return OpenACCPrivateRecipe(AllocaDecl);
}
OpenACCFirstPrivateRecipe
@@ -2828,7 +2828,14 @@ SemaOpenACC::CreateFirstPrivateInitRecipe(const Expr *VarExpr) {
if (!ArrTy) {
ExprResult Init = FinishValueInit(
SemaRef.SemaRef, Entity, VarExpr->getBeginLoc(), VarTy, TemporaryDRE);
- return OpenACCFirstPrivateRecipe(AllocaDecl, Init.get(), Temporary);
+
+ // For 'no bounds' version, we can use this as a shortcut, so set the init
+ // anyway.
+ if (Init.isUsable()) {
+ AllocaDecl->setInit(Init.get());
+ AllocaDecl->setInitStyle(VarDecl::CallInit);
+ }
+ return OpenACCFirstPrivateRecipe(AllocaDecl, Temporary);
}
// Arrays need to have each individual element initialized as there
@@ -2875,8 +2882,16 @@ SemaOpenACC::CreateFirstPrivateInitRecipe(const Expr *VarExpr) {
ExprResult Init = FinishValueInit(SemaRef.SemaRef, Entity,
VarExpr->getBeginLoc(), VarTy, InitExpr);
- return OpenACCFirstPrivateRecipe(AllocaDecl, Init.get(), Temporary);
+ // For 'no bounds' version, we can use this as a shortcut, so set the init
+ // anyway.
+ if (Init.isUsable()) {
+ AllocaDecl->setInit(Init.get());
+ AllocaDecl->setInitStyle(VarDecl::CallInit);
+ }
+
+ return OpenACCFirstPrivateRecipe(AllocaDecl, Temporary);
}
+
OpenACCReductionRecipe SemaOpenACC::CreateReductionInitRecipe(
OpenACCReductionOperator ReductionOperator, const Expr *VarExpr) {
// TODO: OpenACC: This shouldn't be necessary, see PrivateInitRecipe
@@ -2932,5 +2947,12 @@ OpenACCReductionRecipe SemaOpenACC::CreateReductionInitRecipe(
ExprResult Init = FinishValueInit(SemaRef.SemaRef, Entity,
VarExpr->getBeginLoc(), VarTy, InitExpr);
- return OpenACCReductionRecipe(AllocaDecl, Init.get());
+
+ // For 'no bounds' version, we can use this as a shortcut, so set the init
+ // anyway.
+ if (Init.isUsable()) {
+ AllocaDecl->setInit(Init.get());
+ AllocaDecl->setInitStyle(VarDecl::CallInit);
+ }
+ return OpenACCReductionRecipe(AllocaDecl);
}
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index c05e428a6fb39..6acf79acea111 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -12860,10 +12860,9 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;
for (unsigned I = 0; I < VarList.size(); ++I) {
- static_assert(sizeof(OpenACCPrivateRecipe) == 2 * sizeof(int *));
+ static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));
VarDecl *Alloca = readDeclAs<VarDecl>();
- Expr *InitExpr = readSubExpr();
- RecipeList.push_back({Alloca, InitExpr});
+ RecipeList.push_back({Alloca});
}
return OpenACCPrivateClause::Create(getContext(), BeginLoc, LParenLoc,
@@ -12886,11 +12885,10 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;
for (unsigned I = 0; I < VarList.size(); ++I) {
- static_assert(sizeof(OpenACCFirstPrivateRecipe) == 3 * sizeof(int *));
+ static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));
VarDecl *Recipe = readDeclAs<VarDecl>();
- Expr *InitExpr = readSubExpr();
VarDecl *RecipeTemp = readDeclAs<VarDecl>();
- RecipeList.push_back({Recipe, InitExpr, RecipeTemp});
+ RecipeList.push_back({Recipe, RecipeTemp});
}
return OpenACCFirstPrivateClause::Create(getContext(), BeginLoc, LParenLoc,
@@ -13011,10 +13009,9 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
llvm::SmallVector<OpenACCReductionRecipe> RecipeList;
for (unsigned I = 0; I < VarList.size(); ++I) {
- static_assert(sizeof(OpenACCReductionRecipe) == 2 * sizeof(int *));
+ static_assert(sizeof(OpenACCReductionRecipe) == sizeof(int *));
VarDecl *Recipe = readDeclAs<VarDecl>();
- Expr *InitExpr = readSubExpr();
- RecipeList.push_back({Recipe, InitExpr});
+ RecipeList.push_back({Recipe});
}
return OpenACCReductionClause::Create(getContext(), BeginLoc, LParenLoc, Op,
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index cdf95ba1c4ba5..09b1e58ef220c 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -8779,9 +8779,8 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
writeOpenACCVarList(PC);
for (const OpenACCPrivateRecipe &R : PC->getInitRecipes()) {
- static_assert(sizeof(R) == 2 * sizeof(int *));
+ static_assert(sizeof(R) == 1 * sizeof(int *));
AddDeclRef(R.AllocaDecl);
- AddStmt(const_cast<Expr *>(R.InitExpr));
}
return;
}
@@ -8803,9 +8802,8 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
writeOpenACCVarList(FPC);
for (const OpenACCFirstPrivateRecipe &R : FPC->getInitRecipes()) {
- static_assert(sizeof(R) == 3 * sizeof(int *));
+ static_assert(sizeof(R) == 2 * sizeof(int *));
AddDeclRef(R.AllocaDecl);
- AddStmt(const_cast<Expr *>(R.InitExpr));
AddDeclRef(R.InitFromTemporary);
}
return;
@@ -8927,9 +8925,8 @@ void ASTRecordWriter::writeOpenACCClause(const OpenACCClause *C) {
writeOpenACCVarList(RC);
for (const OpenACCReductionRecipe &R : RC->getRecipes()) {
- static_assert(sizeof(OpenACCReductionRecipe) == 2 * sizeof(int *));
+ static_assert(sizeof(OpenACCReductionRecipe) == 1 * sizeof(int *));
AddDeclRef(R.AllocaDecl);
- AddStmt(const_cast<Expr *>(R.InitExpr));
}
return;
}
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 30e2be758cd39..c39f337665a40 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -2832,10 +2832,8 @@ void OpenACCClauseEnqueue::VisitTileClause(const OpenACCTileClause &C) {
void OpenACCClauseEnqueue::VisitPrivateClause(const OpenACCPrivateClause &C) {
VisitVarList(C);
- for (const OpenACCPrivateRecipe &R : C.getInitRecipes()) {
+ for (const OpenACCPrivateRecipe &R : C.getInitRecipes())
Visitor.AddDecl(R.AllocaDecl);
- Visitor.AddStmt(R.InitExpr);
- }
}
void OpenACCClauseEnqueue::VisitHostClause(const OpenACCHostClause &C) {
@@ -2851,7 +2849,6 @@ void OpenACCClauseEnqueue::VisitFirstPrivateClause(
VisitVarList(C);
for (const OpenACCFirstPrivateRecipe &R : C.getInitRecipes()) {
Visitor.AddDecl(R.AllocaDecl);
- Visitor.AddStmt(R.InitExpr);
Visitor.AddDecl(R.InitFromTemporary);
}
}
@@ -2927,10 +2924,8 @@ void OpenACCClauseEnqueue::VisitDeviceTypeClause(
void OpenACCClauseEnqueue::VisitReductionClause(
const OpenACCReductionClause &C) {
VisitVarList(C);
- for (const OpenACCReductionRecipe &R : C.getRecipes()) {
+ for (const OpenACCReductionRecipe &R : C.getRecipes())
Visitor.AddDecl(R.AllocaDecl);
- Visitor.AddStmt(R.InitExpr);
- }
}
void OpenACCClauseEnqueue::VisitAutoClause(const OpenACCAutoClause &C) {}
void OpenACCClauseEnqueue::VisitIndependentClause(
More information about the cfe-commits
mailing list