Skip to content

Commit b9c55e2

Browse files
committed
[OPNEMP]Allow grainsize clause in combined task-based directives.
The expression of the grainsize clause must be captured in the combined task-based directives, like 'parallel master taskloop' directive. llvm-svn: 374810
1 parent 6362a21 commit b9c55e2

File tree

8 files changed

+129
-22
lines changed

8 files changed

+129
-22
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5268,7 +5268,7 @@ class OMPPriorityClause : public OMPClause {
52685268
/// \endcode
52695269
/// In this example directive '#pragma omp taskloop' has clause 'grainsize'
52705270
/// with single expression '4'.
5271-
class OMPGrainsizeClause : public OMPClause {
5271+
class OMPGrainsizeClause : public OMPClause, public OMPClauseWithPreInit {
52725272
friend class OMPClauseReader;
52735273

52745274
/// Location of '('.
@@ -5284,16 +5284,23 @@ class OMPGrainsizeClause : public OMPClause {
52845284
/// Build 'grainsize' clause.
52855285
///
52865286
/// \param Size Expression associated with this clause.
5287+
/// \param HelperSize Helper grainsize for the construct.
5288+
/// \param CaptureRegion Innermost OpenMP region where expressions in this
5289+
/// clause must be captured.
52875290
/// \param StartLoc Starting location of the clause.
52885291
/// \param EndLoc Ending location of the clause.
5289-
OMPGrainsizeClause(Expr *Size, SourceLocation StartLoc,
5292+
OMPGrainsizeClause(Expr *Size, Stmt *HelperSize,
5293+
OpenMPDirectiveKind CaptureRegion, SourceLocation StartLoc,
52905294
SourceLocation LParenLoc, SourceLocation EndLoc)
5291-
: OMPClause(OMPC_grainsize, StartLoc, EndLoc), LParenLoc(LParenLoc),
5292-
Grainsize(Size) {}
5295+
: OMPClause(OMPC_grainsize, StartLoc, EndLoc), OMPClauseWithPreInit(this),
5296+
LParenLoc(LParenLoc), Grainsize(Size) {
5297+
setPreInitStmt(HelperSize, CaptureRegion);
5298+
}
52935299

52945300
/// Build an empty clause.
52955301
explicit OMPGrainsizeClause()
5296-
: OMPClause(OMPC_grainsize, SourceLocation(), SourceLocation()) {}
5302+
: OMPClause(OMPC_grainsize, SourceLocation(), SourceLocation()),
5303+
OMPClauseWithPreInit(this) {}
52975304

52985305
/// Sets the location of '('.
52995306
void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
@@ -5310,11 +5317,10 @@ class OMPGrainsizeClause : public OMPClause {
53105317
return const_child_range(&Grainsize, &Grainsize + 1);
53115318
}
53125319

5313-
child_range used_children() {
5314-
return child_range(child_iterator(), child_iterator());
5315-
}
5320+
child_range used_children();
53165321
const_child_range used_children() const {
5317-
return const_child_range(const_child_iterator(), const_child_iterator());
5322+
auto Children = const_cast<OMPGrainsizeClause *>(this)->used_children();
5323+
return const_child_range(Children.begin(), Children.end());
53185324
}
53195325

53205326
static bool classof(const OMPClause *T) {

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,6 +3275,7 @@ bool RecursiveASTVisitor<Derived>::VisitOMPPriorityClause(
32753275
template <typename Derived>
32763276
bool RecursiveASTVisitor<Derived>::VisitOMPGrainsizeClause(
32773277
OMPGrainsizeClause *C) {
3278+
TRY_TO(VisitOMPClauseWithPreInit(C));
32783279
TRY_TO(TraverseStmt(C->getGrainsize()));
32793280
return true;
32803281
}

clang/lib/AST/OpenMPClause.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
8484
return static_cast<const OMPThreadLimitClause *>(C);
8585
case OMPC_device:
8686
return static_cast<const OMPDeviceClause *>(C);
87+
case OMPC_grainsize:
88+
return static_cast<const OMPGrainsizeClause *>(C);
8789
case OMPC_default:
8890
case OMPC_proc_bind:
8991
case OMPC_final:
@@ -113,7 +115,6 @@ const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
113115
case OMPC_simd:
114116
case OMPC_map:
115117
case OMPC_priority:
116-
case OMPC_grainsize:
117118
case OMPC_nogroup:
118119
case OMPC_num_tasks:
119120
case OMPC_hint:
@@ -234,6 +235,12 @@ OMPClause::child_range OMPIfClause::used_children() {
234235
return child_range(&Condition, &Condition + 1);
235236
}
236237

238+
OMPClause::child_range OMPGrainsizeClause::used_children() {
239+
if (Stmt **C = getAddrOfExprAsWritten(getPreInitStmt()))
240+
return child_range(C, C + 1);
241+
return child_range(&Grainsize, &Grainsize + 1);
242+
}
243+
237244
OMPOrderedClause *OMPOrderedClause::Create(const ASTContext &C, Expr *Num,
238245
unsigned NumLoops,
239246
SourceLocation StartLoc,

clang/lib/AST/StmtProfile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ void OMPClauseProfiler::VisitOMPPriorityClause(const OMPPriorityClause *C) {
740740
Profiler->VisitStmt(C->getPriority());
741741
}
742742
void OMPClauseProfiler::VisitOMPGrainsizeClause(const OMPGrainsizeClause *C) {
743+
VistOMPClauseWithPreInit(C);
743744
if (C->getGrainsize())
744745
Profiler->VisitStmt(C->getGrainsize());
745746
}

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4590,12 +4590,16 @@ StmtResult Sema::ActOnOpenMPExecutableDirective(
45904590
continue;
45914591
case OMPC_schedule:
45924592
break;
4593+
case OMPC_grainsize:
4594+
// Do not analyze if no parent parallel directive.
4595+
if (isOpenMPParallelDirective(DSAStack->getCurrentDirective()))
4596+
break;
4597+
continue;
45934598
case OMPC_ordered:
45944599
case OMPC_device:
45954600
case OMPC_num_teams:
45964601
case OMPC_thread_limit:
45974602
case OMPC_priority:
4598-
case OMPC_grainsize:
45994603
case OMPC_num_tasks:
46004604
case OMPC_hint:
46014605
case OMPC_collapse:
@@ -10773,6 +10777,74 @@ static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
1077310777
llvm_unreachable("Unknown OpenMP directive");
1077410778
}
1077510779
break;
10780+
case OMPC_grainsize:
10781+
switch (DKind) {
10782+
case OMPD_task:
10783+
case OMPD_taskloop:
10784+
case OMPD_taskloop_simd:
10785+
case OMPD_master_taskloop:
10786+
break;
10787+
case OMPD_parallel_master_taskloop:
10788+
CaptureRegion = OMPD_parallel;
10789+
break;
10790+
case OMPD_target_update:
10791+
case OMPD_target_enter_data:
10792+
case OMPD_target_exit_data:
10793+
case OMPD_target:
10794+
case OMPD_target_simd:
10795+
case OMPD_target_teams:
10796+
case OMPD_target_parallel:
10797+
case OMPD_target_teams_distribute:
10798+
case OMPD_target_teams_distribute_simd:
10799+
case OMPD_target_parallel_for:
10800+
case OMPD_target_parallel_for_simd:
10801+
case OMPD_target_teams_distribute_parallel_for:
10802+
case OMPD_target_teams_distribute_parallel_for_simd:
10803+
case OMPD_target_data:
10804+
case OMPD_teams_distribute_parallel_for:
10805+
case OMPD_teams_distribute_parallel_for_simd:
10806+
case OMPD_teams:
10807+
case OMPD_teams_distribute:
10808+
case OMPD_teams_distribute_simd:
10809+
case OMPD_distribute_parallel_for:
10810+
case OMPD_distribute_parallel_for_simd:
10811+
case OMPD_cancel:
10812+
case OMPD_parallel:
10813+
case OMPD_parallel_sections:
10814+
case OMPD_parallel_for:
10815+
case OMPD_parallel_for_simd:
10816+
case OMPD_threadprivate:
10817+
case OMPD_allocate:
10818+
case OMPD_taskyield:
10819+
case OMPD_barrier:
10820+
case OMPD_taskwait:
10821+
case OMPD_cancellation_point:
10822+
case OMPD_flush:
10823+
case OMPD_declare_reduction:
10824+
case OMPD_declare_mapper:
10825+
case OMPD_declare_simd:
10826+
case OMPD_declare_variant:
10827+
case OMPD_declare_target:
10828+
case OMPD_end_declare_target:
10829+
case OMPD_simd:
10830+
case OMPD_for:
10831+
case OMPD_for_simd:
10832+
case OMPD_sections:
10833+
case OMPD_section:
10834+
case OMPD_single:
10835+
case OMPD_master:
10836+
case OMPD_critical:
10837+
case OMPD_taskgroup:
10838+
case OMPD_distribute:
10839+
case OMPD_ordered:
10840+
case OMPD_atomic:
10841+
case OMPD_distribute_simd:
10842+
case OMPD_requires:
10843+
llvm_unreachable("Unexpected OpenMP directive with grainsize-clause");
10844+
case OMPD_unknown:
10845+
llvm_unreachable("Unknown OpenMP directive");
10846+
}
10847+
break;
1077610848
case OMPC_firstprivate:
1077710849
case OMPC_lastprivate:
1077810850
case OMPC_reduction:
@@ -10808,7 +10880,6 @@ static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
1080810880
case OMPC_simd:
1080910881
case OMPC_map:
1081010882
case OMPC_priority:
10811-
case OMPC_grainsize:
1081210883
case OMPC_nogroup:
1081310884
case OMPC_num_tasks:
1081410885
case OMPC_hint:
@@ -10926,9 +10997,12 @@ ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1092610997
return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1092710998
}
1092810999

10929-
static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
10930-
OpenMPClauseKind CKind,
10931-
bool StrictlyPositive) {
11000+
static bool
11001+
isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef, OpenMPClauseKind CKind,
11002+
bool StrictlyPositive, bool BuildCapture = false,
11003+
OpenMPDirectiveKind DKind = OMPD_unknown,
11004+
OpenMPDirectiveKind *CaptureRegion = nullptr,
11005+
Stmt **HelperValStmt = nullptr) {
1093211006
if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
1093311007
!ValExpr->isInstantiationDependent()) {
1093411008
SourceLocation Loc = ValExpr->getExprLoc();
@@ -10949,6 +11023,16 @@ static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
1094911023
<< ValExpr->getSourceRange();
1095011024
return false;
1095111025
}
11026+
if (!BuildCapture)
11027+
return true;
11028+
*CaptureRegion = getOpenMPCaptureRegionForClause(DKind, CKind);
11029+
if (*CaptureRegion != OMPD_unknown &&
11030+
!SemaRef.CurContext->isDependentContext()) {
11031+
ValExpr = SemaRef.MakeFullExpr(ValExpr).get();
11032+
llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
11033+
ValExpr = tryBuildCapture(SemaRef, ValExpr, Captures).get();
11034+
*HelperValStmt = buildPreInits(SemaRef.Context, Captures);
11035+
}
1095211036
}
1095311037
return true;
1095411038
}
@@ -15847,15 +15931,20 @@ OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
1584715931
SourceLocation LParenLoc,
1584815932
SourceLocation EndLoc) {
1584915933
Expr *ValExpr = Grainsize;
15934+
Stmt *HelperValStmt = nullptr;
15935+
OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
1585015936

1585115937
// OpenMP [2.9.2, taskloop Constrcut]
1585215938
// The parameter of the grainsize clause must be a positive integer
1585315939
// expression.
15854-
if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
15855-
/*StrictlyPositive=*/true))
15940+
if (!isNonNegativeIntegerValue(
15941+
ValExpr, *this, OMPC_grainsize,
15942+
/*StrictlyPositive=*/true, /*BuildCapture=*/true,
15943+
DSAStack->getCurrentDirective(), &CaptureRegion, &HelperValStmt))
1585615944
return nullptr;
1585715945

15858-
return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
15946+
return new (Context) OMPGrainsizeClause(ValExpr, HelperValStmt, CaptureRegion,
15947+
StartLoc, LParenLoc, EndLoc);
1585915948
}
1586015949

1586115950
OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,

clang/lib/Serialization/ASTReader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12934,6 +12934,7 @@ void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
1293412934
}
1293512935

