Skip to content

[OpenMP] No long crash on an invalid sizes argument #139118

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 1 commit into from
May 9, 2025

Conversation

AaronBallman
Copy link
Collaborator

We were trying to get type information out of an expression node which contained errors. That causes the type of the expression to be dependent, which the code was not expecting. Now we handle error conditions with an early return.

Fixes #139073

We were trying to get type information out of an expression node which
contained errors. That causes the type of the expression to be
dependent, which the code was not expecting. Now we handle error
conditions with an early return.

Fixes llvm#139073
@AaronBallman AaronBallman requested a review from alexey-bataev May 8, 2025 17:54
@AaronBallman AaronBallman added clang Clang issues not falling into any other category openmp clang:frontend Language frontend issues, e.g. anything involving "Sema" crash-on-invalid labels May 8, 2025
@llvmbot llvmbot added the clang:openmp OpenMP related changes to Clang label May 8, 2025
@llvmbot
Copy link
Member

llvmbot commented May 8, 2025

@llvm/pr-subscribers-openmp

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

We were trying to get type information out of an expression node which contained errors. That causes the type of the expression to be dependent, which the code was not expecting. Now we handle error conditions with an early return.

Fixes #139073


Full diff: https://github.com/llvm/llvm-project/pull/139118.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaOpenMP.cpp (+12-2)
  • (modified) clang/test/OpenMP/tile_messages.cpp (+9)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7f68fd877e4e2..e6d7a19051965 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -891,6 +891,8 @@ OpenMP Support
 - Added support 'no_openmp_constructs' assumption clause.
 - Added support for 'self_maps' in map and requirement clause.
 - Added support for 'omp stripe' directive.
+- Fixed a crashing bug with ``omp tile sizes`` if the argument to ``sizes`` was
+  an invalid expression. (#GH139073)
 
 Improvements
 ^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 835dba22a858d..2534822b72f80 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -14328,6 +14328,10 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
   auto MakeDimTileSize = [&SemaRef = this->SemaRef, &CopyTransformer, &Context,
                           SizesClause, CurScope](int I) -> Expr * {
     Expr *DimTileSizeExpr = SizesClause->getSizesRefs()[I];
+
+    if (DimTileSizeExpr->containsErrors())
+      return nullptr;
+
     if (isa<ConstantExpr>(DimTileSizeExpr))
       return AssertSuccess(CopyTransformer.TransformExpr(DimTileSizeExpr));
 
@@ -14397,10 +14401,13 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
 
     // For cond-expression:
     //   .tile.iv < min(.floor.iv + DimTileSize, NumIterations)
+    Expr *DimTileSize = MakeDimTileSize(I);
+    if (!DimTileSize)
+      return StmtError();
     ExprResult EndOfTile = SemaRef.BuildBinOp(
         CurScope, LoopHelper.Cond->getExprLoc(), BO_Add,
         makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
-        MakeDimTileSize(I));
+        DimTileSize);
     if (!EndOfTile.isUsable())
       return StmtError();
     ExprResult IsPartialTile =
@@ -14482,10 +14489,13 @@ StmtResult SemaOpenMP::ActOnOpenMPTileDirective(ArrayRef<OMPClause *> Clauses,
       return StmtError();
 
     // For incr-statement: .floor.iv += DimTileSize
+    Expr *DimTileSize = MakeDimTileSize(I);
+    if (!DimTileSize)
+      return StmtError();
     ExprResult IncrStmt = SemaRef.BuildBinOp(
         CurScope, LoopHelper.Inc->getExprLoc(), BO_AddAssign,
         makeFloorIVRef(SemaRef, FloorIndVars, I, IVTy, OrigCntVar),
-        MakeDimTileSize(I));
+        DimTileSize);
     if (!IncrStmt.isUsable())
       return StmtError();
 
diff --git a/clang/test/OpenMP/tile_messages.cpp b/clang/test/OpenMP/tile_messages.cpp
index 5268dfe97e0c8..e1f5155c924e5 100644
--- a/clang/test/OpenMP/tile_messages.cpp
+++ b/clang/test/OpenMP/tile_messages.cpp
@@ -161,3 +161,12 @@ void template_inst() {
   // expected-note@+1 {{in instantiation of function template specialization 'templated_func_type_dependent<int>' requested here}}
   templated_func_type_dependent<int>();
 }
+
+namespace GH139073 {
+void f(void) {
+  // Clang would previously crash on this because of the invalid DeclRefExpr.
+#pragma omp tile sizes(a) // expected-error {{use of undeclared identifier 'a'}}
+  for (int i = 0; i < 10; i++)
+    ;
+}
+} // namespace GH139073

@AaronBallman AaronBallman merged commit 187a83f into llvm:main May 9, 2025
18 checks passed
@AaronBallman AaronBallman deleted the aballman-gh139073 branch May 9, 2025 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:openmp OpenMP related changes to Clang clang Clang issues not falling into any other category crash-on-invalid openmp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[OpenMP] UNREACHABLE executed at /root/llvm-project/llvm/tools/clang/lib/AST/ASTContext.cpp:2094!
3 participants