Skip to content

Commit deda50f

Browse files
authored
[clang][OpenMP] Implement isOpenMPCapturingDirective (#97090)
Check if the given directive can capture variables, and thus needs a captured statement. Simplify some code using this function.
1 parent 781ba3c commit deda50f

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

clang/include/clang/Basic/OpenMPKinds.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ bool checkFailClauseParameter(OpenMPClauseKind FailClauseParameter);
376376
/// \return true - if the above condition is met for this directive
377377
/// otherwise - false.
378378
bool isOpenMPExecutableDirective(OpenMPDirectiveKind DKind);
379+
380+
/// Checks if the specified directive can capture variables.
381+
/// \param DKind Specified directive.
382+
/// \return true - if the above condition is met for this directive
383+
/// otherwise - false.
384+
bool isOpenMPCapturingDirective(OpenMPDirectiveKind DKind);
379385
}
380386

381387
#endif

clang/lib/Basic/OpenMPKinds.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,44 @@ bool clang::isOpenMPExecutableDirective(OpenMPDirectiveKind DKind) {
709709
return Cat == Category::Executable || Cat == Category::Subsidiary;
710710
}
711711

712+
bool clang::isOpenMPCapturingDirective(OpenMPDirectiveKind DKind) {
713+
if (isOpenMPExecutableDirective(DKind)) {
714+
switch (DKind) {
715+
case OMPD_atomic:
716+
case OMPD_barrier:
717+
case OMPD_cancel:
718+
case OMPD_cancellation_point:
719+
case OMPD_critical:
720+
case OMPD_depobj:
721+
case OMPD_error:
722+
case OMPD_flush:
723+
case OMPD_masked:
724+
case OMPD_master:
725+
case OMPD_section:
726+
case OMPD_taskwait:
727+
case OMPD_taskyield:
728+
return false;
729+
default:
730+
return !isOpenMPLoopTransformationDirective(DKind);
731+
}
732+
}
733+
// Non-executable directives.
734+
switch (DKind) {
735+
case OMPD_metadirective:
736+
case OMPD_nothing:
737+
return true;
738+
default:
739+
break;
740+
}
741+
return false;
742+
}
743+
712744
void clang::getOpenMPCaptureRegions(
713745
SmallVectorImpl<OpenMPDirectiveKind> &CaptureRegions,
714746
OpenMPDirectiveKind DKind) {
715747
assert(unsigned(DKind) < llvm::omp::Directive_enumSize);
748+
assert(isOpenMPCapturingDirective(DKind) && "Expecting capturing directive");
749+
716750
switch (DKind) {
717751
case OMPD_metadirective:
718752
CaptureRegions.push_back(OMPD_metadirective);
@@ -799,48 +833,18 @@ void clang::getOpenMPCaptureRegions(
799833
case OMPD_for:
800834
case OMPD_for_simd:
801835
case OMPD_sections:
802-
case OMPD_section:
803836
case OMPD_single:
804-
case OMPD_master:
805-
case OMPD_critical:
806837
case OMPD_taskgroup:
807838
case OMPD_distribute:
808839
case OMPD_ordered:
809-
case OMPD_atomic:
810840
case OMPD_target_data:
811841
case OMPD_distribute_simd:
812842
case OMPD_scope:
813843
case OMPD_dispatch:
814844
CaptureRegions.push_back(OMPD_unknown);
815845
break;
816-
case OMPD_tile:
817-
case OMPD_unroll:
818-
// loop transformations do not introduce captures.
819-
break;
820-
case OMPD_threadprivate:
821-
case OMPD_allocate:
822-
case OMPD_taskyield:
823-
case OMPD_barrier:
824-
case OMPD_error:
825-
case OMPD_taskwait:
826-
case OMPD_cancellation_point:
827-
case OMPD_cancel:
828-
case OMPD_flush:
829-
case OMPD_depobj:
830-
case OMPD_scan:
831-
case OMPD_declare_reduction:
832-
case OMPD_declare_mapper:
833-
case OMPD_declare_simd:
834-
case OMPD_declare_target:
835-
case OMPD_end_declare_target:
836-
case OMPD_requires:
837-
case OMPD_declare_variant:
838-
case OMPD_begin_declare_variant:
839-
case OMPD_end_declare_variant:
840-
llvm_unreachable("OpenMP Directive is not allowed");
841-
case OMPD_unknown:
842846
default:
843-
llvm_unreachable("Unknown OpenMP directive");
847+
llvm_unreachable("Unhandled OpenMP directive");
844848
}
845849
}
846850

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4862,11 +4862,7 @@ StmtResult SemaOpenMP::ActOnOpenMPRegionEnd(StmtResult S,
48624862
ArrayRef<OMPClause *> Clauses) {
48634863
handleDeclareVariantConstructTrait(DSAStack, DSAStack->getCurrentDirective(),
48644864
/* ScopeEntry */ false);
4865-
if (DSAStack->getCurrentDirective() == OMPD_atomic ||
4866-
DSAStack->getCurrentDirective() == OMPD_critical ||
4867-
DSAStack->getCurrentDirective() == OMPD_section ||
4868-
DSAStack->getCurrentDirective() == OMPD_master ||
4869-
DSAStack->getCurrentDirective() == OMPD_masked)
4865+
if (!isOpenMPCapturingDirective(DSAStack->getCurrentDirective()))
48704866
return S;
48714867

48724868
bool ErrorFound = false;
@@ -4909,10 +4905,6 @@ StmtResult SemaOpenMP::ActOnOpenMPRegionEnd(StmtResult S,
49094905
}
49104906
}
49114907
DSAStack->setForceVarCapturing(/*V=*/false);
4912-
} else if (isOpenMPLoopTransformationDirective(
4913-
DSAStack->getCurrentDirective())) {
4914-
assert(CaptureRegions.empty() &&
4915-
"No captured regions in loop transformation directives.");
49164908
} else if (CaptureRegions.size() > 1 ||
49174909
CaptureRegions.back() != OMPD_unknown) {
49184910
if (auto *C = OMPClauseWithPreInit::get(Clause))
@@ -6396,9 +6388,7 @@ StmtResult SemaOpenMP::ActOnOpenMPExecutableDirective(
63966388
ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
63976389
}
63986390
if (AStmt && !SemaRef.CurContext->isDependentContext() &&
6399-
Kind != OMPD_atomic && Kind != OMPD_critical && Kind != OMPD_section &&
6400-
Kind != OMPD_master && Kind != OMPD_masked &&
6401-
!isOpenMPLoopTransformationDirective(Kind)) {
6391+
isOpenMPCapturingDirective(Kind)) {
64026392
assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
64036393

64046394
// Check default data sharing attributes for referenced variables.

0 commit comments

Comments
 (0)