Skip to content

Commit c747300

Browse files
[clang][OpenMP] Move "loop" directive mapping from sema to codegen (#99905)
Given "loop" construct, clang will try to treat it as "for", "distribute" or "simd", depending on either the implied binding, or the bind clause if present. This patch moves the code that performs this construct remapping from sema to codegen. For a "loop" construct without a bind clause, this patch will create an implicit bind clause based on implied binding to simplify further analysis. During codegen the function `EmitOMPGenericLoopDirective` (i.e. "loop") will invoke the "emit" functions for "for", "distribute" or "simd", depending on the bind clause. --------- Co-authored-by: Alexey Bataev <[email protected]>
1 parent 2eea9d6 commit c747300

File tree

11 files changed

+385
-462
lines changed

11 files changed

+385
-462
lines changed

clang/include/clang/AST/StmtOpenMP.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,6 @@ 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-
293284
protected:
294285
/// Data, associated with the directive.
295286
OMPChildren *Data = nullptr;
@@ -354,10 +345,6 @@ class OMPExecutableDirective : public Stmt {
354345
return Inst;
355346
}
356347

357-
void setMappedDirective(OpenMPDirectiveKind MappedDirective) {
358-
PrevMappedDirective = MappedDirective;
359-
}
360-
361348
public:
362349
/// Iterates over expressions/statements used in the construct.
363350
class used_clauses_child_iterator
@@ -611,8 +598,6 @@ class OMPExecutableDirective : public Stmt {
611598
"Expected directive with the associated statement.");
612599
return Data->getRawStmt();
613600
}
614-
615-
OpenMPDirectiveKind getMappedDirective() const { return PrevMappedDirective; }
616601
};
617602

618603
/// This represents '#pragma omp parallel' directive.
@@ -1620,8 +1605,7 @@ class OMPSimdDirective : public OMPLoopDirective {
16201605
SourceLocation EndLoc, unsigned CollapsedNum,
16211606
ArrayRef<OMPClause *> Clauses,
16221607
Stmt *AssociatedStmt,
1623-
const HelperExprs &Exprs,
1624-
OpenMPDirectiveKind ParamPrevMappedDirective);
1608+
const HelperExprs &Exprs);
16251609

16261610
/// Creates an empty directive with the place
16271611
/// for \a NumClauses clauses.
@@ -1699,8 +1683,7 @@ class OMPForDirective : public OMPLoopDirective {
16991683
SourceLocation EndLoc, unsigned CollapsedNum,
17001684
ArrayRef<OMPClause *> Clauses,
17011685
Stmt *AssociatedStmt, const HelperExprs &Exprs,
1702-
Expr *TaskRedRef, bool HasCancel,
1703-
OpenMPDirectiveKind ParamPrevMappedDirective);
1686+
Expr *TaskRedRef, bool HasCancel);
17041687

17051688
/// Creates an empty directive with the place
17061689
/// for \a NumClauses clauses.
@@ -4478,8 +4461,7 @@ class OMPDistributeDirective : public OMPLoopDirective {
44784461
static OMPDistributeDirective *
44794462
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
44804463
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses,
4481-
Stmt *AssociatedStmt, const HelperExprs &Exprs,
4482-
OpenMPDirectiveKind ParamPrevMappedDirective);
4464+
Stmt *AssociatedStmt, const HelperExprs &Exprs);
44834465

44844466
/// Creates an empty directive with the place
44854467
/// for \a NumClauses clauses.

