@@ -15109,6 +15109,8 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
15109
15109
SourceLocation StartLoc,
15110
15110
SourceLocation EndLoc) {
15111
15111
ASTContext &Context = getASTContext();
15112
+ Scope *CurScope = SemaRef.getCurScope();
15113
+
15112
15114
auto SizesClauses =
15113
15115
OMPExecutableDirective::getClausesOfKind<OMPSizesClause>(Clauses);
15114
15116
if (SizesClauses.empty()) {
@@ -15137,6 +15139,7 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
15137
15139
NumLoops, AStmt, nullptr, nullptr);
15138
15140
15139
15141
SmallVector<Decl *, 4> PreInits;
15142
+ CaptureVars CopyTransformer(SemaRef);
15140
15143
15141
15144
// Create iteration variables for the generated loops.
15142
15145
SmallVector<VarDecl *, 4> FloorIndVars;
@@ -15200,30 +15203,42 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
15200
15203
Expr *NumIterations = LoopHelper.NumIterations;
15201
15204
auto *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]);
15202
15205
QualType CntTy = OrigCntVar->getType();
15203
- Expr *DimTileSize = SizesClause->getSizesRefs()[I];
15204
- Scope *CurScope = SemaRef.getCurScope();
15205
15206
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
+ };
15211
15225
15212
15226
// 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);
15216
15230
Decl *CounterDecl = TileIndVars[I];
15217
15231
StmtResult InitStmt = new (Context)
15218
15232
DeclStmt(DeclGroupRef::Create(Context, &CounterDecl, 1),
15219
15233
OrigCntVar->getBeginLoc(), OrigCntVar->getEndLoc());
15220
15234
if (!InitStmt.isUsable())
15221
15235
return StmtError();
15222
15236
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());
15227
15242
if (!EndOfTile.isUsable())
15228
15243
return StmtError();
15229
15244
ExprResult IsPartialTile =
@@ -15238,25 +15253,28 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
15238
15253
return StmtError();
15239
15254
ExprResult CondExpr =
15240
15255
SemaRef.BuildBinOp(CurScope, LoopHelper.Cond->getExprLoc(), BO_LT,
15241
- TileIV , MinTileAndIterSpace.get());
15256
+ MakeTileIVRef() , MinTileAndIterSpace.get());
15242
15257
if (!CondExpr.isUsable())
15243
15258
return StmtError();
15244
15259
15245
15260
// For incr-statement: ++.tile.iv
15246
15261
ExprResult IncrStmt = SemaRef.BuildUnaryOp(
15247
- CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, TileIV );
15262
+ CurScope, LoopHelper.Inc->getExprLoc(), UO_PreInc, MakeTileIVRef() );
15248
15263
if (!IncrStmt.isUsable())
15249
15264
return StmtError();
15250
15265
15251
15266
// Statements to set the original iteration variable's value from the
15252
15267
// logical iteration number.
15253
15268
// Generated for loop is:
15269
+ // \code
15254
15270
// 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) {
15257
15274
// Original_Body;
15258
15275
// Original_counter_update;
15259
15276
// }
15277
+ // \endcode
15260
15278
// FIXME: If the innermost body is an loop itself, inserting these
15261
15279
// statements stops it being recognized as a perfectly nested loop (e.g.
15262
15280
// for applying tiling again). If this is the case, sink the expressions
@@ -15278,12 +15296,18 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
15278
15296
Expr *NumIterations = LoopHelper.NumIterations;
15279
15297
DeclRefExpr *OrigCntVar = cast<DeclRefExpr>(LoopHelper.Counters[0]);
15280
15298
QualType CntTy = OrigCntVar->getType();
15281
- Expr *DimTileSize = SizesClause->getSizesRefs()[I];
15282
- Scope *CurScope = SemaRef.getCurScope();
15283
15299
15284
15300
// 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
+ };
15287
15311
15288
15312
// For init-statement: auto .floor.iv = 0
15289
15313
SemaRef.AddInitializerToDecl(
@@ -15298,15 +15322,16 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
15298
15322
return StmtError();
15299
15323
15300
15324
// 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);
15303
15328
if (!CondExpr.isUsable())
15304
15329
return StmtError();
15305
15330
15306
15331
// For incr-statement: .floor.iv += DimTileSize
15307
15332
ExprResult IncrStmt =
15308
15333
SemaRef.BuildBinOp(CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign,
15309
- FloorIV, DimTileSize );
15334
+ MakeFloorIVRef(), MakeDimTileSize() );
15310
15335
if (!IncrStmt.isUsable())
15311
15336
return StmtError();
15312
15337
0 commit comments