[clang] [CodeGenPrepare] Transform ldexp into target supported intrinsics (PR #67552)

2023-09-27 Thread via cfe-commits

huhu233 wrote:

> Why does this need special handling in CGP instead of being a normal custom 
> lowering for FLDEXP?

@nikic , thanks for your suggestion, I'll update the patch as soon!

https://github.com/llvm/llvm-project/pull/67552
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CodeGenPrepare] Transform ldexp into target supported intrinsics (PR #67552)

2023-09-27 Thread via cfe-commits

huhu233 wrote:

> I agree and custom lowering also gives a straight forward way to support 
> vectors types as well.

You are right, I'll update the patch as soon, thanks!

https://github.com/llvm/llvm-project/pull/67552
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CodeGenPrepare] Transform ldexp into target supported intrinsics (PR #67552)

2023-09-27 Thread Paul Walker via cfe-commits

paulwalker-arm wrote:

I agree and custom lowering also gives a straight forward way to support 
vectors types as well.

https://github.com/llvm/llvm-project/pull/67552
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CodeGenPrepare] Transform ldexp into target supported intrinsics (PR #67552)

2023-09-27 Thread Nikita Popov via cfe-commits

nikic wrote:

Why does this need special handling in CGP instead of being a normal custom 
lowering for FLDEXP?

https://github.com/llvm/llvm-project/pull/67552
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CodeGenPrepare] Transform ldexp into target supported intrinsics (PR #67552)

2023-09-27 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-backend-aarch64


Changes

There are more efficient implementations for llvm.ldexp on different targets. 
This patch transforms llvm.ldexp into target supported intrinsics before 
lowering.

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


10 Files Affected:

- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+3) 
- (modified) clang/test/CodeGen/math-libcalls.c (+6-6) 
- (modified) llvm/include/llvm/Analysis/TargetTransformInfo.h (+6) 
- (modified) llvm/include/llvm/Analysis/TargetTransformInfoImpl.h (+2) 
- (modified) llvm/lib/Analysis/TargetTransformInfo.cpp (+4) 
- (modified) llvm/lib/CodeGen/CodeGenPrepare.cpp (+68) 
- (modified) llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h (+7) 
- (modified) llvm/lib/Target/X86/X86TargetTransformInfo.h (+11) 
- (added) llvm/test/Transforms/CodeGenPrepare/AArch64/optimize-ldexp.ll (+46) 
- (added) llvm/test/Transforms/CodeGenPrepare/X86/optimize-ldexp.ll (+38) 


``diff
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 04c0325c7fd038b..da01c34731386e0 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -2719,6 +2719,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   return RValue::get(emitMaybeConstrainedFPToIntRoundBuiltin(
   *this, E, Intrinsic::llrint,
   Intrinsic::experimental_constrained_llrint));
+case Builtin::BIldexp:
+case Builtin::BIldexpf:
+case Builtin::BIldexpl:
 case Builtin::BI__builtin_ldexp:
 case Builtin::BI__builtin_ldexpf:
 case Builtin::BI__builtin_ldexpl:
