[llvm] [X86][GlobalIsel] support G_FABS for f80 (PR #136718)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 23 23:28:47 PDT 2025


https://github.com/mahesh-attarde updated https://github.com/llvm/llvm-project/pull/136718

>From 92eef97ef5d4afdeb2d63982ca4e4db2ab22aa8c Mon Sep 17 00:00:00 2001
From: mattarde <mattarde at intel.com>
Date: Tue, 22 Apr 2025 08:27:06 -0700
Subject: [PATCH 1/2] [X86][GlobalIsel] support fabs for f80

---
 .../lib/Target/X86/GISel/X86LegalizerInfo.cpp |  5 +++++
 .../CodeGen/X86/GlobalISel/fabs-scalar.ll     | 20 +++++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/GlobalISel/fabs-scalar.ll

diff --git a/llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp b/llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp
index f008cb1bea839..d8c1fb5717df8 100644
--- a/llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp
+++ b/llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp
@@ -466,6 +466,11 @@ X86LegalizerInfo::X86LegalizerInfo(const X86Subtarget &STI,
                (UseX87 && typeInSet(0, {s80})(Query));
       });
 
+  //TODO: f32 and f64 FABS require xmm support
+  getActionDefinitionsBuilder(G_FABS)
+      .legalFor(UseX87, {s8, s80})
+      .lower();
+
   // fp comparison
   getActionDefinitionsBuilder(G_FCMP)
       .legalFor(HasSSE1 || UseX87, {s8, s32})
diff --git a/llvm/test/CodeGen/X86/GlobalISel/fabs-scalar.ll b/llvm/test/CodeGen/X86/GlobalISel/fabs-scalar.ll
new file mode 100644
index 0000000000000..39ce3a492a33c
--- /dev/null
+++ b/llvm/test/CodeGen/X86/GlobalISel/fabs-scalar.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=x86_64-linux-gnu -global-isel -verify-machineinstrs < %s -o - | FileCheck %s --check-prefix=X64
+; RUN: llc < %s -mtriple=x86_64-linux -mattr=+x87,-sse,-sse2 | FileCheck %s -check-prefixes=X64
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -global-isel -mattr=+x87,-sse,-sse2  | FileCheck %s -check-prefixes=GISEL
+
+define x86_fp80 @test_x86_fp80_abs(x86_fp80 %arg) {
+; X64-LABEL: test_x86_fp80_abs:
+; X64:       # %bb.0:
+; X64-NEXT:    fldt {{[0-9]+}}(%rsp)
+; X64-NEXT:    fabs
+; X64-NEXT:    retq
+;
+; GISEL-LABEL: test_x86_fp80_abs:
+; GISEL:       # %bb.0:
+; GISEL-NEXT:    fldt {{[0-9]+}}(%rsp)
+; GISEL-NEXT:    fabs
+; GISEL-NEXT:    retq
+  %abs = tail call x86_fp80 @llvm.fabs.f80(x86_fp80 %arg)
+  ret x86_fp80 %abs
+}

>From 75b3115035a20548ea6837cea9f39192fed2a8ce Mon Sep 17 00:00:00 2001
From: mattarde <mattarde at intel.com>
Date: Wed, 23 Apr 2025 22:40:23 -0700
Subject: [PATCH 2/2] add float and double

---
 .../lib/Target/X86/GISel/X86LegalizerInfo.cpp |  5 +-
 .../CodeGen/X86/GlobalISel/fabs-scalar.ll     | 20 -------
 llvm/test/CodeGen/X86/isel-fabs.ll            | 52 +++++++++++++++++++
 3 files changed, 53 insertions(+), 24 deletions(-)
 delete mode 100644 llvm/test/CodeGen/X86/GlobalISel/fabs-scalar.ll
 create mode 100644 llvm/test/CodeGen/X86/isel-fabs.ll

diff --git a/llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp b/llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp
index d8c1fb5717df8..106b62d0e6169 100644
--- a/llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp
+++ b/llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp
@@ -466,10 +466,7 @@ X86LegalizerInfo::X86LegalizerInfo(const X86Subtarget &STI,
                (UseX87 && typeInSet(0, {s80})(Query));
       });
 
-  //TODO: f32 and f64 FABS require xmm support
-  getActionDefinitionsBuilder(G_FABS)
-      .legalFor(UseX87, {s8, s80})
-      .lower();
+  getActionDefinitionsBuilder(G_FABS).legalFor(UseX87, {s8, s80}).lower();
 
   // fp comparison
   getActionDefinitionsBuilder(G_FCMP)
