[llvm] Add option to print entire function instead of just the loops for loo… (PR #123229)

Akshay Deodhar via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 16 14:57:34 PST 2025


https://github.com/akshayrdeodhar updated https://github.com/llvm/llvm-project/pull/123229

>From 35361b7d6d3057cf3c9574f0b78559203b9fad65 Mon Sep 17 00:00:00 2001
From: Akshay Deodhar <adeodhar at nvidia.com>
Date: Wed, 18 Dec 2024 06:47:21 +0000
Subject: [PATCH 1/4] Add option to print entire function instead of just the
 loops for loop passes

---
 llvm/include/llvm/IR/PrintPasses.h |  3 +++
 llvm/lib/Analysis/LoopInfo.cpp     | 12 ++++++++++++
 llvm/lib/IR/PrintPasses.cpp        |  8 ++++++++
 3 files changed, 23 insertions(+)

diff --git a/llvm/include/llvm/IR/PrintPasses.h b/llvm/include/llvm/IR/PrintPasses.h
index 95b97e76c867cb..0aa1b379c35cf2 100644
--- a/llvm/include/llvm/IR/PrintPasses.h
+++ b/llvm/include/llvm/IR/PrintPasses.h
@@ -51,6 +51,9 @@ std::vector<std::string> printAfterPasses();
 // Returns true if we should always print the entire module.
 bool forcePrintModuleIR();
 
+// Returns true if we should print the entire function for loop passes.
+bool forcePrintFuncIR();
+
 // Return true if -filter-passes is empty or contains the pass name.
 bool isPassInPrintList(StringRef PassName);
 bool isFilterPassesEmpty();
diff --git a/llvm/lib/Analysis/LoopInfo.cpp b/llvm/lib/Analysis/LoopInfo.cpp
index 6bb5f001e9bd1d..7bd5e1e0cfac8f 100644
--- a/llvm/lib/Analysis/LoopInfo.cpp
+++ b/llvm/lib/Analysis/LoopInfo.cpp
@@ -999,6 +999,18 @@ void llvm::printLoop(Loop &L, raw_ostream &OS, const std::string &Banner) {
     return;
   }
 
+  if (forcePrintFuncIR()) {
+    // handling -print-loop-func-scope.
+    // -print-module-scope overrides this.
+    OS << Banner << " (loop: ";
+    L.getHeader()->printAsOperand(OS, false);
+    OS << ")\n";
+
+    // printing whole function.
+    OS << *L.getHeader()->getParent();
+    return;
+  }
+
   OS << Banner;
 
   auto *PreHeader = L.getLoopPreheader();
diff --git a/llvm/lib/IR/PrintPasses.cpp b/llvm/lib/IR/PrintPasses.cpp
index e2ef20bb81ba7d..466d16ccf32fca 100644
--- a/llvm/lib/IR/PrintPasses.cpp
+++ b/llvm/lib/IR/PrintPasses.cpp
@@ -88,6 +88,12 @@ static cl::opt<bool>
                               "always print a module IR"),
                      cl::init(false), cl::Hidden);
 
