Skip to content

Commit 68c3846

Browse files
committed
[Flang][MLIR][OpenMP] Temporarily re-add basic handling of uses in target regions to avoid gfortran test-suite regressions
This was a regression introduced by myself in: 6a62707 where I too hastily removed the basic handling of implicit captures we have currently. This will be superseded by all implicit captures being added to target operations map_info entries in a soon landing series of patches, however, that is currently not the case so we must continue to do some basic handling of these captures for the time being. This patch re-adds that behaviour to avoid regressions. Unfortunately this means some test changes as well as getUsedValuesDefinedAbove grabs constants used outside of the target region which aren't handled particularly well currently.
1 parent 0d5b7dd commit 68c3846

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "llvm/TargetParser/Triple.h"
3333
#include "llvm/Transforms/Utils/ModuleUtils.h"
3434

35+
#include <any>
3536
#include <optional>
3637
#include <utility>
3738

@@ -2407,6 +2408,23 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
24072408
kernelInput.push_back(mapData.OriginalValue[i]);
24082409
}
24092410

2411+
// Do some very basic handling of implicit captures that are caught
2412+
// by use in the target region.
2413+
// TODO/FIXME: Remove on addition of IsolatedFromAbove patch series
2414+
// as this will become redundant and perhaps erroneous in cases
2415+
// where more complex implicit capture semantics are required.
2416+
llvm::SetVector<Value> uses;
2417+
getUsedValuesDefinedAbove(targetRegion, uses);
2418+
2419+
for (mlir::Value use : uses) {
2420+
llvm::Value *useValue = moduleTranslation.lookupValue(use);
2421+
if (useValue &&
2422+
!std::any_of(
2423+
mapData.OriginalValue.begin(), mapData.OriginalValue.end(),
2424+
[&](llvm::Value *mapValue) { return mapValue == useValue; }))
2425+
kernelInput.push_back(useValue);
2426+
}
2427+
24102428
builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createTarget(
24112429
ompLoc, allocaIP, builder.saveIP(), entryInfo, defaultValTeams,
24122430
defaultValThreads, kernelInput, genMapInfoCB, bodyCB, argAccessorCB));

openmp/libomptarget/test/offloading/fortran/basic-target-region-1D-array-section.f90

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ program main
1414
integer :: write_arr(10) = (/0,0,0,0,0,0,0,0,0,0/)
1515
integer :: read_arr(10) = (/1,2,3,4,5,6,7,8,9,10/)
1616
integer :: i = 2
17-
18-
!$omp target map(to:read_arr(2:5)) map(from:write_arr(2:5)) map(tofrom:i)
19-
do i = 2, 5
17+
integer :: j = 5
18+
!$omp target map(to:read_arr(2:5)) map(from:write_arr(2:5)) map(to:i,j)
19+
do while (i <= j)
2020
write_arr(i) = read_arr(i)
21+
i = i + 1
2122
end do
2223
!$omp end target
2324

openmp/libomptarget/test/offloading/fortran/basic-target-region-3D-array-section.f90

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ program main
1414
integer :: inArray(3,3,3)
1515
integer :: outArray(3,3,3)
1616
integer :: i, j, k
17+
integer :: j2 = 3, k2 = 3
1718

1819
do i = 1, 3
1920
do j = 1, 3
@@ -24,11 +25,16 @@ program main
2425
end do
2526
end do
2627

27-
!$omp target map(tofrom:inArray(1:3, 1:3, 2:2), outArray(1:3, 1:3, 1:3), j, k)
28-
do j = 1, 3
29-
do k = 1, 3
28+
j = 1
29+
k = 1
30+
!$omp target map(tofrom:inArray(1:3, 1:3, 2:2), outArray(1:3, 1:3, 1:3), j, k, j2, k2)
31+
do while (j <= j2)
32+
k = 1
33+
do while (k <= k2)
3034
outArray(k, j, 2) = inArray(k, j, 2)
35+
k = k + 1
3136
end do
37+
j = j + 1
3238
end do
3339
!$omp end target
3440

openmp/libomptarget/test/offloading/fortran/basic-target-region-3D-array.f90

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
! RUN: %libomptarget-compile-fortran-run-and-check-generic
1212
program main
13-
implicit none
1413
integer :: x(2,2,2)
15-
integer :: i = 1, j = 1, k = 1
14+
integer :: i, j, k
15+
integer :: i2 = 2, j2 = 2, k2 = 2
1616
integer :: counter = 1
1717
do i = 1, 2
1818
do j = 1, 2
@@ -22,14 +22,23 @@ program main
2222
end do
2323
end do
2424

25-
!$omp target map(tofrom:x, i, j, k, counter)
26-
do i = 1, 2
27-
do j = 1, 2
28-
do k = 1, 2
25+
i = 1
26+
j = 1
27+
k = 1
28+
29+
!$omp target map(tofrom:x, counter) map(to: i, j, k, i2, j2, k2)
30+
do while (i <= i2)
31+
j = 1
32+
do while (j <= j2)
33+
k = 1
34+
do while (k <= k2)
2935
x(i, j, k) = counter
3036
counter = counter + 1
37+
k = k + 1
3138
end do
39+
j = j + 1
3240
end do
41+
i = i + 1
3342
end do
3443
!$omp end target
3544

0 commit comments

Comments
 (0)