Skip to content
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: 3 additions & 4 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,7 @@ getImplicitMapTypeAndKind(fir::FirOpBuilder &firOpBuilder,
mlir::omp::VariableCaptureKind::ByRef);
break;
case DefMap::ImplicitBehavior::Firstprivate:
case DefMap::ImplicitBehavior::None:
TODO(loc, "Firstprivate and None are currently unsupported defaultmap "
"behaviour");
TODO(loc, "Firstprivate is currently unsupported defaultmap behaviour");
break;
case DefMap::ImplicitBehavior::From:
return std::make_pair(mapFlag |= mlir::omp::ClauseMapFlags::from,
Expand All @@ -1032,8 +1030,9 @@ getImplicitMapTypeAndKind(fir::FirOpBuilder &firOpBuilder,
mlir::omp::VariableCaptureKind::ByRef);
break;
case DefMap::ImplicitBehavior::Default:
case DefMap::ImplicitBehavior::None:
llvm_unreachable(
"Implicit None Behaviour Should Have Been Handled Earlier");
"Implicit None and Default behaviour should have been handled earlier");
break;
}

Expand Down
91 changes: 91 additions & 0 deletions flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2965,6 +2965,67 @@ void OmpAttributeVisitor::CreateImplicitSymbols(const Symbol *symbol) {
}
}

static bool IsOpenMPPointer(const Symbol &symbol) {
if (IsPointer(symbol) || IsBuiltinCPtr(symbol))
return true;
return false;
}

static bool IsOpenMPAggregate(const Symbol &symbol) {
if (IsAllocatable(symbol) || IsOpenMPPointer(symbol))
return false;

const auto *type{symbol.GetType()};
// OpenMP categorizes Fortran characters as aggregates.
if (type->category() == Fortran::semantics::DeclTypeSpec::Category::Character)
return true;

if (const auto *det{symbol.GetUltimate()
.detailsIf<Fortran::semantics::ObjectEntityDetails>()})
if (det->IsArray())
return true;

if (type->AsDerived())
return true;

if (IsDeferredShape(symbol) || IsAssumedRank(symbol) ||
IsAssumedShape(symbol))
return true;
return false;
}

static bool IsOpenMPScalar(const Symbol &symbol) {
if (IsOpenMPAggregate(symbol) || IsOpenMPPointer(symbol) ||
IsAllocatable(symbol))
return false;
const auto *type{symbol.GetType()};
if ((!symbol.GetShape() || symbol.GetShape()->empty()) &&
(type->category() ==
Fortran::semantics::DeclTypeSpec::Category::Numeric ||
type->category() ==
Fortran::semantics::DeclTypeSpec::Category::Logical))
return true;
return false;
}

static bool DefaultMapCategoryMatchesSymbol(
parser::OmpVariableCategory::Value category, const Symbol &symbol) {
using VarCat = parser::OmpVariableCategory::Value;
switch (category) {
case VarCat::Scalar:
return IsOpenMPScalar(symbol);
case VarCat::Allocatable:
return IsAllocatable(symbol);
case VarCat::Aggregate:
return IsOpenMPAggregate(symbol);
case VarCat::Pointer:
return IsOpenMPPointer(symbol);
case VarCat::All:
return true;
}
return false;
}

// For OpenMP constructs, check all the data-refs within the constructs
// and adjust the symbol for each Name if necessary
void OmpAttributeVisitor::Post(const parser::Name &name) {
Expand Down Expand Up @@ -3000,6 +3061,36 @@ void OmpAttributeVisitor::Post(const parser::Name &name) {
}
}

