[clang] [mlir] [Clang]Check for a valid Index in array before getting it (PR #172399)
Balaji V. Iyer. via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 23 14:54:15 PST 2025
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" <bviyer at gmail.com>
Date: Mon, 15 Dec 2025 20:36:00 -0600
Subject: [PATCH 1/5] 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" <bviyer at gmail.com>
Date: Thu, 18 Dec 2025 20:08:30 -0600
Subject: [PATCH 2/5] 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 at +1 {{constexpr function never produces a constant expression}}
+constexpr int get_last_element(void) {
+ // expected-note at +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()
>From f0d75b1e43c6687d44dfb5b0a01ed6ce47afa7a8 Mon Sep 17 00:00:00 2001
From: "Balaji V. Iyer" <bviyer at gmail.com>
Date: Fri, 19 Dec 2025 14:19:51 -0600
Subject: [PATCH 3/5] Reformatted a expected-note string to fix windows CI
---
clang/test/AST/array-overflow-index.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/AST/array-overflow-index.cpp b/clang/test/AST/array-overflow-index.cpp
index 197f8628d0f71..7352f3af04ca4 100644
--- a/clang/test/AST/array-overflow-index.cpp
+++ b/clang/test/AST/array-overflow-index.cpp
@@ -5,6 +5,6 @@ constexpr int __attribute__((vector_size(4))) test_vector = {1};
// expected-error at +1 {{constexpr function never produces a constant expression}}
constexpr int get_last_element(void) {
- // expected-note at +1 {{cannot refer to element 18446744073709551615 of array of 1 element in a constant expression}}
+ // expected-note at +1 {{cannot refer to element}}
return test_vector[~0UL];
}
>From d7679cbd4a256fa1bbbc70b5919835403e23c224 Mon Sep 17 00:00:00 2001
From: "Balaji V. Iyer" <bviyer at gmail.com>
Date: Mon, 22 Dec 2025 18:36:25 -0600
Subject: [PATCH 4/5] Added test without constant interpreter
---
clang/test/AST/array-overflow-index.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang/test/AST/array-overflow-index.cpp b/clang/test/AST/array-overflow-index.cpp
index 7352f3af04ca4..80a9c23ec124f 100644
--- a/clang/test/AST/array-overflow-index.cpp
+++ b/clang/test/AST/array-overflow-index.cpp
@@ -1,10 +1,10 @@
-// RUN: %clang_cc1 --std=c++17 -fexperimental-new-constant-interpreter -verify %s
+// RUN: %clang_cc1 --std=c++17 -verify=none %s
+// RUN: %clang_cc1 --std=c++17 -fexperimental-new-constant-interpreter -verify=experiment %s
+// none-no-diagnostics
constexpr int __attribute__((vector_size(4))) test_vector = {1};
-// expected-error at +1 {{constexpr function never produces a constant expression}}
-constexpr int get_last_element(void) {
- // expected-note at +1 {{cannot refer to element}}
- return test_vector[~0UL];
+constexpr int get_last_element(void) { // experiment-error {{constexpr function never produces a constant expression}}
+ return test_vector[~0UL]; // experiment-note {{cannot refer to element}}
}
>From 8b26452dc5ff40b71566cecaff0826785da4abba Mon Sep 17 00:00:00 2001
From: "Balaji V. Iyer" <bviyer at gmail.com>
Date: Tue, 23 Dec 2025 16:53:58 -0600
Subject: [PATCH 5/5] Added fix for CI
---
clang/test/AST/array-overflow-index.cpp | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/clang/test/AST/array-overflow-index.cpp b/clang/test/AST/array-overflow-index.cpp
index 80a9c23ec124f..b712feaf9502a 100644
--- a/clang/test/AST/array-overflow-index.cpp
+++ b/clang/test/AST/array-overflow-index.cpp
@@ -1,10 +1,13 @@
-// RUN: %clang_cc1 --std=c++17 -verify=none %s
-// RUN: %clang_cc1 --std=c++17 -fexperimental-new-constant-interpreter -verify=experiment %s
+// RUN: %clang_cc1 --std=c++17 -fexperimental-new-constant-interpreter -verify=experiment %s
+// RUN: %clang_cc1 --std=c++17 -triple x86_64-pc-win32 -verify=experiment %s
+// RUN: %clang_cc1 --std=c++17 -triple x86_64-pc-linux -verify=none %s
+#ifndef _MSC_VER
// none-no-diagnostics
+#endif
constexpr int __attribute__((vector_size(4))) test_vector = {1};
-
constexpr int get_last_element(void) { // experiment-error {{constexpr function never produces a constant expression}}
return test_vector[~0UL]; // experiment-note {{cannot refer to element}}
}
+
More information about the cfe-commits
mailing list