Skip to content

Fix a crash in constant evaluation of ExtVectorElementExprs #136771

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
Apr 24, 2025

Conversation

ahatanak
Copy link
Collaborator

Handle the case where the base expression is a pointer to a vector type.

rdar://149223362

Handle the case where the base expression is a pointer to a vector
type.

rdar://149223362
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 22, 2025

@llvm/pr-subscribers-clang

Author: Akira Hatanaka (ahatanak)

Changes

Handle the case where the base expression is a pointer to a vector type.

rdar://149223362


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

2 Files Affected:

  • (modified) clang/lib/AST/ExprConstant.cpp (+4-1)
  • (modified) clang/test/SemaCXX/constexpr-vectors-access-elements.cpp (+2)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index f598ef5929aa4..fd870c84796a5 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9197,7 +9197,10 @@ bool LValueExprEvaluator::VisitExtVectorElementExpr(
 
   if (Success) {
     Result.setFrom(Info.Ctx, Val);
-    const auto *VT = E->getBase()->getType()->castAs<VectorType>();
+    QualType BaseType = E->getBase()->getType();
+    if (E->isArrow())
+      BaseType = BaseType->getPointeeType();
+    const auto *VT = BaseType->castAs<VectorType>();
     HandleLValueVectorElement(Info, E, Result, VT->getElementType(),
                               VT->getNumElements(), Indices[0]);
   }
diff --git a/clang/test/SemaCXX/constexpr-vectors-access-elements.cpp b/clang/test/SemaCXX/constexpr-vectors-access-elements.cpp
index 08223e15feb72..58efcde414af2 100644
--- a/clang/test/SemaCXX/constexpr-vectors-access-elements.cpp
+++ b/clang/test/SemaCXX/constexpr-vectors-access-elements.cpp
@@ -43,4 +43,6 @@ static_assert(b.lo.lo == 1); // expected-error {{not an integral constant expres
 // make sure clang rejects taking address of a vector element
 static_assert(&b[1]); // expected-error {{address of vector element requested}}
 
+constexpr const FourIntsExtVec *p = &b;
+static_assert(p->x == 1);
 }

@@ -9197,7 +9197,10 @@ bool LValueExprEvaluator::VisitExtVectorElementExpr(

if (Success) {
Result.setFrom(Info.Ctx, Val);
const auto *VT = E->getBase()->getType()->castAs<VectorType>();
QualType BaseType = E->getBase()->getType();
if (E->isArrow())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little surprised this isn't already handled in the AST. I would expect the sub-expression to be a dereference of the vector type in the AST instead to having to do this (for the same reason that I would expect *vecTyPtr to have one).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is sub-expression the base expression in this case? For ExtVectorElementExpr, the base expression is whatever the LHS of -> or . is and that decision was made a long time ago. I think MemberExpr's base expression can be a pointer type too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, huh, I didn't realize that about MemberExpr, I guess this IS consistent. Odd choice that we made...

@ahatanak ahatanak merged commit feaa5aa into llvm:main Apr 24, 2025
14 checks passed
@ahatanak ahatanak deleted the ext-vector-access-arrow branch April 24, 2025 15:47
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
)

Handle the case where the base expression is a pointer to a vector type.

rdar://149223362
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
)

Handle the case where the base expression is a pointer to a vector type.

rdar://149223362
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
)

Handle the case where the base expression is a pointer to a vector type.

rdar://149223362
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
)

Handle the case where the base expression is a pointer to a vector type.

rdar://149223362
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 Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants