Skip to content

[LV] Prevent query the computeCost() when VF=1 in emitInvalidCostRemarks(). #117288

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

Conversation

ElvisWang123
Copy link
Contributor

We should only query the computeCost() when the VF is vector.

@llvmbot
Copy link
Member

llvmbot commented Nov 22, 2024

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: Elvis Wang (ElvisWang123)

Changes

We should only query the computeCost() when the VF is vector.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+3)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index d13770a35c108f..5a61a6f83e3764 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -4373,6 +4373,9 @@ void LoopVectorizationPlanner::emitInvalidCostRemarks(
   SmallVector<RecipeVFPair> InvalidCosts;
   for (const auto &Plan : VPlans) {
     for (ElementCount VF : Plan->vectorFactors()) {
+      if (VF.isScalar())
+        continue;
+
       VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(),
                             CM);
       precomputeCosts(*Plan, VF, CostCtx);

@llvmbot
Copy link
Member

llvmbot commented Nov 22, 2024

@llvm/pr-subscribers-vectorizers

Author: Elvis Wang (ElvisWang123)

Changes

We should only query the computeCost() when the VF is vector.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+3)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index d13770a35c108f..5a61a6f83e3764 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -4373,6 +4373,9 @@ void LoopVectorizationPlanner::emitInvalidCostRemarks(
   SmallVector<RecipeVFPair> InvalidCosts;
   for (const auto &Plan : VPlans) {
     for (ElementCount VF : Plan->vectorFactors()) {
+      if (VF.isScalar())
+        continue;
+
       VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(),
                             CM);
       precomputeCosts(*Plan, VF, CostCtx);

@ElvisWang123 ElvisWang123 force-pushed the fix-missing-VF-check-in-emitRemark-LV branch from 7619544 to 6f1c1c7 Compare November 22, 2024 14:13
@ElvisWang123 ElvisWang123 force-pushed the fix-missing-VF-check-in-emitRemark-LV branch from 6f1c1c7 to 2e9579d Compare December 4, 2024 00:09
@ElvisWang123
Copy link
Contributor Author

Soft ping!

Copy link
Contributor

@david-arm david-arm left a comment

Choose a reason for hiding this comment

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

LGTM! Seems sensible.

@@ -4372,6 +4372,9 @@ void LoopVectorizationPlanner::emitInvalidCostRemarks(
SmallVector<RecipeVFPair> InvalidCosts;
for (const auto &Plan : VPlans) {
for (ElementCount VF : Plan->vectorFactors()) {
if (VF.isScalar())
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment here why we skip scalar VFs? Presumably because we don’t expect them to have invalid costs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated comments.

Currently, we cannot query the vplan-based cost model with scalar VF because we use lots of cast<VectorType>(toVectorTy(SrcTy, VF)) in computeCost().

@ElvisWang123 ElvisWang123 force-pushed the fix-missing-VF-check-in-emitRemark-LV branch from f30cdb0 to 9c8caeb Compare December 18, 2024 07:33
@ElvisWang123
Copy link
Contributor Author

Found a test case extracted from TSVC s311.
https://godbolt.org/z/acj6M6nfE

@@ -0,0 +1,65 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -mtriple=riscv64 -mattr=+v -p loop-vectorize -pass-remarks-analysis=loop-vectorize -S | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't feel quite right, since you're passing -pass-remarks-analysis=loop-vectorize but not testing the output. Given this test is about remarks, I think we should check the output and also ensure we don't see the VF=1 case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Passing -pass-remarks-analysis=loop-vectorize just for triggering emitInvalidCostRemarks() and make sure the compiler don't crash. I don't have any ideas about testing the remark output.

Something I can imagine is that using --debug-only=loop-vecotorize to print the cost. But it always using the legacy cost model to calculate/print the scalar cost.

Do you have better suggestion to test this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

You could also check the remark output, assuming there is any? Or of not, check that there is no remark output

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update test, thanks!

@ElvisWang123 ElvisWang123 force-pushed the fix-missing-VF-check-in-emitRemark-LV branch from 6385ea0 to bb4f8f5 Compare February 4, 2025 07:41
@@ -0,0 +1,65 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt < %s -mtriple=riscv64 -mattr=+v -p loop-vectorize -pass-remarks-analysis=loop-vectorize -S | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

You could also check the remark output, assuming there is any? Or of not, check that there is no remark output

loop:
%iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
%red = phi float [ 0.000000e+00, %entry ], [ %red.next, %loop ]
%red.next = fadd float 0.000000e+00, %red
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a no-op reduction, can this also reproduce with a 'real' reductions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, update test case to real reduction.
https://godbolt.org/z/dj3hrK8fT

@ElvisWang123 ElvisWang123 force-pushed the fix-missing-VF-check-in-emitRemark-LV branch from bb4f8f5 to 5ad2ad5 Compare February 5, 2025 02:54
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, thanks

@ElvisWang123 ElvisWang123 merged commit 2e3729b into llvm:main Feb 10, 2025
8 checks passed
@ElvisWang123 ElvisWang123 deleted the fix-missing-VF-check-in-emitRemark-LV branch February 10, 2025 00:40
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…rks(). (llvm#117288)

We should only query the computeCost() when the VF is vector.
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