Skip to content

[clang] [OpenMP] New OpenMP 6.0 self_maps clause #129888

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 3 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion clang/docs/OpenMPSupport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ implementation.
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
| Private reductions | :none:`unclaimed` | :none:`unclaimed` | |
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
| Self maps | :none:`unclaimed` | :none:`unclaimed` | |
| Self maps | :part:`partial` | :none:`unclaimed` | parsing/sema done: https://github.com/llvm/llvm-project/pull/129888 |
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
| Release map type for declare mapper | :none:`unclaimed` | :none:`unclaimed` | |
+-------------------------------------------------------------+---------------------------+---------------------------+--------------------------------------------------------------------------+
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ Python Binding Changes
OpenMP Support
--------------
- Added support 'no_openmp_constructs' assumption clause.
- Added support for 'self_maps' in map and requirement clause.

Improvements
^^^^^^^^^^^^
Expand Down
46 changes: 45 additions & 1 deletion clang/include/clang/AST/OpenMPClause.h
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,49 @@ class OMPAtomicDefaultMemOrderClause final : public OMPClause {
}
};

/// This represents 'self_maps' clause in the '#pragma omp requires'
/// directive.
///
/// \code
/// #pragma omp requires self_maps
/// \endcode
/// In this example directive '#pragma omp requires' has 'self_maps'
/// clause.
class OMPSelfMapsClause final : public OMPClause {
public:
friend class OMPClauseReader;
/// Build 'self_maps' clause.
///
/// \param StartLoc Starting location of the clause.
/// \param EndLoc Ending location of the clause.
OMPSelfMapsClause(SourceLocation StartLoc, SourceLocation EndLoc)
: OMPClause(llvm::omp::OMPC_self_maps, StartLoc, EndLoc) {}

/// Build an empty clause.
OMPSelfMapsClause()
: OMPClause(llvm::omp::OMPC_self_maps, SourceLocation(),
SourceLocation()) {}

child_range children() {
return child_range(child_iterator(), child_iterator());
}

const_child_range children() const {
return const_child_range(const_child_iterator(), const_child_iterator());
}

child_range used_children() {
return child_range(child_iterator(), child_iterator());
}
const_child_range used_children() const {
return const_child_range(const_child_iterator(), const_child_iterator());
}

static bool classof(const OMPClause *T) {
return T->getClauseKind() == llvm::omp::OMPC_self_maps;
}
};

/// This represents 'at' clause in the '#pragma omp error' directive
///
/// \code
Expand Down Expand Up @@ -6349,7 +6392,8 @@ class OMPMapClause final : public OMPMappableExprListClause<OMPMapClause>,
OpenMPMapModifierKind MapTypeModifiers[NumberOfOMPMapClauseModifiers] = {
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown};
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
OMPC_MAP_MODIFIER_unknown};

/// Location of map-type-modifiers for the 'map' clause.
SourceLocation MapTypeModifiersLoc[NumberOfOMPMapClauseModifiers];
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/AST/RecursiveASTVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3432,6 +3432,11 @@ bool RecursiveASTVisitor<Derived>::VisitOMPAtomicDefaultMemOrderClause(
return true;
}

template <typename Derived>
bool RecursiveASTVisitor<Derived>::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {
return true;
}

template <typename Derived>
bool RecursiveASTVisitor<Derived>::VisitOMPAtClause(OMPAtClause *) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ def note_previous_map_type_specified_here
: Note<"map type '%0' is previous specified here">;
def err_omp_unknown_map_type_modifier : Error<
"incorrect map type modifier, expected one of: 'always', 'close', 'mapper'"
"%select{|, 'present'|, 'present', 'iterator'}0%select{|, 'ompx_hold'}1">;
"%select{|, 'present'|, 'present', 'iterator'}0%select{|, 'ompx_hold'}1%select{|, 'self'}2">;
def err_omp_map_type_missing : Error<
"missing map type">;
def err_omp_map_type_modifier_missing : Error<
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/OpenMPKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ OPENMP_MAP_MODIFIER_KIND(close)
OPENMP_MAP_MODIFIER_KIND(mapper)
OPENMP_MAP_MODIFIER_KIND(iterator)
OPENMP_MAP_MODIFIER_KIND(present)
OPENMP_MAP_MODIFIER_KIND(self)
// This is an OpenMP extension for the sake of OpenACC support.
OPENMP_MAP_MODIFIER_KIND(ompx_hold)

Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Sema/SemaOpenMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,10 @@ class SemaOpenMP : public SemaBase {
OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindLoc,
SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc);

