[llvm] [PartiallyInlineLibCalls] Emit missed- and passed-optimization remarks when partially inlining sqrt (PR #123966)

Tibor Győri via llvm-commits llvm-commits at lists.llvm.org
Sun May 18 09:49:35 PDT 2025


https://github.com/TiborGY updated https://github.com/llvm/llvm-project/pull/123966

>From 12e8f4aa8d6642f920d24881f4889e0de028469d Mon Sep 17 00:00:00 2001
From: GYT <tiborgyri at gmail.com>
Date: Wed, 22 Jan 2025 17:52:25 +0100
Subject: [PATCH 1/4] Add passed and missed opt-remarks to
 PartiallyInlineLibCalls

---
 .../Scalar/PartiallyInlineLibCalls.cpp        | 38 +++++++++++++++++--
 .../X86/good-prototype.ll                     |  6 ++-
 .../PartiallyInlineLibCalls/X86/musttail.ll   |  4 +-
 .../PartiallyInlineLibCalls/strictfp.ll       |  4 +-
 4 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp b/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
index 2b50ccdc2eeb4..e56b561ef1f59 100644
--- a/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
+++ b/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
@@ -56,6 +56,20 @@ static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
   // dst = phi(v0, v1)
   //
 
+  ORE->emit([&]() {
+    return OptimizationRemark(DEBUG_TYPE, "SqrtPartiallyInlined",
+                              Call->getDebugLoc(), &CurrBB)
+           << "Partially inlined call to sqrt function despite having to use "
+              "errno for error handling: target has fast sqrt instruction";
+  });
+  ORE->emit([&]() {
+    return OptimizationRemarkMissed(DEBUG_TYPE, "BranchInserted",
+                                    Call->getDebugLoc(), &CurrBB)
+           << "Branch to library sqrt fn had to be inserted to satisfy the "
+              "current target's requirement for math functions to set errno on "
+              "invalid inputs";
+  });
+
   Type *Ty = Call->getType();
   IRBuilder<> Builder(Call->getNextNode());
 
@@ -125,11 +139,29 @@ static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI,
       if (!Call || !(CalledFunc = Call->getCalledFunction()))
         continue;
 
-      if (Call->isNoBuiltin() || Call->isStrictFP())
+      if (Call->isNoBuiltin())
         continue;
-
-      if (Call->isMustTailCall())
+      if (Call->isStrictFP()) {
+        ORE->emit([&]() {
+          return OptimizationRemarkMissed(DEBUG_TYPE, "StrictFloat",
+                                          Call->getDebugLoc(), &*CurrBB)
+                 << "Could not consider library function for partial inlining:"
+                    " strict FP exception behavior is active";
+        });
         continue;
+      }
+      // Partially inlining a libcall that has the musttail attribute leads to
+      // broken LLVM IR, triggering an assertion in the IR verifier.
+      // Work around that by forgoing this optimization for musttail calls.
+      if (Call->isMustTailCall()) {
+        ORE->emit([&]() {
+          return OptimizationRemarkMissed(DEBUG_TYPE, "MustTailCall",
+                                          Call->getDebugLoc(), &*CurrBB)
+                 << "Could not consider library function for partial inlining:"
+                    " must tail call";
+        });
+        continue;
+      }
 
       // Skip if function either has local linkage or is not a known library
       // function.
diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
index e6c2a7e629a5d..49a34d9d4a0a6 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
@@ -1,8 +1,12 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu -pass-remarks=partially-inline-libcalls \
+; RUN:     -pass-remarks-missed=partially-inline-libcalls 2>%t < %s | FileCheck %s
+; RUN: cat %t | FileCheck %s -check-prefix=CHECK-REMARK
 
 define float @f(float %val) {
 ; CHECK-LABEL: @f(
+; CHECK-REMARK: Partially inlined call to sqrt function despite having to use errno for error handling: target has fast sqrt instruction
+; CHECK-REMARK: Branch to library sqrt fn had to be inserted to satisfy the current target's requirement for math functions to set errno on invalid inputs
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[RES:%.*]] = tail call float @sqrtf(float [[VAL:%.*]]) #[[READNONE:.*]]
 ; CHECK-NEXT:    [[TMP0:%.*]] = fcmp oge float [[VAL]], 0.000000e+00
diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
index 65dd616b43ea6..c877990df309a 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
@@ -1,8 +1,10 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu -pass-remarks-missed=partially-inline-libcalls 2>%t < %s | FileCheck %s
+; RUN: cat %t | FileCheck %s -check-prefix=CHECK-REMARK
 
 define double @foo(double %x) {
 ; CHECK-LABEL: @foo(
+; CHECK-REMARK: Could not consider library function for partial inlining: must tail call
 ; CHECK-NEXT:    [[R:%.*]] = musttail call double @sqrt(double [[X:%.*]])
 ; CHECK-NEXT:    ret double [[R]]
 ;
diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
index 8d18d1969b04d..6a58de096b84a 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
@@ -1,7 +1,9 @@
-; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu -pass-remarks-missed=partially-inline-libcalls 2>%t < %s | FileCheck %s
+; RUN: cat %t | FileCheck %s -check-prefix=CHECK-REMARK
 
 define float @f(float %val) strictfp {
 ; CHECK-LABEL: @f
+; CHECK-REMARK: Could not consider library function for partial inlining: strict FP exception behavior is active
 ; CHECK: call{{.*}}@sqrtf
 ; CHECK-NOT: call{{.*}}@sqrtf
   %res = tail call float @sqrtf(float %val) strictfp

>From fb45df170695ebcf510bdefb99eda507e0adf11d Mon Sep 17 00:00:00 2001
From: TiborGY <tibor.gyori at chem.u-szeged.hu>
Date: Sat, 17 May 2025 19:06:15 +0200
Subject: [PATCH 2/4] Messages now start with lowercase

---
 llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp    | 8 ++++----
 .../PartiallyInlineLibCalls/X86/good-prototype.ll         | 4 ++--
 .../Transforms/PartiallyInlineLibCalls/X86/musttail.ll    | 2 +-
 llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll  | 2 +-
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp b/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
index e56b561ef1f59..02a9d62273f90 100644
--- a/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
+++ b/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
@@ -59,13 +59,13 @@ static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
   ORE->emit([&]() {
     return OptimizationRemark(DEBUG_TYPE, "SqrtPartiallyInlined",
                               Call->getDebugLoc(), &CurrBB)
-           << "Partially inlined call to sqrt function despite having to use "
+           << "partially inlined call to sqrt function despite having to use "
               "errno for error handling: target has fast sqrt instruction";
   });
   ORE->emit([&]() {
     return OptimizationRemarkMissed(DEBUG_TYPE, "BranchInserted",
                                     Call->getDebugLoc(), &CurrBB)
-           << "Branch to library sqrt fn had to be inserted to satisfy the "
+           << "branch to library sqrt fn had to be inserted to satisfy the "
               "current target's requirement for math functions to set errno on "
               "invalid inputs";
   });
@@ -145,7 +145,7 @@ static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI,
         ORE->emit([&]() {
           return OptimizationRemarkMissed(DEBUG_TYPE, "StrictFloat",
                                           Call->getDebugLoc(), &*CurrBB)
-                 << "Could not consider library function for partial inlining:"
+                 << "could not consider library function for partial inlining:"
                     " strict FP exception behavior is active";
         });
         continue;
@@ -157,7 +157,7 @@ static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI,
         ORE->emit([&]() {
           return OptimizationRemarkMissed(DEBUG_TYPE, "MustTailCall",
                                           Call->getDebugLoc(), &*CurrBB)
-                 << "Could not consider library function for partial inlining:"
+                 << "could not consider library function for partial inlining:"
                     " must tail call";
         });
         continue;
diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
index 49a34d9d4a0a6..78235f8911fbf 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
@@ -5,8 +5,8 @@
 
 define float @f(float %val) {
 ; CHECK-LABEL: @f(
-; CHECK-REMARK: Partially inlined call to sqrt function despite having to use errno for error handling: target has fast sqrt instruction
-; CHECK-REMARK: Branch to library sqrt fn had to be inserted to satisfy the current target's requirement for math functions to set errno on invalid inputs
+; CHECK-REMARK: partially inlined call to sqrt function despite having to use errno for error handling: target has fast sqrt instruction
+; CHECK-REMARK: branch to library sqrt fn had to be inserted to satisfy the current target's requirement for math functions to set errno on invalid inputs
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[RES:%.*]] = tail call float @sqrtf(float [[VAL:%.*]]) #[[READNONE:.*]]
 ; CHECK-NEXT:    [[TMP0:%.*]] = fcmp oge float [[VAL]], 0.000000e+00
diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
index c877990df309a..3f8fab3caee3e 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
@@ -4,7 +4,7 @@
 
 define double @foo(double %x) {
 ; CHECK-LABEL: @foo(
-; CHECK-REMARK: Could not consider library function for partial inlining: must tail call
+; CHECK-REMARK: could not consider library function for partial inlining: must tail call
 ; CHECK-NEXT:    [[R:%.*]] = musttail call double @sqrt(double [[X:%.*]])
 ; CHECK-NEXT:    ret double [[R]]
 ;
diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
index 6a58de096b84a..e7ed0b8311cdc 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
@@ -3,7 +3,7 @@
 
 define float @f(float %val) strictfp {
 ; CHECK-LABEL: @f
-; CHECK-REMARK: Could not consider library function for partial inlining: strict FP exception behavior is active
+; CHECK-REMARK: could not consider library function for partial inlining: strict FP exception behavior is active
 ; CHECK: call{{.*}}@sqrtf
 ; CHECK-NOT: call{{.*}}@sqrtf
   %res = tail call float @sqrtf(float %val) strictfp

>From ce81d5c9583897557e70732fada264b37a004dcd Mon Sep 17 00:00:00 2001
From: TiborGY <tibor.gyori at chem.u-szeged.hu>
Date: Sun, 18 May 2025 15:44:00 +0200
Subject: [PATCH 3/4] Move analysis remark about fast sqrt isn next to the
 condition that checks for it

---
 .../Scalar/PartiallyInlineLibCalls.cpp           | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp b/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
index 02a9d62273f90..ce75f8eff6170 100644
--- a/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
+++ b/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp
@@ -56,12 +56,6 @@ static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
   // dst = phi(v0, v1)
   //
 
-  ORE->emit([&]() {
-    return OptimizationRemark(DEBUG_TYPE, "SqrtPartiallyInlined",
-                              Call->getDebugLoc(), &CurrBB)
-           << "partially inlined call to sqrt function despite having to use "
-              "errno for error handling: target has fast sqrt instruction";
-  });
   ORE->emit([&]() {
     return OptimizationRemarkMissed(DEBUG_TYPE, "BranchInserted",
                                     Call->getDebugLoc(), &CurrBB)
@@ -175,8 +169,16 @@ static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI,
       case LibFunc_sqrt:
         if (TTI->haveFastSqrt(Call->getType()) &&
             optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI,
-                         DTU ? &*DTU : nullptr, ORE))
+                         DTU ? &*DTU : nullptr, ORE)) {
+          ORE->emit([&]() {
+            return OptimizationRemark(DEBUG_TYPE, "SqrtPartiallyInlined",
+                                      Call->getDebugLoc(), &*CurrBB)
+                   << "partially inlined call to sqrt function despite having "
+                      "to use errno for error handling: target has fast sqrt "
+                      "instruction";
+          });
           break;
+        }
         continue;
       default:
         continue;

>From 128574519e76dcfca74d9e10ad22f5b3178a0dff Mon Sep 17 00:00:00 2001
From: TiborGY <tibor.gyori at chem.u-szeged.hu>
Date: Sun, 18 May 2025 18:40:54 +0200
Subject: [PATCH 4/4] Use FileCheck's --input-file option instead of cat|

---
 .../Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll    | 2 +-
 llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll    | 2 +-
 llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll        | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
index 78235f8911fbf..d6deb84994cdd 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/good-prototype.ll
@@ -1,7 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu -pass-remarks=partially-inline-libcalls \
 ; RUN:     -pass-remarks-missed=partially-inline-libcalls 2>%t < %s | FileCheck %s
-; RUN: cat %t | FileCheck %s -check-prefix=CHECK-REMARK
+; RUN: FileCheck %s -check-prefix=CHECK-REMARK --input-file=%t
 
 define float @f(float %val) {
 ; CHECK-LABEL: @f(
diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
index 3f8fab3caee3e..0d0075c04109e 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/X86/musttail.ll
@@ -1,6 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu -pass-remarks-missed=partially-inline-libcalls 2>%t < %s | FileCheck %s
-; RUN: cat %t | FileCheck %s -check-prefix=CHECK-REMARK
+; RUN: FileCheck %s -check-prefix=CHECK-REMARK --input-file=%t
 
 define double @foo(double %x) {
 ; CHECK-LABEL: @foo(
diff --git a/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll b/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
index e7ed0b8311cdc..e300e3874b715 100644
--- a/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
+++ b/llvm/test/Transforms/PartiallyInlineLibCalls/strictfp.ll
@@ -1,5 +1,5 @@
 ; RUN: opt -S -passes=partially-inline-libcalls -mtriple=x86_64-unknown-linux-gnu -pass-remarks-missed=partially-inline-libcalls 2>%t < %s | FileCheck %s
-; RUN: cat %t | FileCheck %s -check-prefix=CHECK-REMARK
+; RUN: FileCheck %s -check-prefix=CHECK-REMARK --input-file=%t
 
 define float @f(float %val) strictfp {
 ; CHECK-LABEL: @f



More information about the llvm-commits mailing list