Skip to content

Commit e76b257

Browse files
authored
[Clang][OpenMP][Tile] Ensure AST node uniqueness. (#91325)
One of the constraints of an AST is that every node object must appear at most once, hence we define lamdas that create a new AST node at every use.
1 parent 5e9401c commit e76b257

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15109,6 +15109,8 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
1510915109
SourceLocation StartLoc,
1511015110
SourceLocation EndLoc) {
1511115111
ASTContext &Context = getASTContext();
15112+
Scope *CurScope = SemaRef.getCurScope();
15113+
1511215114
auto SizesClauses =
1511315115
OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses);
1511415116
if (SizesClauses.empty()) {
@@ -15137,6 +15139,7 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
1513715139
NumLoops, AStmt, nullptr, nullptr);
1513815140

1513915141
SmallVector<Decl *, 4> PreInits;
15142+
CaptureVars CopyTransformer(SemaRef);
1514015143

1514115144
// Create iteration variables for the generated loops.
1514215145
SmallVector<VarDecl *, 4> FloorIndVars;
@@ -15200,30 +15203,42 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
1520015203
Expr *NumIterations = LoopHelper.NumIterations;
1520115204
auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]);
1520215205
QualType CntTy = OrigCntVar->getType();
15203-
Expr *DimTileSize = SizesClause->getSizesRefs()[I];
15204-
Scope *CurScope = SemaRef.getCurScope();
1520515206

15206-
// Commonly used variables.
15207-
DeclRefExpr *TileIV = buildDeclRefExpr(SemaRef, TileIndVars[I], CntTy,
15208-
OrigCntVar->getExprLoc());
15209-
DeclRefExpr *FloorIV = buildDeclRefExpr(SemaRef, FloorIndVars[I], CntTy,
15210-
OrigCntVar->getExprLoc());
15207+
// Commonly used variables. One of the constraints of an AST is that every
15208+
// node object must appear at most once, hence we define lamdas that create
15209+
// a new AST node at every use.
15210+
auto MakeDimTileSize = [&SemaRef = this->SemaRef, &CopyTransformer, I,
15211+
SizesClause]() -> Expr * {
15212+
Expr *DimTileSize = SizesClause->getSizesRefs()[I];
15213+
return AssertSuccess(CopyTransformer.TransformExpr(DimTileSize));
15214+
};
15215+
auto MakeTileIVRef = [&SemaRef = this->SemaRef, &TileIndVars, I, CntTy,
15216+
OrigCntVar]() {
15217+
return buildDeclRefExpr(SemaRef, TileIndVars[I], CntTy,
15218+
OrigCntVar->getExprLoc());
15219+
};
15220+
auto MakeFloorIVRef = [&SemaRef = this->SemaRef, &FloorIndVars, I, CntTy,
15221+
OrigCntVar]() {
15222+
return buildDeclRefExpr(SemaRef, FloorIndVars[I], CntTy,
15223+
OrigCntVar->getExprLoc());
15224+
};
1521115225

1521215226
// For init-statement: auto .tile.iv = .floor.iv
15213-
SemaRef.AddInitializerToDecl(TileIndVars[I],
15214-
SemaRef.DefaultLvalueConversion(FloorIV).get(),
15215-
/*DirectInit=*/false);
15227+
SemaRef.AddInitializerToDecl(
15228+
TileIndVars[I], SemaRef.DefaultLvalueConversion(MakeFloorIVRef()).get(),
15229+
/*DirectInit=*/false);
1521615230
Decl *CounterDecl = TileIndVars[I];
1521715231
StmtResult InitStmt = new (Context)
1521815232
DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1),
1521915233
OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
1522015234
if (!InitStmt.isUsable())
1522115235
return StmtError();
1522215236

