Skip to content

Commit f62a8ab

Browse files
authored
[CIR] Extend VecShuffleOp verifier to catch invalid index (#143262)
Extend the verifier to catch index larger than the size of vector elements in VecShuffleOp Issue #136487
1 parent be5c96b commit f62a8ab

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
@@ -1643,6 +1643,15 @@ LogicalResult cir::VecShuffleOp::verify() {
16431643
<< " and " << getResult().getType() << " don't match";
16441644
}
16451645

1646+
const uint64_t maxValidIndex =
1647+
getVec1().getType().getSize() + getVec2().getType().getSize() - 1;
1648+
if (llvm::any_of(
1649+
getIndices().getAsRange<cir::IntAttr>(), [&](cir::IntAttr idxAttr) {
1650+
return idxAttr.getSInt() != -1 && idxAttr.getUInt() > maxValidIndex;
1651+
})) {
1652+
return emitOpError() << ": index for __builtin_shufflevector must be "
1653+
"less than the total number of vector elements";
1654+
}
16461655
return success();
16471656
}
16481657

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<4 x !s32i> {
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<4 x !s32i>
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<4 x !s32i>
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<4 x !s32i>) [#cir.int<9> : !s64i, #cir.int<4> : !s64i,
13+
#cir.int<1> : !s64i, #cir.int<5> : !s64i] : !cir.vector<4 x !s32i>
14+
cir.return %new_vec : !cir.vector<4 x !s32i>
15+
}
16+
}

0 commit comments

Comments
 (0)