Skip to content

Commit 5ea58a4

Browse files
authored
[Flang][MLIR][OMPIRBuilder] Upstream target SPMD conflict resolution (llvm#803)
2 parents 902b8fd + 6ac4900 commit 5ea58a4

File tree

6 files changed

+494
-207
lines changed

6 files changed

+494
-207
lines changed

flang/test/Lower/OpenMP/host-eval.f90

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,106 @@ subroutine distribute_parallel_do_simd()
155155
!$omp end distribute parallel do simd
156156
!$omp end teams
157157
end subroutine distribute_parallel_do_simd
158+
159+
! BOTH-LABEL: func.func @_QPdistribute
160+
subroutine distribute()
161+
! BOTH: omp.target
162+
163+
! HOST-SAME: host_eval(%{{.*}} -> %[[LB:.*]], %{{.*}} -> %[[UB:.*]], %{{.*}} -> %[[STEP:.*]] : i32, i32, i32)
164+
165+
! DEVICE-NOT: host_eval({{.*}})
166+
! DEVICE-SAME: {
167+
168+
! BOTH: omp.teams
169+
!$omp target teams
170+
171+
! BOTH: omp.distribute
172+
! BOTH-NEXT: omp.loop_nest
173+
174+
! HOST-SAME: (%{{.*}}) : i32 = (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]])
175+
!$omp distribute
176+
do i=1,10
177+
call foo()
178+
end do
179+
!$omp end distribute
180+
!$omp end target teams
181+
182+
! BOTH: omp.target
183+
! BOTH-NOT: host_eval({{.*}})
184+
! BOTH-SAME: {
185+
! BOTH: omp.teams
186+
!$omp target teams
187+
call foo() !< Prevents this from being Generic-SPMD.
188+
189+
! BOTH: omp.distribute
190+
!$omp distribute
191+
do i=1,10
192+
call foo()
193+
end do
194+
!$omp end distribute
195+
!$omp end target teams
196+
197+
! BOTH: omp.teams
198+
!$omp teams
199+
200+
! BOTH: omp.distribute
201+
!$omp distribute
202+
do i=1,10
203+
call foo()
204+
end do
205+
!$omp end distribute
206+
!$omp end teams
207+
end subroutine distribute
208+
209+
! BOTH-LABEL: func.func @_QPdistribute_simd
210+
subroutine distribute_simd()
211+
! BOTH: omp.target
212+
213+
! HOST-SAME: host_eval(%{{.*}} -> %[[LB:.*]], %{{.*}} -> %[[UB:.*]], %{{.*}} -> %[[STEP:.*]] : i32, i32, i32)
214+
215+
! DEVICE-NOT: host_eval({{.*}})
216+
! DEVICE-SAME: {
217+
218+
! BOTH: omp.teams
219+
!$omp target teams
220+
221+
! BOTH: omp.distribute
222+
! BOTH-NEXT: omp.simd
223+
! BOTH-NEXT: omp.loop_nest
224+
225+
! HOST-SAME: (%{{.*}}) : i32 = (%[[LB]]) to (%[[UB]]) inclusive step (%[[STEP]])
226+
!$omp distribute simd
227+
do i=1,10
228+
call foo()
229+
end do
230+
!$omp end distribute simd
231+
!$omp end target teams
232+
233+
! BOTH: omp.target
234+
! BOTH-NOT: host_eval({{.*}})
235+
! BOTH-SAME: {
236+
! BOTH: omp.teams
237+
!$omp target teams
238+
call foo() !< Prevents this from being Generic-SPMD.
239+
240+
! BOTH: omp.distribute
241+
! BOTH-NEXT: omp.simd
242+
!$omp distribute simd
243+
do i=1,10
244+
call foo()
245+
end do
246+
!$omp end distribute simd
247+
!$omp end target teams
248+
249+
! BOTH: omp.teams
250+
!$omp teams
251+
252+
! BOTH: omp.distribute
253+
! BOTH-NEXT: omp.simd
254+
!$omp distribute simd
255+
do i=1,10
256+
call foo()
257+
end do
258+
!$omp end distribute simd
259+
!$omp end teams
260+
end subroutine distribute_simd

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2703,12 +2703,13 @@ class OpenMPIRBuilder {
27032703

27042704
/// Generator for `#omp distribute`
27052705
///
2706-
/// \param Loc The location where the teams construct was encountered.
2706+
/// \param Loc The location where the distribute construct was encountered.
27072707
/// \param AllocaIP The insertion points to be used for alloca instructions.
27082708
/// \param BodyGenCB Callback that will generate the region code.
27092709
InsertPointOrErrorTy createDistribute(const LocationDescription &Loc,
27102710
InsertPointTy AllocaIP,
27112711
BodyGenCallbackTy BodyGenCB);
2712+
27122713
/// Generate conditional branch and relevant BasicBlocks through which private
27132714
/// threads copy the 'copyin' variables from Master copy to threadprivate
27142715
/// copies.

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,6 +4150,7 @@ OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc,
41504150
Value *OpenMPIRBuilder::calculateCanonicalLoopTripCount(
41514151
const LocationDescription &Loc, Value *Start, Value *Stop, Value *Step,
41524152
bool IsSigned, bool InclusiveStop, const Twine &Name) {
4153+
41534154
// Consider the following difficulties (assuming 8-bit signed integers):
41544155
// * Adding \p Step to the loop counter which passes \p Stop may overflow:
41554156
// DO I = 1, 100, 50
@@ -9301,44 +9302,6 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare(
93019302
return Builder.saveIP();
93029303
}
93039304

9304-
OpenMPIRBuilder::InsertPointOrErrorTy
9305-
OpenMPIRBuilder::createDistribute(const LocationDescription &Loc,
9306-
InsertPointTy OuterAllocaIP,
9307-
BodyGenCallbackTy BodyGenCB) {
9308-
if (!updateToLocation(Loc))
9309-
return InsertPointTy();
9310-
9311-
BasicBlock *OuterAllocaBB = OuterAllocaIP.getBlock();
9312-
9313-
if (OuterAllocaBB == Builder.GetInsertBlock()) {
9314-
BasicBlock *BodyBB =
9315-
splitBB(Builder, /*CreateBranch=*/true, "distribute.entry");
9316-
Builder.SetInsertPoint(BodyBB, BodyBB->begin());
9317-
}
9318-
BasicBlock *ExitBB =
9319-
splitBB(Builder, /*CreateBranch=*/true, "distribute.exit");
9320-
BasicBlock *BodyBB =
9321-
splitBB(Builder, /*CreateBranch=*/true, "distribute.body");
9322-
BasicBlock *AllocaBB =
9323-
splitBB(Builder, /*CreateBranch=*/true, "distribute.alloca");
9324-
9325-
// Generate the body of distribute clause
9326-
InsertPointTy AllocaIP(AllocaBB, AllocaBB->begin());
9327-
InsertPointTy CodeGenIP(BodyBB, BodyBB->begin());
9328-
if (Error Err = BodyGenCB(AllocaIP, CodeGenIP))
9329-
return Err;
9330-
9331-
OutlineInfo OI;
9332-
OI.OuterAllocaBB = OuterAllocaIP.getBlock();
9333-
OI.EntryBB = AllocaBB;
9334-
OI.ExitBB = ExitBB;
9335-
9336-
addOutlineInfo(std::move(OI));
9337-
Builder.SetInsertPoint(ExitBB, ExitBB->begin());
9338-
9339-
return Builder.saveIP();
9340-
}
9341-
93429305
OpenMPIRBuilder::InsertPointOrErrorTy
93439306
OpenMPIRBuilder::createTeams(const LocationDescription &Loc,
93449307
BodyGenCallbackTy BodyGenCB, Value *NumTeamsLower,
@@ -9484,6 +9447,44 @@ OpenMPIRBuilder::createTeams(const LocationDescription &Loc,
94849447
return Builder.saveIP();
94859448
}
94869449

9450+
OpenMPIRBuilder::InsertPointOrErrorTy
9451+
OpenMPIRBuilder::createDistribute(const LocationDescription &Loc,
9452+
InsertPointTy OuterAllocaIP,
9453+
BodyGenCallbackTy BodyGenCB) {
9454+
if (!updateToLocation(Loc))
9455+
return InsertPointTy();
9456+
9457+
BasicBlock *OuterAllocaBB = OuterAllocaIP.getBlock();
9458+
9459+
if (OuterAllocaBB == Builder.GetInsertBlock()) {
9460+
BasicBlock *BodyBB =
9461+
splitBB(Builder, /*CreateBranch=*/true, "distribute.entry");
9462+
Builder.SetInsertPoint(BodyBB, BodyBB->begin());
9463+
}
9464+
BasicBlock *ExitBB =
9465+
splitBB(Builder, /*CreateBranch=*/true, "distribute.exit");
9466+
BasicBlock *BodyBB =
9467+
splitBB(Builder, /*CreateBranch=*/true, "distribute.body");
9468+
BasicBlock *AllocaBB =
9469+
splitBB(Builder, /*CreateBranch=*/true, "distribute.alloca");
9470+
9471+
// Generate the body of distribute clause
9472+
InsertPointTy AllocaIP(AllocaBB, AllocaBB->begin());
9473+
InsertPointTy CodeGenIP(BodyBB, BodyBB->begin());
9474+
if (Error Err = BodyGenCB(AllocaIP, CodeGenIP))
9475+
return Err;
9476+
9477+
OutlineInfo OI;
9478+
OI.OuterAllocaBB = OuterAllocaIP.getBlock();
9479+
OI.EntryBB = AllocaBB;
9480+
OI.ExitBB = ExitBB;
9481+
9482+
addOutlineInfo(std::move(OI));
9483+
Builder.SetInsertPoint(ExitBB, ExitBB->begin());
9484+
9485+
return Builder.saveIP();
9486+
}
9487+
94879488
GlobalVariable *
94889489
OpenMPIRBuilder::createOffloadMapnames(SmallVectorImpl<llvm::Constant *> &Names,
94899490
std::string VarName) {

0 commit comments

Comments
 (0)