Skip to content

Commit 8e7f032

Browse files
SunilKuravinakopSandeep Kosuri
authored andcommitted
[Clang][OpenMP] Support for Code Generation of loop bind clause.
1 parent 724b40a commit 8e7f032

15 files changed

+1056
-353
lines changed

clang/include/clang/AST/StmtOpenMP.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ class OMPExecutableDirective : public Stmt {
281281
return Data->getClauses();
282282
}
283283

284+
/// Was this directive mapped from an another directive?
285+
/// e.g. 1) omp loop bind(parallel) is mapped to OMPD_for
286+
/// 2) omp loop bind(teams) is mapped to OMPD_distribute
287+
/// 3) omp loop bind(thread) is mapped to OMPD_simd
288+
/// It was necessary to note it down in the Directive because of
289+
/// clang::TreeTransform::TransformOMPExecutableDirective() pass in
290+
/// the frontend.
291+
OpenMPDirectiveKind PrevMappedDirective = llvm::omp::OMPD_unknown;
292+
284293
protected:
285294
/// Data, associated with the directive.
286295
OMPChildren *Data = nullptr;
@@ -345,6 +354,10 @@ class OMPExecutableDirective : public Stmt {
345354
return Inst;
346355
}
347356

357+
void setMappedDirective(OpenMPDirectiveKind MappedDirective) {
358+
PrevMappedDirective = MappedDirective;
359+
}
360+
348361
public:
349362
/// Iterates over expressions/statements used in the construct.
350363
class used_clauses_child_iterator
@@ -598,6 +611,8 @@ class OMPExecutableDirective : public Stmt {
598611
"Expected directive with the associated statement.");
599612
return Data->getRawStmt();
600613
}
614+
615+
OpenMPDirectiveKind getMappedDirective() const { return PrevMappedDirective; }
601616
};
602617

603618
/// This represents '#pragma omp parallel' directive.
@@ -1604,7 +1619,8 @@ class OMPSimdDirective : public OMPLoopDirective {
16041619
SourceLocation EndLoc, unsigned CollapsedNum,
16051620
ArrayRef<OMPClause *> Clauses,
16061621
Stmt *AssociatedStmt,
1607-
const HelperExprs &Exprs);
1622+
const HelperExprs &Exprs,
1623+
OpenMPDirectiveKind ParamPrevMappedDirective);
16081624

16091625
/// Creates an empty directive with the place
16101626
/// for \a NumClauses clauses.
@@ -1682,7 +1698,8 @@ class OMPForDirective : public OMPLoopDirective {
16821698
SourceLocation EndLoc, unsigned CollapsedNum,
16831699
ArrayRef<OMPClause *> Clauses,
16841700
Stmt *AssociatedStmt, const HelperExprs &Exprs,
1685-
Expr *TaskRedRef, bool HasCancel);
1701+
Expr *TaskRedRef, bool HasCancel,
1702+
OpenMPDirectiveKind ParamPrevMappedDirective);
16861703

16871704
/// Creates an empty directive with the place
16881705
/// for \a NumClauses clauses.
@@ -4406,7 +4423,8 @@ class OMPDistributeDirective : public OMPLoopDirective {
44064423
static OMPDistributeDirective *
44074424
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
44084425
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses,
4409-
Stmt *AssociatedStmt, const HelperExprs &Exprs);
4426+
Stmt *AssociatedStmt, const HelperExprs &Exprs,
4427+
OpenMPDirectiveKind ParamPrevMappedDirective);
44104428

44114429
/// Creates an empty directive with the place
44124430
/// for \a NumClauses clauses.

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9875,6 +9875,11 @@ def err_break_not_in_loop_or_switch : Error<
98759875
def warn_loop_ctrl_binds_to_inner : Warning<
98769876
"'%0' is bound to current loop, GCC binds it to the enclosing loop">,
98779877
InGroup<GccCompat>;
9878+
def err_omp_bind_required_on_loop : Error<
9879+
"expected 'bind' clause for 'loop' construct without an enclosing OpenMP "
9880+
"construct">;
9881+
def err_omp_loop_reduction_clause : Error<
9882+
"'reduction' clause not allowed with '#pragma omp loop bind(teams)'">;
98789883
def warn_break_binds_to_switch : Warning<
98799884
"'break' is bound to loop, GCC binds it to switch">,
98809885
InGroup<GccCompat>;

