Skip to content

Conversation

@Mel-Chen
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-vectorizers

Author: Mel Chen (Mel-Chen)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VPlanUtils.h (+32-23)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index 28c1a6af2570b..5aa0b0ecea9aa 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -10,6 +10,7 @@
 #define LLVM_TRANSFORMS_VECTORIZE_VPLANUTILS_H
 
 #include "VPlan.h"
+#include "llvm/ADT/TypeSwitch.h"
 
 namespace llvm {
 class ScalarEvolution;
@@ -59,29 +60,37 @@ inline bool isSingleScalar(const VPValue *VPV) {
   if (VPV->isLiveIn())
     return true;
 
-  if (auto *Rep = dyn_cast<VPReplicateRecipe>(VPV)) {
-    const VPRegionBlock *RegionOfR = Rep->getParent()->getParent();
-    // Don't consider recipes in replicate regions as uniform yet; their first
-    // lane cannot be accessed when executing the replicate region for other
-    // lanes.
-    if (RegionOfR && RegionOfR->isReplicator())
-      return false;
-    return Rep->isSingleScalar() || (PreservesUniformity(Rep->getOpcode()) &&
-                                     all_of(Rep->operands(), isSingleScalar));
-  }
-  if (isa<VPWidenGEPRecipe, VPDerivedIVRecipe, VPBlendRecipe>(VPV))
-    return all_of(VPV->getDefiningRecipe()->operands(), isSingleScalar);
-  if (auto *WidenR = dyn_cast<VPWidenRecipe>(VPV)) {
-    return PreservesUniformity(WidenR->getOpcode()) &&
-           all_of(WidenR->operands(), isSingleScalar);
-  }
-  if (auto *VPI = dyn_cast<VPInstruction>(VPV))
-    return VPI->isSingleScalar() || VPI->isVectorToScalar() ||
-           (PreservesUniformity(VPI->getOpcode()) &&
-            all_of(VPI->operands(), isSingleScalar));
-
-  // VPExpandSCEVRecipes must be placed in the entry and are alway uniform.
-  return isa<VPExpandSCEVRecipe>(VPV);
+  return TypeSwitch<const VPValue *, bool>(VPV)
+      .Case<VPReplicateRecipe>([&](const auto *Rep) {
+        const VPRegionBlock *RegionOfR = Rep->getParent()->getParent();
+        // Don't consider recipes in replicate regions as uniform yet; their
+        // first lane cannot be accessed when executing the replicate region for
+        // other lanes.
+        if (RegionOfR && RegionOfR->isReplicator())
+          return false;
+        return Rep->isSingleScalar() ||
+               (PreservesUniformity(Rep->getOpcode()) &&
+                all_of(Rep->operands(), isSingleScalar));
+      })
+      .Case<VPWidenGEPRecipe, VPDerivedIVRecipe, VPBlendRecipe>(
+          [&](const auto *R) {
+            return all_of(R->getDefiningRecipe()->operands(), isSingleScalar);
+          })
+      .Case<VPWidenRecipe>([&](const auto *WidenR) {
+        return PreservesUniformity(WidenR->getOpcode()) &&
+               all_of(WidenR->operands(), isSingleScalar);
+      })
+      .Case<VPInstruction>([&](const auto *VPI) {
+        return VPI->isSingleScalar() || VPI->isVectorToScalar() ||
+               (PreservesUniformity(VPI->getOpcode()) &&
+                all_of(VPI->operands(), isSingleScalar));
+      })
+      .Case<VPExpandSCEVRecipe>([](const VPValue *) {
+        // VPExpandSCEVRecipes must be placed in the entry and are alway
+        // uniform.
+        return true;
+      })
+      .Default([](const VPValue *) { return false; });
 }
 
 /// Return true if \p V is a header mask in \p Plan.

@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Mel Chen (Mel-Chen)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VPlanUtils.h (+32-23)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanUtils.h b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
index 28c1a6af2570b..5aa0b0ecea9aa 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanUtils.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanUtils.h
@@ -10,6 +10,7 @@
 #define LLVM_TRANSFORMS_VECTORIZE_VPLANUTILS_H
 
 #include "VPlan.h"
+#include "llvm/ADT/TypeSwitch.h"
 
 namespace llvm {
 class ScalarEvolution;
@@ -59,29 +60,37 @@ inline bool isSingleScalar(const VPValue *VPV) {
   if (VPV->isLiveIn())
     return true;
 
-  if (auto *Rep = dyn_cast<VPReplicateRecipe>(VPV)) {
-    const VPRegionBlock *RegionOfR = Rep->getParent()->getParent();
-    // Don't consider recipes in replicate regions as uniform yet; their first
-    // lane cannot be accessed when executing the replicate region for other
-    // lanes.
-    if (RegionOfR && RegionOfR->isReplicator())
-      return false;
-    return Rep->isSingleScalar() || (PreservesUniformity(Rep->getOpcode()) &&
-                                     all_of(Rep->operands(), isSingleScalar));
-  }
-  if (isa<VPWidenGEPRecipe, VPDerivedIVRecipe, VPBlendRecipe>(VPV))
-    return all_of(VPV->getDefiningRecipe()->operands(), isSingleScalar);
-  if (auto *WidenR = dyn_cast<VPWidenRecipe>(VPV)) {
-    return PreservesUniformity(WidenR->getOpcode()) &&
-           all_of(WidenR->operands(), isSingleScalar);
-  }
-  if (auto *VPI = dyn_cast<VPInstruction>(VPV))
-    return VPI->isSingleScalar() || VPI->isVectorToScalar() ||
-           (PreservesUniformity(VPI->getOpcode()) &&
-            all_of(VPI->operands(), isSingleScalar));
-
-  // VPExpandSCEVRecipes must be placed in the entry and are alway uniform.
-  return isa<VPExpandSCEVRecipe>(VPV);
+  return TypeSwitch<const VPValue *, bool>(VPV)
+      .Case<VPReplicateRecipe>([&](const auto *Rep) {
+        const VPRegionBlock *RegionOfR = Rep->getParent()->getParent();
+        // Don't consider recipes in replicate regions as uniform yet; their
+        // first lane cannot be accessed when executing the replicate region for
+        // other lanes.
+        if (RegionOfR && RegionOfR->isReplicator())
+          return false;
+        return Rep->isSingleScalar() ||
+               (PreservesUniformity(Rep->getOpcode()) &&
+                all_of(Rep->operands(), isSingleScalar));
+      })
+      .Case<VPWidenGEPRecipe, VPDerivedIVRecipe, VPBlendRecipe>(
+          [&](const auto *R) {
+            return all_of(R->getDefiningRecipe()->operands(), isSingleScalar);
+          })
+      .Case<VPWidenRecipe>([&](const auto *WidenR) {
+        return PreservesUniformity(WidenR->getOpcode()) &&
+               all_of(WidenR->operands(), isSingleScalar);
+      })
+      .Case<VPInstruction>([&](const auto *VPI) {
+        return VPI->isSingleScalar() || VPI->isVectorToScalar() ||
+               (PreservesUniformity(VPI->getOpcode()) &&
+                all_of(VPI->operands(), isSingleScalar));
+      })
+      .Case<VPExpandSCEVRecipe>([](const VPValue *) {
+        // VPExpandSCEVRecipes must be placed in the entry and are alway
+        // uniform.
+        return true;
+      })
+      .Default([](const VPValue *) { return false; });
 }
 
 /// Return true if \p V is a header mask in \p Plan.

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this is a regression in code readability, not an improvement.

We generally do not use TypeSwitch in llvm/, it looks like it leaked into VPlan (and VPlan only).

@Mel-Chen
Copy link
Contributor Author

IMHO this is a regression in code readability, not an improvement.

We generally do not use TypeSwitch in llvm/, it looks like it leaked into VPlan (and VPlan only).

Ok, we can continue the work using if (isa<...>).

@Mel-Chen Mel-Chen closed this May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants