[llvm] [MachineVerifier] Check that the destination of G_EXTRACT_SUBVECTOR is smaller than the source. (PR #109202)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 18 14:16:57 PDT 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/109202

None

>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] [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
 
 ...



More information about the llvm-commits mailing list