Skip to content

[VPlan] Refine return types in VPBuilder (NFC) #108858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025

Conversation

artagnon
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Sep 16, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Ramkumar Ramachandra (artagnon)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h (+14-13)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
index 2fe9af6b0d14f5..d3056b8859a9d4 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
@@ -164,33 +164,34 @@ class VPBuilder {
     return tryInsertInstruction(
         new VPInstruction(Opcode, Operands, WrapFlags, DL, Name));
   }
-  VPValue *createNot(VPValue *Operand, DebugLoc DL = {},
-                     const Twine &Name = "") {
+  VPInstruction *createNot(VPValue *Operand, DebugLoc DL = {},
+                           const Twine &Name = "") {
     return createInstruction(VPInstruction::Not, {Operand}, DL, Name);
   }
 
-  VPValue *createAnd(VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
-                     const Twine &Name = "") {
+  VPInstruction *createAnd(VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
+                           const Twine &Name = "") {
     return createInstruction(Instruction::BinaryOps::And, {LHS, RHS}, DL, Name);
   }
 
-  VPValue *createOr(VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
-                    const Twine &Name = "") {
+  VPInstruction *createOr(VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
+                          const Twine &Name = "") {
 
     return tryInsertInstruction(new VPInstruction(
         Instruction::BinaryOps::Or, {LHS, RHS},
         VPRecipeWithIRFlags::DisjointFlagsTy(false), DL, Name));
   }
 
-  VPValue *createLogicalAnd(VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
-                            const Twine &Name = "") {
+  VPInstruction *createLogicalAnd(VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
+                                  const Twine &Name = "") {
     return tryInsertInstruction(
         new VPInstruction(VPInstruction::LogicalAnd, {LHS, RHS}, DL, Name));
   }
 
-  VPValue *createSelect(VPValue *Cond, VPValue *TrueVal, VPValue *FalseVal,
-                        DebugLoc DL = {}, const Twine &Name = "",
-                        std::optional<FastMathFlags> FMFs = std::nullopt) {
+  VPInstruction *
+  createSelect(VPValue *Cond, VPValue *TrueVal, VPValue *FalseVal,
+               DebugLoc DL = {}, const Twine &Name = "",
+               std::optional<FastMathFlags> FMFs = std::nullopt) {
     auto *Select =
         FMFs ? new VPInstruction(Instruction::Select, {Cond, TrueVal, FalseVal},
                                  *FMFs, DL, Name)
@@ -202,8 +203,8 @@ class VPBuilder {
   /// Create a new ICmp VPInstruction with predicate \p Pred and operands \p A
   /// and \p B.
   /// TODO: add createFCmp when needed.
-  VPValue *createICmp(CmpInst::Predicate Pred, VPValue *A, VPValue *B,
-                      DebugLoc DL = {}, const Twine &Name = "") {
+  VPInstruction *createICmp(CmpInst::Predicate Pred, VPValue *A, VPValue *B,
+                            DebugLoc DL = {}, const Twine &Name = "") {
     assert(Pred >= CmpInst::FIRST_ICMP_PREDICATE &&
            Pred <= CmpInst::LAST_ICMP_PREDICATE && "invalid predicate");
     return tryInsertInstruction(

@david-arm
Copy link
Contributor

One problem I see with this patch is that it forbids any kind of optimisation such as constant folding, which may want to return a result that isn't a VPInstruction.

@david-arm
Copy link
Contributor

That's why IRBuilder.h defines functions like this:

Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "")

@artagnon
Copy link
Contributor Author

Yes, I'm aware of this. However, I don't know if constant-folding makes sense at the VPlan-level: in practice, we use the existing constant-folder via IRBuilder while executing the plan. For the motivation for this change, see #93998.

@fhahn
Copy link
Contributor

fhahn commented Sep 24, 2024

Yes, I'm aware of this. However, I don't know if constant-folding makes sense at the VPlan-level: in practice, we use the existing constant-folder via IRBuilder while executing the plan. For the motivation for this change, see #93998.

Do you know if this would help to simplify any code that uses the result of the builders (e.g. removes unnecessary cast<>)? If it helps to simplify it would be great. I don't see VPBuilder including constant-folding any time soon, but if it doesn't help to simplify existing code, it might be better to stick with the more general type for now

@artagnon artagnon force-pushed the vplan-builder-vpinst branch from a044209 to eb12867 Compare September 24, 2024 14:18
@artagnon
Copy link
Contributor Author

Do you know if this would help to simplify any code that uses the result of the builders (e.g. removes unnecessary cast<>)? If it helps to simplify it would be great.

Yes, it helps strip a cast. See latest commit.

@artagnon
Copy link
Contributor Author

artagnon commented Oct 7, 2024

@fhahn Are you happy with the simplification, or should we not pursue this change?

@artagnon artagnon closed this Jan 9, 2025
@artagnon artagnon deleted the vplan-builder-vpinst branch January 9, 2025 18:32
@artagnon artagnon restored the vplan-builder-vpinst branch June 3, 2025 15:00
@artagnon
Copy link
Contributor Author

artagnon commented Jun 3, 2025

Reviving this PR, as the constant-folder is done.

@artagnon artagnon reopened this Jun 3, 2025
@artagnon artagnon changed the title VPlan/Builder: refine return types (NFC) [VPlan] Refine return types in VPBuilder (NFC) Jun 3, 2025
@artagnon artagnon force-pushed the vplan-builder-vpinst branch from eb12867 to e7547fc Compare June 3, 2025 15:37
@artagnon artagnon requested review from lukel97 and david-arm June 3, 2025 15:48
@artagnon artagnon force-pushed the vplan-builder-vpinst branch from e7547fc to 632d9fe Compare June 20, 2025 10:13
@artagnon
Copy link
Contributor Author

Gentle ping.

Copy link

github-actions bot commented Jun 20, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

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.

LGTM, with the formatting fixed. All folding is currently done as VPlan transforms which also naturally handles recipes not constructed via the builder (and opportunities exposed through other folding).

We can always adjust as needed in the future.

@artagnon artagnon force-pushed the vplan-builder-vpinst branch from 632d9fe to d270e6b Compare June 20, 2025 11:04
@artagnon artagnon merged commit b334ffd into llvm:main Jun 20, 2025
7 checks passed
@artagnon artagnon deleted the vplan-builder-vpinst branch June 20, 2025 13:01
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