Skip to content

Commit 009398b

Browse files
authored
[MachineVerifier] Improve checks for G_INSERT_SUBVECTOR. (#109209)
-Improve messages. -Remove redundant checks that are handled in generic code. -Add check that the subvector is smaller than the vector. -Add checks that subvector is smaller than the vector.
1 parent e494e2a commit 009398b

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,41 +1710,51 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
17101710
}
17111711

17121712
LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1713-
LLT Src0Ty = MRI->getType(Src0Op.getReg());
17141713
LLT Src1Ty = MRI->getType(Src1Op.getReg());
17151714

17161715
if (!DstTy.isVector()) {
17171716
report("Destination type must be a vector", MI);
17181717
break;
17191718
}
17201719

1721-
if (!Src0Ty.isVector()) {
1722-
report("First source must be a vector", MI);
1720+
if (!Src1Ty.isVector()) {
1721+
report("Second source must be a vector", MI);
17231722
break;
17241723
}
17251724

1726-
if (!Src1Ty.isVector()) {
1727-
report("Second source must be a vector", MI);
1725+
if (DstTy.getElementType() != Src1Ty.getElementType()) {
1726+
report("Element type of vectors must be the same", MI);
17281727
break;
17291728
}
17301729

1731-
if (DstTy != Src0Ty) {
1732-
report("Destination type must match the first source vector type", MI);
1730+
if (Src1Ty.isScalable() != DstTy.isScalable()) {
1731+
report("Vector types must both be fixed or both be scalable", MI);
17331732
break;
17341733
}
17351734

1736-
if (Src0Ty.getElementType() != Src1Ty.getElementType()) {
1737-
report("Element type of source vectors must be the same", MI);
1735+
if (ElementCount::isKnownGT(Src1Ty.getElementCount(),
1736+
DstTy.getElementCount())) {
1737+
report("Second source must be smaller than destination vector", MI);
17381738
break;
17391739
}
17401740

1741-
if (IndexOp.getImm() != 0 &&
1742-
IndexOp.getImm() % Src1Ty.getElementCount().getKnownMinValue() != 0) {
1741+
uint64_t Idx = IndexOp.getImm();
1742+
uint64_t Src1MinLen = Src1Ty.getElementCount().getKnownMinValue();
1743+
if (IndexOp.getImm() % Src1MinLen != 0) {
17431744
report("Index must be a multiple of the second source vector's "
17441745
"minimum vector length",
17451746
MI);
17461747
break;
17471748
}
1749+
1750+
uint64_t DstMinLen = DstTy.getElementCount().getKnownMinValue();
1751+
if (Idx >= DstMinLen || Idx + Src1MinLen > DstMinLen) {
1752+
report("Subvector type and index must not cause insert to overrun the "
1753+
"vector being inserted into",
1754+
MI);
1755+
break;
1756+
}
1757+
17481758
break;
17491759
}
17501760
case TargetOpcode::G_EXTRACT_SUBVECTOR: {

llvm/test/MachineVerifier/test_g_insert_subvector.mir

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ body: |
1111
%1:_(<vscale x 2 x s32>) = G_IMPLICIT_DEF
1212
%2:_(<vscale x 1 x s32>) = G_IMPLICIT_DEF
1313
14+
; CHECK: generic instruction must use register operands
1415
; CHECK: G_INSERT_SUBVECTOR first source must be a register
1516
%3:_(<vscale x 2 x s32>) = G_INSERT_SUBVECTOR 1, %2, 0
1617
18+
; CHECK: generic instruction must use register operands
1719
; CHECK: G_INSERT_SUBVECTOR second source must be a register
1820
%4:_(<vscale x 2 x s32>) = G_INSERT_SUBVECTOR %1, 1, 0
1921
@@ -23,18 +25,18 @@ body: |
2325
; CHECK: Destination type must be a vector
2426
%6:_(s32) = G_INSERT_SUBVECTOR %1, %2, 0
2527
26-
; CHECK: First source must be a vector
28+
; CHECK: Type mismatch in generic instruction
2729
%7:_(<vscale x 2 x s32>) = G_INSERT_SUBVECTOR %0, %2, 0
2830
2931
; CHECK: Second source must be a vector
3032
%8:_(<vscale x 2 x s32>) = G_INSERT_SUBVECTOR %1, %0, 0
3133
32-
; CHECK: Destination type must match the first source vector type
34+
; CHECK: Type mismatch in generic instruction
3335
%9:_(<vscale x 2 x s32>) = G_INSERT_SUBVECTOR %2, %1, 0
3436
3537
%10:_(<vscale x 1 x s16>) = G_IMPLICIT_DEF
3638
37-
; CHECK: Element type of source vectors must be the same
39+
; CHECK: Element type of vectors must be the same
3840
%11:_(<vscale x 2 x s32>) = G_INSERT_SUBVECTOR %1, %10, 0
3941
4042
%12:_(<vscale x 4 x s32>) = G_IMPLICIT_DEF
@@ -43,5 +45,23 @@ body: |
4345
%13:_(<vscale x 4 x s32>) = G_INSERT_SUBVECTOR %12, %1, 3
4446
4547
; CHECK: Index must be a multiple of the second source vector's minimum vector length
46-
%13:_(<vscale x 4 x s32>) = G_INSERT_SUBVECTOR %12, %1, 1
48+
%14:_(<vscale x 4 x s32>) = G_INSERT_SUBVECTOR %12, %1, 1
49+
50+
%15:_(<vscale x 4 x s32>) = G_IMPLICIT_DEF
51+
52+
; CHECK: Second source must be smaller than destination vector
53+
%16:_(<vscale x 2 x s32>) = G_INSERT_SUBVECTOR %1, %15, 0
54+
55+
; CHECK: Subvector type and index must not cause insert to overrun the vector being inserted into
56+
%17:_(<vscale x 4 x s32>) = G_INSERT_SUBVECTOR %12, %1, 4
57+
58+
%18:_(<vscale x 3 x s32>) = G_IMPLICIT_DEF
59+
60+
; CHECK: Subvector type and index must not cause insert to overrun the vector being inserted into
61+
%19:_(<vscale x 3 x s32>) = G_INSERT_SUBVECTOR %18, %1, 2
62+
63+
%20:_(<2 x s32>) = G_IMPLICIT_DEF
64+
65+
; CHECK: Vector types must both be fixed or both be scalable
66+
%21:_(<vscale x 1 x s32>) = G_INSERT_SUBVECTOR %12, %20, 2
4767
...

0 commit comments

Comments
 (0)