[llvm] [MachineVerifier] Improve G_EXTRACT_SUBVECTOR checking (PR #109202)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 18 15:02:44 PDT 2024
https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/109202
>From 1b0dd2b2ca9f499d95ef30a0a1a74645450d3cd9 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 18 Sep 2024 14:12:59 -0700
Subject: [PATCH 1/3] [MachineVerifier] Check that the destination of
G_EXTRACT_SUBVECTOR is smaller than the source.
---
llvm/lib/CodeGen/MachineVerifier.cpp | 11 ++++++++---
.../test/MachineVerifier/test_g_extract_subvector.mir | 8 +++++---
2 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp
index 1fcbeeec6f64cc..7251b2b5f3d212 100644
--- a/llvm/lib/CodeGen/MachineVerifier.cpp
+++ b/llvm/lib/CodeGen/MachineVerifier.cpp
@@ -1769,7 +1769,7 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
}
if (!SrcTy.isVector()) {
- report("First source must be a vector", MI);
+ report("Source must be a vector", MI);
break;
}
@@ -1783,6 +1783,12 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
break;
}
+ if (ElementCount::isKnownGT(DstTy.getElementCount(),
+ SrcTy.getElementCount())) {
+ report("Destination vector must be smaller than source vector", MI);
+ break;
+ }
+
uint64_t Idx = IndexOp.getImm();
uint64_t DstMinLen = DstTy.getElementCount().getKnownMinValue();
if (Idx % DstMinLen != 0) {
@@ -1793,8 +1799,7 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
}
uint64_t SrcMinLen = SrcTy.getElementCount().getKnownMinValue();
- if (SrcTy.isScalable() == DstTy.isScalable() &&
- (Idx >= SrcMinLen || Idx + DstMinLen > SrcMinLen)) {
+ if ((Idx >= SrcMinLen || Idx + DstMinLen > SrcMinLen)) {
report("Source type and index must not cause extract to overrun to the "
"destination type",
MI);
diff --git a/llvm/test/MachineVerifier/test_g_extract_subvector.mir b/llvm/test/MachineVerifier/test_g_extract_subvector.mir
index 6a0b7ebfb4b0b2..1afb84984c2967 100644
--- a/llvm/test/MachineVerifier/test_g_extract_subvector.mir
+++ b/llvm/test/MachineVerifier/test_g_extract_subvector.mir
@@ -19,7 +19,7 @@ body: |
; CHECK: Destination type must be a vector
%5:_(s32) = G_EXTRACT_SUBVECTOR %2, 0
- ; CHECK: First source must be a vector
+ ; CHECK: Source must be a vector
%6:_(<vscale x 2 x s32>) = G_EXTRACT_SUBVECTOR %0, 0
%7:_(<vscale x 1 x s16>) = G_IMPLICIT_DEF
@@ -27,10 +27,10 @@ body: |
; CHECK: Element type of vectors must be the same
%8:_(<vscale x 2 x s32>) = G_EXTRACT_SUBVECTOR %7, 0
- ; CHECK: Index must be a multiple of the destination vector's minimum vector length
+ ; CHECK: Destination vector must be smaller than source vector
%9:_(<vscale x 4 x s32>) = G_EXTRACT_SUBVECTOR %1, 3
- ; CHECK: Index must be a multiple of the destination vector's minimum vector length
+ ; CHECK: Destination vector must be smaller than source vector
%10:_(<vscale x 4 x s32>) = G_EXTRACT_SUBVECTOR %1, 2
; CHECK: Source type and index must not cause extract to overrun to the destination type
@@ -56,5 +56,7 @@ body: |
; CHECK: Vector types must both be fixed or both be scalable
%19:_(<2 x s32>) = G_EXTRACT_SUBVECTOR %12, 0
+ ; CHECK: Index must be a multiple of the destination vector's minimum vector length
+ %20:_(<2 x s32>) = G_EXTRACT_SUBVECTOR %12, 1
...
>From 70fedddb629411c79332cfcafe18ca0614aba013 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 18 Sep 2024 14:44:59 -0700
Subject: [PATCH 2/3] fixup! address comment. fix bad test case. fix diagnostic
message.
---
llvm/lib/CodeGen/MachineVerifier.cpp | 6 +++---
llvm/test/MachineVerifier/test_g_extract_subvector.mir | 10 +++++-----
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp
index 7251b2b5f3d212..651de06cfac25d 100644
--- a/llvm/lib/CodeGen/MachineVerifier.cpp
+++ b/llvm/lib/CodeGen/MachineVerifier.cpp
@@ -1799,9 +1799,9 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
}
uint64_t SrcMinLen = SrcTy.getElementCount().getKnownMinValue();
- if ((Idx >= SrcMinLen || Idx + DstMinLen > SrcMinLen)) {
- report("Source type and index must not cause extract to overrun to the "
- "destination type",
+ if (Idx >= SrcMinLen || Idx + DstMinLen > SrcMinLen) {
+ report("Destination type and index must not cause extract to overrun the "
+ "source vector",
MI);
break;
}
diff --git a/llvm/test/MachineVerifier/test_g_extract_subvector.mir b/llvm/test/MachineVerifier/test_g_extract_subvector.mir
index 1afb84984c2967..8065953916827d 100644
--- a/llvm/test/MachineVerifier/test_g_extract_subvector.mir
+++ b/llvm/test/MachineVerifier/test_g_extract_subvector.mir
@@ -33,21 +33,21 @@ body: |
; CHECK: Destination vector must be smaller than source vector
%10:_(<vscale x 4 x s32>) = G_EXTRACT_SUBVECTOR %1, 2
- ; CHECK: Source type and index must not cause extract to overrun to the destination type
+ ; CHECK: Destination type and index must not cause extract to overrun the source vector
%11:_(<vscale x 1 x s32>) = G_EXTRACT_SUBVECTOR %1, 4
%12:_(<vscale x 4 x s32>) = G_IMPLICIT_DEF
- ; CHECK: Source type and index must not cause extract to overrun to the destination type
+ ; CHECK: Destination type and index must not cause extract to overrun the source vector
%13:_(<vscale x 3 x s32>) = G_EXTRACT_SUBVECTOR %12, 3
%14:_(<2 x s32>) = G_IMPLICIT_DEF
%15:_(<4 x s32>) = G_IMPLICIT_DEF
- ; CHECK: Source type and index must not cause extract to overrun to the destination type
+ ; CHECK: Destination type and index must not cause extract to overrun the source vector
%16:_(<2 x s32>) = G_EXTRACT_SUBVECTOR %14, 4
- ; CHECK: Source type and index must not cause extract to overrun to the destination type
+ ; CHECK: Destination type and index must not cause extract to overrun the source vector
%17:_(<3 x s32>) = G_EXTRACT_SUBVECTOR %15, 3
; CHECK: Vector types must both be fixed or both be scalable
@@ -57,6 +57,6 @@ body: |
%19:_(<2 x s32>) = G_EXTRACT_SUBVECTOR %12, 0
; CHECK: Index must be a multiple of the destination vector's minimum vector length
- %20:_(<2 x s32>) = G_EXTRACT_SUBVECTOR %12, 1
+ %20:_(<vscale x 2 x s32>) = G_EXTRACT_SUBVECTOR %12, 1
...
>From 27cd76e17be27d30a0ef6e1b93d89c5f0cb0b25d Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Wed, 18 Sep 2024 15:02:25 -0700
Subject: [PATCH 3/3] fixup! Add check for a generic error that's also being
emitted
---
llvm/test/MachineVerifier/test_g_extract_subvector.mir | 1 +
1 file changed, 1 insertion(+)
diff --git a/llvm/test/MachineVerifier/test_g_extract_subvector.mir b/llvm/test/MachineVerifier/test_g_extract_subvector.mir
index 8065953916827d..3b9c8314f7ceca 100644
--- a/llvm/test/MachineVerifier/test_g_extract_subvector.mir
+++ b/llvm/test/MachineVerifier/test_g_extract_subvector.mir
@@ -10,6 +10,7 @@ body: |
%1:_(<vscale x 2 x s32>) = G_IMPLICIT_DEF
%2:_(<vscale x 1 x s32>) = G_IMPLICIT_DEF
+ ; CHECK: generic instruction must use register operands
; CHECK: G_EXTRACT_SUBVECTOR first source must be a register
%3:_(<vscale x 2 x s32>) = G_EXTRACT_SUBVECTOR 1, 0
More information about the llvm-commits
mailing list