Skip to content

Conversation

madhur13490
Copy link
Contributor

Successful vectorization message is emitted even
after "Result" is false. "Result" = false indicates failure of one of the legality check and thus
successful message should not be printed.

@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Madhur Amilkanthwar (madhur13490)

Changes

Successful vectorization message is emitted even
after "Result" is false. "Result" = false indicates failure of one of the legality check and thus
successful message should not be printed.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp (+11-7)
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 465d0df30e3f7..35c841f470261 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1441,10 +1441,12 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
   // Check whether the loop-related control flow in the loop nest is expected by
   // vectorizer.
   if (!canVectorizeLoopNestCFG(TheLoop, UseVPlanNativePath)) {
-    if (DoExtraAnalysis)
+    if (DoExtraAnalysis) {
+      LLVM_DEBUG(dbgs() << "LV legality check failed: loop nest");
       Result = false;
-    else
+    } else {
       return false;
+    }
   }
 
   // We need to have a loop header.
@@ -1509,11 +1511,6 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
       return false;
   }
 
-  LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop"
-                    << (LAI->getRuntimePointerChecking()->Need
-                            ? " (with a runtime bound check)"
-                            : "")
-                    << "!\n");
 
   unsigned SCEVThreshold = VectorizeSCEVCheckThreshold;
   if (Hints->getForce() == LoopVectorizeHints::FK_Enabled)
@@ -1528,6 +1525,13 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
     else
       return false;
   }
+  if (Result) {
+    LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop"
+                      << (LAI->getRuntimePointerChecking()->Need
+                            ? " (with a runtime bound check)"
+                            : "")
+                      << "!\n");
+  }
 
   // Okay! We've done all the tests. If any have failed, return false. Otherwise
   // we can vectorize, and at this point we don't have any other mem analysis

@madhur13490 madhur13490 requested a review from davemgreen August 5, 2024 06:20
Copy link

github-actions bot commented Aug 5, 2024

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

@madhur13490 madhur13490 force-pushed the madhur13490/correct-lv-info branch from 6782629 to 444a13a Compare August 5, 2024 06:29
@artagnon
Copy link
Contributor

artagnon commented Aug 7, 2024

Kindly add a test?

@madhur13490 madhur13490 force-pushed the madhur13490/correct-lv-info branch from 444a13a to d5faf52 Compare August 10, 2024 05:54
@madhur13490
Copy link
Contributor Author

Kindly add a test?

Done. The test makes sure that we don't emit both successful and unsuccessful message about vectorization.

@madhur13490
Copy link
Contributor Author

Gentle ping to the reviewers!

@@ -1528,6 +1525,13 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
else
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'd add an empty line between these.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refreshed code.

; successful and unsuccessful message about vectorization.

; RUN: opt -passes=loop-vectorize -debug -disable-output -pass-remarks-missed=loop-vectorize %s 2>&1 | FileCheck %s
; CHECK-NOT: LV: We can vectorize this loop
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd also add this CHECK-NOT after the CHECK as well, to make sure it doesn't happen in either order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

; CHECK: LV: Not vectorizing: Cannot prove legality

target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
target triple = "aarch64-unknown-linux-gnu"
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this test depend on the target triple? If so, it must go in the AArch64 directory. Though ideally it would be triple-independent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this does not depend on TT. I have done the change.

@a = dso_local global [32000 x i32] zeroinitializer, align 4
@b = dso_local global [32000 x i32] zeroinitializer, align 4

