Skip to content

AArch64: Select FCANONICALIZE #104429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2024
Merged

AArch64: Select FCANONICALIZE #104429

merged 3 commits into from
Oct 11, 2024

Conversation

wzssyqa
Copy link
Contributor

@wzssyqa wzssyqa commented Aug 15, 2024

FMINNM/FMAXNM instructions of AArch64 follow IEEE754-2008. We can use them to canonicalize a floating point number. And FMINNUM_IEEE/FMAXNUM_IEEE is used by something like expanding FMINIMUMNUM/FMAXIMUMNUM, so let's define them.

@llvmbot
Copy link
Member

llvmbot commented Aug 15, 2024

@llvm/pr-subscribers-backend-aarch64

Author: YunQiang Su (wzssyqa)

Changes

FMINNM/FMAXNM instructions of AArch64 follow IEEE754-2008. We can use them to canonicalize a floating point number. And FMINNUM_IEEE/FMAXNUM_IEEE is used by something like expanding FMINIMUMNUM/FMAXIMUMNUM, so let's define them.


Full diff: https://github.com/llvm/llvm-project/pull/104429.diff

2 Files Affected:

  • (modified) llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (+32-11)
  • (modified) llvm/lib/Target/AArch64/AArch64InstrInfo.td (+12)
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 97fb2c5f552731..b34d1eaf8828e2 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -860,17 +860,38 @@ AArch64TargetLowering::AArch64TargetLowering(const TargetMachine &TM,
   setOperationAction(ISD::FP_ROUND, MVT::v4bf16, Custom);
 
   // AArch64 has implementations of a lot of rounding-like FP operations.
-  for (auto Op :
-       {ISD::FFLOOR,          ISD::FNEARBYINT,      ISD::FCEIL,
-        ISD::FRINT,           ISD::FTRUNC,          ISD::FROUND,
-        ISD::FROUNDEVEN,      ISD::FMINNUM,         ISD::FMAXNUM,
-        ISD::FMINIMUM,        ISD::FMAXIMUM,        ISD::LROUND,
-        ISD::LLROUND,         ISD::LRINT,           ISD::LLRINT,
-        ISD::STRICT_FFLOOR,   ISD::STRICT_FCEIL,    ISD::STRICT_FNEARBYINT,
-        ISD::STRICT_FRINT,    ISD::STRICT_FTRUNC,   ISD::STRICT_FROUNDEVEN,
-        ISD::STRICT_FROUND,   ISD::STRICT_FMINNUM,  ISD::STRICT_FMAXNUM,
-        ISD::STRICT_FMINIMUM, ISD::STRICT_FMAXIMUM, ISD::STRICT_LROUND,
-        ISD::STRICT_LLROUND,  ISD::STRICT_LRINT,    ISD::STRICT_LLRINT}) {
+  for (auto Op : {ISD::FFLOOR,
+                  ISD::FNEARBYINT,
+                  ISD::FCEIL,
+                  ISD::FRINT,
+                  ISD::FTRUNC,
+                  ISD::FROUND,
+                  ISD::FROUNDEVEN,
+                  ISD::FMINNUM,
+                  ISD::FMAXNUM,
+                  ISD::FMINNUM_IEEE,
+                  ISD::FMAXNUM_IEEE,
+                  ISD::FMINIMUM,
+                  ISD::FMAXIMUM,
+                  ISD::LROUND,
+                  ISD::LLROUND,
+                  ISD::LRINT,
+                  ISD::LLRINT,
+                  ISD::STRICT_FFLOOR,
+                  ISD::STRICT_FCEIL,
+                  ISD::STRICT_FNEARBYINT,
+                  ISD::STRICT_FRINT,
+                  ISD::STRICT_FTRUNC,
+                  ISD::STRICT_FROUNDEVEN,
+                  ISD::STRICT_FROUND,
+                  ISD::STRICT_FMINNUM,
+                  ISD::STRICT_FMAXNUM,
+                  ISD::STRICT_FMINIMUM,
+                  ISD::STRICT_FMAXIMUM,
+                  ISD::STRICT_LROUND,
+                  ISD::STRICT_LLROUND,
+                  ISD::STRICT_LRINT,
+                  ISD::STRICT_LLRINT}) {
     for (MVT Ty : {MVT::f32, MVT::f64})
       setOperationAction(Op, Ty, Legal);
     if (Subtarget->hasFullFP16())
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.td b/llvm/lib/Target/AArch64/AArch64InstrInfo.td
index 403453a4ee0a92..fe69e5bd638b50 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.td
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.td
@@ -5051,6 +5051,18 @@ def : Pat<(v1f64 (fmaxnum (v1f64 FPR64:$Rn), (v1f64 FPR64:$Rm))),
 def : Pat<(v1f64 (fminnum (v1f64 FPR64:$Rn), (v1f64 FPR64:$Rm))),
           (FMINNMDrr FPR64:$Rn, FPR64:$Rm)>;
 
+def : Pat<(fminnum_ieee (f64 FPR64:$a), (f64 FPR64:$b)),
+          (FMINNMDrr FPR64:$a, FPR64:$b)>;
+def : Pat<(fminnum_ieee (f32 FPR32:$a), (f32 FPR32:$b)),
+          (FMINNMSrr FPR32:$a, FPR32:$b)>;
+def : Pat<(fmaxnum_ieee (f64 FPR64:$a), (f64 FPR64:$b)),
+          (FMAXNMDrr FPR64:$a, FPR64:$b)>;
+def : Pat<(fmaxnum_ieee (f32 FPR32:$a), (f32 FPR32:$b)),
+          (FMAXNMSrr FPR32:$a, FPR32:$b)>;
+def : Pat<(f32 (fcanonicalize f32:$a)),
+          (FMINSrr   f32:$a, f32:$a)>;
+def : Pat<(f64 (fcanonicalize f64:$a)),
+          (FMINDrr   f64:$a, f64:$a)>;
 //===----------------------------------------------------------------------===//
 // Floating point three operand instructions.
 //===----------------------------------------------------------------------===//

@wzssyqa wzssyqa force-pushed the fminimum_num_arm64 branch 2 times, most recently from c9f84ce to 3340528 Compare August 16, 2024 03:11
@wzssyqa
Copy link
Contributor Author

wzssyqa commented Sep 3, 2024

ping

@wzssyqa wzssyqa requested review from arsenm and TNorthover September 6, 2024 02:39
@wzssyqa wzssyqa changed the title AArch64: Add FCANONICALIZE and FMINNUM_IEEE support AArch64: Add FCANONICALIZE Sep 9, 2024
@wzssyqa wzssyqa marked this pull request as draft September 11, 2024 04:02
@wzssyqa wzssyqa marked this pull request as ready for review October 10, 2024 08:01
@wzssyqa wzssyqa force-pushed the fminimum_num_arm64 branch from 3340528 to edd7b19 Compare October 10, 2024 08:01
@wzssyqa wzssyqa requested a review from arsenm October 10, 2024 08:01
Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In future could try to select to a copy in cases where the source is known canonicalized already

@arsenm arsenm changed the title AArch64: Add FCANONICALIZE AArch64: Select FCANONICALIZE Oct 10, 2024
FMINNM/FMAXNM instructions of AArch64 follow IEEE754-2008.
We can use them to canonicalize a floating point number.
@wzssyqa wzssyqa force-pushed the fminimum_num_arm64 branch from 725d593 to cfc50bd Compare October 10, 2024 08:29
Copy link

github-actions bot commented Oct 10, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@wzssyqa wzssyqa force-pushed the fminimum_num_arm64 branch from 2b24ec4 to 8798df3 Compare October 10, 2024 09:19
@wzssyqa wzssyqa requested review from davemgreen and arsenm October 10, 2024 09:23
Copy link
Collaborator

@davemgreen davemgreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this. LGTM with some suggestions for where code can be moved.

@wzssyqa wzssyqa requested a review from davemgreen October 11, 2024 00:35
@wzssyqa wzssyqa merged commit 72fb379 into llvm:main Oct 11, 2024
8 checks passed
@wzssyqa wzssyqa deleted the fminimum_num_arm64 branch October 11, 2024 00:45
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
FMINNM/FMAXNM instructions of AArch64 follow IEEE754-2008. We can use
them to canonicalize a floating point number. And
FMINNUM_IEEE/FMAXNUM_IEEE is used by something like expanding
FMINIMUMNUM/FMAXIMUMNUM, so let's define them.

---------

Co-authored-by: Your Name <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants