[llvm] [RISCVInsertVSETVLI] Don't allow getSEW/getLMUL to be called for hasSEWLMULRatioOnly(). NFC (PR #171554)

via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 9 17:58:10 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Craig Topper (topperc)

<details>
<summary>Changes</summary>

Refactor some logic in transferBefore to handle hasSEWLMULRatioOnly() before calling getSEW/getLMUL.

Update adjustIncoming to use getSEWLMULRatio(). Update the interface of RISCVVType::getSameRatioLMUL to take the ratio instead of SEW and LMUL. Update the few other callers to call RISCVVType::getSEWLMULRatio first. I can keep the old signature too if reviewers prefer.

---
Full diff: https://github.com/llvm/llvm-project/pull/171554.diff


5 Files Affected:

- (modified) llvm/include/llvm/TargetParser/RISCVTargetParser.h (+1-2) 
- (modified) llvm/lib/Target/RISCV/MCA/RISCVCustomBehaviour.cpp (+2-1) 
- (modified) llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp (+21-21) 
- (modified) llvm/lib/TargetParser/RISCVTargetParser.cpp (+1-2) 
- (modified) llvm/unittests/TargetParser/RISCVTargetParserTest.cpp (+12-6) 


``````````diff
diff --git a/llvm/include/llvm/TargetParser/RISCVTargetParser.h b/llvm/include/llvm/TargetParser/RISCVTargetParser.h
index 2ac58a539d5ee..0b4c45e445bb6 100644
--- a/llvm/include/llvm/TargetParser/RISCVTargetParser.h
+++ b/llvm/include/llvm/TargetParser/RISCVTargetParser.h
@@ -165,8 +165,7 @@ LLVM_ABI void printXSfmmVType(unsigned VType, raw_ostream &OS);
 
 LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul);
 
-LLVM_ABI std::optional<VLMUL> getSameRatioLMUL(unsigned SEW, VLMUL VLMUL,
-                                               unsigned EEW);
+LLVM_ABI std::optional<VLMUL> getSameRatioLMUL(unsigned Ratio, unsigned EEW);
 } // namespace RISCVVType
 
 } // namespace llvm
diff --git a/llvm/lib/Target/RISCV/MCA/RISCVCustomBehaviour.cpp b/llvm/lib/Target/RISCV/MCA/RISCVCustomBehaviour.cpp
index b00589a2d75be..6d278106646a1 100644
--- a/llvm/lib/Target/RISCV/MCA/RISCVCustomBehaviour.cpp
+++ b/llvm/lib/Target/RISCV/MCA/RISCVCustomBehaviour.cpp
@@ -215,7 +215,8 @@ getEEWAndEMUL(unsigned Opcode, RISCVVType::VLMUL LMUL, uint8_t SEW) {
     llvm_unreachable("Could not determine EEW from Opcode");
   }
 
-  auto EMUL = RISCVVType::getSameRatioLMUL(SEW, LMUL, EEW);
+  auto EMUL =
+      RISCVVType::getSameRatioLMUL(RISCVVType::getSEWLMULRatio(SEW, LMUL), EEW);
   if (!EEW)
     llvm_unreachable("Invalid SEW or LMUL for new ratio");
   return std::make_pair(EEW, *EMUL);
diff --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
index b1ba8701663c9..04065ba1131aa 100644
--- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
@@ -607,13 +607,15 @@ class VSETVLIInfo {
     }
   }
 
+  bool hasSEWLMULRatioOnly() const { return SEWLMULRatioOnly; }
+
   unsigned getSEW() const {
-    assert(isValid() && !isUnknown() &&
+    assert(isValid() && !isUnknown() && !hasSEWLMULRatioOnly() &&
            "Can't use VTYPE for uninitialized or unknown");
     return SEW;
   }
   RISCVVType::VLMUL getVLMUL() const {
-    assert(isValid() && !isUnknown() &&
+    assert(isValid() && !isUnknown() && !hasSEWLMULRatioOnly() &&
            "Can't use VTYPE for uninitialized or unknown");
     return VLMul;
   }
@@ -727,8 +729,6 @@ class VSETVLIInfo {
                                    AltFmt);
   }
 
-  bool hasSEWLMULRatioOnly() const { return SEWLMULRatioOnly; }
-
   bool hasSameVTYPE(const VSETVLIInfo &Other) const {
     assert(isValid() && Other.isValid() &&
            "Can't compare invalid VSETVLIInfos");
@@ -1317,8 +1317,8 @@ static VSETVLIInfo adjustIncoming(const VSETVLIInfo &PrevInfo,
 
   if (!Demanded.LMUL && !Demanded.SEWLMULRatio && PrevInfo.isValid() &&
       !PrevInfo.isUnknown()) {
-    if (auto NewVLMul = RISCVVType::getSameRatioLMUL(
-            PrevInfo.getSEW(), PrevInfo.getVLMUL(), Info.getSEW()))
+    if (auto NewVLMul = RISCVVType::getSameRatioLMUL(PrevInfo.getSEWLMULRatio(),
+                                                     Info.getSEW()))
       Info.setVLMul(*NewVLMul);
     Demanded.LMUL = DemandedFields::LMULEqual;
   }
@@ -1371,25 +1371,25 @@ void RISCVInsertVSETVLI::transferBefore(VSETVLIInfo &Info,
   if (Demanded.VLAny || (Demanded.VLZeroness && !EquallyZero))
     Info.setAVL(IncomingInfo);
 
-  Info.setVTYPE(
-      ((Demanded.LMUL || Demanded.SEWLMULRatio) ? IncomingInfo : Info)
-          .getVLMUL(),
-      ((Demanded.SEW || Demanded.SEWLMULRatio) ? IncomingInfo : Info).getSEW(),
-      // Prefer tail/mask agnostic since it can be relaxed to undisturbed later
-      // if needed.
-      (Demanded.TailPolicy ? IncomingInfo : Info).getTailAgnostic() ||
-          IncomingInfo.getTailAgnostic(),
-      (Demanded.MaskPolicy ? IncomingInfo : Info).getMaskAgnostic() ||
-          IncomingInfo.getMaskAgnostic(),
-      (Demanded.AltFmt ? IncomingInfo : Info).getAltFmt(),
-      Demanded.TWiden ? IncomingInfo.getTWiden() : 0);
-
-  // If we only knew the sew/lmul ratio previously, replace the VTYPE but keep
-  // the AVL.
+  // If we only knew the sew/lmul ratio previously, replace the VTYPE.
   if (Info.hasSEWLMULRatioOnly()) {
     VSETVLIInfo RatiolessInfo = IncomingInfo;
     RatiolessInfo.setAVL(Info);
     Info = RatiolessInfo;
+  } else {
+    Info.setVTYPE(
+        ((Demanded.LMUL || Demanded.SEWLMULRatio) ? IncomingInfo : Info)
+            .getVLMUL(),
+        ((Demanded.SEW || Demanded.SEWLMULRatio) ? IncomingInfo : Info)
+            .getSEW(),
+        // Prefer tail/mask agnostic since it can be relaxed to undisturbed
+        // later if needed.
+        (Demanded.TailPolicy ? IncomingInfo : Info).getTailAgnostic() ||
+            IncomingInfo.getTailAgnostic(),
+        (Demanded.MaskPolicy ? IncomingInfo : Info).getMaskAgnostic() ||
+            IncomingInfo.getMaskAgnostic(),
+        (Demanded.AltFmt ? IncomingInfo : Info).getAltFmt(),
+        Demanded.TWiden ? IncomingInfo.getTWiden() : 0);
   }
 }
 
diff --git a/llvm/lib/TargetParser/RISCVTargetParser.cpp b/llvm/lib/TargetParser/RISCVTargetParser.cpp
index 5ea63a973ea37..d0bd39f15afc4 100644
--- a/llvm/lib/TargetParser/RISCVTargetParser.cpp
+++ b/llvm/lib/TargetParser/RISCVTargetParser.cpp
@@ -244,8 +244,7 @@ unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul) {
   return (SEW * 8) / LMul;
 }
 
-std::optional<VLMUL> getSameRatioLMUL(unsigned SEW, VLMUL VLMul, unsigned EEW) {
-  unsigned Ratio = RISCVVType::getSEWLMULRatio(SEW, VLMul);
+std::optional<VLMUL> getSameRatioLMUL(unsigned Ratio, unsigned EEW) {
   unsigned EMULFixedPoint = (EEW * 8) / Ratio;
   bool Fractional = EMULFixedPoint < 8;
   unsigned EMUL = Fractional ? 8 / EMULFixedPoint : EMULFixedPoint / 8;
diff --git a/llvm/unittests/TargetParser/RISCVTargetParserTest.cpp b/llvm/unittests/TargetParser/RISCVTargetParserTest.cpp
index 63ac8f993ecdc..f778568dd373a 100644
--- a/llvm/unittests/TargetParser/RISCVTargetParserTest.cpp
+++ b/llvm/unittests/TargetParser/RISCVTargetParserTest.cpp
@@ -15,19 +15,25 @@ namespace {
 TEST(RISCVVType, CheckSameRatioLMUL) {
   // Smaller LMUL.
   EXPECT_EQ(RISCVVType::LMUL_1,
-            RISCVVType::getSameRatioLMUL(16, RISCVVType::LMUL_2, 8));
+            RISCVVType::getSameRatioLMUL(
+                RISCVVType::getSEWLMULRatio(16, RISCVVType::LMUL_2), 8));
   EXPECT_EQ(RISCVVType::LMUL_F2,
-            RISCVVType::getSameRatioLMUL(16, RISCVVType::LMUL_1, 8));
+            RISCVVType::getSameRatioLMUL(
+                RISCVVType::getSEWLMULRatio(16, RISCVVType::LMUL_1), 8));
   // Smaller fractional LMUL.
   EXPECT_EQ(RISCVVType::LMUL_F8,
-            RISCVVType::getSameRatioLMUL(16, RISCVVType::LMUL_F4, 8));
+            RISCVVType::getSameRatioLMUL(
+                RISCVVType::getSEWLMULRatio(16, RISCVVType::LMUL_F4), 8));
   // Bigger LMUL.
   EXPECT_EQ(RISCVVType::LMUL_2,
-            RISCVVType::getSameRatioLMUL(8, RISCVVType::LMUL_1, 16));
+            RISCVVType::getSameRatioLMUL(
+                RISCVVType::getSEWLMULRatio(8, RISCVVType::LMUL_1), 16));
   EXPECT_EQ(RISCVVType::LMUL_1,
-            RISCVVType::getSameRatioLMUL(8, RISCVVType::LMUL_F2, 16));
+            RISCVVType::getSameRatioLMUL(
+                RISCVVType::getSEWLMULRatio(8, RISCVVType::LMUL_F2), 16));
   // Bigger fractional LMUL.
   EXPECT_EQ(RISCVVType::LMUL_F2,
-            RISCVVType::getSameRatioLMUL(8, RISCVVType::LMUL_F4, 16));
+            RISCVVType::getSameRatioLMUL(
+                RISCVVType::getSEWLMULRatio(8, RISCVVType::LMUL_F4), 16));
 }
 } // namespace

``````````

</details>


https://github.com/llvm/llvm-project/pull/171554


More information about the llvm-commits mailing list