+static cl::opt<bool>
+    LoopPrintFuncScope("print-loop-func-scope",
+                       cl::desc("When printing IR for print-[before|after]{-all} "
+                                "for a loop pass, always print function IR"),
+                       cl::init(false), cl::Hidden);
+
 // See the description for -print-changed for an explanation of the use
 // of this option.
 static cl::list<std::string> FilterPasses(
@@ -141,6 +147,8 @@ std::vector<std::string> llvm::printAfterPasses() {
 
 bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
 
+bool llvm::forcePrintFuncIR() { return LoopPrintFuncScope; }
+
 bool llvm::isPassInPrintList(StringRef PassName) {
   static std::unordered_set<std::string> Set(FilterPasses.begin(),
                                              FilterPasses.end());

>From 334ca62a7c79b431e750e8ce38f3fe979171879d Mon Sep 17 00:00:00 2001
From: Akshay Deodhar <adeodhar at nvidia.com>
Date: Thu, 16 Jan 2025 19:47:24 +0000
Subject: [PATCH 2/4] clang-format

---
 llvm/lib/IR/PrintPasses.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/IR/PrintPasses.cpp b/llvm/lib/IR/PrintPasses.cpp
index 466d16ccf32fca..610411a3cf978a 100644
--- a/llvm/lib/IR/PrintPasses.cpp
+++ b/llvm/lib/IR/PrintPasses.cpp
@@ -88,11 +88,11 @@ static cl::opt<bool>
                               "always print a module IR"),
                      cl::init(false), cl::Hidden);
 
-static cl::opt<bool>
-    LoopPrintFuncScope("print-loop-func-scope",
-                       cl::desc("When printing IR for print-[before|after]{-all} "
-                                "for a loop pass, always print function IR"),
-                       cl::init(false), cl::Hidden);
+static cl::opt<bool> LoopPrintFuncScope(
+    "print-loop-func-scope",
+    cl::desc("When printing IR for print-[before|after]{-all} "
+             "for a loop pass, always print function IR"),
+    cl::init(false), cl::Hidden);
 
 // See the description for -print-changed for an explanation of the use
 // of this option.

>From 9a615a559431975bf0ee704b9e667072c7c7e950 Mon Sep 17 00:00:00 2001
From: Akshay Deodhar <adeodhar at nvidia.com>
Date: Thu, 16 Jan 2025 22:27:19 +0000
Subject: [PATCH 3/4] add test

---
 llvm/test/Other/print-loop-func-scope.ll | 82 ++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
 create mode 100644 llvm/test/Other/print-loop-func-scope.ll

diff --git a/llvm/test/Other/print-loop-func-scope.ll b/llvm/test/Other/print-loop-func-scope.ll
new file mode 100644
index 00000000000000..1a9b62ee489935
--- /dev/null
+++ b/llvm/test/Other/print-loop-func-scope.ll
@@ -0,0 +1,82 @@
+; This test documents how the IR dumped for loop passes differs with -print-loop-func-scope
+; and -print-module-scope
+;   - Without -print-loop-func-scope, dumps only the loop, with 3 sections- preheader,
+;     loop, and exit blocks
+;   - With -print-loop-func-scope, dumps only the function which contains the loop
+;   - With -print-module-scope, dumps the entire module containing the loop, and disregards
+;     the -print-loop-func-scope flag.
+
+; RUN: opt < %s 2>&1 -disable-output \
+; RUN: 	   -passes=licm -print-after=licm\
+; RUN:	   | FileCheck %s -check-prefix=VANILLA
+; RUN: opt < %s 2>&1 -disable-output \
+; RUN: 	   -passes=licm -print-after=licm -print-loop-func-scope \
+; RUN:	   | FileCheck %s -check-prefix=LOOPFUNC
+; RUN: opt < %s 2>&1 -disable-output \
+; RUN: 	   -passes=licm -print-after=licm -print-module-scope \
+; RUN:	   | FileCheck %s -check-prefix=MODULE
+; RUN: opt < %s 2>&1 -disable-output \
+; RUN: 	   -passes=licm -print-after=licm -print-module-scope -print-loop-func-scope\
+; RUN:	   | FileCheck %s -check-prefix=MODULEWITHLOOP
+
+; VANILLA: IR Dump After LICMPass
+; VANILLA-NOT: define void @foo
+; VANILLA: Preheader:
+; VANILLA: Loop:
+; VANILLA: Exit blocks
+
+; LOOPFUNC: IR Dump After LICMPass
+; LOOPFUNC: (loop:
+; LOOPFUNC: define void @foo
+; LOOPFUNC-NOT: Preheader:
+; LOOPFUNC-NOT: Loop:
+; LOOPFUNC-NOT: Exit blocks
+
+; MODULE: IR Dump After LICMPass
+; MODULE: ModuleID =
+; MODULE: define void @foo
+; MODULE-NOT: Preheader:
+; MODULE-NOT: Loop:
+; MODULE-NOT: Exit blocks
+; MODULE: define void @bar
+
+; MODULEWITHLOOP: IR Dump After LICMPass
+; MODULEWITHLOOP: ModuleID =
+; MODULEWITHLOOP: define void @foo
+; MODULEWITHLOOP-NOT: Preheader:
+; MODULEWITHLOOP-NOT: Loop:
+; MODULEWITHLOOP-NOT: Exit blocks
+; MODULEWITHLOOP: define void @bar
+
+
+
+define void @foo(i32 %0, i32 %1, i32* %2) {
+  %4 = icmp sgt i32 %0, 0
+  br i1 %4, label %5, label %8
+
+5:                                                ; preds = %3
+  br label %12
+
+6:                                                ; preds = %12
+  %7 = phi i32 [ %15, %12 ]
+  br label %8
+
+8:                                                ; preds = %6, %3
+  %9 = phi i32 [ 1, %3 ], [ %7, %6 ]
+  %10 = add nsw i32 %1, %0
+  %11 = add nsw i32 %10, %9
+  store i32 %11, i32* %2, align 4
+  ret void
+
+12:                                               ; preds = %5, %12
+  %13 = phi i32 [ %16, %12 ], [ 0, %5 ]
+  %14 = phi i32 [ %15, %12 ], [ 1, %5 ]
+  %15 = mul nsw i32 %14, %1
+  %16 = add nuw nsw i32 %13, 1
+  %17 = icmp eq i32 %16, %0
+  br i1 %17, label %6, label %12
+}
+
+define void @bar() {
+  ret void
+}

>From 535833793ca227b8336579d55efcc0ff88914857 Mon Sep 17 00:00:00 2001
From: Akshay Deodhar <adeodhar at nvidia.com>
Date: Thu, 16 Jan 2025 22:57:12 +0000
Subject: [PATCH 4/4] reduced test

---
 llvm/test/Other/print-loop-func-scope.ll | 39 ++++++++++--------------
 1 file changed, 16 insertions(+), 23 deletions(-)

diff --git a/llvm/test/Other/print-loop-func-scope.ll b/llvm/test/Other/print-loop-func-scope.ll
index 1a9b62ee489935..fc04a822f90b05 100644
--- a/llvm/test/Other/print-loop-func-scope.ll
+++ b/llvm/test/Other/print-loop-func-scope.ll
@@ -39,6 +39,7 @@
 ; MODULE-NOT: Loop:
 ; MODULE-NOT: Exit blocks
 ; MODULE: define void @bar
+; MODULE: declare void @baz(i32)
 
 ; MODULEWITHLOOP: IR Dump After LICMPass
 ; MODULEWITHLOOP: ModuleID =
@@ -47,36 +48,28 @@
 ; MODULEWITHLOOP-NOT: Loop:
 ; MODULEWITHLOOP-NOT: Exit blocks
 ; MODULEWITHLOOP: define void @bar
+; MODULEWITHLOOP: declare void @baz(i32)
 
+define void @foo (i32 %n) {
+entry:
+  br label %loop_cond
 
+loop_cond:
+  %i = phi i32 [ 0, %entry ], [ %i_next, %loop_body ]
+  %cmp = icmp slt i32 %i, %n
+  br i1 %cmp, label %loop_body, label %loop_end
 
-define void @foo(i32 %0, i32 %1, i32* %2) {
-  %4 = icmp sgt i32 %0, 0
-  br i1 %4, label %5, label %8
+loop_body:
+  call void @baz(i32 %i)
+  %i_next = add i32 %i, 1
+  br label %loop_cond
 
-5:                                                ; preds = %3
-  br label %12
-
-6:                                                ; preds = %12
-  %7 = phi i32 [ %15, %12 ]
-  br label %8
-
-8:                                                ; preds = %6, %3
-  %9 = phi i32 [ 1, %3 ], [ %7, %6 ]
-  %10 = add nsw i32 %1, %0
-  %11 = add nsw i32 %10, %9
-  store i32 %11, i32* %2, align 4
+loop_end:
   ret void
-
-12:                                               ; preds = %5, %12
-  %13 = phi i32 [ %16, %12 ], [ 0, %5 ]
-  %14 = phi i32 [ %15, %12 ], [ 1, %5 ]
-  %15 = mul nsw i32 %14, %1
-  %16 = add nuw nsw i32 %13, 1
-  %17 = icmp eq i32 %16, %0
-  br i1 %17, label %6, label %12
 }
 
 define void @bar() {
   ret void
 }
+
+declare void @baz(i32)



More information about the llvm-commits mailing list