Skip to content

[OpenMP] Fix crash with invalid argument to simd collapse #139313

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 2 commits into from
May 12, 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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,8 @@ OpenMP Support
- Added support for 'omp stripe' directive.
- Fixed a crashing bug with ``omp tile sizes`` if the argument to ``sizes`` was
an invalid expression. (#GH139073)
- Fixed a crashing bug with ``omp simd collapse`` if the argument to
``collapse`` was an invalid expression. (#GH138493)
- Fixed a crashing bug with ``omp distribute dist_schedule`` if the argument to
``dist_schedule`` was not strictly positive. (#GH139266)

Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9648,6 +9648,13 @@ checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
DSAStackTy &DSA,
SemaOpenMP::VarsWithInheritedDSAType &VarsWithImplicitDSA,
OMPLoopBasedDirective::HelperExprs &Built) {
// If either of the loop expressions exist and contain errors, we bail out
// early because diagnostics have already been emitted and we can't reliably
// check more about the loop.
if ((CollapseLoopCountExpr && CollapseLoopCountExpr->containsErrors()) ||
(OrderedLoopCountExpr && OrderedLoopCountExpr->containsErrors()))
return 0;

unsigned NestedLoopCount = 1;
bool SupportsNonPerfectlyNested = (SemaRef.LangOpts.OpenMP >= 50) &&
!isOpenMPLoopTransformationDirective(DKind);
Expand Down
9 changes: 9 additions & 0 deletions clang/test/OpenMP/simd_collapse_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ int main(int argc, char **argv) {
return tmain<int, char, 1, 0>(argc, argv);
}

namespace GH138493 {
void f(void) {
// This would previously crash when processing an invalid expression as an
// argument to collapse.
#pragma omp simd collapse(a) // expected-error {{use of undeclared identifier 'a'}}
for (int i = 0; i < 10; i++)
;
}
} // namespace GH138493