Skip to content

Commit 347f3c1

Browse files
alokmishra.besuchichunchen
authored andcommitted
OpenMP 5.0 metadirective
This patch supports OpenMP 5.0 metadirective features. It is implemented keeping the OpenMP 5.1 features like dynamic user condition in mind. A new function, getBestWhenMatchForContext, is defined in llvm/Frontend/OpenMP/OMPContext.h Currently this function return the index of the when clause with the highest score from the ones applicable in the Context. But this function is declared with an array which can be used in OpenMP 5.1 implementation to select all the valid when clauses which can be resolved in runtime. Currently this array is set to null by default and its implementation is left for future. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D91944
1 parent 7efb825 commit 347f3c1

35 files changed

+773
-5
lines changed

clang/include/clang-c/Index.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,11 @@ enum CXCursorKind {
25922592
*/
25932593
CXCursor_OMPUnrollDirective = 293,
25942594

2595-
CXCursor_LastStmt = CXCursor_OMPUnrollDirective,
2595+
/** OpenMP metadirective directive.
2596+
*/
2597+
CXCursor_OMPMetaDirective = 294,
2598+
2599+
CXCursor_LastStmt = CXCursor_OMPMetaDirective,
25962600

25972601
/**
25982602
* Cursor that represents the translation unit itself.

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,6 +2842,9 @@ RecursiveASTVisitor<Derived>::TraverseOMPLoopDirective(OMPLoopDirective *S) {
28422842
return TraverseOMPExecutableDirective(S);
28432843
}
28442844

2845+
DEF_TRAVERSE_STMT(OMPMetaDirective,
2846+
{ TRY_TO(TraverseOMPExecutableDirective(S)); })
2847+
28452848
DEF_TRAVERSE_STMT(OMPParallelDirective,
28462849
{ TRY_TO(TraverseOMPExecutableDirective(S)); })
28472850

clang/include/clang/AST/StmtOpenMP.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5379,6 +5379,44 @@ class OMPMaskedDirective final : public OMPExecutableDirective {
53795379
}
53805380
};
53815381

5382+
/// This represents '#pragma omp metadirective' directive.
5383+
///
5384+
/// \code
5385+
/// #pragma omp metadirective when(user={condition(N>10)}: parallel for)
5386+
/// \endcode
5387+
/// In this example directive '#pragma omp metadirective' has clauses 'when'
5388+
/// with a dynamic user condition to check if a variable 'N > 10'
5389+
///
5390+
class OMPMetaDirective final : public OMPExecutableDirective {
5391+
friend class ASTStmtReader;
5392+
friend class OMPExecutableDirective;
5393+
Stmt *IfStmt;
5394+
5395+
OMPMetaDirective(SourceLocation StartLoc, SourceLocation EndLoc)
5396+
: OMPExecutableDirective(OMPMetaDirectiveClass,
5397+
llvm::omp::OMPD_metadirective, StartLoc,
5398+
EndLoc) {}
5399+
explicit OMPMetaDirective()
5400+
: OMPExecutableDirective(OMPMetaDirectiveClass,
5401+
llvm::omp::OMPD_metadirective, SourceLocation(),
5402+
SourceLocation()) {}
5403+
5404+
void setIfStmt(Stmt *S) { IfStmt = S; }
5405+
5406+
public:
5407+
static OMPMetaDirective *Create(const ASTContext &C, SourceLocation StartLoc,
5408+
SourceLocation EndLoc,
5409+
ArrayRef<OMPClause *> Clauses,
5410+
Stmt *AssociatedStmt, Stmt *IfStmt);
5411+
static OMPMetaDirective *CreateEmpty(const ASTContext &C, unsigned NumClauses,
5412+
EmptyShell);
5413+
Stmt *getIfStmt() const { return IfStmt; }
5414+
5415+
static bool classof(const Stmt *T) {
5416+
return T->getStmtClass() == OMPMetaDirectiveClass;
5417+
}
5418+
};
5419+
53825420
} // end namespace clang
53835421

53845422
#endif

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,9 @@ def warn_omp51_compat_attributes : Warning<
14361436
"specifying OpenMP directives with [[]] is incompatible with OpenMP "
14371437
"standards before OpenMP 5.1">,
14381438
InGroup<OpenMPPre51Compat>, DefaultIgnore;
1439+
def err_omp_expected_colon : Error<"missing ':' in %0">;
1440+
def err_omp_expected_context_selector
1441+
: Error<"expected valid context selector in %0">;
14391442

14401443
// Pragma loop support.
14411444
def err_pragma_loop_missing_argument : Error<

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10794,6 +10794,8 @@ def err_omp_dispatch_statement_call
1079410794
def err_omp_unroll_full_variable_trip_count : Error<
1079510795
"loop to be fully unrolled must have a constant trip count">;
1079610796
def note_omp_directive_here : Note<"'%0' directive found here">;
10797+
def err_omp_instantiation_not_supported
10798+
: Error<"instantiation of '%0' not supported yet">;
1079710799
} // end of OpenMP category
1079810800

1079910801
let CategoryName = "Related Result Type Issue" in {

clang/include/clang/Basic/StmtNodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def AsTypeExpr : StmtNode<Expr>;
219219
// OpenMP Directives.
220220
def OMPCanonicalLoop : StmtNode<Stmt>;
221221
def OMPExecutableDirective : StmtNode<Stmt, 1>;
222+
def OMPMetaDirective : StmtNode<OMPExecutableDirective>;
222223
def OMPLoopBasedDirective : StmtNode<OMPExecutableDirective, 1>;
223224
def OMPLoopDirective : StmtNode<OMPLoopBasedDirective, 1>;
224225
def OMPParallelDirective : StmtNode<OMPExecutableDirective>;

clang/include/clang/Sema/Sema.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10456,6 +10456,12 @@ class Sema final {
1045610456
/// \param Init First part of the for loop.
1045710457
void ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init);
1045810458

10459+
/// Called on well-formed '\#pragma omp metadirective' after parsing
10460+
/// of the associated statement.
10461+
StmtResult ActOnOpenMPMetaDirective(ArrayRef<OMPClause *> Clauses,
10462+
Stmt *AStmt, SourceLocation StartLoc,
10463+
SourceLocation EndLoc);
10464+
1045910465
// OpenMP directives and clauses.
1046010466
/// Called on correct id-expression from the '#pragma omp
1046110467
/// threadprivate'.
@@ -11023,6 +11029,10 @@ class Sema final {
1102311029
SourceLocation StartLoc,
1102411030
SourceLocation LParenLoc,
1102511031
SourceLocation EndLoc);
11032+
/// Called on well-formed 'when' clause.
11033+
OMPClause *ActOnOpenMPWhenClause(OMPTraitInfo &TI, SourceLocation StartLoc,
11034+
SourceLocation LParenLoc,
11035+
SourceLocation EndLoc);
1102611036
/// Called on well-formed 'default' clause.
1102711037
OMPClause *ActOnOpenMPDefaultClause(llvm::omp::DefaultKind Kind,
1102811038
SourceLocation KindLoc,

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,7 @@ enum StmtCode {
18931893
STMT_SEH_TRY, // SEHTryStmt
18941894

18951895
// OpenMP directives
1896+
STMT_OMP_META_DIRECTIVE,
18961897
STMT_OMP_CANONICAL_LOOP,
18971898
STMT_OMP_PARALLEL_DIRECTIVE,
18981899
STMT_OMP_SIMD_DIRECTIVE,

clang/lib/AST/OpenMPClause.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
160160
case OMPC_exclusive:
161161
case OMPC_uses_allocators:
162162
case OMPC_affinity:
163+
case OMPC_when:
163164
break;
164165
default:
165166
break;
@@ -257,6 +258,7 @@ const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C)
257258
case OMPC_exclusive:
258259
case OMPC_uses_allocators:
259260
case OMPC_affinity:
261+
case OMPC_when:
260262
break;
261263
default:
262264
break;

clang/lib/AST/StmtOpenMP.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,25 @@ void OMPLoopDirective::setFinalsConditions(ArrayRef<Expr *> A) {
253253
llvm::copy(A, getFinalsConditions().begin());
254254
}
255255

256+
OMPMetaDirective *OMPMetaDirective::Create(const ASTContext &C,
257+
SourceLocation StartLoc,
258+
SourceLocation EndLoc,
259+
ArrayRef<OMPClause *> Clauses,
260+
Stmt *AssociatedStmt, Stmt *IfStmt) {
261+
auto *Dir = createDirective<OMPMetaDirective>(
262+
C, Clauses, AssociatedStmt, /*NumChildren=*/1, StartLoc, EndLoc);
263+
Dir->setIfStmt(IfStmt);
264+
return Dir;
265+
}
266+
267+
OMPMetaDirective *OMPMetaDirective::CreateEmpty(const ASTContext &C,
268+
unsigned NumClauses,
269+
EmptyShell) {
270+
return createEmptyDirective<OMPMetaDirective>(C, NumClauses,
271+
/*HasAssociatedStmt=*/true,
272+
/*NumChildren=*/1);
273+
}
274+
256275
OMPParallelDirective *OMPParallelDirective::Create(
257276
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
258277
ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt, Expr *TaskRedRef,

clang/lib/AST/StmtPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,11 @@ void StmtPrinter::PrintOMPExecutableDirective(OMPExecutableDirective *S,
654654
PrintStmt(S->getRawStmt());
655655
}
656656

657+
void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective *Node) {
658+
Indent() << "#pragma omp metadirective";
659+
PrintOMPExecutableDirective(Node);
660+
}
661+
657662
void StmtPrinter::VisitOMPParallelDirective(OMPParallelDirective *Node) {
658663
Indent() << "#pragma omp parallel";
659664
PrintOMPExecutableDirective(Node);

clang/lib/AST/StmtProfile.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,10 @@ void StmtProfiler::VisitOMPLoopDirective(const OMPLoopDirective *S) {
903903
VisitOMPLoopBasedDirective(S);
904904
}
905905

906+
void StmtProfiler::VisitOMPMetaDirective(const OMPMetaDirective *S) {
907+
VisitOMPExecutableDirective(S);
908+
}
909+
906910
void StmtProfiler::VisitOMPParallelDirective(const OMPParallelDirective *S) {
907911
VisitOMPExecutableDirective(S);
908912
}

clang/lib/Basic/OpenMPKinds.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ unsigned clang::getOpenMPSimpleClauseType(OpenMPClauseKind Kind, StringRef Str,
185185
case OMPC_exclusive:
186186
case OMPC_uses_allocators:
187187
case OMPC_affinity:
188+
case OMPC_when:
188189
break;
189190
default:
190191
break;
@@ -428,6 +429,7 @@ const char *clang::getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind,
428429
case OMPC_exclusive:
429430
case OMPC_uses_allocators:
430431
case OMPC_affinity:
432+
case OMPC_when:
431433
break;
432434
default:
433435
break;
@@ -591,6 +593,9 @@ void clang::getOpenMPCaptureRegions(
591593
OpenMPDirectiveKind DKind) {
592594
assert(unsigned(DKind) < llvm::omp::Directive_enumSize);
593595
switch (DKind) {
596+
case OMPD_metadirective:
597+
CaptureRegions.push_back(OMPD_metadirective);
598+
break;
594599
case OMPD_parallel:
595600
case OMPD_parallel_for:
596601
case OMPD_parallel_for_simd:

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6740,6 +6740,7 @@ const Expr *CGOpenMPRuntime::getNumTeamsExprForTargetDirective(
67406740
case OMPD_parallel_master_taskloop:
67416741
case OMPD_parallel_master_taskloop_simd:
67426742
case OMPD_requires:
6743+
case OMPD_metadirective:
67436744
case OMPD_unknown:
67446745
break;
67456746
default:
@@ -7214,6 +7215,7 @@ llvm::Value *CGOpenMPRuntime::emitNumThreadsForTargetDirective(
72147215
case OMPD_parallel_master_taskloop:
72157216
case OMPD_parallel_master_taskloop_simd:
72167217
case OMPD_requires:
7218+
case OMPD_metadirective:
72177219
case OMPD_unknown:
72187220
break;
72197221
default:
@@ -9851,6 +9853,7 @@ getNestedDistributeDirective(ASTContext &Ctx, const OMPExecutableDirective &D) {
98519853
case OMPD_parallel_master_taskloop:
98529854
case OMPD_parallel_master_taskloop_simd:
98539855
case OMPD_requires:
9856+
case OMPD_metadirective:
98549857
case OMPD_unknown:
98559858
default:
98569859
llvm_unreachable("Unexpected directive.");
@@ -10701,6 +10704,7 @@ void CGOpenMPRuntime::scanForTargetRegionsFunctions(const Stmt *S,
1070110704
case OMPD_parallel_master_taskloop:
1070210705
case OMPD_parallel_master_taskloop_simd:
1070310706
case OMPD_requires:
10707+
case OMPD_metadirective:
1070410708
case OMPD_unknown:
1070510709
default:
1070610710
llvm_unreachable("Unknown target directive for OpenMP device codegen.");
@@ -11382,6 +11386,7 @@ void CGOpenMPRuntime::emitTargetDataStandAloneCall(
1138211386
case OMPD_target_parallel_for:
1138311387
case OMPD_target_parallel_for_simd:
1138411388
case OMPD_requires:
11389+
case OMPD_metadirective:
1138511390
case OMPD_unknown:
1138611391
default:
1138711392
llvm_unreachable("Unexpected standalone target data directive.");

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) {
196196
case Stmt::SEHTryStmtClass:
197197
EmitSEHTryStmt(cast<SEHTryStmt>(*S));
198198
break;
199+
case Stmt::OMPMetaDirectiveClass:
200+
EmitOMPMetaDirective(cast<OMPMetaDirective>(*S));
201+
break;
199202
case Stmt::OMPCanonicalLoopClass:
200203
EmitOMPCanonicalLoop(cast<OMPCanonicalLoop>(S));
201204
break;

clang/lib/CodeGen/CGStmtOpenMP.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,10 @@ void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
17841784
checkForLastprivateConditionalUpdate(*this, S);
17851785
}
17861786

1787+
void CodeGenFunction::EmitOMPMetaDirective(const OMPMetaDirective &S) {
1788+
EmitStmt(S.getIfStmt());
1789+
}
1790+
17871791
namespace {
17881792
/// RAII to handle scopes for loop transformation directives.
17891793
class OMPTransformDirectiveScopeRAII {
@@ -5960,6 +5964,7 @@ static void emitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
59605964
case OMPC_novariants:
59615965
case OMPC_nocontext:
59625966
case OMPC_filter:
5967+
case OMPC_when:
59635968
llvm_unreachable("Clause is not allowed in 'omp atomic'.");
59645969
}
59655970
}

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,6 +3464,7 @@ class CodeGenFunction : public CodeGenTypeCache {
34643464
const RegionCodeGenTy &BodyGen,
34653465
OMPTargetDataInfo &InputInfo);
34663466

3467+
void EmitOMPMetaDirective(const OMPMetaDirective &S);
34673468
void EmitOMPParallelDirective(const OMPParallelDirective &S);
34683469
void EmitOMPSimdDirective(const OMPSimdDirective &S);
34693470
void EmitOMPTileDirective(const OMPTileDirective &S);

0 commit comments

Comments
 (0)