Skip to content

Commit fd22706

Browse files
authored
[flang][OpenMP] Skip compiler directives in getCollapsedLoopEval (#169565)
Use `getNestedDoConstruct` from Utils to get the nested DoConstructs. Fixes #169532
1 parent 9534ed9 commit fd22706

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "flang/Lower/ConvertVariable.h"
2626
#include "flang/Lower/DirectivesCommon.h"
2727
#include "flang/Lower/OpenMP/Clauses.h"
28+
#include "flang/Lower/PFTBuilder.h"
2829
#include "flang/Lower/StatementContext.h"
2930
#include "flang/Lower/Support/ReductionProcessor.h"
3031
#include "flang/Lower/SymbolMap.h"
@@ -568,14 +569,9 @@ getCollapsedLoopEval(lower::pft::Evaluation &eval, int collapseValue) {
568569
if (collapseValue == 0)
569570
return &eval;
570571

571-
lower::pft::Evaluation *curEval = &eval.getFirstNestedEvaluation();
572-
for (int i = 1; i < collapseValue; i++) {
573-
// The nested evaluations should be DoConstructs (i.e. they should form
574-
// a loop nest). Each DoConstruct is a tuple <NonLabelDoStmt, Block,
575-
// EndDoStmt>.
576-
assert(curEval->isA<parser::DoConstruct>());
577-
curEval = &*std::next(curEval->getNestedEvaluations().begin());
578-
}
572+
lower::pft::Evaluation *curEval = &eval;
573+
for (int i = 0; i < collapseValue; i++)
574+
curEval = getNestedDoConstruct(*curEval);
579575
return curEval;
580576
}
581577

flang/lib/Lower/OpenMP/Utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ static void processTileSizesFromOpenMPConstruct(
796796
}
797797
}
798798

799-
static pft::Evaluation *getNestedDoConstruct(pft::Evaluation &eval) {
799+
pft::Evaluation *getNestedDoConstruct(pft::Evaluation &eval) {
800800
for (pft::Evaluation &nested : eval.getNestedEvaluations()) {
801801
// In an OpenMPConstruct there can be compiler directives:
802802
// 1 <<OpenMPConstruct>>

flang/lib/Lower/OpenMP/Utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ void genObjectList(const ObjectList &objects,
167167
void lastprivateModifierNotSupported(const omp::clause::Lastprivate &lastp,
168168
mlir::Location loc);
169169

170+
pft::Evaluation *getNestedDoConstruct(pft::Evaluation &eval);
171+
170172
int64_t collectLoopRelatedInfo(
171173
lower::AbstractConverter &converter, mlir::Location currentLocation,
172174
lower::pft::Evaluation &eval, const omp::List<omp::Clause> &clauses,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
!RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=60 %s -o - | FileCheck %s
2+
3+
! Check that we generate proper body of the do-construct.
4+
5+
!CHECK: omp.loop_nest (%[[ARG1:arg[0-9]+]]) : i32 = (%c1_i32) to (%c10_i32) inclusive step (%c1_i32_1) {
6+
!CHECK: %[[V0:[0-9]+]]:2 = hlfir.declare %arg0 {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
7+
!CHECK: hlfir.assign %[[ARG1]] to %[[V0]]#0 : i32, !fir.ref<i32>
8+
!CHECK: %[[V1:[0-9]+]] = fir.load %[[V0]]#0 : !fir.ref<i32>
9+
!CHECK: %[[V2:[0-9]+]] = fir.convert %[[V1]] : (i32) -> f32
10+
!CHECK: %[[V3:[0-9]+]] = fir.load %[[V0]]#0 : !fir.ref<i32>
11+
!CHECK: %[[V4:[0-9]+]] = fir.convert %[[V3]] : (i32) -> i64
12+
!CHECK: %[[V5:[0-9]+]] = hlfir.designate %3#0 (%[[V4]]) : (!fir.ref<!fir.array<10xf32>>, i64) -> !fir.ref<f32>
13+
!CHECK: hlfir.assign %[[V2]] to %[[V5]] : f32, !fir.ref<f32>
14+
!CHECK: omp.yield
15+
!CHECK: }
16+
17+
program omp_cdir_codegen
18+
implicit none
19+
integer, parameter :: n = 10
20+
real :: a(n)
21+
integer :: i
22+
23+
!$omp parallel do
24+
!dir$ unroll
25+
do i = 1, n
26+
a(i) = real(i)
27+
end do
28+
!$omp end parallel do
29+
30+
print *, 'a(1)=', a(1), ' a(n)=', a(n)
31+
end program omp_cdir_codegen

0 commit comments

Comments
 (0)