1293612936
void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12937+
VisitOMPClauseWithPreInit(C);
1293712938
C->setGrainsize(Record.readSubExpr());
1293812939
C->setLParenLoc(Record.readSourceLocation());
1293912940
}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6938,6 +6938,7 @@ void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
69386938
}
69396939

69406940
void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
6941+
VisitOMPClauseWithPreInit(C);
69416942
Record.AddStmt(C->getGrainsize());
69426943
Record.AddSourceLocation(C->getLParenLoc());
69436944
}

clang/test/OpenMP/parallel_master_taskloop_codegen.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
int main(int argc, char **argv) {
1515
// CHECK: [[GTID:%.+]] = call i32 @__kmpc_global_thread_num(%struct.ident_t* [[DEFLOC:@.+]])
1616
// CHECK: call void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEFLOC]], i32 0, void (i32*, i32*, ...)* bitcast (void (i32*, i32*)* [[OMP_OUTLINED1:@.+]] to void (i32*, i32*, ...)*))
17-
// CHECK: call void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEFLOC]], i32 0, void (i32*, i32*, ...)* bitcast (void (i32*, i32*)* [[OMP_OUTLINED2:@.+]] to void (i32*, i32*, ...)*))
17+
// CHECK: call void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEFLOC]], i32 1, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, i64)* [[OMP_OUTLINED2:@.+]] to void (i32*, i32*, ...)*), i64 [[GRAINSIZE:%.+]])
1818
// CHECK: call void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEFLOC]], i32 3, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, i32*, i8***, i64)* [[OMP_OUTLINED3:@.+]] to void (i32*, i32*, ...)*), i32* [[ARGC:%.+]], i8*** [[ARGV:%.+]], i64 [[COND:%.+]])
1919
// CHECK: call void @__kmpc_serialized_parallel(%struct.ident_t* [[DEFLOC]], i32 [[GTID]])
2020
// CHECK: call void [[OMP_OUTLINED3]](i32* %{{.+}}, i32* %{{.+}}, i32* [[ARGC]], i8*** [[ARGV]], i64 [[COND]])
@@ -77,7 +77,7 @@ int main(int argc, char **argv) {
7777
#pragma omp parallel master taskloop priority(4)
7878
for (int i = 0; i < 10; ++i)
7979
;
80-
// CHECK: define internal void [[OMP_OUTLINED2]](i32* noalias %{{.+}}, i32* noalias %{{.+}})
80+
// CHECK: define internal void [[OMP_OUTLINED2]](i32* noalias %{{.+}}, i32* noalias %{{.+}}, i64 %{{.+}})
8181
// CHECK: [[RES:%.+]] = call {{.*}}i32 @__kmpc_master(%struct.ident_t* [[DEFLOC]], i32 [[GTID:%.+]])
8282
// CHECK-NEXT: [[IS_MASTER:%.+]] = icmp ne i32 [[RES]], 0
8383
// CHECK-NEXT: br i1 [[IS_MASTER]], label {{%?}}[[THEN:.+]], label {{%?}}[[EXIT:.+]]
@@ -92,7 +92,8 @@ int main(int argc, char **argv) {
9292
// CHECK: [[ST:%.+]] = getelementptr inbounds [[TD_TY]], [[TD_TY]]* [[TASK_DATA]], i32 0, i32 7
9393
// CHECK: store i64 1, i64* [[ST]],
9494
// CHECK: [[ST_VAL:%.+]] = load i64, i64* [[ST]],
95-
// CHECK: call void @__kmpc_taskloop(%struct.ident_t* [[DEFLOC]], i32 [[GTID]], i8* [[TASKV]], i32 1, i64* [[DOWN]], i64* [[UP]], i64 [[ST_VAL]], i32 1, i32 1, i64 4, i8* null)
95+
// CHECK: [[GRAINSIZE:%.+]] = zext i32 %{{.+}} to i64
96+
// CHECK: call void @__kmpc_taskloop(%struct.ident_t* [[DEFLOC]], i32 [[GTID]], i8* [[TASKV]], i32 1, i64* [[DOWN]], i64* [[UP]], i64 [[ST_VAL]], i32 1, i32 1, i64 [[GRAINSIZE]], i8* null)
9697
// CHECK-NEXT: call {{.*}}void @__kmpc_end_master(%struct.ident_t* [[DEFLOC]], i32 [[GTID]])
9798
// CHECK-NEXT: br label {{%?}}[[EXIT]]
9899
// CHECK: [[EXIT]]
@@ -128,7 +129,7 @@ int main(int argc, char **argv) {
128129
// CHECK: br label %
129130
// CHECK: ret i32 0
130131

131-
#pragma omp parallel master taskloop nogroup grainsize(4)
132+
#pragma omp parallel master taskloop nogroup grainsize(argc)
132133
for (int i = 0; i < 10; ++i)
133134
;
134135
// CHECK: define internal void [[OMP_OUTLINED3]](i32* noalias %{{.+}}, i32* noalias %{{.+}}, i32* dereferenceable(4) %{{.+}}, i8*** dereferenceable(8) %{{.+}}, i64 %{{.+}})

0 commit comments

Comments
 (0)