clang/include/clang/Sema/SemaOpenMP.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ class SemaOpenMP : public SemaBase {
398398
StmtResult ActOnOpenMPExecutableDirective(
399399
OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
400400
OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
401-
Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc,
402-
OpenMPDirectiveKind PrevMappedDirective = llvm::omp::OMPD_unknown);
401+
Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc);
403402
/// Called on well-formed '\#pragma omp parallel' after parsing
404403
/// of the associated statement.
405404
StmtResult ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
@@ -1430,26 +1429,6 @@ class SemaOpenMP : public SemaBase {
14301429

14311430
/// All `omp assumes` we encountered so far.
14321431
SmallVector<OMPAssumeAttr *, 4> OMPAssumeGlobal;
1433-
1434-
/// OMPD_loop is mapped to OMPD_for, OMPD_distribute or OMPD_simd depending
1435-
/// on the parameter of the bind clause. In the methods for the
1436-
/// mapped directives, check the parameters of the lastprivate clause.
1437-
bool checkLastPrivateForMappedDirectives(ArrayRef<OMPClause *> Clauses);
1438-
/// Depending on the bind clause of OMPD_loop map the directive to new
1439-
/// directives.
1440-
/// 1) loop bind(parallel) --> OMPD_for
1441-
/// 2) loop bind(teams) --> OMPD_distribute
1442-
/// 3) loop bind(thread) --> OMPD_simd
1443-
/// This is being handled in Sema instead of Codegen because of the need for
1444-
/// rigorous semantic checking in the new mapped directives.
1445-
bool mapLoopConstruct(llvm::SmallVector<OMPClause *> &ClausesWithoutBind,
1446-
ArrayRef<OMPClause *> Clauses,
1447-
OpenMPBindClauseKind &BindKind,
1448-
OpenMPDirectiveKind &Kind,
1449-
OpenMPDirectiveKind &PrevMappedDirective,
1450-
SourceLocation StartLoc, SourceLocation EndLoc,
1451-
const DeclarationNameInfo &DirName,
1452-
OpenMPDirectiveKind CancelRegion);
14531432
};
14541433

14551434
} // namespace clang

clang/lib/AST/StmtOpenMP.cpp

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

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) {
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) {
304305
auto *Dir = createDirective<OMPSimdDirective>(
305306
C, Clauses, AssociatedStmt, numLoopChildren(CollapsedNum, OMPD_simd),
306307
StartLoc, EndLoc, CollapsedNum);
@@ -320,7 +321,6 @@ OMPSimdDirective *OMPSimdDirective::Create(
320321
Dir->setDependentInits(Exprs.DependentInits);
321322
Dir->setFinalsConditions(Exprs.FinalsConditions);
322323
Dir->setPreInits(Exprs.PreInits);
323-
Dir->setMappedDirective(ParamPrevMappedDirective);
324324
return Dir;
325325
}
326326

@@ -336,8 +336,7 @@ 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,
340-
OpenMPDirectiveKind ParamPrevMappedDirective) {
339+
const HelperExprs &Exprs, Expr *TaskRedRef, bool HasCancel) {
341340
auto *Dir = createDirective<OMPForDirective>(
342341
C, Clauses, AssociatedStmt, numLoopChildren(CollapsedNum, OMPD_for) + 1,
343342
StartLoc, EndLoc, CollapsedNum);
@@ -367,7 +366,6 @@ OMPForDirective *OMPForDirective::Create(
367366
Dir->setPreInits(Exprs.PreInits);
368367
Dir->setTaskReductionRefExpr(TaskRedRef);
369368
Dir->setHasCancel(HasCancel);
370-
Dir->setMappedDirective(ParamPrevMappedDirective);
371369
return Dir;
372370
}
373371

@@ -1569,10 +1567,11 @@ OMPParallelMaskedTaskLoopSimdDirective::CreateEmpty(const ASTContext &C,
15691567
CollapsedNum);
15701568
}
15711569

1572-
OMPDistributeDirective *OMPDistributeDirective::Create(
1573-
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
1574-
unsigned CollapsedNum, ArrayRef<OMPClause *> Clauses, Stmt *AssociatedStmt,
1575-
const HelperExprs &Exprs, OpenMPDirectiveKind ParamPrevMappedDirective) {
1570+
OMPDistributeDirective *
1571+
OMPDistributeDirective::Create(const ASTContext &C, SourceLocation StartLoc,
1572+
SourceLocation EndLoc, unsigned CollapsedNum,
1573+
ArrayRef<OMPClause *> Clauses,
1574+
Stmt *AssociatedStmt, const HelperExprs &Exprs) {
15761575
auto *Dir = createDirective<OMPDistributeDirective>(
15771576
C, Clauses, AssociatedStmt,
15781577
numLoopChildren(CollapsedNum, OMPD_distribute), StartLoc, EndLoc,
@@ -1601,7 +1600,6 @@ OMPDistributeDirective *OMPDistributeDirective::Create(
16011600
Dir->setDependentInits(Exprs.DependentInits);
16021601
Dir->setFinalsConditions(Exprs.FinalsConditions);
16031602
Dir->setPreInits(Exprs.PreInits);
1604-
Dir->setMappedDirective(ParamPrevMappedDirective);
16051603
return Dir;
16061604
}
16071605

0 commit comments

Comments
 (0)