Skip to content

Commit 56975b4

Browse files
authored
[OpenMPIRBuilder] Split calculation of canonical loop trip count, NFC (#127820)
This patch splits off the calculation of canonical loop trip counts from the creation of canonical loops. This makes it possible to reuse this logic to, for instance, populate the `__tgt_target_kernel` runtime call for SPMD kernels. This feature is used to simplify one of the existing OpenMPIRBuilder tests.
1 parent 48397fe commit 56975b4

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,12 @@ class OpenMPIRBuilder {
728728
LoopBodyGenCallbackTy BodyGenCB, Value *TripCount,
729729
const Twine &Name = "loop");
730730

731-
/// Generator for the control flow structure of an OpenMP canonical loop.
731+
/// Calculate the trip count of a canonical loop.
732732
///
733-
/// Instead of a logical iteration space, this allows specifying user-defined
734-
/// loop counter values using increment, upper- and lower bounds. To
735-
/// disambiguate the terminology when counting downwards, instead of lower
736-
/// bounds we use \p Start for the loop counter value in the first body
737-
/// iteration.
733+
/// This allows specifying user-defined loop counter values using increment,
734+
/// upper- and lower bounds. To disambiguate the terminology when counting
735+
/// downwards, instead of lower bounds we use \p Start for the loop counter
736+
/// value in the first body iteration.
738737
///
739738
/// Consider the following limitations:
740739
///
@@ -758,7 +757,32 @@ class OpenMPIRBuilder {
758757
///
759758
/// for (int i = 0; i < 42; i -= 1u)
760759
///
761-
//
760+
/// \param Loc The insert and source location description.
761+
/// \param Start Value of the loop counter for the first iterations.
762+
/// \param Stop Loop counter values past this will stop the loop.
763+
/// \param Step Loop counter increment after each iteration; negative
764+
/// means counting down.
765+
/// \param IsSigned Whether Start, Stop and Step are signed integers.
766+
/// \param InclusiveStop Whether \p Stop itself is a valid value for the loop
767+
/// counter.
768+
/// \param Name Base name used to derive instruction names.
769+
///
770+
/// \returns The value holding the calculated trip count.
771+
Value *calculateCanonicalLoopTripCount(const LocationDescription &Loc,
772+
Value *Start, Value *Stop, Value *Step,
773+
bool IsSigned, bool InclusiveStop,
774+
const Twine &Name = "loop");
775+
776+
/// Generator for the control flow structure of an OpenMP canonical loop.
777+
///
778+
/// Instead of a logical iteration space, this allows specifying user-defined
779+
/// loop counter values using increment, upper- and lower bounds. To
780+
/// disambiguate the terminology when counting downwards, instead of lower
781+
/// bounds we use \p Start for the loop counter value in the first body
782+
///
783+
/// It calls \see calculateCanonicalLoopTripCount for trip count calculations,
784+
/// so limitations of that method apply here as well.
785+
///
762786
/// \param Loc The insert and source location description.
763787
/// \param BodyGenCB Callback that will generate the loop body code.
764788
/// \param Start Value of the loop counter for the first iterations.

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,10 +4059,9 @@ OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc,
40594059
return CL;
40604060
}
40614061

4062-
Expected<CanonicalLoopInfo *> OpenMPIRBuilder::createCanonicalLoop(
4063-
const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB,
4064-
Value *Start, Value *Stop, Value *Step, bool IsSigned, bool InclusiveStop,
4065-
InsertPointTy ComputeIP, const Twine &Name) {
4062+
Value *OpenMPIRBuilder::calculateCanonicalLoopTripCount(
4063+
const LocationDescription &Loc, Value *Start, Value *Stop, Value *Step,
4064+
bool IsSigned, bool InclusiveStop, const Twine &Name) {
40664065

40674066
// Consider the following difficulties (assuming 8-bit signed integers):
40684067
// * Adding \p Step to the loop counter which passes \p Stop may overflow:
@@ -4075,9 +4074,7 @@ Expected<CanonicalLoopInfo *> OpenMPIRBuilder::createCanonicalLoop(
40754074
assert(IndVarTy == Stop->getType() && "Stop type mismatch");
40764075
assert(IndVarTy == Step->getType() && "Step type mismatch");
40774076

4078-
LocationDescription ComputeLoc =
4079-
ComputeIP.isSet() ? LocationDescription(ComputeIP, Loc.DL) : Loc;
4080-
updateToLocation(ComputeLoc);
4077+
updateToLocation(Loc);
40814078

40824079
ConstantInt *Zero = ConstantInt::get(IndVarTy, 0);
40834080
ConstantInt *One = ConstantInt::get(IndVarTy, 1);
@@ -4117,8 +4114,20 @@ Expected<CanonicalLoopInfo *> OpenMPIRBuilder::createCanonicalLoop(
41174114
Value *OneCmp = Builder.CreateICmp(CmpInst::ICMP_ULE, Span, Incr);
41184115
CountIfLooping = Builder.CreateSelect(OneCmp, One, CountIfTwo);
41194116
}
4120-
Value *TripCount = Builder.CreateSelect(ZeroCmp, Zero, CountIfLooping,
4121-
"omp_" + Name + ".tripcount");
4117+
4118+
return Builder.CreateSelect(ZeroCmp, Zero, CountIfLooping,
4119+
"omp_" + Name + ".tripcount");
4120+
}
4121+
4122+
Expected<CanonicalLoopInfo *> OpenMPIRBuilder::createCanonicalLoop(
4123+
const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB,
4124+
Value *Start, Value *Stop, Value *Step, bool IsSigned, bool InclusiveStop,
4125+
InsertPointTy ComputeIP, const Twine &Name) {
4126+
LocationDescription ComputeLoc =
4127+
ComputeIP.isSet() ? LocationDescription(ComputeIP, Loc.DL) : Loc;
4128+
4129+
Value *TripCount = calculateCanonicalLoopTripCount(
4130+
ComputeLoc, Start, Stop, Step, IsSigned, InclusiveStop, Name);
41224131

41234132
auto BodyGen = [=](InsertPointTy CodeGenIP, Value *IV) {
41244133
Builder.restoreIP(CodeGenIP);

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,8 +1441,7 @@ TEST_F(OpenMPIRBuilderTest, CanonicalLoopSimple) {
14411441
EXPECT_EQ(&Loop->getAfter()->front(), RetInst);
14421442
}
14431443

1444-
TEST_F(OpenMPIRBuilderTest, CanonicalLoopBounds) {
1445-
using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
1444+
TEST_F(OpenMPIRBuilderTest, CanonicalLoopTripCount) {
14461445
OpenMPIRBuilder OMPBuilder(*M);
14471446
OMPBuilder.initialize();
14481447
IRBuilder<> Builder(BB);
@@ -1458,17 +1457,8 @@ TEST_F(OpenMPIRBuilderTest, CanonicalLoopBounds) {
14581457
Value *StartVal = ConstantInt::get(LCTy, Start);
14591458
Value *StopVal = ConstantInt::get(LCTy, Stop);
14601459
Value *StepVal = ConstantInt::get(LCTy, Step);
1461-
auto LoopBodyGenCB = [&](InsertPointTy CodeGenIP, llvm::Value *LC) {
1462-
return Error::success();
1463-
};
1464-
ASSERT_EXPECTED_INIT_RETURN(
1465-
CanonicalLoopInfo *, Loop,
1466-
OMPBuilder.createCanonicalLoop(Loc, LoopBodyGenCB, StartVal, StopVal,
1467-
StepVal, IsSigned, InclusiveStop),
1468-
-1);
1469-
Loop->assertOK();
1470-
Builder.restoreIP(Loop->getAfterIP());
1471-
Value *TripCount = Loop->getTripCount();
1460+
Value *TripCount = OMPBuilder.calculateCanonicalLoopTripCount(
1461+
Loc, StartVal, StopVal, StepVal, IsSigned, InclusiveStop);
14721462
return cast<ConstantInt>(TripCount)->getValue().getZExtValue();
14731463
};
14741464

0 commit comments

Comments
 (0)