Skip to content

Commit d02ad56

Browse files
authored
[flang][OpenMP] Catch assumed-rank/size variables for privatization a… (llvm#152764)
…nd reduction Fixes llvm#152312 Assumed size is valid for privatization and simple examples generate sensible looking HLFIR.
1 parent dc84f3a commit d02ad56

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

flang/lib/Lower/Support/PrivateReductionUtils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,8 @@ void PopulateInitAndCleanupRegionsHelper::populateByRefInitAndCleanupRegions() {
616616
assert(sym && "Symbol information is required to privatize derived types");
617617
assert(!scalarInitValue && "ScalarInitvalue is unused for privatization");
618618
}
619+
if (hlfir::Entity{moldArg}.isAssumedRank())
620+
TODO(loc, "Privatization of assumed rank variable");
619621
mlir::Type valTy = fir::unwrapRefType(argType);
620622

621623
if (fir::isa_trivial(valTy)) {

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,7 +2891,8 @@ static bool CheckSymbolSupportsType(const Scope &scope,
28912891

28922892
static bool IsReductionAllowedForType(
28932893
const parser::OmpReductionIdentifier &ident, const DeclTypeSpec &type,
2894-
const Scope &scope, SemanticsContext &context) {
2894+
bool cannotBeBuiltinReduction, const Scope &scope,
2895+
SemanticsContext &context) {
28952896
auto isLogical{[](const DeclTypeSpec &type) -> bool {
28962897
return type.category() == DeclTypeSpec::Logical;
28972898
}};
@@ -2902,6 +2903,10 @@ static bool IsReductionAllowedForType(
29022903
auto checkOperator{[&](const parser::DefinedOperator &dOpr) {
29032904
if (const auto *intrinsicOp{
29042905
std::get_if<parser::DefinedOperator::IntrinsicOperator>(&dOpr.u)}) {
2906+
if (cannotBeBuiltinReduction) {
2907+
return false;
2908+
}
2909+
29052910
// OMP5.2: The type [...] of a list item that appears in a
29062911
// reduction clause must be valid for the combiner expression
29072912
// See F2023: Table 10.2
@@ -2953,16 +2958,18 @@ static bool IsReductionAllowedForType(
29532958
// IAND: arguments must be integers: F2023 16.9.100
29542959
// IEOR: arguments must be integers: F2023 16.9.106
29552960
// IOR: arguments must be integers: F2023 16.9.111
2956-
if (type.IsNumeric(TypeCategory::Integer)) {
2961+
if (type.IsNumeric(TypeCategory::Integer) &&
2962+
!cannotBeBuiltinReduction) {
29572963
return true;
29582964
}
29592965
} else if (realName == "max" || realName == "min") {
29602966
// MAX: arguments must be integer, real, or character:
29612967
// F2023 16.9.135
29622968
// MIN: arguments must be integer, real, or character:
29632969
// F2023 16.9.141
2964-
if (type.IsNumeric(TypeCategory::Integer) ||
2965-
type.IsNumeric(TypeCategory::Real) || isCharacter(type)) {
2970+
if ((type.IsNumeric(TypeCategory::Integer) ||
2971+
type.IsNumeric(TypeCategory::Real) || isCharacter(type)) &&
2972+
!cannotBeBuiltinReduction) {
29662973
return true;
29672974
}
29682975
}
@@ -2995,9 +3002,16 @@ void OmpStructureChecker::CheckReductionObjectTypes(
29953002
GetSymbolsInObjectList(objects, symbols);
29963003

29973004
for (auto &[symbol, source] : symbols) {
3005+
// Built in reductions require types which can be used in their initializer
3006+
// and combiner expressions. For example, for +:
3007+
// r = 0; r = r + r2
3008+
// But it might be valid to use these with DECLARE REDUCTION.
3009+
// Assumed size is already caught elsewhere.
3010+
bool cannotBeBuiltinReduction{evaluate::IsAssumedRank(*symbol)};
29983011
if (auto *type{symbol->GetType()}) {
29993012
const auto &scope{context_.FindScope(symbol->name())};
3000-
if (!IsReductionAllowedForType(ident, *type, scope, context_)) {
3013+
if (!IsReductionAllowedForType(
3014+
ident, *type, cannotBeBuiltinReduction, scope, context_)) {
30013015
context_.Say(source,
30023016
"The type of '%s' is incompatible with the reduction operator."_err_en_US,
30033017
symbol->name());
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! RUN: %not_todo_cmd %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
2+
3+
! CHECK: not yet implemented: Privatization of assumed rank variable
4+
subroutine assumedPriv(a)
5+
integer :: a(..)
6+
7+
!$omp parallel private(a)
8+
!$omp end parallel
9+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
2+
3+
! Types for built in reductions must have types which are valid for the
4+
! initialization and combiner expressions in the spec. This implies assumed
5+
! rank and assumed size cannot be used.
6+
7+
subroutine assumedRank1(a)
8+
integer :: a(..)
9+
10+
! ERROR: The type of 'a' is incompatible with the reduction operator.
11+
!$omp parallel reduction(+:a)
12+
!$omp end parallel
13+
end
14+
15+
subroutine assumedRank2(a)
16+
integer :: a(..)
17+
18+
! ERROR: The type of 'a' is incompatible with the reduction operator.
19+
!$omp parallel reduction(min:a)
20+
!$omp end parallel
21+
end
22+
23+
subroutine assumedRank3(a)
24+
integer :: a(..)
25+
26+
! ERROR: The type of 'a' is incompatible with the reduction operator.
27+
!$omp parallel reduction(iand:a)
28+
!$omp end parallel
29+
end
30+
31+
subroutine assumedSize1(a)
32+
integer :: a(*)
33+
34+
! ERROR: Whole assumed-size array 'a' may not appear here without subscripts
35+
!$omp parallel reduction(+:a)
36+
!$omp end parallel
37+
end
38+
39+
subroutine assumedSize2(a)
40+
integer :: a(*)
41+
42+
! ERROR: Whole assumed-size array 'a' may not appear here without subscripts
43+
!$omp parallel reduction(max:a)
44+
!$omp end parallel
45+
end
46+
47+
subroutine assumedSize3(a)
48+
integer :: a(*)
49+
50+
! ERROR: Whole assumed-size array 'a' may not appear here without subscripts
51+
!$omp parallel reduction(ior:a)
52+
!$omp end parallel
53+
end

0 commit comments

Comments
 (0)