Skip to content

Commit 32faf43

Browse files
authored
[flang][OpenMP] Handle fixed length charaters in delayed privatization (#126704)
We currently handle sequences of fixed-length arrays properly by **not** emitting length parameters for `embox` ops inside the `omp.private` op. However, we do not handle the scalar case. This PR extends `getLengthParameters` defined in `PrivateReductionUtils.cpp` to handle such cases. Fixes issue reported in #125732.
1 parent 37f36cb commit 32faf43

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

flang/include/flang/Optimizer/Dialect/FIRType.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ inline mlir::Type unwrapPassByRefType(mlir::Type t) {
276276
return t;
277277
}
278278

279+
/// Extracts the innermost type, T, **potentially** wrapped inside:
280+
/// <fir.[ref|ptr|heap] <fir.[ref|ptr|heap|box] <fir.array<T>>>
281+
///
282+
/// Any element absent from the above pattern does not affect the returned
283+
/// value: T.
284+
mlir::Type getFortranElementType(mlir::Type ty);
285+
279286
/// Unwrap either a sequence or a boxed sequence type, returning the element
280287
/// type of the sequence type.
281288
/// e.g.,

flang/lib/Lower/OpenMP/PrivateReductionUtils.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,11 @@ static void getLengthParameters(fir::FirOpBuilder &builder, mlir::Location loc,
197197
// The verifier for EmboxOp doesn't allow length parameters when the the
198198
// character already has static LEN. genLengthParameters may still return them
199199
// in this case.
200-
mlir::Type unwrappedType =
201-
fir::unwrapRefType(fir::unwrapSeqOrBoxedSeqType(moldArg.getType()));
202-
if (auto strTy = mlir::dyn_cast<fir::CharacterType>(unwrappedType)) {
203-
if (strTy.hasConstantLen())
204-
lenParams.resize(0);
205-
}
200+
auto strTy = mlir::dyn_cast<fir::CharacterType>(
201+
fir::getFortranElementType(moldArg.getType()));
202+
203+
if (strTy && strTy.hasConstantLen())
204+
lenParams.resize(0);
206205
}
207206

208207
static bool

flang/lib/Optimizer/Dialect/FIRType.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ mlir::Type unwrapAllRefAndSeqType(mlir::Type ty) {
426426
}
427427
}
428428

429+
mlir::Type getFortranElementType(mlir::Type ty) {
430+
return fir::unwrapSequenceType(
431+
fir::unwrapPassByRefType(fir::unwrapRefType(ty)));
432+
}
433+
429434
mlir::Type unwrapSeqOrBoxedSeqType(mlir::Type ty) {
430435
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(ty))
431436
return seqTy.getEleTy();

flang/test/Lower/OpenMP/parallel-private-clause-str.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 \
99
! RUN: | FileCheck %s
1010

11+
!CHECK: omp.private {type = private} @{{.*}}test_allocatable_fixed_len_stringEfixed_len_str{{.*}} init {
12+
!CHECK: fir.if {{.*}} {
13+
!CHECK: fir.embox %{{[^[:space:]]+}} : {{.*}}
14+
!CHECK: } else {
15+
!CHECK: fir.embox %{{[^[:space:]]+}} : {{.*}}
16+
!CHECK: }
17+
!CHECK: }
18+
1119
!CHECK: omp.private {type = private} @[[STR_ARR_PRIVATIZER:_QFtest_allocatable_string_arrayEc_private_box_heap_Uxc8xU]] : [[TYPE:.*]] init {
1220
!CHECK: ^bb0(%[[ORIG_REF:.*]]: !fir.ref<[[TYPE]]>, %[[C_PVT_BOX_REF:.*]]: !fir.ref<[[TYPE]]>):
1321
!CHECK: %{{.*}} = fir.load %[[ORIG_REF]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>>
@@ -73,3 +81,11 @@ subroutine test_allocatable_string_array(n)
7381
!$omp parallel private(c)
7482
!$omp end parallel
7583
end subroutine
84+
85+
subroutine test_allocatable_fixed_len_string()
86+
character(42), allocatable :: fixed_len_str
87+
!$omp parallel do private(fixed_len_str)
88+
do i = 1,10
89+
end do
90+
!$omp end parallel do
91+
end subroutine

0 commit comments

Comments
 (0)