define dso_local void @foo() local_unnamed_addr {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary dso_local / local_unnamed_addr.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done!

@@ -1528,6 +1525,13 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
else
return false;
}
if (Result) {
LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be kept at the original place? the check above is a profitability check, we can still vectorize the loop

Might be good to have a test 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.

Done. But I think the meaning attached to "false" returned from this function is slightly misleading. If "false" is returned through the last instance then the loop is vectorizable but not profitable. For other instances, it is not vectorizable at all. I think the name of the function should be canVectorizeAndProfitable.

@madhur13490 madhur13490 force-pushed the madhur13490/correct-lv-info branch from 6777222 to 8bb9dc7 Compare August 21, 2024 11:24
ret void

for.body: ; preds = %entry, %for.body
%0 = phi i32 [ %.pre17, %entry ], [ %add6, %for.body ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to give the variables better names: %0, %1, %.pre, %.pre17 etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

; CHECK: LV: Not vectorizing: Cannot prove legality
; CHECK-NOT: LV: We can vectorize this loop

target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, removed.

; This test asserts that we don't emit both
; successful and unsuccessful message about vectorization.

; RUN: opt -passes=loop-vectorize -debug -disable-output -pass-remarks-missed=loop-vectorize %s 2>&1 | 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.

-debug-only=loop-vectorize? Are we checking pass remarks as well?

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, thanks for catching!

@@ -0,0 +1,36 @@
; This test asserts that we don't emit both
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I'd probably name this test dbg-print-no-vectorize.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the name is fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

Reflow comment

Suggested change
; This test asserts that we don't emit both
; This test checks that we don't emit both successful and unsuccessful

@@ -1441,10 +1441,12 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
// Check whether the loop-related control flow in the loop nest is expected by
// vectorizer.
if (!canVectorizeLoopNestCFG(TheLoop, UseVPlanNativePath)) {
if (DoExtraAnalysis)
if (DoExtraAnalysis) {
LLVM_DEBUG(dbgs() << "LV legality check failed: loop nest");
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps possible to check this message as well? Shouldn't the debug message start with an "LV: " prefix for uniformity?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

; This test asserts that we don't emit both
; successful and unsuccessful message about vectorization.

; RUN: opt -passes=loop-vectorize -debug -disable-output -pass-remarks-missed=loop-vectorize %s 2>&1 | 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.

Needs REQUIRES: asserts, since we're checking debug printing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@madhur13490 madhur13490 force-pushed the madhur13490/correct-lv-info branch from 8bb9dc7 to 01a81a0 Compare August 27, 2024 11:51
@madhur13490 madhur13490 requested review from nikic and fhahn August 27, 2024 11:55
for.body: ; preds = %entry, %for.body
%0 = phi i32 [ %val_a, %entry ], [ %add6, %for.body ]
%1 = phi i32 [ %load_a_gep, %entry ], [ %2, %for.body ]
%indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
%indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ]
%iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

%1 = phi i32 [ %load_a_gep, %entry ], [ %2, %for.body ]
%indvars.iv = phi i64 [ 1, %entry ], [ %indvars.iv.next, %for.body ]
%arrayidx = getelementptr inbounds [32000 x i32], ptr @a, i64 0, i64 %indvars.iv
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
%iv.next = add nuw nsw i64 %indvars.iv, 1


define void @foo() {
entry:
%load_a_gep = load i32, ptr getelementptr inbounds (i8, ptr @a, i64 4), align 4
Copy link
Contributor

Choose a reason for hiding this comment

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

pass as arg to simplify test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

define void @foo() {
entry:
%load_a_gep = load i32, ptr getelementptr inbounds (i8, ptr @a, i64 4), align 4
%val_a = load i32, ptr @a, align 4
Copy link
Contributor

Choose a reason for hiding this comment

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

pass as arg to simplify test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, sure. Done

; CHECK-NOT: LV: We can vectorize this loop

@a = global [32000 x i32] zeroinitializer, align 4
@b = global [32000 x i32] zeroinitializer, align 4
Copy link
Contributor

Choose a reason for hiding this comment

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

unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! yes, removed.

%val_a = load i32, ptr @a, align 4
br label %for.body

for.cond.cleanup: ; preds = %for.body
Copy link
Contributor

Choose a reason for hiding this comment

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

might be good to move this to the end of the function and shorten name to something like exit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -0,0 +1,36 @@
; This test asserts that we don't emit both
Copy link
Contributor

Choose a reason for hiding this comment

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

Reflow comment

Suggested change
; This test asserts that we don't emit both
; This test checks that we don't emit both successful and unsuccessful

@@ -1451,10 +1451,12 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
// Check whether the loop-related control flow in the loop nest is expected by
// vectorizer.
if (!canVectorizeLoopNestCFG(TheLoop, UseVPlanNativePath)) {
if (DoExtraAnalysis)
if (DoExtraAnalysis) {
LLVM_DEBUG(dbgs() << "LV: legality check failed: loop nest");
Copy link
Contributor

Choose a reason for hiding this comment

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

unrelated to the original change?

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, but doing it as good to have a debug message.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, generally it's preferable to keep things separate if possible. Fine to keep it here in the PR for now though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, thanks!

@madhur13490 madhur13490 force-pushed the madhur13490/correct-lv-info branch from 01a81a0 to b99b73e Compare August 27, 2024 16:51
@madhur13490
Copy link
Contributor Author

@fhahn Does the new revision look ok now?

@@ -1451,10 +1451,12 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
// Check whether the loop-related control flow in the loop nest is expected by
// vectorizer.
if (!canVectorizeLoopNestCFG(TheLoop, UseVPlanNativePath)) {
if (DoExtraAnalysis)
if (DoExtraAnalysis) {
LLVM_DEBUG(dbgs() << "LV: legality check failed: loop nest");
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, generally it's preferable to keep things separate if possible. Fine to keep it here in the PR for now though

unsigned SCEVThreshold = VectorizeSCEVCheckThreshold;
if (Hints->getForce() == LoopVectorizeHints::FK_Enabled)
SCEVThreshold = PragmaVectorizeSCEVCheckThreshold;

if (Result) {
Copy link
Contributor

Choose a reason for hiding this comment

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

also keep where the message is emitted at the original place, so SCEVThreshold's definition is closer to use?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes, there is no reason to change. Thanks! Hopefully, looks good now :)

@madhur13490 madhur13490 force-pushed the madhur13490/correct-lv-info branch from b99b73e to 800f54b Compare September 4, 2024 06:08
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!

Successful vectorization message is emitted even
after "Result" is false.  "Result" = false indicates
failure of one of the legality check and thus
successful message should not be printed.
@madhur13490 madhur13490 force-pushed the madhur13490/correct-lv-info branch from 800f54b to 2881500 Compare September 4, 2024 10:58
@madhur13490 madhur13490 merged commit cd46829 into llvm:main Sep 4, 2024
5 of 7 checks passed
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.

5 participants