@@ -257,18 +257,17 @@ class OpenMPIRBuilder {
257
257
// /
258
258
// / * Sign of the step and the comparison operator might disagree:
259
259
// /
260
- // / for (int i = 0; i < 42; --i )
260
+ // / for (int i = 0; i < 42; i -= 1u )
261
261
// /
262
262
//
263
263
// / \param Loc The insert and source location description.
264
264
// / \param BodyGenCB Callback that will generate the loop body code.
265
265
// / \param Start Value of the loop counter for the first iterations.
266
- // / \param Stop Loop counter values past this will stop the the
267
- // / iterations.
266
+ // / \param Stop Loop counter values past this will stop the loop.
268
267
// / \param Step Loop counter increment after each iteration; negative
269
- // / means counting down. \param IsSigned Whether Start, Stop
270
- // / and Stop are signed integers.
271
- // / \param InclusiveStop Whether \p Stop itself is a valid value for the loop
268
+ // / means counting down.
269
+ // / \param IsSigned Whether Start, Stop and Step are signed integers.
270
+ // / \param InclusiveStop Whether \p Stop itself is a valid value for the loop
272
271
// / counter.
273
272
// / \param ComputeIP Insertion point for instructions computing the trip
274
273
// / count. Can be used to ensure the trip count is available
@@ -335,7 +334,7 @@ class OpenMPIRBuilder {
335
334
// / has a trip count of 0). This is permitted by the OpenMP specification.
336
335
// /
337
336
// / \param DL Debug location for instructions added for collapsing,
338
- // / such as instructions to compute derive the input loop's
337
+ // / such as instructions to compute/ derive the input loop's
339
338
// / induction variables.
340
339
// / \param Loops Loops in the loop nest to collapse. Loops are specified
341
340
// / from outermost-to-innermost and every control flow of a
@@ -358,8 +357,16 @@ class OpenMPIRBuilder {
358
357
// / the current thread, updates the relevant instructions in the canonical
359
358
// / loop and calls to an OpenMP runtime finalization function after the loop.
360
359
// /
361
- // / \param Loc The source location description, the insertion location
362
- // / is not used.
360
+ // / TODO: Workshare loops with static scheduling may contain up to two loops
361
+ // / that fulfill the requirements of an OpenMP canonical loop. One for
362
+ // / iterating over all iterations of a chunk and another one for iterating
363
+ // / over all chunks that are executed on the same thread. Returning
364
+ // / CanonicalLoopInfo objects representing them may eventually be useful for
365
+ // / the apply clause planned in OpenMP 6.0, but currently whether these are
366
+ // / canonical loops is irrelevant.
367
+ // /
368
+ // / \param DL Debug location for instructions added for the
369
+ // / workshare-loop construct itself.
363
370
// / \param CLI A descriptor of the canonical loop to workshare.
364
371
// / \param AllocaIP An insertion point for Alloca instructions usable in the
365
372
// / preheader of the loop.
@@ -368,12 +375,11 @@ class OpenMPIRBuilder {
368
375
// / \param Chunk The size of loop chunk considered as a unit when
369
376
// / scheduling. If \p nullptr, defaults to 1.
370
377
// /
371
- // / \returns Updated CanonicalLoopInfo.
372
- CanonicalLoopInfo *createStaticWorkshareLoop (const LocationDescription &Loc,
373
- CanonicalLoopInfo *CLI,
374
- InsertPointTy AllocaIP,
375
- bool NeedsBarrier,
376
- Value *Chunk = nullptr );
378
+ // / \returns Point where to insert code after the workshare construct.
379
+ InsertPointTy applyStaticWorkshareLoop (DebugLoc DL, CanonicalLoopInfo *CLI,
380
+ InsertPointTy AllocaIP,
381
+ bool NeedsBarrier,
382
+ Value *Chunk = nullptr );
377
383
378
384
// / Modifies the canonical loop to be a dynamically-scheduled workshare loop.
379
385
// /
@@ -382,8 +388,9 @@ class OpenMPIRBuilder {
382
388
// / turn it into a workshare loop. In particular, it calls to an OpenMP
383
389
// / runtime function in the preheader to obtain, and then in each iteration
384
390
// / to update the loop counter.
385
- // / \param Loc The source location description, the insertion location
386
- // / is not used.
391
+ // /
392
+ // / \param DL Debug location for instructions added for the
393
+ // / workshare-loop construct itself.
387
394
// / \param CLI A descriptor of the canonical loop to workshare.
388
395
// / \param AllocaIP An insertion point for Alloca instructions usable in the
389
396
// / preheader of the loop.
@@ -393,13 +400,12 @@ class OpenMPIRBuilder {
393
400
// / \param Chunk The size of loop chunk considered as a unit when
394
401
// / scheduling. If \p nullptr, defaults to 1.
395
402
// /
396
- // / \returns Point where to insert code after the loop.
397
- InsertPointTy createDynamicWorkshareLoop (const LocationDescription &Loc,
398
- CanonicalLoopInfo *CLI,
399
- InsertPointTy AllocaIP,
400
- omp::OMPScheduleType SchedType,
401
- bool NeedsBarrier,
402
- Value *Chunk = nullptr );
403
+ // / \returns Point where to insert code after the workshare construct.
404
+ InsertPointTy applyDynamicWorkshareLoop (DebugLoc DL, CanonicalLoopInfo *CLI,
405
+ InsertPointTy AllocaIP,
406
+ omp::OMPScheduleType SchedType,
407
+ bool NeedsBarrier,
408
+ Value *Chunk = nullptr );
403
409
404
410
// / Modifies the canonical loop to be a workshare loop.
405
411
// /
@@ -410,19 +416,17 @@ class OpenMPIRBuilder {
410
416
// / the current thread, updates the relevant instructions in the canonical
411
417
// / loop and calls to an OpenMP runtime finalization function after the loop.
412
418
// /
413
- // / \param Loc The source location description, the insertion location
414
- // / is not used .
419
+ // / \param DL Debug location for instructions added for the
420
+ // / workshare-loop construct itself .
415
421
// / \param CLI A descriptor of the canonical loop to workshare.
416
422
// / \param AllocaIP An insertion point for Alloca instructions usable in the
417
423
// / preheader of the loop.
418
424
// / \param NeedsBarrier Indicates whether a barrier must be insterted after
419
425
// / the loop.
420
426
// /
421
- // / \returns Updated CanonicalLoopInfo.
422
- CanonicalLoopInfo *createWorkshareLoop (const LocationDescription &Loc,
423
- CanonicalLoopInfo *CLI,
424
- InsertPointTy AllocaIP,
425
- bool NeedsBarrier);
427
+ // / \returns Point where to insert code after the workshare construct.
428
+ InsertPointTy applyWorkshareLoop (DebugLoc DL, CanonicalLoopInfo *CLI,
429
+ InsertPointTy AllocaIP, bool NeedsBarrier);
426
430
427
431
// / Tile a loop nest.
428
432
// /
@@ -637,6 +641,10 @@ class OpenMPIRBuilder {
637
641
Constant *getOrCreateSrcLocStr (StringRef FunctionName, StringRef FileName,
638
642
unsigned Line, unsigned Column);
639
643
644
+ // / Return the (LLVM-IR) string describing the DebugLoc \p DL. Use \p F as
645
+ // / fallback if \p DL does not specify the function name.
646
+ Constant *getOrCreateSrcLocStr (DebugLoc DL, Function *F = nullptr );
647
+
640
648
// / Return the (LLVM-IR) string describing the source location \p Loc.
641
649
Constant *getOrCreateSrcLocStr (const LocationDescription &Loc);
642
650
@@ -1193,7 +1201,25 @@ class OpenMPIRBuilder {
1193
1201
// / The control-flow structure is standardized for easy consumption by
1194
1202
// / directives associated with loops. For instance, the worksharing-loop
1195
1203
// / construct may change this control flow such that each loop iteration is
1196
- // / executed on only one thread.
1204
+ // / executed on only one thread. The constraints of a canonical loop in brief
1205
+ // / are:
1206
+ // /
1207
+ // / * The number of loop iterations must have been computed before entering the
1208
+ // / loop.
1209
+ // /
1210
+ // / * Has an (unsigned) logical induction variable that starts at zero and
1211
+ // / increments by one.
1212
+ // /
1213
+ // / * The loop's CFG itself has no side-effects. The OpenMP specification
1214
+ // / itself allows side-effects, but the order in which they happen, including
1215
+ // / how often or whether at all, is unspecified. We expect that the frontend
1216
+ // / will emit those side-effect instructions somewhere (e.g. before the loop)
1217
+ // / such that the CanonicalLoopInfo itself can be side-effect free.
1218
+ // /
1219
+ // / Keep in mind that CanonicalLoopInfo is meant to only describe a repeated
1220
+ // / execution of a loop body that satifies these constraints. It does NOT
1221
+ // / represent arbitrary SESE regions that happen to contain a loop. Do not use
1222
+ // / CanonicalLoopInfo for such purposes.
1197
1223
// /
1198
1224
// / The control flow can be described as follows:
1199
1225
// /
@@ -1213,73 +1239,149 @@ class OpenMPIRBuilder {
1213
1239
// / |
1214
1240
// / After
1215
1241
// /
1216
- // / Code in the header, condition block, latch and exit block must not have any
1217
- // / side-effect. The body block is the single entry point into the loop body,
1218
- // / which may contain arbitrary control flow as long as all control paths
1219
- // / eventually branch to the latch block.
1242
+ // / The loop is thought to start at PreheaderIP (at the Preheader's terminator,
1243
+ // / including) and end at AfterIP (at the After's first instruction, excluding).
1244
+ // / That is, instructions in the Preheader and After blocks (except the
1245
+ // / Preheader's terminator) are out of CanonicalLoopInfo's control and may have
1246
+ // / side-effects. Typically, the Preheader is used to compute the loop's trip
1247
+ // / count. The instructions from BodyIP (at the Body block's first instruction,
1248
+ // / excluding) until the Latch are also considered outside CanonicalLoopInfo's
1249
+ // / control and thus can have side-effects. The body block is the single entry
1250
+ // / point into the loop body, which may contain arbitrary control flow as long
1251
+ // / as all control paths eventually branch to the Latch block.
1252
+ // /
1253
+ // / TODO: Consider adding another standardized BasicBlock between Body CFG and
1254
+ // / Latch to guarantee that there is only a single edge to the latch. It would
1255
+ // / make loop transformations easier to not needing to consider multiple
1256
+ // / predecessors of the latch (See redirectAllPredecessorsTo) and would give us
1257
+ // / an equivalant to PreheaderIP, AfterIP and BodyIP for inserting code that
1258
+ // / executes after each body iteration.
1259
+ // /
1260
+ // / There must be no loop-carried dependencies through llvm::Values. This is
1261
+ // / equivalant to that the Latch has no PHINode and the Header's only PHINode is
1262
+ // / for the induction variable.
1263
+ // /
1264
+ // / All code in Header, Cond, Latch and Exit (plus the terminator of the
1265
+ // / Preheader) are CanonicalLoopInfo's responsibility and their build-up checked
1266
+ // / by assertOK(). They are expected to not be modified unless explicitly
1267
+ // / modifying the CanonicalLoopInfo through a methods that applies a OpenMP
1268
+ // / loop-associated construct such as applyWorkshareLoop, tileLoops, unrollLoop,
1269
+ // / etc. These methods usually invalidate the CanonicalLoopInfo and re-use its
1270
+ // / basic blocks. After invalidation, the CanonicalLoopInfo must not be used
1271
+ // / anymore as its underlying control flow may not exist anymore.
1272
+ // / Loop-transformation methods such as tileLoops, collapseLoops and unrollLoop
1273
+ // / may also return a new CanonicalLoopInfo that can be passed to other
1274
+ // / loop-associated construct implementing methods. These loop-transforming
1275
+ // / methods may either create a new CanonicalLoopInfo usually using
1276
+ // / createLoopSkeleton and invalidate the input CanonicalLoopInfo, or reuse and
1277
+ // / modify one of the input CanonicalLoopInfo and return it as representing the
1278
+ // / modified loop. What is done is an implementation detail of
1279
+ // / transformation-implementing method and callers should always assume that the
1280
+ // / CanonicalLoopInfo passed to it is invalidated and a new object is returned.
1281
+ // / Returned CanonicalLoopInfo have the same structure and guarantees as the one
1282
+ // / created by createCanonicalLoop, such that transforming methods do not have
1283
+ // / to special case where the CanonicalLoopInfo originated from.
1284
+ // /
1285
+ // / Generally, methods consuming CanonicalLoopInfo do not need an
1286
+ // / OpenMPIRBuilder::InsertPointTy as argument, but use the locations of the
1287
+ // / CanonicalLoopInfo to insert new or modify existing instructions. Unless
1288
+ // / documented otherwise, methods consuming CanonicalLoopInfo do not invalidate
1289
+ // / any InsertPoint that is outside CanonicalLoopInfo's control. Specifically,
1290
+ // / any InsertPoint in the Preheader, After or Block can still be used after
1291
+ // / calling such a method.
1292
+ // /
1293
+ // / TODO: Provide mechanisms for exception handling and cancellation points.
1220
1294
// /
1221
- // / Defined outside OpenMPIRBuilder because one cannot forward-declare nested
1222
- // / classes .
1295
+ // / Defined outside OpenMPIRBuilder because nested classes cannot be
1296
+ // / forward-declared, e.g. to avoid having to include the entire OMPIRBuilder.h .
1223
1297
class CanonicalLoopInfo {
1224
1298
friend class OpenMPIRBuilder ;
1225
1299
1226
1300
private:
1227
- // / Whether this object currently represents a loop.
1228
- bool IsValid = false ;
1229
-
1230
- BasicBlock *Preheader;
1231
- BasicBlock *Header;
1232
- BasicBlock *Cond;
1233
- BasicBlock *Body;
1234
- BasicBlock *Latch;
1235
- BasicBlock *Exit;
1236
- BasicBlock *After;
1301
+ BasicBlock *Preheader = nullptr ;
1302
+ BasicBlock *Header = nullptr ;
1303
+ BasicBlock *Cond = nullptr ;
1304
+ BasicBlock *Body = nullptr ;
1305
+ BasicBlock *Latch = nullptr ;
1306
+ BasicBlock *Exit = nullptr ;
1307
+ BasicBlock *After = nullptr ;
1237
1308
1238
1309
// / Add the control blocks of this loop to \p BBs.
1239
1310
// /
1240
1311
// / This does not include any block from the body, including the one returned
1241
1312
// / by getBody().
1313
+ // /
1314
+ // / FIXME: This currently includes the Preheader and After blocks even though
1315
+ // / their content is (mostly) not under CanonicalLoopInfo's control.
1316
+ // / Re-evaluated whether this makes sense.
1242
1317
void collectControlBlocks (SmallVectorImpl<BasicBlock *> &BBs);
1243
1318
1244
1319
public:
1320
+ // / Returns whether this object currently represents the IR of a loop. If
1321
+ // / returning false, it may have been consumed by a loop transformation or not
1322
+ // / been intialized. Do not use in this case;
1323
+ bool isValid () const { return Header; }
1324
+
1245
1325
// / The preheader ensures that there is only a single edge entering the loop.
1246
1326
// / Code that must be execute before any loop iteration can be emitted here,
1247
1327
// / such as computing the loop trip count and begin lifetime markers. Code in
1248
1328
// / the preheader is not considered part of the canonical loop.
1249
- BasicBlock *getPreheader () const { return Preheader; }
1329
+ BasicBlock *getPreheader () const {
1330
+ assert (isValid () && " Requires a valid canonical loop" );
1331
+ return Preheader;
1332
+ }
1250
1333
1251
1334
// / The header is the entry for each iteration. In the canonical control flow,
1252
1335
// / it only contains the PHINode for the induction variable.
1253
- BasicBlock *getHeader () const { return Header; }
1336
+ BasicBlock *getHeader () const {
1337
+ assert (isValid () && " Requires a valid canonical loop" );
1338
+ return Header;
1339
+ }
1254
1340
1255
1341
// / The condition block computes whether there is another loop iteration. If
1256
1342
// / yes, branches to the body; otherwise to the exit block.
1257
- BasicBlock *getCond () const { return Cond; }
1343
+ BasicBlock *getCond () const {
1344
+ assert (isValid () && " Requires a valid canonical loop" );
1345
+ return Cond;
1346
+ }
1258
1347
1259
1348
// / The body block is the single entry for a loop iteration and not controlled
1260
1349
// / by CanonicalLoopInfo. It can contain arbitrary control flow but must
1261
1350
// / eventually branch to the \p Latch block.
1262
- BasicBlock *getBody () const { return Body; }
1351
+ BasicBlock *getBody () const {
1352
+ assert (isValid () && " Requires a valid canonical loop" );
1353
+ return Body;
1354
+ }
1263
1355
1264
1356
// / Reaching the latch indicates the end of the loop body code. In the
1265
1357
// / canonical control flow, it only contains the increment of the induction
1266
1358
// / variable.
1267
- BasicBlock *getLatch () const { return Latch; }
1359
+ BasicBlock *getLatch () const {
1360
+ assert (isValid () && " Requires a valid canonical loop" );
1361
+ return Latch;
1362
+ }
1268
1363
1269
1364
// / Reaching the exit indicates no more iterations are being executed.
1270
- BasicBlock *getExit () const { return Exit; }
1365
+ BasicBlock *getExit () const {
1366
+ assert (isValid () && " Requires a valid canonical loop" );
1367
+ return Exit;
1368
+ }
1271
1369
1272
1370
// / The after block is intended for clean-up code such as lifetime end
1273
1371
// / markers. It is separate from the exit block to ensure, analogous to the
1274
1372
// / preheader, it having just a single entry edge and being free from PHI
1275
1373
// / nodes should there be multiple loop exits (such as from break
1276
1374
// / statements/cancellations).
1277
- BasicBlock *getAfter () const { return After; }
1375
+ BasicBlock *getAfter () const {
1376
+ assert (isValid () && " Requires a valid canonical loop" );
1377
+ return After;
1378
+ }
1278
1379
1279
1380
// / Returns the llvm::Value containing the number of loop iterations. It must
1280
1381
// / be valid in the preheader and always interpreted as an unsigned integer of
1281
1382
// / any bit-width.
1282
1383
Value *getTripCount () const {
1384
+ assert (isValid () && " Requires a valid canonical loop" );
1283
1385
Instruction *CmpI = &Cond->front ();
1284
1386
assert (isa<CmpInst>(CmpI) && " First inst must compare IV with TripCount" );
1285
1387
return CmpI->getOperand (1 );
@@ -1288,33 +1390,47 @@ class CanonicalLoopInfo {
1288
1390
// / Returns the instruction representing the current logical induction
1289
1391
// / variable. Always unsigned, always starting at 0 with an increment of one.
1290
1392
Instruction *getIndVar () const {
1393
+ assert (isValid () && " Requires a valid canonical loop" );
1291
1394
Instruction *IndVarPHI = &Header->front ();
1292
1395
assert (isa<PHINode>(IndVarPHI) && " First inst must be the IV PHI" );
1293
1396
return IndVarPHI;
1294
1397
}
1295
1398
1296
1399
// / Return the type of the induction variable (and the trip count).
1297
- Type *getIndVarType () const { return getIndVar ()->getType (); }
1400
+ Type *getIndVarType () const {
1401
+ assert (isValid () && " Requires a valid canonical loop" );
1402
+ return getIndVar ()->getType ();
1403
+ }
1298
1404
1299
1405
// / Return the insertion point for user code before the loop.
1300
1406
OpenMPIRBuilder::InsertPointTy getPreheaderIP () const {
1407
+ assert (isValid () && " Requires a valid canonical loop" );
1301
1408
return {Preheader, std::prev (Preheader->end ())};
1302
1409
};
1303
1410
1304
1411
// / Return the insertion point for user code in the body.
1305
1412
OpenMPIRBuilder::InsertPointTy getBodyIP () const {
1413
+ assert (isValid () && " Requires a valid canonical loop" );
1306
1414
return {Body, Body->begin ()};
1307
1415
};
1308
1416
1309
1417
// / Return the insertion point for user code after the loop.
1310
1418
OpenMPIRBuilder::InsertPointTy getAfterIP () const {
1419
+ assert (isValid () && " Requires a valid canonical loop" );
1311
1420
return {After, After->begin ()};
1312
1421
};
1313
1422
1314
- Function *getFunction () const { return Header->getParent (); }
1423
+ Function *getFunction () const {
1424
+ assert (isValid () && " Requires a valid canonical loop" );
1425
+ return Header->getParent ();
1426
+ }
1315
1427
1316
1428
// / Consistency self-check.
1317
1429
void assertOK () const ;
1430
+
1431
+ // / Invalidate this loop. That is, the underlying IR does not fulfill the
1432
+ // / requirements of an OpenMP canonical loop anymore.
1433
+ void invalidate ();
1318
1434
};
1319
1435
1320
1436
} // end namespace llvm
0 commit comments