// TODO: handle case where default and defaultmap are present on the same
// construct and conflict, defaultmap should supersede default if they
// conflict.
if (!GetContext().defaultMap.empty()) {
// Checked before implicit data sharing attributes as this rule ignores
// them and expects explicit predetermined/specified attributes to be in
// place for the types specified.
if (Symbol * found{currScope().FindSymbol(name.source)}) {
// If the variable has declare target applied to it (enter or link) it
// is exempt from defaultmap(none) restrictions
if (!symbol->GetUltimate().test(Symbol::Flag::OmpDeclareTarget)) {
auto &dMap = GetContext().defaultMap;
for (auto defaults : dMap) {
if (defaults.second ==
parser::OmpDefaultmapClause::ImplicitBehavior::None) {
if (DefaultMapCategoryMatchesSymbol(defaults.first, *found)) {
if (!IsObjectWithDSA(*symbol)) {
context_.Say(name.source,
"The DEFAULTMAP(NONE) clause requires that '%s' must be "
"listed in a "
"data-sharing attribute, data-mapping attribute, or is_device_ptr clause"_err_en_US,
symbol->name());
}
}
}
}
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole thing above really belongs in check-omp-structure. Would it be hard to move it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too sure, I can try to do so on Monday when I look at updating the PR! The main reason it's here is that the equivalent check for default is above, and there is some interconnection between the two clauses if they appear on the same construct that I'll need to handle in a follow up PR.

The only thing I can think of that might be a problem (aside from needing to supersede the default check above) is if check-omp-structure occurs after resolve-directives, as resolve-directives applies implicit DSAs to things almost immediately after this and from my understanding (from my admittedly shaky reading of the specification and testing with Clang at least) if you apply defaultmap(none), it'd require everything to be explicitly specified in a data sharing attribute or data mapping attribute (or use_device/has_device etc.). So the implicit application of the DSAs below would cause issues (and from last I checked they don't all get the implicit keyword applied at the moment as it impacts some other handling elsewhere, so it's likely not possible to check if all of the DSAs have appeared implicitly or if a user explicitly specified it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.s. Thank you for the quick review!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setting of flags on a symbol should be done here, but error reporting should go to check-omp-structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aware of that, I'd have to move the checking for default(none) as well as defaultmap(none) and we wouldn't want to set the flags on these symbols implicitly here and skip them when things are set to none, I'm not sure if there would be any fallout from here to check-omp-structure because of that, will see soon enough I suppose.

Copy link
Contributor

@kparzysz kparzysz Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the second thought, feel free to leave it here. I'll have to investigate a bit more closely how we handle DSA and mapping attributes here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good @kparzysz thank you very much. I'm a little sparse on knowledge for it myself, other than having worked with this segment of code a little bit when adding the implicit firstprivate DSA, from what I recollect the segment directly after this applies implicit DSA's (firstprivate etc.), but we don't mark every implicitly applied DSA with implicit, if this was fixed I imagine moving both the defaultmap/default segments to check-omp-structure would be fairly easy (if it isn't already and I'm overthinking it) as we could just check if the DSA's are implicitly generated and error out if they are. I recall there being an issue that the implicit flag was tied to some other CodeGen related things, making it not quite possible to tag everything that's implicit with it currently. Perhaps a new flag could be used in its place but that doesn't seem like the most elegant solution.

if (Symbol * found{currScope().FindSymbol(name.source)}) {
if (found->GetUltimate().test(semantics::Symbol::Flag::OmpThreadprivate))
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ subroutine f00
! NOTE: This is implemented for scalars as it is the default behaviour, so we utilise
! a different data type.
integer, allocatable :: i
!CHECK: not yet implemented: Firstprivate and None are currently unsupported defaultmap behaviour
!CHECK: not yet implemented: Firstprivate is currently unsupported defaultmap behaviour
!$omp target defaultmap(firstprivate)
i = 10
!$omp end target
Expand Down
11 changes: 0 additions & 11 deletions flang/test/Lower/OpenMP/Todo/defaultmap-clause-none.f90

This file was deleted.

96 changes: 96 additions & 0 deletions flang/test/Semantics/OpenMP/defaultmap-clause-none.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=51

subroutine defaultmap_all_none_no_errors
implicit none
real :: array(10)
integer, pointer :: ptr(:)
real, allocatable :: alloca
integer :: index

!$omp target defaultmap(none) map(to: index, alloca) map(tofrom: array, ptr)
do index = 1, 10
ptr(index) = array(index) + alloca
end do
!$omp end target
end subroutine defaultmap_all_none_no_errors

subroutine defaultmap_all_none
implicit none
real :: array(10)
integer, pointer :: ptr(:)
real, allocatable :: alloca
integer :: index
!$omp target defaultmap(none)
!ERROR: The DEFAULTMAP(NONE) clause requires that 'index' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
do index = 1, 10
!ERROR: The DEFAULTMAP(NONE) clause requires that 'ptr' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
!ERROR: The DEFAULTMAP(NONE) clause requires that 'index' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
!ERROR: The DEFAULTMAP(NONE) clause requires that 'array' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
!ERROR: The DEFAULTMAP(NONE) clause requires that 'index' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
!ERROR: The DEFAULTMAP(NONE) clause requires that 'alloca' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
ptr(index) = array(index) + alloca
end do
!$omp end target
end subroutine defaultmap_all_none

subroutine defaultmap_scalar_none
implicit none
real :: array(10)
integer, pointer :: ptr(:)
real, allocatable :: alloca
integer :: index

!$omp target defaultmap(none: scalar)
!ERROR: The DEFAULTMAP(NONE) clause requires that 'index' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
do index = 1, 10
!ERROR: The DEFAULTMAP(NONE) clause requires that 'index' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
!ERROR: The DEFAULTMAP(NONE) clause requires that 'index' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
ptr(index) = array(index) + alloca
end do
!$omp end target
end subroutine defaultmap_scalar_none

subroutine defaultmap_pointer_none
implicit none
real :: array(10)
integer, pointer :: ptr(:)
real, allocatable :: alloca
integer :: index

!$omp target defaultmap(none: pointer)
do index = 1, 10
!ERROR: The DEFAULTMAP(NONE) clause requires that 'ptr' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
ptr(index) = array(index) + alloca
end do
!$omp end target
end subroutine defaultmap_pointer_none

subroutine defaultmap_allocatable_none
implicit none
real :: array(10)
integer, pointer :: ptr(:)
real, allocatable :: alloca
integer :: index

!$omp target defaultmap(none: allocatable)
do index = 1, 10
!ERROR: The DEFAULTMAP(NONE) clause requires that 'alloca' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
ptr(index) = array(index) + alloca
end do
!$omp end target
end subroutine defaultmap_allocatable_none

subroutine defaultmap_aggregate_none
implicit none
real :: array(10)
integer, pointer :: ptr(:)
real, allocatable :: alloca
integer :: index

!$omp target defaultmap(none: aggregate)
do index = 1, 10
!ERROR: The DEFAULTMAP(NONE) clause requires that 'array' must be listed in a data-sharing attribute, data-mapping attribute, or is_device_ptr clause
ptr(index) = array(index) + alloca
end do
!$omp end target
end subroutine defaultmap_aggregate_none