[clang] [CIR] Extend VecShuffleOp verifier to catch invalid index (PR #143262)
Amr Hesham via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 13 13:02:05 PDT 2025
https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/143262
>From 5f98861a88e544ec80bbaf5fe0ffb650c2d71e67 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sat, 7 Jun 2025 14:57:53 +0200
Subject: [PATCH 1/4] [CIR] Extend VecShuffleOp verifier to catch invalid index
---
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 8 ++++++++
.../IR/invalid-vector-shuffle-wrong-index.cir | 16 ++++++++++++++++
2 files changed, 24 insertions(+)
create mode 100644 clang/test/CIR/IR/invalid-vector-shuffle-wrong-index.cir
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index bfd3a0a62a8e7..b0678ee737a62 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1599,6 +1599,14 @@ LogicalResult cir::VecShuffleOp::verify() {
<< " and " << getResult().getType() << " don't match";
}
+ const uint64_t maxValidIndex =
+ getVec1().getType().getSize() + getVec2().getType().getSize() - 1;
+ for (const auto &idxAttr : getIndices().getAsRange<cir::IntAttr>()) {
+ if (idxAttr.getUInt() > maxValidIndex)
+ return emitOpError() << ": index for __builtin_shufflevector must be "
+ "less than the total number of vector elements";
+ }
+
return success();
}
diff --git a/clang/test/CIR/IR/invalid-vector-shuffle-wrong-index.cir b/clang/test/CIR/IR/invalid-vector-shuffle-wrong-index.cir
new file mode 100644
index 0000000000000..375b2d3dc563e
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-vector-shuffle-wrong-index.cir
@@ -0,0 +1,16 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+!s32i = !cir.int<s, 32>
+!s64i = !cir.int<s, 64>
+
+module {
+ cir.func @fold_shuffle_vector_op_test() -> !cir.vector<4 x !s32i> {
+ %vec_1 = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<3> : !s32i, #cir.int<5> : !s32i, #cir.int<7> : !s32i]> : !cir.vector<4 x !s32i>
+ %vec_2 = cir.const #cir.const_vector<[#cir.int<2> : !s32i, #cir.int<4> : !s32i, #cir.int<6> : !s32i, #cir.int<8> : !s32i]> : !cir.vector<4 x !s32i>
+
+ // expected-error @below {{index for __builtin_shufflevector must be less than the total number of vector elements}}
+ %new_vec = cir.vec.shuffle(%vec_1, %vec_2 : !cir.vector<4 x !s32i>) [#cir.int<9> : !s64i, #cir.int<4> : !s64i,
+ #cir.int<1> : !s64i, #cir.int<5> : !s64i] : !cir.vector<4 x !s32i>
+ cir.return %new_vec : !cir.vector<4 x !s32i>
+ }
+}
>From abe1bc9442a11af1da15d20283245b2fca775f45 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sun, 8 Jun 2025 12:27:20 +0200
Subject: [PATCH 2/4] ix the index constraint to accept -1 as valid index
---
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index b0678ee737a62..58b8fa53c1722 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1602,7 +1602,7 @@ LogicalResult cir::VecShuffleOp::verify() {
const uint64_t maxValidIndex =
getVec1().getType().getSize() + getVec2().getType().getSize() - 1;
for (const auto &idxAttr : getIndices().getAsRange<cir::IntAttr>()) {
- if (idxAttr.getUInt() > maxValidIndex)
+ if (idxAttr.getSInt() != -1 && idxAttr.getUInt() > maxValidIndex)
return emitOpError() << ": index for __builtin_shufflevector must be "
"less than the total number of vector elements";
}
>From 573e7caa235305ba8ed754a8a880bf42a79f4759 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Mon, 9 Jun 2025 19:34:36 +0200
Subject: [PATCH 3/4] Replace for loop with llvm::any_of
---
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 58b8fa53c1722..865b3bb0fa127 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1586,26 +1586,25 @@ OpFoldResult cir::VecExtractOp::fold(FoldAdaptor adaptor) {
LogicalResult cir::VecShuffleOp::verify() {
// The number of elements in the indices array must match the number of
// elements in the result type.
- if (getIndices().size() != getResult().getType().getSize()) {
+ if (getIndices().size() != getResult().getType().getSize())
return emitOpError() << ": the number of elements in " << getIndices()
<< " and " << getResult().getType() << " don't match";
- }
// The element types of the two input vectors and of the result type must
// match.
if (getVec1().getType().getElementType() !=
- getResult().getType().getElementType()) {
+ getResult().getType().getElementType())
return emitOpError() << ": element types of " << getVec1().getType()
<< " and " << getResult().getType() << " don't match";
- }
const uint64_t maxValidIndex =
getVec1().getType().getSize() + getVec2().getType().getSize() - 1;
- for (const auto &idxAttr : getIndices().getAsRange<cir::IntAttr>()) {
- if (idxAttr.getSInt() != -1 && idxAttr.getUInt() > maxValidIndex)
- return emitOpError() << ": index for __builtin_shufflevector must be "
- "less than the total number of vector elements";
- }
+ if (llvm::any_of(
+ getIndices().getAsRange<cir::IntAttr>(), [&](cir::IntAttr idxAttr) {
+ return idxAttr.getSInt() != -1 && idxAttr.getUInt() > maxValidIndex;
+ }))
+ return emitOpError() << ": index for __builtin_shufflevector must be "
+ "less than the total number of vector elements";
return success();
}
>From ca7722d275c198c55a8621952a01929f3aab9940 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Fri, 13 Jun 2025 22:01:42 +0200
Subject: [PATCH 4/4] Keep braces
---
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 865b3bb0fa127..5d06af35d43e9 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1586,26 +1586,28 @@ OpFoldResult cir::VecExtractOp::fold(FoldAdaptor adaptor) {
LogicalResult cir::VecShuffleOp::verify() {
// The number of elements in the indices array must match the number of
// elements in the result type.
- if (getIndices().size() != getResult().getType().getSize())
+ if (getIndices().size() != getResult().getType().getSize()) {
return emitOpError() << ": the number of elements in " << getIndices()
<< " and " << getResult().getType() << " don't match";
+ }
// The element types of the two input vectors and of the result type must
// match.
if (getVec1().getType().getElementType() !=
- getResult().getType().getElementType())
+ getResult().getType().getElementType()) {
return emitOpError() << ": element types of " << getVec1().getType()
<< " and " << getResult().getType() << " don't match";
+ }
const uint64_t maxValidIndex =
getVec1().getType().getSize() + getVec2().getType().getSize() - 1;
if (llvm::any_of(
getIndices().getAsRange<cir::IntAttr>(), [&](cir::IntAttr idxAttr) {
return idxAttr.getSInt() != -1 && idxAttr.getUInt() > maxValidIndex;
- }))
+ })) {
return emitOpError() << ": index for __builtin_shufflevector must be "
"less than the total number of vector elements";
-
+ }
return success();
}
More information about the cfe-commits
mailing list