Skip to content

Commit 870f442

Browse files
committed
[flang][OpenMP] Fix the types of worksharing-loop variables
The types of lower bound, upper bound, and step are converted into the type of the loop variable if necessary. OpenMP runtime requires 32-bit or 64-bit loop variables. OpenMP loop iteration variable cannot have more than 64 bits size and will be narrowed. This patch is part of upstreaming code from the fir-dev branch of https://github.com/flang-compiler/f18-llvm-project. (#1256) Co-authored-by: kiranchandramohan <[email protected]> Reviewed By: kiranchandramohan, shraiysh Differential Revision: https://reviews.llvm.org/D125740
1 parent 8fc4fce commit 870f442

File tree

2 files changed

+169
-6
lines changed

2 files changed

+169
-6
lines changed

flang/lib/Lower/OpenMP.cpp

+43-6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ static void genObjectList(const Fortran::parser::OmpObjectList &objectList,
120120
}
121121
}
122122

123+
static mlir::Type getLoopVarType(Fortran::lower::AbstractConverter &converter,
124+
std::size_t loopVarTypeSize) {
125+
// OpenMP runtime requires 32-bit or 64-bit loop variables.
126+
loopVarTypeSize = loopVarTypeSize * 8;
127+
if (loopVarTypeSize < 32) {
128+
loopVarTypeSize = 32;
129+
} else if (loopVarTypeSize > 64) {
130+
loopVarTypeSize = 64;
131+
mlir::emitWarning(converter.getCurrentLocation(),
132+
"OpenMP loop iteration variable cannot have more than 64 "
133+
"bits size and will be narrowed into 64 bits.");
134+
}
135+
assert((loopVarTypeSize == 32 || loopVarTypeSize == 64) &&
136+
"OpenMP loop iteration variable size must be transformed into 32-bit "
137+
"or 64-bit");
138+
return converter.getFirOpBuilder().getIntegerType(loopVarTypeSize);
139+
}
140+
123141
/// Create the body (block) for an OpenMP Operation.
124142
///
125143
/// \param [in] op - the operation the body belongs to.
@@ -143,15 +161,19 @@ createBodyOfOp(Op &op, Fortran::lower::AbstractConverter &converter,
143161
// e.g. For loops the arguments are the induction variable. And all further
144162
// uses of the induction variable should use this mlir value.
145163
if (args.size()) {
164+
std::size_t loopVarTypeSize = 0;
165+
for (const Fortran::semantics::Symbol *arg : args)
166+
loopVarTypeSize = std::max(loopVarTypeSize, arg->GetUltimate().size());
167+
mlir::Type loopVarType = getLoopVarType(converter, loopVarTypeSize);
146168
SmallVector<Type> tiv;
147169
SmallVector<Location> locs;
148-
int argIndex = 0;
149-
for (auto &arg : args) {
150-
tiv.push_back(converter.genType(*arg));
170+
for (int i = 0; i < (int)args.size(); i++) {
171+
tiv.push_back(loopVarType);
151172
locs.push_back(loc);
152173
}
153174
firOpBuilder.createBlock(&op.getRegion(), {}, tiv, locs);
154-
for (auto &arg : args) {
175+
int argIndex = 0;
176+
for (const Fortran::semantics::Symbol *arg : args) {
155177
fir::ExtendedValue exval = op.getRegion().front().getArgument(argIndex);
156178
converter.bindSymbol(*arg, exval);
157179
argIndex++;
@@ -490,11 +512,12 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
490512
TODO(converter.getCurrentLocation(), "Construct enclosing do loop");
491513
}
492514

493-
int64_t collapseValue = Fortran::lower::getCollapseValue(wsLoopOpClauseList);
494-
495515
// Collect the loops to collapse.
496516
auto *doConstructEval = &eval.getFirstNestedEvaluation();
497517

518+
std::int64_t collapseValue =
519+
Fortran::lower::getCollapseValue(wsLoopOpClauseList);
520+
std::size_t loopVarTypeSize = 0;
498521
SmallVector<const Fortran::semantics::Symbol *> iv;
499522
do {
500523
auto *doLoop = &doConstructEval->getFirstNestedEvaluation();
@@ -518,12 +541,26 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
518541
currentLocation, firOpBuilder.getIntegerType(32), 1));
519542
}
520543
iv.push_back(bounds->name.thing.symbol);
544+
loopVarTypeSize = std::max(loopVarTypeSize,
545+
bounds->name.thing.symbol->GetUltimate().size());
521546

522547
collapseValue--;
523548
doConstructEval =
524549
&*std::next(doConstructEval->getNestedEvaluations().begin());
525550
} while (collapseValue > 0);
526551

552+
// The types of lower bound, upper bound, and step are converted into the
553+
// type of the loop variable if necessary.
554+
mlir::Type loopVarType = getLoopVarType(converter, loopVarTypeSize);
555+
for (unsigned it = 0; it < (unsigned)lowerBound.size(); it++) {
556+
lowerBound[it] = firOpBuilder.createConvert(currentLocation, loopVarType,
557+
lowerBound[it]);
558+
upperBound[it] = firOpBuilder.createConvert(currentLocation, loopVarType,
559+
upperBound[it]);
560+
step[it] =
561+
firOpBuilder.createConvert(currentLocation, loopVarType, step[it]);
562+
}
563+
527564
// FIXME: Add support for following clauses:
528565
// 1. linear
529566
// 2. order
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
! This test checks lowering of OpenMP DO Directive(Worksharing) for different
2+
! types of loop iteration variable, lower bound, upper bound, and step.
3+
4+
!REQUIRES: shell
5+
!RUN: bbc -fopenmp -emit-fir %s -o - 2>&1 | FileCheck %s
6+
7+
!CHECK: OpenMP loop iteration variable cannot have more than 64 bits size and will be narrowed into 64 bits.
8+
9+
program wsloop_variable
10+
integer(kind=1) :: i1_lb, i1_ub
11+
integer(kind=2) :: i2, i2_ub, i2_s
12+
integer(kind=4) :: i4_s
13+
integer(kind=8) :: i8, i8_s
14+
integer(kind=16) :: i16, i16_lb
15+
real :: x
16+
17+
!CHECK: [[TMP0:%.*]] = arith.constant 1 : i32
18+
!CHECK: [[TMP1:%.*]] = arith.constant 100 : i32
19+
!CHECK: [[TMP2:%.*]] = fir.convert [[TMP0]] : (i32) -> i64
20+
!CHECK: [[TMP3:%.*]] = fir.convert %{{.*}} : (i8) -> i64
21+
!CHECK: [[TMP4:%.*]] = fir.convert %{{.*}} : (i16) -> i64
22+
!CHECK: [[TMP5:%.*]] = fir.convert %{{.*}} : (i128) -> i64
23+
!CHECK: [[TMP6:%.*]] = fir.convert [[TMP1]] : (i32) -> i64
24+
!CHECK: [[TMP7:%.*]] = fir.convert %{{.*}} : (i32) -> i64
25+
!CHECK: omp.wsloop collapse(2) for ([[TMP8:%.*]], [[TMP9:%.*]]) : i64 = ([[TMP2]], [[TMP5]]) to ([[TMP3]], [[TMP6]]) inclusive step ([[TMP4]], [[TMP7]]) {
26+
!CHECK: [[TMP10:%.*]] = arith.addi [[TMP8]], [[TMP9]] : i64
27+
!CHECK: [[TMP11:%.*]] = fir.convert [[TMP10]] : (i64) -> f32
28+
!CHECK: fir.store [[TMP11]] to %{{.*}} : !fir.ref<f32>
29+
!CHECK: omp.yield
30+
!CHECK: }
31+
32+
!$omp do collapse(2)
33+
do i2 = 1, i1_ub, i2_s
34+
do i8 = i16_lb, 100, i4_s
35+
x = i2 + i8
36+
end do
37+
end do
38+
!$omp end do
39+
40+
!CHECK: [[TMP12:%.*]] = arith.constant 1 : i32
41+
!CHECK: [[TMP13:%.*]] = fir.convert %{{.*}} : (i8) -> i32
42+
!CHECK: [[TMP14:%.*]] = fir.convert %{{.*}} : (i64) -> i32
43+
!CHECK: omp.wsloop for ([[TMP15:%.*]]) : i32 = ([[TMP12]]) to ([[TMP13]]) inclusive step ([[TMP14]]) {
44+
!CHECK: [[TMP16:%.*]] = fir.convert [[TMP15]] : (i32) -> f32
45+
!CHECK: fir.store [[TMP16]] to %{{.*}} : !fir.ref<f32>
46+
!CHECK: omp.yield
47+
!CHECK: }
48+
49+
!$omp do
50+
do i2 = 1, i1_ub, i8_s
51+
x = i2
52+
end do
53+
!$omp end do
54+
55+
!CHECK: [[TMP17:%.*]] = fir.convert %{{.*}} : (i8) -> i64
56+
!CHECK: [[TMP18:%.*]] = fir.convert %{{.*}} : (i16) -> i64
57+
!CHECK: [[TMP19:%.*]] = fir.convert %{{.*}} : (i32) -> i64
58+
!CHECK: omp.wsloop for ([[TMP20:%.*]]) : i64 = ([[TMP17]]) to ([[TMP18]]) inclusive step ([[TMP19]]) {
59+
!CHECK: [[TMP21:%.*]] = fir.convert [[TMP20]] : (i64) -> f32
60+
!CHECK: fir.store [[TMP21]] to %{{.*}} : !fir.ref<f32>
61+
!CHECK: omp.yield
62+
!CHECK: }
63+
64+
!$omp do
65+
do i16 = i1_lb, i2_ub, i4_s
66+
x = i16
67+
end do
68+
!$omp end do
69+
70+
end program wsloop_variable
71+
72+
!CHECK-LABEL: func.func @_QPwsloop_variable_sub() {
73+
!CHECK: %[[VAL_0:.*]] = fir.alloca i128 {bindc_name = "i16_lb", uniq_name = "_QFwsloop_variable_subEi16_lb"}
74+
!CHECK: %[[VAL_1:.*]] = fir.alloca i8 {bindc_name = "i1_ub", uniq_name = "_QFwsloop_variable_subEi1_ub"}
75+
!CHECK: %[[VAL_2:.*]] = fir.alloca i16 {bindc_name = "i2", uniq_name = "_QFwsloop_variable_subEi2"}
76+
!CHECK: %[[VAL_3:.*]] = fir.alloca i16 {bindc_name = "i2_s", uniq_name = "_QFwsloop_variable_subEi2_s"}
77+
!CHECK: %[[VAL_4:.*]] = fir.alloca i32 {bindc_name = "i4_s", uniq_name = "_QFwsloop_variable_subEi4_s"}
78+
!CHECK: %[[VAL_5:.*]] = fir.alloca i64 {bindc_name = "i8", uniq_name = "_QFwsloop_variable_subEi8"}
79+
!CHECK: %[[VAL_6:.*]] = fir.alloca f32 {bindc_name = "x", uniq_name = "_QFwsloop_variable_subEx"}
80+
!CHECK: %[[VAL_7:.*]] = arith.constant 1 : i32
81+
!CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_1]] : !fir.ref<i8>
82+
!CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_3]] : !fir.ref<i16>
83+
!CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_8]] : (i8) -> i32
84+
!CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_9]] : (i16) -> i32
85+
!CHECK: omp.wsloop for (%[[VAL_12:.*]]) : i32 = (%[[VAL_7]]) to (%[[VAL_10]]) inclusive step (%[[VAL_11]]) {
86+
!CHECK: %[[VAL_13:.*]] = fir.load %[[VAL_0]] : !fir.ref<i128>
87+
!CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (i128) -> index
88+
!CHECK: %[[VAL_15:.*]] = arith.constant 100 : i32
89+
!CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_15]] : (i32) -> index
90+
!CHECK: %[[VAL_17:.*]] = fir.load %[[VAL_4]] : !fir.ref<i32>
91+
!CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_17]] : (i32) -> index
92+
!CHECK: %[[VAL_19:.*]] = fir.do_loop %[[VAL_20:.*]] = %[[VAL_14]] to %[[VAL_16]] step %[[VAL_18]] -> index {
93+
!CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (index) -> i64
94+
!CHECK: fir.store %[[VAL_21]] to %[[VAL_5]] : !fir.ref<i64>
95+
!CHECK: %[[VAL_22:.*]] = fir.convert %[[VAL_12]] : (i32) -> i64
96+
!CHECK: %[[VAL_23:.*]] = fir.load %[[VAL_5]] : !fir.ref<i64>
97+
!CHECK: %[[VAL_24:.*]] = arith.addi %[[VAL_22]], %[[VAL_23]] : i64
98+
!CHECK: %[[VAL_25:.*]] = fir.convert %[[VAL_24]] : (i64) -> f32
99+
!CHECK: fir.store %[[VAL_25]] to %[[VAL_6]] : !fir.ref<f32>
100+
!CHECK: %[[VAL_26:.*]] = arith.addi %[[VAL_20]], %[[VAL_18]] : index
101+
!CHECK: fir.result %[[VAL_26]] : index
102+
!CHECK: }
103+
!CHECK: %[[VAL_27:.*]] = fir.convert %[[VAL_28:.*]] : (index) -> i64
104+
!CHECK: fir.store %[[VAL_27]] to %[[VAL_5]] : !fir.ref<i64>
105+
!CHECK: omp.yield
106+
!CHECK: }
107+
!CHECK: return
108+
!CHECK: }
109+
110+
subroutine wsloop_variable_sub
111+
integer(kind=1) :: i1_ub
112+
integer(kind=2) :: i2, i2_s
113+
integer(kind=4) :: i4_s
114+
integer(kind=8) :: i8
115+
integer(kind=16) :: i16_lb
116+
real :: x
117+
118+
!$omp do
119+
do i2 = 1, i1_ub, i2_s
120+
do i8 = i16_lb, 100, i4_s
121+
x = i2 + i8
122+
end do
123+
end do
124+
!$omp end do
125+
126+
end

0 commit comments

Comments
 (0)