Skip to content

[Clang][OpenMP][LoopTransformations] Fix incorrect number of generated loops for Tile and Reverse directives #140532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions clang/include/clang/AST/StmtOpenMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -5787,10 +5787,13 @@ class OMPReverseDirective final : public OMPLoopTransformationDirective {
TransformedStmtOffset,
};

explicit OMPReverseDirective(SourceLocation StartLoc, SourceLocation EndLoc)
explicit OMPReverseDirective(SourceLocation StartLoc, SourceLocation EndLoc,
unsigned NumLoops)
: OMPLoopTransformationDirective(OMPReverseDirectiveClass,
llvm::omp::OMPD_reverse, StartLoc,
EndLoc, 1) {}
EndLoc, NumLoops) {
setNumGeneratedLoops(NumLoops);
}

void setPreInits(Stmt *PreInits) {
Data->getChildren()[PreInitsOffset] = PreInits;
Expand All @@ -5806,19 +5809,23 @@ class OMPReverseDirective final : public OMPLoopTransformationDirective {
/// \param C Context of the AST.
/// \param StartLoc Location of the introducer (e.g. the 'omp' token).
/// \param EndLoc Location of the directive's end (e.g. the tok::eod).
/// \param NumLoops Number of affected loops
/// \param AssociatedStmt The outermost associated loop.
/// \param TransformedStmt The loop nest after tiling, or nullptr in
/// dependent contexts.
/// \param PreInits Helper preinits statements for the loop nest.
static OMPReverseDirective *
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
Stmt *AssociatedStmt, Stmt *TransformedStmt, Stmt *PreInits);
static OMPReverseDirective *Create(const ASTContext &C,
SourceLocation StartLoc,
SourceLocation EndLoc,
Stmt *AssociatedStmt, unsigned NumLoops,
Stmt *TransformedStmt, Stmt *PreInits);

/// Build an empty '#pragma omp reverse' AST node for deserialization.
///
/// \param C Context of the AST.
/// \param NumClauses Number of clauses to allocate.
static OMPReverseDirective *CreateEmpty(const ASTContext &C);
/// \param NumLoops Number of associated loops to allocate
static OMPReverseDirective *CreateEmpty(const ASTContext &C,
unsigned NumLoops);

/// Gets/sets the associated loops after the transformation, i.e. after
/// de-sugaring.
Expand Down Expand Up @@ -5857,7 +5864,7 @@ class OMPInterchangeDirective final : public OMPLoopTransformationDirective {
: OMPLoopTransformationDirective(OMPInterchangeDirectiveClass,
llvm::omp::OMPD_interchange, StartLoc,
EndLoc, NumLoops) {
setNumGeneratedLoops(3 * NumLoops);
setNumGeneratedLoops(NumLoops);
}

void setPreInits(Stmt *PreInits) {
Expand Down
11 changes: 7 additions & 4 deletions clang/lib/AST/StmtOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,18 +471,21 @@ OMPUnrollDirective *OMPUnrollDirective::CreateEmpty(const ASTContext &C,
OMPReverseDirective *
OMPReverseDirective::Create(const ASTContext &C, SourceLocation StartLoc,
SourceLocation EndLoc, Stmt *AssociatedStmt,
Stmt *TransformedStmt, Stmt *PreInits) {
unsigned NumLoops, Stmt *TransformedStmt,
Stmt *PreInits) {
OMPReverseDirective *Dir = createDirective<OMPReverseDirective>(
C, {}, AssociatedStmt, TransformedStmtOffset + 1, StartLoc, EndLoc);
C, {}, AssociatedStmt, TransformedStmtOffset + 1, StartLoc, EndLoc,
NumLoops);
Dir->setTransformedStmt(TransformedStmt);
Dir->setPreInits(PreInits);
return Dir;
}

OMPReverseDirective *OMPReverseDirective::CreateEmpty(const ASTContext &C) {
OMPReverseDirective *OMPReverseDirective::CreateEmpty(const ASTContext &C,
unsigned NumLoops) {
return createEmptyDirective<OMPReverseDirective>(
C, /*NumClauses=*/0, /*HasAssociatedStmt=*/true,
TransformedStmtOffset + 1, SourceLocation(), SourceLocation());
TransformedStmtOffset + 1, SourceLocation(), SourceLocation(), NumLoops);
}

OMPInterchangeDirective *OMPInterchangeDirective::Create(
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15140,7 +15140,7 @@ StmtResult SemaOpenMP::ActOnOpenMPReverseDirective(Stmt *AStmt,
// instantiated.
if (SemaRef.CurContext->isDependentContext())
return OMPReverseDirective::Create(Context, StartLoc, EndLoc, AStmt,
nullptr, nullptr);
NumLoops, nullptr, nullptr);

assert(LoopHelpers.size() == NumLoops &&
"Expecting a single-dimensional loop iteration space");
Expand Down Expand Up @@ -15299,7 +15299,7 @@ StmtResult SemaOpenMP::ActOnOpenMPReverseDirective(Stmt *AStmt,
ForStmt(Context, Init.get(), Cond.get(), nullptr, Incr.get(),
ReversedBody, LoopHelper.Init->getBeginLoc(),
LoopHelper.Init->getBeginLoc(), LoopHelper.Inc->getEndLoc());
return OMPReverseDirective::Create(Context, StartLoc, EndLoc, AStmt,
return OMPReverseDirective::Create(Context, StartLoc, EndLoc, AStmt, NumLoops,
ReversedFor,
buildPreInits(Context, PreInits));
}
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/Serialization/ASTReaderStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3602,11 +3602,10 @@ Stmt *ASTReader::ReadStmtFromStream(ModuleFile &F) {
}

case STMT_OMP_REVERSE_DIRECTIVE: {
assert(Record[ASTStmtReader::NumStmtFields] == 1 &&
"Reverse directive accepts only a single loop");
unsigned NumLoops = Record[ASTStmtReader::NumStmtFields];
assert(Record[ASTStmtReader::NumStmtFields + 1] == 0 &&
"Reverse directive has no clauses");
S = OMPReverseDirective::CreateEmpty(Context);
S = OMPReverseDirective::CreateEmpty(Context, NumLoops);
break;
}

Expand Down