/// Called on well-formed 'self_maps' clause.
OMPClause *ActOnOpenMPSelfMapsClause(SourceLocation StartLoc,
SourceLocation EndLoc);

/// Called on well-formed 'at' clause.
OMPClause *ActOnOpenMPAtClause(OpenMPAtClauseKind Kind,
SourceLocation KindLoc,
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/AST/OpenMPClause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_atomic_default_mem_order:
case OMPC_self_maps:
case OMPC_at:
case OMPC_severity:
case OMPC_message:
Expand Down Expand Up @@ -259,6 +260,7 @@ const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C)
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_atomic_default_mem_order:
case OMPC_self_maps:
case OMPC_at:
case OMPC_severity:
case OMPC_message:
Expand Down Expand Up @@ -1941,6 +1943,10 @@ void OMPClausePrinter::VisitOMPAtomicDefaultMemOrderClause(
<< ")";
}

void OMPClausePrinter::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {
OS << "self_maps";
}

void OMPClausePrinter::VisitOMPAtClause(OMPAtClause *Node) {
OS << "at(" << getOpenMPSimpleClauseTypeName(OMPC_at, Node->getAtKind())
<< ")";
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/AST/StmtProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ void OMPClauseProfiler::VisitOMPDynamicAllocatorsClause(
void OMPClauseProfiler::VisitOMPAtomicDefaultMemOrderClause(
const OMPAtomicDefaultMemOrderClause *C) {}

void OMPClauseProfiler::VisitOMPSelfMapsClause(const OMPSelfMapsClause *C) {}

void OMPClauseProfiler::VisitOMPAtClause(const OMPAtClause *C) {}

void OMPClauseProfiler::VisitOMPSeverityClause(const OMPSeverityClause *C) {}
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/OpenMPKinds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
case OMPC_unified_shared_memory:
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_self_maps:
case OMPC_match:
case OMPC_nontemporal:
case OMPC_destroy:
Expand Down Expand Up @@ -569,6 +570,7 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
case OMPC_unified_shared_memory:
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_self_maps:
case OMPC_match:
case OMPC_nontemporal:
case OMPC_destroy:
Expand Down
33 changes: 31 additions & 2 deletions clang/lib/Parse/ParseOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3343,6 +3343,20 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
ErrorFound = true;
}

Clause = ParseOpenMPClause(CKind, WrongDirective);
break;
case OMPC_self_maps:
// OpenMP [6.0, self_maps clause]
if (getLangOpts().OpenMP < 60) {
Diag(Tok, diag::err_omp_expected_clause)
<< getOpenMPDirectiveName(OMPD_requires);
ErrorFound = true;
}
if (!FirstClause) {
Diag(Tok, diag::err_omp_more_one_clause)
<< getOpenMPDirectiveName(DKind) << getOpenMPClauseName(CKind) << 0;
ErrorFound = true;
}
Clause = ParseOpenMPClause(CKind, WrongDirective);
break;
case OMPC_update:
Expand Down Expand Up @@ -4323,6 +4337,20 @@ bool Parser::parseMapTypeModifiers(SemaOpenMP::OpenMPVarListDataTy &Data) {
<< PreMapName;
}
ConsumeToken();
} else if (TypeModifier == OMPC_MAP_MODIFIER_self) {
Data.MapTypeModifiers.push_back(TypeModifier);
Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
if (PP.LookAhead(0).isNot(tok::comma) &&
PP.LookAhead(0).isNot(tok::colon))
Diag(Tok.getLocation(), diag::err_omp_missing_comma)
<< "map type modifier";
if (getLangOpts().OpenMP < 60)
Diag(Tok, diag::err_omp_unknown_map_type_modifier)
<< (getLangOpts().OpenMP >= 51
? (getLangOpts().OpenMP >= 52 ? 2 : 1)
: 0)
<< getLangOpts().OpenMPExtensions << 0;
ConsumeToken();
} else {
// For the case of unknown map-type-modifier or a map-type.
// Map-type is followed by a colon; the function returns when it
Expand All @@ -4344,7 +4372,8 @@ bool Parser::parseMapTypeModifiers(SemaOpenMP::OpenMPVarListDataTy &Data) {
Diag(Tok, diag::err_omp_unknown_map_type_modifier)
<< (getLangOpts().OpenMP >= 51 ? (getLangOpts().OpenMP >= 52 ? 2 : 1)
: 0)
<< getLangOpts().OpenMPExtensions;
<< getLangOpts().OpenMPExtensions
<< (getLangOpts().OpenMP >= 60 ? 1 : 0);
ConsumeToken();
}
if (getCurToken().is(tok::comma))
Expand Down Expand Up @@ -4819,7 +4848,7 @@ bool Parser::ParseOpenMPVarList(OpenMPDirectiveKind DKind,
if (getLangOpts().OpenMP < 52) {
Diag(Tok, diag::err_omp_unknown_map_type_modifier)
<< (getLangOpts().OpenMP >= 51 ? 1 : 0)
<< getLangOpts().OpenMPExtensions;
<< getLangOpts().OpenMPExtensions << 0;
InvalidIterator = true;
}
}
Expand Down
16 changes: 15 additions & 1 deletion clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6620,6 +6620,7 @@ StmtResult SemaOpenMP::ActOnOpenMPExecutableDirective(
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_atomic_default_mem_order:
case OMPC_self_maps:
case OMPC_device_type:
case OMPC_match:
case OMPC_when:
Expand Down Expand Up @@ -15273,6 +15274,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_atomic_default_mem_order:
case OMPC_self_maps:
case OMPC_device_type:
case OMPC_match:
case OMPC_nontemporal:
Expand Down Expand Up @@ -15928,6 +15930,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPSimpleClause(
case OMPC_unified_shared_memory:
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_self_maps:
case OMPC_device_type:
case OMPC_match:
case OMPC_nontemporal:
Expand Down Expand Up @@ -16400,6 +16403,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPSingleExprWithArgClause(
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_atomic_default_mem_order:
case OMPC_self_maps:
case OMPC_device_type:
case OMPC_match:
case OMPC_nontemporal:
Expand Down Expand Up @@ -16607,6 +16611,9 @@ OMPClause *SemaOpenMP::ActOnOpenMPClause(OpenMPClauseKind Kind,
case OMPC_dynamic_allocators:
Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
break;
case OMPC_self_maps:
Res = ActOnOpenMPSelfMapsClause(StartLoc, EndLoc);
break;
case OMPC_destroy:
Res = ActOnOpenMPDestroyClause(/*InteropVar=*/nullptr, StartLoc,
/*LParenLoc=*/SourceLocation(),
Expand Down Expand Up @@ -16817,6 +16824,11 @@ SemaOpenMP::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
return new (getASTContext()) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
}

OMPClause *SemaOpenMP::ActOnOpenMPSelfMapsClause(SourceLocation StartLoc,
SourceLocation EndLoc) {
return new (getASTContext()) OMPSelfMapsClause(StartLoc, EndLoc);
}

StmtResult
SemaOpenMP::ActOnOpenMPInteropDirective(ArrayRef<OMPClause *> Clauses,
SourceLocation StartLoc,
Expand Down Expand Up @@ -17289,6 +17301,7 @@ OMPClause *SemaOpenMP::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
case OMPC_reverse_offload:
case OMPC_dynamic_allocators:
case OMPC_atomic_default_mem_order:
case OMPC_self_maps:
case OMPC_device_type:
case OMPC_match:
case OMPC_order:
Expand Down Expand Up @@ -21697,7 +21710,8 @@ OMPClause *SemaOpenMP::ActOnOpenMPMapClause(
OpenMPMapModifierKind Modifiers[] = {
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown};
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
OMPC_MAP_MODIFIER_unknown};
SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];

if (IteratorModifier && !IteratorModifier->getType()->isSpecificBuiltinType(
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -10813,6 +10813,12 @@ OMPClause *TreeTransform<Derived>::TransformOMPAtomicDefaultMemOrderClause(
"atomic_default_mem_order clause cannot appear in dependent context");
}

template <typename Derived>
OMPClause *
TreeTransform<Derived>::TransformOMPSelfMapsClause(OMPSelfMapsClause *C) {
llvm_unreachable("self_maps clause cannot appear in dependent context");
}

template <typename Derived>
OMPClause *TreeTransform<Derived>::TransformOMPAtClause(OMPAtClause *C) {
return getDerived().RebuildOMPAtClause(C->getAtKind(), C->getAtKindKwLoc(),
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11101,6 +11101,9 @@ OMPClause *OMPClauseReader::readClause() {
case llvm::omp::OMPC_atomic_default_mem_order:
C = new (Context) OMPAtomicDefaultMemOrderClause();
break;
case llvm::omp::OMPC_self_maps:
C = new (Context) OMPSelfMapsClause();
break;
case llvm::omp::OMPC_at:
C = new (Context) OMPAtClause();
break;
Expand Down Expand Up @@ -11582,6 +11585,8 @@ void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
}

void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}

void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {
C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));
C->setLParenLoc(Record.readSourceLocation());
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8439,6 +8439,8 @@ void OMPClauseWriter::VisitOMPAtomicDefaultMemOrderClause(
Record.AddSourceLocation(C->getAtomicDefaultMemOrderKindKwLoc());
}

void OMPClauseWriter::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}

void OMPClauseWriter::VisitOMPAtClause(OMPAtClause *C) {
Record.push_back(C->getAtKind());
Record.AddSourceLocation(C->getLParenLoc());
Expand Down
9 changes: 9 additions & 0 deletions clang/test/OpenMP/requires_ast_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
// RUN: %clang_cc1 -verify -fopenmp-simd -fopenmp-version=99 -DOMP99 -ast-print %s | FileCheck --check-prefixes=CHECK,REV %s
// RUN: %clang_cc1 -fopenmp-simd -fopenmp-version=99 -DOMP99 -x c++ -std=c++11 -emit-pch -o %t %s
// RUN: %clang_cc1 -fopenmp-simd -fopenmp-version=99 -DOMP99 -std=c++11 -include-pch %t -verify %s -ast-print | FileCheck --check-prefixes=CHECK,REV %s

// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=60 -DOMP60 -ast-print %s | FileCheck --check-prefixes=CHECK,CHECK_SELF %s
// RUN: %clang_cc1 -fopenmp -fopenmp-version=60 -DOMP60 -x c++ -std=c++11 -emit-pch -o %t %s
// RUN: %clang_cc1 -fopenmp -fopenmp-version=60 -DOMP60 -std=c++11 -include-pch %t -verify %s -ast-print | FileCheck --check-prefixes=CHECK,CHECK_SELF %s
// expected-no-diagnostics

#ifndef HEADER
Expand All @@ -24,6 +28,11 @@
#pragma omp requires unified_shared_memory
// CHECK:#pragma omp requires unified_shared_memory

#ifdef OMP60
#pragma omp requires self_maps
// CHECK_SELF:#pragma omp requires self_maps
#endif

#ifdef OMP99
#pragma omp requires reverse_offload
// REV:#pragma omp requires reverse_offload
Expand Down
7 changes: 7 additions & 0 deletions clang/test/OpenMP/requires_messages.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %clang_cc1 -verify -fopenmp -ferror-limit 100 %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=99 -DOMP99 -verify=expected,rev -ferror-limit 100 %s -Wuninitialized
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=60 -DOMP60 -verify=self -ferror-limit 100 %s -Wuninitialized

int a;
#pragma omp requires unified_address allocate(a) // rev-note {{unified_address clause previously used here}} expected-note {{unified_address clause previously used here}} expected-note {{unified_address clause previously used here}} expected-note {{unified_address clause previously used here}} expected-note {{unified_address clause previously used here}} expected-note{{unified_address clause previously used here}} expected-error {{unexpected OpenMP clause 'allocate' in directive '#pragma omp requires'}}
Expand All @@ -22,6 +23,12 @@ int a;

#pragma omp requires dynamic_allocators, dynamic_allocators // expected-error {{only one dynamic_allocators clause can appear on a requires directive in a single translation unit}} expected-error {{directive '#pragma omp requires' cannot contain more than one 'dynamic_allocators' clause}}

#ifdef OMP60
#pragma omp requires self_maps // self-note {{self_maps clause previously used here}}

#pragma omp requires self_maps, self_maps // self-error {{only one self_maps clause can appear on a requires directive in a single translation unit}} self-error {{directive '#pragma omp requires' cannot contain more than one 'self_maps' clause}}
#endif

#pragma omp requires atomic_default_mem_order(seq_cst) // rev-note {{atomic_default_mem_order clause previously used here}} expected-note {{atomic_default_mem_order clause previously used here}} expected-note {{atomic_default_mem_order clause previously used here}} expected-note {{atomic_default_mem_order clause previously used here}} expected-note {{atomic_default_mem_order clause previously used here}}

#pragma omp requires atomic_default_mem_order(acq_rel) // expected-error {{only one atomic_default_mem_order clause can appear on a requires directive in a single translation unit}}
Expand Down
Loading