Skip to content

Commit dd9335c

Browse files
committed
[flang][OpenMP] Handle fixed length charaters in delayed privatization
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 llvm#125732.
1 parent 9d134f2 commit dd9335c

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

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

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

279+
/// Unwrap either a reference or a boxed reference type, returning the
280+
/// (possibly)-boxed and referenced type.
281+
mlir::Type unwrapRefOrBoxedRefType(mlir::Type ty);
282+
279283
/// Unwrap either a sequence or a boxed sequence type, returning the element
280284
/// type of the sequence type.
281285
/// e.g.,

flang/lib/Lower/OpenMP/PrivateReductionUtils.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,20 @@ 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 = [&]() -> fir::CharacterType {
201+
if (auto result = mlir::dyn_cast<fir::CharacterType>(
202+
fir::unwrapRefOrBoxedRefType(moldArg.getType())))
203+
return result;
204+
205+
if (auto result = mlir::dyn_cast<fir::CharacterType>(fir::unwrapRefType(
206+
fir::unwrapSeqOrBoxedSeqType(moldArg.getType()))))
207+
return result;
208+
209+
return nullptr;
210+
}();
211+
212+
if (strTy && strTy.hasConstantLen())
213+
lenParams.resize(0);
206214
}
207215

208216
static bool

flang/lib/Optimizer/Dialect/FIRType.cpp

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

429+
mlir::Type unwrapRefOrBoxedRefType(mlir::Type ty) {
430+
mlir::Type eleTy;
431+
432+
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(ty))
433+
eleTy = boxTy.getEleTy();
434+
435+
return unwrapRefType(eleTy);
436+
}
437+
429438
mlir::Type unwrapSeqOrBoxedSeqType(mlir::Type ty) {
430439
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(ty))
431440
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)