Skip to content

Conversation

artagnon
Copy link
Contributor

@artagnon artagnon commented Sep 13, 2025

Avoid conflating the RepR and RepOrWidenR variables: scope them out, so that they're used in distinct blocks.

@llvmbot
Copy link
Member

llvmbot commented Sep 13, 2025

@llvm/pr-subscribers-vectorizers

@llvm/pr-subscribers-llvm-transforms

Author: Ramkumar Ramachandra (artagnon)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp (+5-4)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 2cac5557daeee..14d32f5225a2d 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1307,13 +1307,13 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
       if (RepR && (RepR->isSingleScalar() || RepR->isPredicated()))
         continue;
 
-      auto *RepOrWidenR = cast<VPSingleDefRecipe>(&R);
-      if (RepR && isa<StoreInst>(RepR->getUnderlyingInstr()) &&
+      // Handle replicate-stores to single-scalar address.
+      if (RepR && RepR->getOpcode() == Instruction::Store &&
           vputils::isSingleScalar(RepR->getOperand(1))) {
         auto *Clone = new VPReplicateRecipe(
-            RepOrWidenR->getUnderlyingInstr(), RepOrWidenR->operands(),
+            RepR->getUnderlyingInstr(), RepR->operands(),
             true /*IsSingleScalar*/, nullptr /*Mask*/, *RepR /*Metadata*/);
-        Clone->insertBefore(RepOrWidenR);
+        Clone->insertBefore(RepR);
         auto *Ext = new VPInstruction(VPInstruction::ExtractLastElement,
                                       {Clone->getOperand(0)});
         Ext->insertBefore(Clone);
@@ -1325,6 +1325,7 @@ static void narrowToSingleScalarRecipes(VPlan &Plan) {
       // Skip recipes that aren't single scalars or don't have only their
       // scalar results used. In the latter case, we would introduce extra
       // broadcasts.
+      auto *RepOrWidenR = cast<VPSingleDefRecipe>(&R);
       if (!vputils::isSingleScalar(RepOrWidenR) ||
           !all_of(RepOrWidenR->users(), [RepOrWidenR](const VPUser *U) {
             return U->usesScalars(RepOrWidenR) ||

@david-arm
Copy link
Contributor

Can you explain in the commit message why this is an improvement? It looks like NFC refactoring, but it's not immediately obvious why it's better than before?

@artagnon
Copy link
Contributor Author

Can you explain in the commit message why this is an improvement? It looks like NFC refactoring, but it's not immediately obvious why it's better than before?

Done.

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

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

Using RepOrWidenR for both cases with cloning should help to denote that we are cloning the same thing.

If possiblem it would probably good to instead refactor so that there is only a single clone needed

@artagnon artagnon closed this Sep 15, 2025
@artagnon artagnon deleted the vplan-narrow-singlescalar-nfc branch September 15, 2025 14:22
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