[llvm] [LV] Fix emission of debug message in legality check (PR #101924)

Madhur Amilkanthwar via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 3 23:09:05 PDT 2024


https://github.com/madhur13490 updated https://github.com/llvm/llvm-project/pull/101924

>From f43dc3780cd0aebdd2f304c0738fb66521090918 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Wed, 31 Jul 2024 22:17:56 +0530
Subject: [PATCH 1/5] [LV] Fix emission of debug message in legality check

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.
---
 .../Vectorize/LoopVectorizationLegality.cpp    | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 66a779da8c25bc..7545f23960ff44 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -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");
       Result = false;
-    else
+    } else {
       return false;
+    }
   }
 
   // We need to have a loop header.
@@ -1519,11 +1521,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)
@@ -1538,6 +1535,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

>From 8f36f8bc299b6244cd8135fb4c923f040a9879d2 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Thu, 8 Aug 2024 21:23:14 +0530
Subject: [PATCH 2/5] Add test

---
 .../LoopVectorize/check-no-vectorize.ll       | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll

diff --git a/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll b/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll
new file mode 100644
index 00000000000000..361200563e1a43
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll
@@ -0,0 +1,36 @@
+; 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
+; CHECK-NOT: LV: We can vectorize this loop
+; 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"
+
+ at a = dso_local global [32000 x i32] zeroinitializer, align 4
+ at b = dso_local global [32000 x i32] zeroinitializer, align 4
+
+define dso_local void @foo() local_unnamed_addr {
+entry:
+  %.pre = load i32, ptr getelementptr inbounds (i8, ptr @a, i64 4), align 4
+  %.pre17 = load i32, ptr @a, align 4
+  br label %for.body
+
+for.cond.cleanup:                                 ; preds = %for.body
+  ret void
+
+for.body:                                         ; preds = %entry, %for.body
+  %0 = phi i32 [ %.pre17, %entry ], [ %add6, %for.body ]
+  %1 = phi i32 [ %.pre, %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
+  %arrayidx2 = getelementptr inbounds [32000 x i32], ptr @a, i64 0, i64 %indvars.iv.next
+  %2 = load i32, ptr %arrayidx2, align 4
+  %add3 = add nsw i32 %2, %1
+  %add6 = add nsw i32 %add3, %0
+  store i32 %add6, ptr %arrayidx, align 4
+  %exitcond.not = icmp eq i64 %indvars.iv.next, 31999
+  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+}

>From 8b316f2e2dc2b3567db6c0ceaf15f1d45f7b3888 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Wed, 21 Aug 2024 16:06:18 +0530
Subject: [PATCH 3/5] Address review comments

---
 .../Vectorize/LoopVectorizationLegality.cpp    | 18 ++++++++++--------
 .../LoopVectorize/check-no-vectorize.ll        |  8 ++++----
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 7545f23960ff44..f1e36f5b8e7a2f 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1521,12 +1521,21 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
       return false;
   }
 
-
   unsigned SCEVThreshold = VectorizeSCEVCheckThreshold;
   if (Hints->getForce() == LoopVectorizeHints::FK_Enabled)
     SCEVThreshold = PragmaVectorizeSCEVCheckThreshold;
 
+  if (Result) {
+    LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop"
+                      << (LAI->getRuntimePointerChecking()->Need
+                              ? " (with a runtime bound check)"
+                              : "")
+                      << "!\n");
+  }
+
   if (PSE.getPredicate().getComplexity() > SCEVThreshold) {
+    LLVM_DEBUG(dbgs() << "LV: Vectorization not profitable "
+                         "due to SCEVThreshold");
     reportVectorizationFailure("Too many SCEV checks needed",
         "Too many SCEV assumptions need to be made and checked at runtime",
         "TooManySCEVRunTimeChecks", ORE, TheLoop);
@@ -1535,13 +1544,6 @@ 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
diff --git a/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll b/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll
index 361200563e1a43..83880da4a7a469 100644
--- a/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll
+++ b/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll
@@ -4,14 +4,14 @@
 ; 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
 ; 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"
-target triple = "aarch64-unknown-linux-gnu"
 
- at a = dso_local global [32000 x i32] zeroinitializer, align 4
- at b = dso_local global [32000 x i32] zeroinitializer, align 4
+ at a = global [32000 x i32] zeroinitializer, align 4
+ at b = global [32000 x i32] zeroinitializer, align 4
 
-define dso_local void @foo() local_unnamed_addr {
+define void @foo() {
 entry:
   %.pre = load i32, ptr getelementptr inbounds (i8, ptr @a, i64 4), align 4
   %.pre17 = load i32, ptr @a, align 4

>From 818fc242967ded96a3c02a56c89b3a597dc382c2 Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Sat, 24 Aug 2024 12:02:01 +0530
Subject: [PATCH 4/5] Address review comments

---
 .../Vectorize/LoopVectorizationLegality.cpp   |  2 +-
 .../LoopVectorize/check-no-vectorize.ll       | 40 +++++++++----------
 2 files changed, 19 insertions(+), 23 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index f1e36f5b8e7a2f..ff5bd2ab5dddd6 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1452,7 +1452,7 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
   // vectorizer.
   if (!canVectorizeLoopNestCFG(TheLoop, UseVPlanNativePath)) {
     if (DoExtraAnalysis) {
-      LLVM_DEBUG(dbgs() << "LV legality check failed: loop nest");
+      LLVM_DEBUG(dbgs() << "LV: legality check failed: loop nest");
       Result = false;
     } else {
       return false;
diff --git a/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll b/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll
index 83880da4a7a469..e45a2049313199 100644
--- a/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll
+++ b/llvm/test/Transforms/LoopVectorize/check-no-vectorize.ll
@@ -1,36 +1,32 @@
-; This test asserts that we don't emit both
+; This test checks 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
+; REQUIRES: asserts
+; RUN: opt -passes=loop-vectorize -debug -disable-output < %s 2>&1 | FileCheck %s
 ; CHECK-NOT: LV: We can vectorize this loop
 ; 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"
-
 @a = global [32000 x i32] zeroinitializer, align 4
- at b = global [32000 x i32] zeroinitializer, align 4
 
-define void @foo() {
+define void @foo(i32 %val1, i32 %val2) {
 entry:
-  %.pre = load i32, ptr getelementptr inbounds (i8, ptr @a, i64 4), align 4
-  %.pre17 = load i32, ptr @a, align 4
   br label %for.body
 
-for.cond.cleanup:                                 ; preds = %for.body
-  ret void
-
 for.body:                                         ; preds = %entry, %for.body
-  %0 = phi i32 [ %.pre17, %entry ], [ %add6, %for.body ]
-  %1 = phi i32 [ %.pre, %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
-  %arrayidx2 = getelementptr inbounds [32000 x i32], ptr @a, i64 0, i64 %indvars.iv.next
+  %0 = phi i32 [ %val1, %entry ], [ %add1, %for.body ]
+  %1 = phi i32 [ %val2, %entry ], [ %2, %for.body ]
+  %iv = phi i64 [ 1, %entry ], [ %iv.next, %for.body ]
+  %arrayidx = getelementptr inbounds [32000 x i32], ptr @a, i64 0, i64 %iv
+  %iv.next = add nuw nsw i64 %iv, 1
+  %arrayidx2 = getelementptr inbounds [32000 x i32], ptr @a, i64 0, i64 %iv.next
   %2 = load i32, ptr %arrayidx2, align 4
-  %add3 = add nsw i32 %2, %1
-  %add6 = add nsw i32 %add3, %0
-  store i32 %add6, ptr %arrayidx, align 4
-  %exitcond.not = icmp eq i64 %indvars.iv.next, 31999
-  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+  %add0 = add nsw i32 %2, %1
+  %add1 = add nsw i32 %add0, %0
+  store i32 %add1, ptr %arrayidx, align 4
+  %exitcond = icmp eq i64 %iv.next, 31999
+  br i1 %exitcond, label %exit, label %for.body
+
+exit:                                 ; preds = %for.body
+  ret void
 }

>From 800f54b8fd46a279ea54d2109daec9802378686e Mon Sep 17 00:00:00 2001
From: Madhur Amilkanthwar <madhura at nvidia.com>
Date: Wed, 4 Sep 2024 11:38:43 +0530
Subject: [PATCH 5/5] Don't change place of the message

---
 .../Transforms/Vectorize/LoopVectorizationLegality.cpp    | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index ff5bd2ab5dddd6..7042af6dd8eae5 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -1521,10 +1521,6 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
       return false;
   }
 
-  unsigned SCEVThreshold = VectorizeSCEVCheckThreshold;
-  if (Hints->getForce() == LoopVectorizeHints::FK_Enabled)
-    SCEVThreshold = PragmaVectorizeSCEVCheckThreshold;
-
   if (Result) {
     LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop"
                       << (LAI->getRuntimePointerChecking()->Need
@@ -1533,6 +1529,10 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
                       << "!\n");
   }
 
+  unsigned SCEVThreshold = VectorizeSCEVCheckThreshold;
+  if (Hints->getForce() == LoopVectorizeHints::FK_Enabled)
+    SCEVThreshold = PragmaVectorizeSCEVCheckThreshold;
+
   if (PSE.getPredicate().getComplexity() > SCEVThreshold) {
     LLVM_DEBUG(dbgs() << "LV: Vectorization not profitable "
                          "due to SCEVThreshold");



More information about the llvm-commits mailing list