Skip to content

Commit f313ef4

Browse files
committed
Flatten the AST so that we dont' get ASan issues
1 parent 9fbd5f4 commit f313ef4

File tree

7 files changed

+75
-30
lines changed

7 files changed

+75
-30
lines changed

clang/include/clang/AST/OpenACCClause.h

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ class OpenACCCreateClause final
12771277
};
12781278

12791279
// A structure to stand in for the recipe on a reduction. RecipeDecl is the
1280-
// 'main' declaration used for initializaiton, which is fixed.
1280+
// 'main' declaration used for initializaiton, which is fixed.
12811281
struct OpenACCReductionRecipe {
12821282
VarDecl *AllocaDecl;
12831283

@@ -1297,36 +1297,69 @@ struct OpenACCReductionRecipe {
12971297
// -For a struct without the operator, this will be 1 element per field, which
12981298
// should be the combiner for that element.
12991299
// -For an array of any of the above, it will be the above for the element.
1300-
llvm::SmallVector<CombinerRecipe, 1> CombinerRecipes;
1300+
// Note: These are necessarily stored in either Trailing Storage (when in the
1301+
// AST), or in a separate collection when being semantically analyzed.
1302+
llvm::ArrayRef<CombinerRecipe> CombinerRecipes;
13011303

13021304
OpenACCReductionRecipe(VarDecl *A, llvm::ArrayRef<CombinerRecipe> Combiners)
13031305
: AllocaDecl(A), CombinerRecipes(Combiners) {}
13041306

13051307
bool isSet() const { return AllocaDecl; }
1306-
static OpenACCReductionRecipe Empty() {
1307-
return OpenACCReductionRecipe(/*AllocaDecl=*/nullptr, {});
1308+
};
1309+
1310+
// A version of the above that is used for semantic analysis, at a time before
1311+
// the OpenACCReductionClause node has been created. This one has storage for
1312+
// the CombinerRecipe, since Trailing storage for it doesn't exist yet.
1313+
struct OpenACCReductionRecipeWithStorage : OpenACCReductionRecipe {
1314+
llvm::SmallVector<CombinerRecipe, 1> CombinerRecipeStorage;
1315+
1316+
OpenACCReductionRecipeWithStorage(VarDecl *A,
1317+
llvm::ArrayRef<CombinerRecipe> Combiners)
1318+
: OpenACCReductionRecipe(A, {}), CombinerRecipeStorage(Combiners) {
1319+
CombinerRecipes = CombinerRecipeStorage;
1320+
}
1321+
static OpenACCReductionRecipeWithStorage Empty() {
1322+
return OpenACCReductionRecipeWithStorage(/*AllocaDecl=*/nullptr, {});
13081323
}
13091324
};
13101325

13111326
class OpenACCReductionClause final
13121327
: public OpenACCClauseWithVarList,
13131328
private llvm::TrailingObjects<OpenACCReductionClause, Expr *,
1314-
OpenACCReductionRecipe> {
1329+
OpenACCReductionRecipe,
1330+
OpenACCReductionRecipe::CombinerRecipe> {
13151331
friend TrailingObjects;
13161332
OpenACCReductionOperator Op;
13171333

13181334
OpenACCReductionClause(SourceLocation BeginLoc, SourceLocation LParenLoc,
13191335
OpenACCReductionOperator Operator,
13201336
ArrayRef<Expr *> VarList,
1321-
ArrayRef<OpenACCReductionRecipe> Recipes,
1337+
ArrayRef<OpenACCReductionRecipeWithStorage> Recipes,
13221338
SourceLocation EndLoc)
13231339
: OpenACCClauseWithVarList(OpenACCClauseKind::Reduction, BeginLoc,
13241340
LParenLoc, EndLoc),
13251341
Op(Operator) {
1326-
assert(VarList.size() == Recipes.size());
1342+
assert(VarList.size() == Recipes.size());
13271343
setExprs(getTrailingObjects<Expr *>(VarList.size()), VarList);
1328-
llvm::uninitialized_copy(Recipes, getTrailingObjects<
1329-
OpenACCReductionRecipe > ());
1344+
1345+
// Since we're using trailing storage on this node to store the 'combiner'
1346+
// recipes of the Reduction Recipes (which have a 1:M relationship), we need
1347+
// to ensure we get the ArrayRef of each of our combiner 'correct'.
1348+
OpenACCReductionRecipe::CombinerRecipe *CurCombinerLoc =
1349+
getTrailingObjects<OpenACCReductionRecipe::CombinerRecipe>();
1350+
for (const auto &[Idx, R] : llvm::enumerate(Recipes)) {
1351+
1352+
// ArrayRef to the 'correct' data location in trailing storage.
1353+
llvm::MutableArrayRef<OpenACCReductionRecipe::CombinerRecipe>
1354+
NewCombiners{CurCombinerLoc, R.CombinerRecipes.size()};
1355+
CurCombinerLoc += R.CombinerRecipes.size();
1356+
1357+
llvm::uninitialized_copy(R.CombinerRecipes, NewCombiners.begin());
1358+
1359+
// Placement new into the correct location in trailng storage.
1360+
new (&getTrailingObjects<OpenACCReductionRecipe>()[Idx])
1361+
OpenACCReductionRecipe(R.AllocaDecl, NewCombiners);
1362+
}
13301363
}
13311364

13321365
public:
@@ -1347,13 +1380,17 @@ class OpenACCReductionClause final
13471380
static OpenACCReductionClause *
13481381
Create(const ASTContext &C, SourceLocation BeginLoc, SourceLocation LParenLoc,
13491382
OpenACCReductionOperator Operator, ArrayRef<Expr *> VarList,
1350-
ArrayRef<OpenACCReductionRecipe> Recipes, SourceLocation EndLoc);
1383+
ArrayRef<OpenACCReductionRecipeWithStorage> Recipes,
1384+
SourceLocation EndLoc);
13511385

13521386
OpenACCReductionOperator getReductionOp() const { return Op; }
13531387

13541388
size_t numTrailingObjects(OverloadToken<Expr *>) const {
13551389
return getExprs().size();
13561390
}
1391+
size_t numTrailingObjects(OverloadToken<OpenACCReductionRecipe>) const {
1392+
return getExprs().size();
1393+
}
13571394
};
13581395

13591396
class OpenACCLinkClause final

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class SemaOpenACC : public SemaBase {
245245

246246
OpenACCPrivateRecipe CreatePrivateInitRecipe(const Expr *VarExpr);
247247
OpenACCFirstPrivateRecipe CreateFirstPrivateInitRecipe(const Expr *VarExpr);
248-
OpenACCReductionRecipe
248+
OpenACCReductionRecipeWithStorage
249249
CreateReductionInitRecipe(OpenACCReductionOperator ReductionOperator,
250250
const Expr *VarExpr);
251251

@@ -951,12 +951,14 @@ class SemaOpenACC : public SemaBase {
951951
ArrayRef<Expr *> IntExprs, SourceLocation EndLoc);
952952
// Does the checking for a 'reduction ' clause that needs to be done in
953953
// dependent and not dependent cases.
954-
OpenACCClause *CheckReductionClause(
955-
ArrayRef<const OpenACCClause *> ExistingClauses,
956-
OpenACCDirectiveKind DirectiveKind, SourceLocation BeginLoc,
957-
SourceLocation LParenLoc, OpenACCReductionOperator ReductionOp,
958-
ArrayRef<Expr *> Vars, ArrayRef<OpenACCReductionRecipe> Recipes,
959-
SourceLocation EndLoc);
954+
OpenACCClause *
955+
CheckReductionClause(ArrayRef<const OpenACCClause *> ExistingClauses,
956+
OpenACCDirectiveKind DirectiveKind,
957+
SourceLocation BeginLoc, SourceLocation LParenLoc,
958+
OpenACCReductionOperator ReductionOp,
959+
ArrayRef<Expr *> Vars,
960+
ArrayRef<OpenACCReductionRecipeWithStorage> Recipes,
961+
SourceLocation EndLoc);
960962

961963
ExprResult BuildOpenACCAsteriskSizeExpr(SourceLocation AsteriskLoc);
962964
ExprResult ActOnOpenACCAsteriskSizeExpr(SourceLocation AsteriskLoc);

clang/lib/AST/OpenACCClause.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,17 @@ 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,
509+
ArrayRef<OpenACCReductionRecipeWithStorage> Recipes,
510510
SourceLocation EndLoc) {
511-
void *Mem = C.Allocate(
512-
OpenACCReductionClause::totalSizeToAlloc<Expr *, OpenACCReductionRecipe>(
513-
VarList.size(), Recipes.size()));
511+
size_t NumCombiners = llvm::accumulate(
512+
Recipes, 0, [](size_t Num, const OpenACCReductionRecipe &R) {
513+
return Num + R.CombinerRecipes.size();
514+
});
515+
516+
void *Mem = C.Allocate(OpenACCReductionClause::totalSizeToAlloc<
517+
Expr *, OpenACCReductionRecipe,
518+
OpenACCReductionRecipe::CombinerRecipe>(
519+
VarList.size(), Recipes.size(), NumCombiners));
514520
return new (Mem) OpenACCReductionClause(BeginLoc, LParenLoc, Operator,
515521
VarList, Recipes, EndLoc);
516522
}

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,12 +2883,12 @@ SemaOpenACC::CreateFirstPrivateInitRecipe(const Expr *VarExpr) {
28832883
return OpenACCFirstPrivateRecipe(AllocaDecl, Temporary);
28842884
}
28852885

2886-
OpenACCReductionRecipe SemaOpenACC::CreateReductionInitRecipe(
2886+
OpenACCReductionRecipeWithStorage SemaOpenACC::CreateReductionInitRecipe(
28872887
OpenACCReductionOperator ReductionOperator, const Expr *VarExpr) {
28882888
// We don't strip bounds here, so that we are doing our recipe init at the
28892889
// 'lowest' possible level. Codegen is going to have to do its own 'looping'.
28902890
if (!VarExpr || VarExpr->getType()->isDependentType())
2891-
return OpenACCReductionRecipe::Empty();
2891+
return OpenACCReductionRecipeWithStorage::Empty();
28922892

28932893
QualType VarTy =
28942894
VarExpr->getType().getNonReferenceType().getUnqualifiedType();
@@ -2905,7 +2905,7 @@ OpenACCReductionRecipe SemaOpenACC::CreateReductionInitRecipe(
29052905
// at any of the combiners.
29062906
if (CreateReductionCombinerRecipe(VarExpr->getBeginLoc(), ReductionOperator,
29072907
VarTy, CombinerRecipes))
2908-
return OpenACCReductionRecipe::Empty();
2908+
return OpenACCReductionRecipeWithStorage::Empty();
29092909

29102910
VarDecl *AllocaDecl = CreateAllocaDecl(
29112911
getASTContext(), SemaRef.getCurContext(), VarExpr->getBeginLoc(),
@@ -2956,7 +2956,7 @@ OpenACCReductionRecipe SemaOpenACC::CreateReductionInitRecipe(
29562956
AllocaDecl->setInitStyle(VarDecl::CallInit);
29572957
}
29582958

2959-
return OpenACCReductionRecipe(AllocaDecl, CombinerRecipes);
2959+
return OpenACCReductionRecipeWithStorage(AllocaDecl, CombinerRecipes);
29602960
}
29612961

29622962
bool SemaOpenACC::CreateReductionCombinerRecipe(

clang/lib/Sema/SemaOpenACCClause.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ OpenACCClause *SemaOpenACCClauseVisitor::VisitReductionClause(
17721772
}
17731773

17741774
SmallVector<Expr *> ValidVars;
1775-
SmallVector<OpenACCReductionRecipe> Recipes;
1775+
SmallVector<OpenACCReductionRecipeWithStorage> Recipes;
17761776

17771777
for (Expr *Var : Clause.getVarList()) {
17781778
ExprResult Res = SemaRef.CheckReductionVar(Clause.getDirectiveKind(),
@@ -2196,7 +2196,7 @@ OpenACCClause *SemaOpenACC::CheckReductionClause(
21962196
ArrayRef<const OpenACCClause *> ExistingClauses,
21972197
OpenACCDirectiveKind DirectiveKind, SourceLocation BeginLoc,
21982198
SourceLocation LParenLoc, OpenACCReductionOperator ReductionOp,
2199-
ArrayRef<Expr *> Vars, ArrayRef<OpenACCReductionRecipe> Recipes,
2199+
ArrayRef<Expr *> Vars, ArrayRef<OpenACCReductionRecipeWithStorage> Recipes,
22002200
SourceLocation EndLoc) {
22012201
if (DirectiveKind == OpenACCDirectiveKind::Loop ||
22022202
isOpenACCCombinedDirectiveKind(DirectiveKind)) {

clang/lib/Sema/TreeTransform.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12374,7 +12374,7 @@ void OpenACCClauseTransform<Derived>::VisitReductionClause(
1237412374
const OpenACCReductionClause &C) {
1237512375
SmallVector<Expr *> TransformedVars = VisitVarList(C.getVarList());
1237612376
SmallVector<Expr *> ValidVars;
12377-
llvm::SmallVector<OpenACCReductionRecipe> Recipes;
12377+
llvm::SmallVector<OpenACCReductionRecipeWithStorage> Recipes;
1237812378

1237912379
for (const auto [Var, OrigRecipe] :
1238012380
llvm::zip(TransformedVars, C.getRecipes())) {
@@ -12384,7 +12384,7 @@ void OpenACCClauseTransform<Derived>::VisitReductionClause(
1238412384
ValidVars.push_back(Res.get());
1238512385

1238612386
if (OrigRecipe.isSet())
12387-
Recipes.push_back(OrigRecipe);
12387+
Recipes.emplace_back(OrigRecipe.AllocaDecl, OrigRecipe.CombinerRecipes);
1238812388
else
1238912389
Recipes.push_back(Self.getSema().OpenACC().CreateReductionInitRecipe(
1239012390
C.getReductionOp(), Res.get()));

clang/lib/Serialization/ASTReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13006,7 +13006,7 @@ OpenACCClause *ASTRecordReader::readOpenACCClause() {
1300613006
SourceLocation LParenLoc = readSourceLocation();
1300713007
OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();
1300813008
llvm::SmallVector<Expr *> VarList = readOpenACCVarList();
13009-
llvm::SmallVector<OpenACCReductionRecipe> RecipeList;
13009+
llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;
1301013010

1301113011
for (unsigned I = 0; I < VarList.size(); ++I) {
1301213012
VarDecl *Recipe = readDeclAs<VarDecl>();

0 commit comments

Comments
 (0)