[llvm] [Clang] Attribute NoFPClass should not prevent tail call optimization. (PR #116741)

FĂ©lix-Antoine Constantin via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 22 16:05:10 PST 2024


https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/116741

>From 1599ddd0864c2c8f3b89ec91a4bb3547530e7875 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 <felix-antoine.constantin at bidgroup.ca>
Date: Mon, 18 Nov 2024 23:29:56 -0500
Subject: [PATCH 1/2] [Clang] Attribute NoFPClass should not prevent tail call
 optimization.

Fixes #111950
---
 llvm/lib/CodeGen/Analysis.cpp                    |  8 ++++----
 llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp |  8 ++++----
 llvm/test/CodeGen/X86/tailcall-nofpclass.ll      | 10 ++++++++++
 3 files changed, 18 insertions(+), 8 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tailcall-nofpclass.ll

diff --git a/llvm/lib/CodeGen/Analysis.cpp b/llvm/lib/CodeGen/Analysis.cpp
index f77b733c6c8f69..e7b9417de8c9f7 100644
--- a/llvm/lib/CodeGen/Analysis.cpp
+++ b/llvm/lib/CodeGen/Analysis.cpp
@@ -596,10 +596,10 @@ bool llvm::attributesPermitTailCall(const Function *F, const Instruction *I,
 
   // Following attributes are completely benign as far as calling convention
   // goes, they shouldn't affect whether the call is a tail call.
-  for (const auto &Attr :
-       {Attribute::Alignment, Attribute::Dereferenceable,
-        Attribute::DereferenceableOrNull, Attribute::NoAlias,
-        Attribute::NonNull, Attribute::NoUndef, Attribute::Range}) {
+  for (const auto &Attr : {Attribute::Alignment, Attribute::Dereferenceable,
+                           Attribute::DereferenceableOrNull, Attribute::NoAlias,
+                           Attribute::NonNull, Attribute::NoUndef,
+                           Attribute::Range, Attribute::NoFPClass}) {
     CallerAttrs.removeAttribute(Attr);
     CalleeAttrs.removeAttribute(Attr);
   }
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 8fbab337cab6f0..bd4bcadb57d7a9 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -61,10 +61,10 @@ bool TargetLowering::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
   // the return. Ignore following attributes because they don't affect the
   // call sequence.
   AttrBuilder CallerAttrs(F.getContext(), F.getAttributes().getRetAttrs());
-  for (const auto &Attr :
-       {Attribute::Alignment, Attribute::Dereferenceable,
-        Attribute::DereferenceableOrNull, Attribute::NoAlias,
-        Attribute::NonNull, Attribute::NoUndef, Attribute::Range})
+  for (const auto &Attr : {Attribute::Alignment, Attribute::Dereferenceable,
+                           Attribute::DereferenceableOrNull, Attribute::NoAlias,
+                           Attribute::NonNull, Attribute::NoUndef,
+                           Attribute::Range, Attribute::NoFPClass})
     CallerAttrs.removeAttribute(Attr);
 
   if (CallerAttrs.hasAttributes())
diff --git a/llvm/test/CodeGen/X86/tailcall-nofpclass.ll b/llvm/test/CodeGen/X86/tailcall-nofpclass.ll
new file mode 100644
index 00000000000000..f469fbbb366228
--- /dev/null
+++ b/llvm/test/CodeGen/X86/tailcall-nofpclass.ll
@@ -0,0 +1,10 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -mtriple=x86_64-linux < %s | FileCheck %s
+
+define dso_local noundef nofpclass(nan inf) float @_Z3foof(float noundef nofpclass(nan inf) %0) {
+; CHECK-LABEL: _Z3foof:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    jmp expf at PLT # TAILCALL
+  %2 = tail call fast float @llvm.exp.f32(float %0)
+  ret float %2
+}

>From 02089e2602e637f9a5ff621a8ae13e7e13893d20 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?=
 <felix-antoine.constantin at bidgroup.ca>
Date: Fri, 22 Nov 2024 19:04:59 -0500
Subject: [PATCH 2/2] fixup! [Clang] Attribute NoFPClass should not prevent
 tail call optimization.

Improved test
---
 llvm/test/CodeGen/X86/tailcall-nofpclass.ll | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/llvm/test/CodeGen/X86/tailcall-nofpclass.ll b/llvm/test/CodeGen/X86/tailcall-nofpclass.ll
index f469fbbb366228..fd085bb1244fb7 100644
--- a/llvm/test/CodeGen/X86/tailcall-nofpclass.ll
+++ b/llvm/test/CodeGen/X86/tailcall-nofpclass.ll
@@ -1,10 +1,13 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
 ; RUN: llc -mtriple=x86_64-linux < %s | FileCheck %s
 
-define dso_local noundef nofpclass(nan inf) float @_Z3foof(float noundef nofpclass(nan inf) %0) {
+; Ensures that taillcall optimization can still be
+; performed when nofpclass is used.
+
+define noundef nofpclass(nan inf) float @_Z3foof(float noundef nofpclass(nan inf) %0) {
 ; CHECK-LABEL: _Z3foof:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    jmp expf at PLT # TAILCALL
-  %2 = tail call fast float @llvm.exp.f32(float %0)
+  %2 = tail call float @llvm.exp.f32(float %0)
   ret float %2
 }



More information about the llvm-commits mailing list