Skip to content

[flang][OpenMP] Handle fixed length charaters in delayed privatization #126704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions flang/include/flang/Optimizer/Dialect/FIRType.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ inline mlir::Type unwrapPassByRefType(mlir::Type t) {
return t;
}

/// Extracts the innermost type, T, **potentially** wrapped inside:
/// <fir.[ref|ptr|heap] <fir.[ref|ptr|heap|box] <fir.array<T>>>
///
/// Any element absent from the above pattern does not affect the returned
/// value: T.
mlir::Type getFortranElementType(mlir::Type ty);

/// Unwrap either a sequence or a boxed sequence type, returning the element
/// type of the sequence type.
/// e.g.,
Expand Down
11 changes: 5 additions & 6 deletions flang/lib/Lower/OpenMP/PrivateReductionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,11 @@ static void getLengthParameters(fir::FirOpBuilder &builder, mlir::Location loc,
// The verifier for EmboxOp doesn't allow length parameters when the the
// character already has static LEN. genLengthParameters may still return them
// in this case.
mlir::Type unwrappedType =
fir::unwrapRefType(fir::unwrapSeqOrBoxedSeqType(moldArg.getType()));
if (auto strTy = mlir::dyn_cast<fir::CharacterType>(unwrappedType)) {
if (strTy.hasConstantLen())
lenParams.resize(0);
}
auto strTy = mlir::dyn_cast<fir::CharacterType>(
fir::getFortranElementType(moldArg.getType()));

if (strTy && strTy.hasConstantLen())
lenParams.resize(0);
}

static bool
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Optimizer/Dialect/FIRType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ mlir::Type unwrapAllRefAndSeqType(mlir::Type ty) {
}
}

mlir::Type getFortranElementType(mlir::Type ty) {
return fir::unwrapSequenceType(
fir::unwrapPassByRefType(fir::unwrapRefType(ty)));
}

mlir::Type unwrapSeqOrBoxedSeqType(mlir::Type ty) {
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(ty))
return seqTy.getEleTy();
Expand Down
16 changes: 16 additions & 0 deletions flang/test/Lower/OpenMP/parallel-private-clause-str.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 \
! RUN: | FileCheck %s

!CHECK: omp.private {type = private} @{{.*}}test_allocatable_fixed_len_stringEfixed_len_str{{.*}} init {
!CHECK: fir.if {{.*}} {
!CHECK: fir.embox %{{[^[:space:]]+}} : {{.*}}
!CHECK: } else {
!CHECK: fir.embox %{{[^[:space:]]+}} : {{.*}}
!CHECK: }
!CHECK: }

!CHECK: omp.private {type = private} @[[STR_ARR_PRIVATIZER:_QFtest_allocatable_string_arrayEc_private_box_heap_Uxc8xU]] : [[TYPE:.*]] init {
!CHECK: ^bb0(%[[ORIG_REF:.*]]: !fir.ref<[[TYPE]]>, %[[C_PVT_BOX_REF:.*]]: !fir.ref<[[TYPE]]>):
!CHECK: %{{.*}} = fir.load %[[ORIG_REF]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x!fir.char<1,?>>>>>
Expand Down Expand Up @@ -73,3 +81,11 @@ subroutine test_allocatable_string_array(n)
!$omp parallel private(c)
!$omp end parallel
end subroutine

subroutine test_allocatable_fixed_len_string()
character(42), allocatable :: fixed_len_str
!$omp parallel do private(fixed_len_str)
do i = 1,10
end do
!$omp end parallel do
end subroutine