clang/include/clang/Sema/Sema.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11185,6 +11185,23 @@ class Sema final {
1118511185
/// All `omp assumes` we encountered so far.
1118611186
SmallVector<AssumptionAttr *, 4> OMPAssumeGlobal;
1118711187

11188+
/// OMPD_loop is mapped to OMPD_for, OMPD_distribute or OMPD_simd depending
11189+
/// on the parameter of the bind clause. In the methods for the
11190+
/// mapped directives, check the parameters of the lastprivate clause.
11191+
bool checkLastPrivateForMappedDirectives(ArrayRef<OMPClause *> Clauses);
11192+
/// Depending on the bind clause of OMPD_loop map the directive to new
11193+
/// directives.
11194+
/// 1) loop bind(parallel) --> OMPD_for
11195+
/// 2) loop bind(teams) --> OMPD_distribute
11196+
/// 3) loop bind(thread) --> OMPD_simd
11197+
/// This is being handled in Sema instead of Codegen because of the need for
11198+
/// rigorous semantic checking in the new mapped directives.
11199+
bool mapLoopConstruct(llvm::SmallVector<OMPClause *> &ClausesWithoutBind,
11200+
ArrayRef<OMPClause *> Clauses,
11201+
OpenMPBindClauseKind BindKind,
11202+
OpenMPDirectiveKind &Kind,
11203+
OpenMPDirectiveKind &PrevMappedDirective);
11204+
1118811205
public:
1118911206
/// The declarator \p D defines a function in the scope \p S which is nested
1119011207
/// in an `omp begin/end declare variant` scope. In this method we create a
@@ -11480,7 +11497,8 @@ class Sema final {
1148011497
StmtResult ActOnOpenMPExecutableDirective(
1148111498
OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1148211499
OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
11483-
Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc);
11500+
Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc,
11501+
OpenMPDirectiveKind PrevMappedDirective = llvm::omp::OMPD_unknown);
1148411502
/// Called on well-formed '\#pragma omp parallel' after parsing
1148511503
/// of the associated statement.
1148611504
StmtResult ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,

clang/lib/AST/StmtOpenMP.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,10 @@ OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
297297
/*NumChildren=*/1);
298298
}
299299

300-
OMPSimdDirective *
301-
OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
302-
SourceLocation EndLoc, unsigned CollapsedNum,
303-
ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
304-
const HelperExprs &Exprs) {
300+
OMPSimdDirective *OMPSimdDirective::Create(
301+
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
302+
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
303+
const HelperExprs &Exprs, OpenMPDirectiveKind ParamPrevMappedDirective) {
305304
auto *Dir = createDirective<OMPSimdDirective>(
306305
C, Clauses, AssociatedStmt, numLoopChildren(CollapsedNum, OMPD_simd),
307306
StartLoc, EndLoc, CollapsedNum);
@@ -321,6 +320,7 @@ OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
321320
Dir->setDependentInits(Exprs.DependentInits);
322321
Dir->setFinalsConditions(Exprs.FinalsConditions);
323322
Dir->setPreInits(Exprs.PreInits);
323+
Dir->setMappedDirective(ParamPrevMappedDirective);
324324
return Dir;
325325
}
326326

@@ -336,7 +336,8 @@ OMPSimdDirective *OMPSimdDirective::CreateEmpty(const ASTContext &C,
336336
OMPForDirective *OMPForDirective::Create(
337337
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
338338
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
339-
const HelperExprs &Exprs, Expr *TaskRedRef, bool HasCancel) {
339+
const HelperExprs &Exprs, Expr *TaskRedRef, bool HasCancel,
340+
OpenMPDirectiveKind ParamPrevMappedDirective) {
340341
auto *Dir = createDirective<OMPForDirective>(
341342
C, Clauses, AssociatedStmt, numLoopChildren(CollapsedNum, OMPD_for) + 1,
342343
StartLoc, EndLoc, CollapsedNum);
@@ -366,6 +367,7 @@ OMPForDirective *OMPForDirective::Create(
366367
Dir->setPreInits(Exprs.PreInits);
367368
Dir->setTaskReductionRefExpr(TaskRedRef);
368369
Dir->setHasCancel(HasCancel);
370+
Dir->setMappedDirective(ParamPrevMappedDirective);
369371
return Dir;
370372
}
371373

@@ -1515,7 +1517,7 @@ OMPParallelMaskedTaskLoopSimdDirective::CreateEmpty(const ASTContext &C,
15151517
OMPDistributeDirective *OMPDistributeDirective::Create(
15161518
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
15171519
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1518-
const HelperExprs &Exprs) {
1520+
const HelperExprs &Exprs, OpenMPDirectiveKind ParamPrevMappedDirective) {
15191521
auto *Dir = createDirective<OMPDistributeDirective>(
15201522
C, Clauses, AssociatedStmt,
15211523
numLoopChildren(CollapsedNum, OMPD_distribute), StartLoc, EndLoc,
@@ -1544,6 +1546,7 @@ OMPDistributeDirective *OMPDistributeDirective::Create(
15441546
Dir->setDependentInits(Exprs.DependentInits);
15451547
Dir->setFinalsConditions(Exprs.FinalsConditions);
15461548
Dir->setPreInits(Exprs.PreInits);
1549+
Dir->setMappedDirective(ParamPrevMappedDirective);
15471550
return Dir;
15481551
}
15491552

0 commit comments

Comments
 (0)