https://github.com/bviyer updated 
https://github.com/llvm/llvm-project/pull/172399

>From f8f07bd967abd5b59244b03b86b2cd93dd5da2ea Mon Sep 17 00:00:00 2001
From: "Balaji V. Iyer" <[email protected]>
Date: Mon, 15 Dec 2025 20:36:00 -0600
Subject: [PATCH 1/2] Check for a valid Index in array before getting it

---
 clang/lib/AST/ExprConstant.cpp                        | 5 +++++
 clang/test/AST/array-overflow-index.c                 | 8 ++++++++
 mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp | 6 ++++++
 3 files changed, 19 insertions(+)
 create mode 100644 clang/test/AST/array-overflow-index.c

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index eb07cfb938a21..d83e8b6b1788b 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9749,6 +9749,11 @@ bool LValueExprEvaluator::VisitArraySubscriptExpr(const 
ArraySubscriptExpr *E) {
 
     if (Success) {
       Result.setFrom(Info.Ctx, Val);
+      // If Index cannot be represented as a 64 bit integer, return
+      // unsuccessful.
+      if (!Index.tryExtValue().has_value())
+        return Error(E);
+
       HandleLValueVectorElement(Info, E, Result, VT->getElementType(),
                                 VT->getNumElements(), Index.getExtValue());
     }
diff --git a/clang/test/AST/array-overflow-index.c 
b/clang/test/AST/array-overflow-index.c
new file mode 100644
index 0000000000000..99bd2b9318017
--- /dev/null
+++ b/clang/test/AST/array-overflow-index.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -verify %s
+
+// expected-no-diagnostics
+
+int __attribute__((vector_size(4))) test_vector = {1};
+int get_last_element(void) {
+    return test_vector[~0UL];
+}
diff --git a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp 
b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
index 2cbb5ab3067f2..6b5ddc5e748be 100644
--- a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
+++ b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
@@ -178,6 +178,12 @@ struct TestVectorUnrollingPatterns
                                      .setFilterConstraint([](Operation *op) {
                                        return success(isa<vector::StepOp>(op));
                                      }));
+    populateVectorUnrollPatterns(
+        patterns, UnrollVectorOptions()
+                      .setNativeShape(ArrayRef<int64_t>{8, 8})
+                      .setFilterConstraint([](Operation *op) {
+                        return success(isa<vector::CreateMaskOp>(op));
+                      }));
     populateVectorUnrollPatterns(
         patterns,
         UnrollVectorOptions()

>From eaef5856ad749a89f91a84348a4049ffafc9cdbe Mon Sep 17 00:00:00 2001
From: "Balaji V. Iyer" <[email protected]>
Date: Thu, 18 Dec 2025 20:08:30 -0600
Subject: [PATCH 2/2] Added changes suggested by reviewers

---
 clang/docs/ReleaseNotes.rst                           |  2 ++
 clang/lib/AST/ExprConstant.cpp                        |  2 +-
 clang/test/AST/array-overflow-index.cpp               | 10 ++++++++++
 mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp |  6 ------
 4 files changed, 13 insertions(+), 7 deletions(-)
 create mode 100644 clang/test/AST/array-overflow-index.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c153c9c77b09f..d2b80238ee825 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -615,6 +615,8 @@ Bug Fixes to C++ Support
 - Fix the result of ``__is_pointer_interconvertible_base_of`` when arguments 
are qualified and passed via template parameters. (#GH135273)
 - Fixed a crash when evaluating nested requirements in requires-expressions 
that reference invented parameters. (#GH166325)
 - Fixed a crash when standard comparison categories (e.g. 
``std::partial_ordering``) are defined with incorrect static member types. 
(#GH170015) (#GH56571)
+- Fixed a crash where the constexpr evaluation for an array index is failing 
when it is not
+  representable as a 64 bit number (#GH154713)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d83e8b6b1788b..a50d20b5320c3 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9752,7 +9752,7 @@ bool LValueExprEvaluator::VisitArraySubscriptExpr(const 
ArraySubscriptExpr *E) {
       // If Index cannot be represented as a 64 bit integer, return
       // unsuccessful.
       if (!Index.tryExtValue().has_value())
-        return Error(E);
+        return false;
 
       HandleLValueVectorElement(Info, E, Result, VT->getElementType(),
                                 VT->getNumElements(), Index.getExtValue());
diff --git a/clang/test/AST/array-overflow-index.cpp 
b/clang/test/AST/array-overflow-index.cpp
new file mode 100644
index 0000000000000..197f8628d0f71
--- /dev/null
+++ b/clang/test/AST/array-overflow-index.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1  --std=c++17 -fexperimental-new-constant-interpreter 
-verify %s
+
+
+constexpr int __attribute__((vector_size(4))) test_vector = {1};
+
+// expected-error@+1 {{constexpr function never produces a constant 
expression}}
+constexpr int get_last_element(void) {
+    // expected-note@+1 {{cannot refer to element 18446744073709551615 of 
array of 1 element in a constant expression}}
+    return test_vector[~0UL];
+}
diff --git a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp 
b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
index 6b5ddc5e748be..2cbb5ab3067f2 100644
--- a/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
+++ b/mlir/test/lib/Dialect/Vector/TestVectorTransforms.cpp
@@ -178,12 +178,6 @@ struct TestVectorUnrollingPatterns
                                      .setFilterConstraint([](Operation *op) {
                                        return success(isa<vector::StepOp>(op));
                                      }));
-    populateVectorUnrollPatterns(
-        patterns, UnrollVectorOptions()
-                      .setNativeShape(ArrayRef<int64_t>{8, 8})
-                      .setFilterConstraint([](Operation *op) {
-                        return success(isa<vector::CreateMaskOp>(op));
-                      }));
     populateVectorUnrollPatterns(
         patterns,
         UnrollVectorOptions()

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to