Skip to content

Commit

Permalink
[Clang][OpenMP] Support for Code Generation of loop bind clause
Browse files Browse the repository at this point in the history
Support for Code Generation of "#pragma loop bind" clause.
1) bind(parallel)
2) bind(teams)
3) bind(thread)

Reviewed By: ABataev

Differential Revision: https://reviews.llvm.org/D144634
  • Loading branch information
chichunchen committed Aug 9, 2023
1 parent 15a08cf commit 8ab62da
Show file tree
Hide file tree
Showing 15 changed files with 1,029 additions and 353 deletions.
24 changes: 21 additions & 3 deletions clang/include/clang/AST/StmtOpenMP.h
Expand Up @@ -281,6 +281,15 @@ class OMPExecutableDirective : public Stmt {
return Data->getClauses();
}

/// Was this directive mapped from an another directive?
/// e.g. 1) omp loop bind(parallel) is mapped to OMPD_for
/// 2) omp loop bind(teams) is mapped to OMPD_distribute
/// 3) omp loop bind(thread) is mapped to OMPD_simd
/// It was necessary to note it down in the Directive because of
/// clang::TreeTransform::TransformOMPExecutableDirective() pass in
/// the frontend.
OpenMPDirectiveKind PrevMappedDirective = llvm::omp::OMPD_unknown;

protected:
/// Data, associated with the directive.
OMPChildren *Data = nullptr;
Expand Down Expand Up @@ -345,6 +354,10 @@ class OMPExecutableDirective : public Stmt {
return Inst;
}

void setMappedDirective(OpenMPDirectiveKind MappedDirective) {
PrevMappedDirective = MappedDirective;
}

public:
/// Iterates over expressions/statements used in the construct.
class used_clauses_child_iterator
Expand Down Expand Up @@ -598,6 +611,8 @@ class OMPExecutableDirective : public Stmt {
"Expected directive with the associated statement.");
return Data->getRawStmt();
}

OpenMPDirectiveKind getMappedDirective() const { return PrevMappedDirective; }
};

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

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

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

/// Creates an empty directive with the place
/// for \a NumClauses clauses.
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -9886,6 +9886,11 @@ def err_break_not_in_loop_or_switch : Error<
def warn_loop_ctrl_binds_to_inner : Warning<
"'%0' is bound to current loop, GCC binds it to the enclosing loop">,
InGroup<GccCompat>;
def err_omp_bind_required_on_loop : Error<
"expected 'bind' clause for 'loop' construct without an enclosing OpenMP "
"construct">;
def err_omp_loop_reduction_clause : Error<
"'reduction' clause not allowed with '#pragma omp loop bind(teams)'">;
def warn_break_binds_to_switch : Warning<
"'break' is bound to loop, GCC binds it to switch">,
InGroup<GccCompat>;
Expand Down
20 changes: 19 additions & 1 deletion clang/include/clang/Sema/Sema.h
Expand Up @@ -11194,6 +11194,23 @@ class Sema final {
/// All `omp assumes` we encountered so far.
SmallVector<AssumptionAttr *, 4> OMPAssumeGlobal;

/// OMPD_loop is mapped to OMPD_for, OMPD_distribute or OMPD_simd depending
/// on the parameter of the bind clause. In the methods for the
/// mapped directives, check the parameters of the lastprivate clause.
bool checkLastPrivateForMappedDirectives(ArrayRef<OMPClause *> Clauses);
/// Depending on the bind clause of OMPD_loop map the directive to new
/// directives.
/// 1) loop bind(parallel) --> OMPD_for
/// 2) loop bind(teams) --> OMPD_distribute
/// 3) loop bind(thread) --> OMPD_simd
/// This is being handled in Sema instead of Codegen because of the need for
/// rigorous semantic checking in the new mapped directives.
bool mapLoopConstruct(llvm::SmallVector<OMPClause *> &ClausesWithoutBind,
ArrayRef<OMPClause *> Clauses,
OpenMPBindClauseKind BindKind,
OpenMPDirectiveKind &Kind,
OpenMPDirectiveKind &PrevMappedDirective);

public:
/// The declarator \p D defines a function in the scope \p S which is nested
/// in an `omp begin/end declare variant` scope. In this method we create a
Expand Down Expand Up @@ -11489,7 +11506,8 @@ class Sema final {
StmtResult ActOnOpenMPExecutableDirective(
OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc);
Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc,
OpenMPDirectiveKind PrevMappedDirective = llvm::omp::OMPD_unknown);
/// Called on well-formed '\#pragma omp parallel' after parsing
/// of the associated statement.
StmtResult ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
Expand Down
17 changes: 10 additions & 7 deletions clang/lib/AST/StmtOpenMP.cpp
Expand Up @@ -297,11 +297,10 @@ OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
/*NumChildren=*/1);
}

OMPSimdDirective *
OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
SourceLocation EndLoc, unsigned CollapsedNum,
ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
const HelperExprs &Exprs) {
OMPSimdDirective *OMPSimdDirective::Create(
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
const HelperExprs &Exprs, OpenMPDirectiveKind ParamPrevMappedDirective) {
auto *Dir = createDirective<OMPSimdDirective>(
C, Clauses, AssociatedStmt, numLoopChildren(CollapsedNum, OMPD_simd),
StartLoc, EndLoc, CollapsedNum);
Expand All @@ -321,6 +320,7 @@ OMPSimdDirective::Create(const ASTContext &C, SourceLocation StartLoc,
Dir->setDependentInits(Exprs.DependentInits);
Dir->setFinalsConditions(Exprs.FinalsConditions);
Dir->setPreInits(Exprs.PreInits);
Dir->setMappedDirective(ParamPrevMappedDirective);
return Dir;
}

Expand All @@ -336,7 +336,8 @@ OMPSimdDirective *OMPSimdDirective::CreateEmpty(const ASTContext &C,
OMPForDirective *OMPForDirective::Create(
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
const HelperExprs &Exprs, Expr *TaskRedRef, bool HasCancel) {
const HelperExprs &Exprs, Expr *TaskRedRef, bool HasCancel,
OpenMPDirectiveKind ParamPrevMappedDirective) {
auto *Dir = createDirective<OMPForDirective>(
C, Clauses, AssociatedStmt, numLoopChildren(CollapsedNum, OMPD_for) + 1,
StartLoc, EndLoc, CollapsedNum);
Expand Down Expand Up @@ -366,6 +367,7 @@ OMPForDirective *OMPForDirective::Create(
Dir->setPreInits(Exprs.PreInits);
Dir->setTaskReductionRefExpr(TaskRedRef);
Dir->setHasCancel(HasCancel);
Dir->setMappedDirective(ParamPrevMappedDirective);
return Dir;
}

Expand Down Expand Up @@ -1515,7 +1517,7 @@ OMPParallelMaskedTaskLoopSimdDirective::CreateEmpty(const ASTContext &C,
OMPDistributeDirective *OMPDistributeDirective::Create(
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
const HelperExprs &Exprs) {
const HelperExprs &Exprs, OpenMPDirectiveKind ParamPrevMappedDirective) {
auto *Dir = createDirective<OMPDistributeDirective>(
C, Clauses, AssociatedStmt,
numLoopChildren(CollapsedNum, OMPD_distribute), StartLoc, EndLoc,
Expand Down Expand Up @@ -1544,6 +1546,7 @@ OMPDistributeDirective *OMPDistributeDirective::Create(
Dir->setDependentInits(Exprs.DependentInits);
Dir->setFinalsConditions(Exprs.FinalsConditions);
Dir->setPreInits(Exprs.PreInits);
Dir->setMappedDirective(ParamPrevMappedDirective);
return Dir;
}

Expand Down

0 comments on commit 8ab62da

Please sign in to comment.