Skip to content

Commit 26b1332

Browse files
authored
[CIR] Backport Extending VecShuffleOp verifier to catch invalid index (#1670)
Backport the VecShuffleOp verifier to catch invalid index Implemented in llvm/llvm-project#143262
1 parent 51e17c0 commit 26b1332

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,15 @@ LogicalResult cir::VecShuffleOp::verify() {
10811081
[](mlir::Attribute attr) { return mlir::isa<cir::IntAttr>(attr); })) {
10821082
return emitOpError() << "all index values must be integers";
10831083
}
1084+
1085+
const uint64_t maxValidIndex =
1086+
getVec1().getType().getSize() + getVec2().getType().getSize() - 1;
1087+
for (const auto &idxAttr : getIndices().getAsRange<cir::IntAttr>()) {
1088+
if (idxAttr.getSInt() != -1 && idxAttr.getUInt() > maxValidIndex)
1089+
return emitOpError() << ": index for __builtin_shufflevector must be "
1090+
"less than the total number of vector elements";
1091+
}
1092+
10841093
return success();
10851094
}
10861095

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: cir-opt %s -verify-diagnostics -split-input-file
2+
3+
!s32i = !cir.int<s, 32>
4+
!s64i = !cir.int<s, 64>
5+
6+
module {
7+
cir.func @fold_shuffle_vector_op_test() -> !cir.vector<!s32i x 4> {
8+
%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<!s32i x 4>
9+
%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<!s32i x 4>
10+
11+
// expected-error @below {{index for __builtin_shufflevector must be less than the total number of vector elements}}
12+
%new_vec = cir.vec.shuffle(%vec_1, %vec_2 : !cir.vector<!s32i x 4>) [#cir.int<9> : !s64i, #cir.int<4> : !s64i,
13+
#cir.int<1> : !s64i, #cir.int<5> : !s64i] : !cir.vector<!s32i x 4>
14+
cir.return %new_vec : !cir.vector<!s32i x 4>
15+
}
16+
}

0 commit comments

Comments
 (0)