diff --git a/llvm/test/CodeGen/X86/GlobalISel/fabs-scalar.ll b/llvm/test/CodeGen/X86/GlobalISel/fabs-scalar.ll
deleted file mode 100644
index 39ce3a492a33c..0000000000000
--- a/llvm/test/CodeGen/X86/GlobalISel/fabs-scalar.ll
+++ /dev/null
@@ -1,20 +0,0 @@
-; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc -mtriple=x86_64-linux-gnu -global-isel -verify-machineinstrs < %s -o - | FileCheck %s --check-prefix=X64
-; RUN: llc < %s -mtriple=x86_64-linux -mattr=+x87,-sse,-sse2 | FileCheck %s -check-prefixes=X64
-; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -global-isel -mattr=+x87,-sse,-sse2  | FileCheck %s -check-prefixes=GISEL
-
-define x86_fp80 @test_x86_fp80_abs(x86_fp80 %arg) {
-; X64-LABEL: test_x86_fp80_abs:
-; X64:       # %bb.0:
-; X64-NEXT:    fldt {{[0-9]+}}(%rsp)
-; X64-NEXT:    fabs
-; X64-NEXT:    retq
-;
-; GISEL-LABEL: test_x86_fp80_abs:
-; GISEL:       # %bb.0:
-; GISEL-NEXT:    fldt {{[0-9]+}}(%rsp)
-; GISEL-NEXT:    fabs
-; GISEL-NEXT:    retq
-  %abs = tail call x86_fp80 @llvm.fabs.f80(x86_fp80 %arg)
-  ret x86_fp80 %abs
-}
diff --git a/llvm/test/CodeGen/X86/isel-fabs.ll b/llvm/test/CodeGen/X86/isel-fabs.ll
new file mode 100644
index 0000000000000..59131ec3f1085
--- /dev/null
+++ b/llvm/test/CodeGen/X86/isel-fabs.ll
@@ -0,0 +1,52 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s  -mtriple=x86_64-unknown-linux-gnu -global-isel=0 -mattr=+x87,+sse,+sse2 -o - | FileCheck %s --check-prefix=X64
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -global-isel=1 -mattr=+x87,+sse,+sse2 -o - | FileCheck %s -check-prefixes=GISEL
+
+define float @test_float_abs(float %arg) {
+; X64-LABEL: test_float_abs:
+; X64:       # %bb.0:
+; X64-NEXT:    andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; X64-NEXT:    retq
+;
+; GISEL-LABEL: test_float_abs:
+; GISEL:       # %bb.0:
+; GISEL-NEXT:    movd %xmm0, %eax
+; GISEL-NEXT:    andl $2147483647, %eax # imm = 0x7FFFFFFF
+; GISEL-NEXT:    movd %eax, %xmm0
+; GISEL-NEXT:    retq
+    %abs = tail call float @llvm.fabs.f32(float %arg)
+    ret float %abs
+}
+
+define double @test_double_abs(double %arg) {
+; X64-LABEL: test_double_abs:
+; X64:       # %bb.0:
+; X64-NEXT:    andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; X64-NEXT:    retq
+;
+; GISEL-LABEL: test_double_abs:
+; GISEL:       # %bb.0:
+; GISEL-NEXT:    movabsq $9223372036854775807, %rax # imm = 0x7FFFFFFFFFFFFFFF
+; GISEL-NEXT:    movq %xmm0, %rcx
+; GISEL-NEXT:    andq %rax, %rcx
+; GISEL-NEXT:    movq %rcx, %xmm0
+; GISEL-NEXT:    retq
+    %abs = tail call double @llvm.fabs.f64(double %arg)
+    ret double %abs
+}
+
+define x86_fp80 @test_x86_fp80_abs(x86_fp80 %arg) {
+; X64-LABEL: test_x86_fp80_abs:
+; X64:       # %bb.0:
+; X64-NEXT:    fldt {{[0-9]+}}(%rsp)
+; X64-NEXT:    fabs
+; X64-NEXT:    retq
+;
+; GISEL-LABEL: test_x86_fp80_abs:
+; GISEL:       # %bb.0:
+; GISEL-NEXT:    fldt {{[0-9]+}}(%rsp)
+; GISEL-NEXT:    fabs
+; GISEL-NEXT:    retq
+  %abs = tail call x86_fp80 @llvm.fabs.f80(x86_fp80 %arg)
+  ret x86_fp80 %abs
+}



More information about the llvm-commits mailing list