diff --git a/clang/test/CodeGen/math-libcalls.c 
b/clang/test/CodeGen/math-libcalls.c
index 02df4fe5fea6018..a906bda4c88c958 100644
--- a/clang/test/CodeGen/math-libcalls.c
+++ b/clang/test/CodeGen/math-libcalls.c
@@ -71,15 +71,15 @@ void foo(double *d, float f, float *fp, long double *l, int 
*i, const char *c) {
 
   ldexp(f,f);ldexpf(f,f);   ldexpl(f,f);
 
-  // NO__ERRNO: declare double @ldexp(double noundef, i32 noundef) [[READNONE]]
-  // NO__ERRNO: declare float @ldexpf(float noundef, i32 noundef) [[READNONE]]
-  // NO__ERRNO: declare x86_fp80 @ldexpl(x86_fp80 noundef, i32 noundef) 
[[READNONE]]
+  // NO__ERRNO: declare double @llvm.ldexp.f64.i32(double, i32) 
[[READNONE_INTRINSIC]]
+  // NO__ERRNO: declare float @llvm.ldexp.f32.i32(float, i32) 
[[READNONE_INTRINSIC]]
+  // NO__ERRNO: declare x86_fp80 @llvm.ldexp.f80.i32(x86_fp80, i32) 
[[READNONE_INTRINSIC]]
   // HAS_ERRNO: declare double @ldexp(double noundef, i32 noundef) 
[[NOT_READNONE]]
   // HAS_ERRNO: declare float @ldexpf(float noundef, i32 noundef) 
[[NOT_READNONE]]
   // HAS_ERRNO: declare x86_fp80 @ldexpl(x86_fp80 noundef, i32 noundef) 
[[NOT_READNONE]]
-  // HAS_MAYTRAP: declare double @ldexp(double noundef, i32 noundef) 
[[NOT_READNONE]]
-  // HAS_MAYTRAP: declare float @ldexpf(float noundef, i32 noundef) 
[[NOT_READNONE]]
-  // HAS_MAYTRAP: declare x86_fp80 @ldexpl(x86_fp80 noundef, i32 noundef) 
[[NOT_READNONE]]
+  // HAS_MAYTRAP: declare double @llvm.experimental.constrained.ldexp.f64.i32(
+  // HAS_MAYTRAP: declare float @llvm.experimental.constrained.ldexp.f32.i32(
+  // HAS_MAYTRAP: declare x86_fp80 
@llvm.experimental.constrained.ldexp.f80.i32(
 
   modf(f,d);   modff(f,fp);  modfl(f,l);
 
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h 
b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index 1ae595d2110457d..c8805aadf146874 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -1669,6 +1669,7 @@ class TargetTransformInfo {
   /// \return The maximum number of function arguments the target supports.
   unsigned getMaxNumArgs() const;
 
+  unsigned getTargetSupportedLdexpInst(Type *Ty) const;
   /// @}
 
 private:
@@ -2035,6 +2036,7 @@ class TargetTransformInfo::Concept {
   getVPLegalizationStrategy(const VPIntrinsic ) const = 0;
   virtual bool hasArmWideBranch(bool Thumb) const = 0;
   virtual unsigned getMaxNumArgs() const = 0;
+  virtual unsigned getTargetSupportedLdexpInst(Type *Ty) const = 0;
 };
 
 template 
@@ -2745,6 +2747,10 @@ class TargetTransformInfo::Model final : public 
TargetTransformInfo::Concept {
   unsigned getMaxNumArgs() const override {
 return Impl.getMaxNumArgs();
   }
+
+  unsigned getTargetSupportedLdexpInst(Type *Ty) const override {
+return Impl.getTargetSupportedLdexpInst(Ty);
+  }
 };
 
 template 
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h 
b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 326c3130c6cff76..6d6a715f62b201c 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -891,6 +891,8 @@ class TargetTransformInfoImplBase {
 
   unsigned getMaxNumArgs() const { return UINT_MAX; }
 
+  unsigned getTargetSupportedLdexpInst(Type *Ty) const { return 0; }
+
 

[clang] [CodeGenPrepare] Transform ldexp into target supported intrinsics (PR #67552)

2023-09-27 Thread via cfe-commits

https://github.com/huhu233 created 
https://github.com/llvm/llvm-project/pull/67552

There are more efficient implementations for llvm.ldexp on different targets. 
This patch transforms llvm.ldexp into target supported intrinsics before 
lowering.

>From dce0b8f8a76da24b27dd6ed61087e97b5f9415ec Mon Sep 17 00:00:00 2001
From: TiehuZhang 
Date: Wed, 27 Sep 2023 20:30:16 +0800
Subject: [PATCH] [CodeGenPrepare] Transform ldexp into target supported
 intrinsics

There are more efficient implementations for llvm.ldexp on different targets.
This patch transforms llvm.ldexp into target supported intrinsics before 
lowering.
---
 clang/lib/CodeGen/CGBuiltin.cpp   |  3 +
 clang/test/CodeGen/math-libcalls.c| 12 ++--
 .../llvm/Analysis/TargetTransformInfo.h   |  6 ++
 .../llvm/Analysis/TargetTransformInfoImpl.h   |  2 +
 llvm/lib/Analysis/TargetTransformInfo.cpp |  4 ++
 llvm/lib/CodeGen/CodeGenPrepare.cpp   | 68 +++
 .../AArch64/AArch64TargetTransformInfo.h  |  7 ++
 llvm/lib/Target/X86/X86TargetTransformInfo.h  | 11 +++
 .../CodeGenPrepare/AArch64/optimize-ldexp.ll  | 46 +
 .../CodeGenPrepare/X86/optimize-ldexp.ll  | 38 +++
 10 files changed, 191 insertions(+), 6 deletions(-)
 create mode 100644 
llvm/test/Transforms/CodeGenPrepare/AArch64/optimize-ldexp.ll
 create mode 100644 llvm/test/Transforms/CodeGenPrepare/X86/optimize-ldexp.ll

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 04c0325c7fd038b..da01c34731386e0 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -2719,6 +2719,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
   return RValue::get(emitMaybeConstrainedFPToIntRoundBuiltin(
   *this, E, Intrinsic::llrint,
   Intrinsic::experimental_constrained_llrint));
+case Builtin::BIldexp:
+case Builtin::BIldexpf:
+case Builtin::BIldexpl:
 case Builtin::BI__builtin_ldexp:
 case Builtin::BI__builtin_ldexpf:
 case Builtin::BI__builtin_ldexpl:
diff --git a/clang/test/CodeGen/math-libcalls.c 
b/clang/test/CodeGen/math-libcalls.c
index 02df4fe5fea6018..a906bda4c88c958 100644
--- a/clang/test/CodeGen/math-libcalls.c
+++ b/clang/test/CodeGen/math-libcalls.c
@@ -71,15 +71,15 @@ void foo(double *d, float f, float *fp, long double *l, int 
*i, const char *c) {
 
   ldexp(f,f);ldexpf(f,f);   ldexpl(f,f);
 
-  // NO__ERRNO: declare double @ldexp(double noundef, i32 noundef) [[READNONE]]
-  // NO__ERRNO: declare float @ldexpf(float noundef, i32 noundef) [[READNONE]]
-  // NO__ERRNO: declare x86_fp80 @ldexpl(x86_fp80 noundef, i32 noundef) 
[[READNONE]]
+  // NO__ERRNO: declare double @llvm.ldexp.f64.i32(double, i32) 
[[READNONE_INTRINSIC]]
+  // NO__ERRNO: declare float @llvm.ldexp.f32.i32(float, i32) 
[[READNONE_INTRINSIC]]
+  // NO__ERRNO: declare x86_fp80 @llvm.ldexp.f80.i32(x86_fp80, i32) 
[[READNONE_INTRINSIC]]
   // HAS_ERRNO: declare double @ldexp(double noundef, i32 noundef) 
[[NOT_READNONE]]
   // HAS_ERRNO: declare float @ldexpf(float noundef, i32 noundef) 
[[NOT_READNONE]]
   // HAS_ERRNO: declare x86_fp80 @ldexpl(x86_fp80 noundef, i32 noundef) 
[[NOT_READNONE]]
-  // HAS_MAYTRAP: declare double @ldexp(double noundef, i32 noundef) 
[[NOT_READNONE]]
-  // HAS_MAYTRAP: declare float @ldexpf(float noundef, i32 noundef) 
[[NOT_READNONE]]
-  // HAS_MAYTRAP: declare x86_fp80 @ldexpl(x86_fp80 noundef, i32 noundef) 
[[NOT_READNONE]]
+  // HAS_MAYTRAP: declare double @llvm.experimental.constrained.ldexp.f64.i32(
+  // HAS_MAYTRAP: declare float @llvm.experimental.constrained.ldexp.f32.i32(
+  // HAS_MAYTRAP: declare x86_fp80 
@llvm.experimental.constrained.ldexp.f80.i32(
 
   modf(f,d);   modff(f,fp);  modfl(f,l);
 
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h 
b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index 1ae595d2110457d..c8805aadf146874 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -1669,6 +1669,7 @@ class TargetTransformInfo {
   /// \return The maximum number of function arguments the target supports.
   unsigned getMaxNumArgs() const;
 
+  unsigned getTargetSupportedLdexpInst(Type *Ty) const;
   /// @}
 
 private:
@@ -2035,6 +2036,7 @@ class TargetTransformInfo::Concept {
   getVPLegalizationStrategy(const VPIntrinsic ) const = 0;
   virtual bool hasArmWideBranch(bool Thumb) const = 0;
   virtual unsigned getMaxNumArgs() const = 0;
+  virtual unsigned getTargetSupportedLdexpInst(Type *Ty) const = 0;
 };
 
 template 
@@ -2745,6 +2747,10 @@ class TargetTransformInfo::Model final : public 
TargetTransformInfo::Concept {
   unsigned getMaxNumArgs() const override {
 return Impl.getMaxNumArgs();
   }
+
+  unsigned getTargetSupportedLdexpInst(Type *Ty) const override {
+return Impl.getTargetSupportedLdexpInst(Ty);
+  }
 };
 
 template 
diff --git