15223-
// For cond-expression: .tile.iv < min(.floor.iv + DimTileSize,
15224-
// NumIterations)
15225-
ExprResult EndOfTile = SemaRef.BuildBinOp(
15226-
CurScope, LoopHelper.Cond->getExprLoc(), BO_Add, FloorIV, DimTileSize);
15237+
// For cond-expression:
15238+
// .tile.iv < min(.floor.iv + DimTileSize, NumIterations)
15239+
ExprResult EndOfTile =
15240+
SemaRef.BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_Add,
15241+
MakeFloorIVRef(), MakeDimTileSize());
1522715242
if (!EndOfTile.isUsable())
1522815243
return StmtError();
1522915244
ExprResult IsPartialTile =
@@ -15238,25 +15253,28 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
1523815253
return StmtError();
1523915254
ExprResult CondExpr =
1524015255
SemaRef.BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT,
15241-
TileIV, MinTileAndIterSpace.get());
15256+
MakeTileIVRef(), MinTileAndIterSpace.get());
1524215257
if (!CondExpr.isUsable())
1524315258
return StmtError();
1524415259

1524515260
// For incr-statement: ++.tile.iv
1524615261
ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15247-
CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV);
15262+
CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, MakeTileIVRef());
1524815263
if (!IncrStmt.isUsable())
1524915264
return StmtError();
1525015265

1525115266
// Statements to set the original iteration variable's value from the
1525215267
// logical iteration number.
1525315268
// Generated for loop is:
15269+
// \code
1525415270
// Original_for_init;
15255-
// for (auto .tile.iv = .floor.iv; .tile.iv < min(.floor.iv + DimTileSize,
15256-
// NumIterations); ++.tile.iv) {
15271+
// for (auto .tile.iv = .floor.iv;
15272+
// .tile.iv < min(.floor.iv + DimTileSize, NumIterations);
15273+
// ++.tile.iv) {
1525715274
// Original_Body;
1525815275
// Original_counter_update;
1525915276
// }
15277+
// \endcode
1526015278
// FIXME: If the innermost body is an loop itself, inserting these
1526115279
// statements stops it being recognized as a perfectly nested loop (e.g.
1526215280
// for applying tiling again). If this is the case, sink the expressions
@@ -15278,12 +15296,18 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
1527815296
Expr *NumIterations = LoopHelper.NumIterations;
1527915297
DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]);
1528015298
QualType CntTy = OrigCntVar->getType();
15281-
Expr *DimTileSize = SizesClause->getSizesRefs()[I];
15282-
Scope *CurScope = SemaRef.getCurScope();
1528315299

1528415300
// Commonly used variables.
15285-
DeclRefExpr *FloorIV = buildDeclRefExpr(SemaRef, FloorIndVars[I], CntTy,
15286-
OrigCntVar->getExprLoc());
15301+
auto MakeDimTileSize = [&SemaRef = this->SemaRef, &CopyTransformer, I,
15302+
SizesClause]() -> Expr * {
15303+
Expr *DimTileSize = SizesClause->getSizesRefs()[I];
15304+
return AssertSuccess(CopyTransformer.TransformExpr(DimTileSize));
15305+
};
15306+
auto MakeFloorIVRef = [&SemaRef = this->SemaRef, &FloorIndVars, I, CntTy,
15307+
OrigCntVar]() {
15308+
return buildDeclRefExpr(SemaRef, FloorIndVars[I], CntTy,
15309+
OrigCntVar->getExprLoc());
15310+
};
1528715311

1528815312
// For init-statement: auto .floor.iv = 0
1528915313
SemaRef.AddInitializerToDecl(
@@ -15298,15 +15322,16 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
1529815322
return StmtError();
1529915323

1530015324
// For cond-expression: .floor.iv < NumIterations
15301-
ExprResult CondExpr = SemaRef.BuildBinOp(
15302-
CurScope, LoopHelper.Cond->getExprLoc(), BO_LT, FloorIV, NumIterations);
15325+
ExprResult CondExpr =
15326+
SemaRef.BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT,
15327+
MakeFloorIVRef(), NumIterations);
1530315328
if (!CondExpr.isUsable())
1530415329
return StmtError();
1530515330

1530615331
// For incr-statement: .floor.iv += DimTileSize
1530715332
ExprResult IncrStmt =
1530815333
SemaRef.BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign,
15309-
FloorIV, DimTileSize);
15334+
MakeFloorIVRef(), MakeDimTileSize());
1531015335
if (!IncrStmt.isUsable())
1531115336
return StmtError();
1531215337

0 commit comments

Comments
 (0)