Skip to content

Commit 308efe8

Browse files
TIFitischencha3
authored andcommitted
[OpenMP] Fix target data region codegen being omitted for device pass (llvm#85218)
This patch enables the BodyCodeGen callback to still trigger for the TargetData nested region during the device pass. There maybe Target code nested within the TargetData region for which this is required. Also add tests for the same.
1 parent e88d716 commit 308efe8

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -4883,8 +4883,11 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData(
48834883
return InsertPointTy();
48844884

48854885
// Disable TargetData CodeGen on Device pass.
4886-
if (Config.IsTargetDevice.value_or(false))
4886+
if (Config.IsTargetDevice.value_or(false)) {
4887+
if (BodyGenCB)
4888+
Builder.restoreIP(BodyGenCB(Builder.saveIP(), BodyGenTy::NoPriv));
48874889
return Builder.saveIP();
4890+
}
48884891

48894892
Builder.restoreIP(CodeGenIP);
48904893
bool IsStandAlone = !BodyGenCB;

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -5856,6 +5856,23 @@ TEST_F(OpenMPIRBuilderTest, TargetDataRegion) {
58565856
EXPECT_TRUE(TargetDataCall->getOperand(2)->getType()->isIntegerTy(32));
58575857
EXPECT_TRUE(TargetDataCall->getOperand(8)->getType()->isPointerTy());
58585858

5859+
// Check that BodyGenCB is still made when IsTargetDevice is set to true.
5860+
OMPBuilder.Config.setIsTargetDevice(true);
5861+
bool CheckDevicePassBodyGen = false;
5862+
auto BodyTargetCB = [&](InsertPointTy CodeGenIP, BodyGenTy BodyGenType) {
5863+
CheckDevicePassBodyGen = true;
5864+
Builder.restoreIP(CodeGenIP);
5865+
CallInst *TargetDataCall =
5866+
dyn_cast<CallInst>(BB->back().getPrevNode()->getPrevNode());
5867+
// Make sure no begin_mapper call is present for device pass.
5868+
EXPECT_EQ(TargetDataCall, nullptr);
5869+
return Builder.saveIP();
5870+
};
5871+
Builder.restoreIP(OMPBuilder.createTargetData(
5872+
Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID),
5873+
/* IfCond= */ nullptr, Info, GenMapInfoCB, nullptr, BodyTargetCB));
5874+
EXPECT_TRUE(CheckDevicePassBodyGen);
5875+
58595876
Builder.CreateRetVoid();
58605877
EXPECT_FALSE(verifyModule(*M, &errs()));
58615878
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
! Offloading test for target nested inside
2+
! a target data region
3+
! REQUIRES: flang
4+
! UNSUPPORTED: nvptx64-nvidia-cuda-LTO
5+
! UNSUPPORTED: aarch64-unknown-linux-gnu
6+
! UNSUPPORTED: aarch64-unknown-linux-gnu-LTO
7+
! UNSUPPORTED: x86_64-pc-linux-gnu
8+
! UNSUPPORTED: x86_64-pc-linux-gnu-LTO
9+
10+
! RUN: %libomptarget-compile-fortran-run-and-check-generic
11+
program main
12+
integer :: A(10), B(10), C(10)
13+
14+
do I = 1, 10
15+
A(I) = 1
16+
B(I) = 2
17+
end do
18+
!$omp target data map(to: A, B) map(alloc: C)
19+
!$omp target map(from: C)
20+
do I = 1, 10
21+
C(I) = A(I) + B(I) ! assigns 3, A:1 + B:2
22+
end do
23+
!$omp end target
24+
!$omp target update from(C) ! updates C device -> host
25+
!$omp end target data
26+
27+
print *, C ! should be all 3's
28+
29+
end program
30+
31+
! CHECK: 3 3 3 3 3 3 3 3 3 3

0 commit comments

Comments
 (0)