From 4f28b24b2dee48af14ba80a5b4949d35dc2879fc Mon Sep 17 00:00:00 2001 From: Nikita Shulga <2453524+malfet@users.noreply.github.com> Date: Tue, 23 Apr 2024 20:48:05 -0700 Subject: [PATCH 1/2] Fix portable is[inf|nan|_out compilation on older Linux By wrapping a potentially non-compliant `isinf`/`isnan` implementations into a lambda with a defined return type Compiler should be able to optimize it out into direct function call, see https://godbolt.org/z/bqYGd47Mx --- kernels/portable/cpu/op_isnan.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernels/portable/cpu/op_isnan.cpp b/kernels/portable/cpu/op_isnan.cpp index 52857990904..cfcc3fe34b5 100644 --- a/kernels/portable/cpu/op_isnan.cpp +++ b/kernels/portable/cpu/op_isnan.cpp @@ -15,8 +15,10 @@ namespace executor { namespace native { Tensor& isnan_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) { + // Lambda is syntactic sugar needed to workaround compilation on some older non-compatible distros + // where isnan is not defined for double, but either for float or for long double return internal::unary_ufunc_realhb_to_bool( - static_cast(std::isnan), ctx, in, out); + [](double x) -> bool { return std::isnan(x); }, ctx, in, out); } } // namespace native From 8a7986c797cbdd1ce120f3283b99e3b624a6d0e3 Mon Sep 17 00:00:00 2001 From: Nikita Shulga <2453524+malfet@users.noreply.github.com> Date: Wed, 24 Apr 2024 03:51:02 +0000 Subject: [PATCH 2/2] Fix both and lint --- kernels/portable/cpu/op_isinf.cpp | 4 +++- kernels/portable/cpu/op_isnan.cpp | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/kernels/portable/cpu/op_isinf.cpp b/kernels/portable/cpu/op_isinf.cpp index 49db65dfcc8..068f402c07d 100644 --- a/kernels/portable/cpu/op_isinf.cpp +++ b/kernels/portable/cpu/op_isinf.cpp @@ -15,8 +15,10 @@ namespace executor { namespace native { Tensor& isinf_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) { + // Lambda is syntactic sugar needed to workaround compilation on some older + // non-compatible distros where isnan is returning int rather than bool return internal::unary_ufunc_realhb_to_bool( - static_cast(std::isinf), ctx, in, out); + [](double x) -> bool { return std::isinf(x); }, ctx, in, out); } } // namespace native diff --git a/kernels/portable/cpu/op_isnan.cpp b/kernels/portable/cpu/op_isnan.cpp index cfcc3fe34b5..09fb4f5f8ac 100644 --- a/kernels/portable/cpu/op_isnan.cpp +++ b/kernels/portable/cpu/op_isnan.cpp @@ -15,8 +15,8 @@ namespace executor { namespace native { Tensor& isnan_out(RuntimeContext& ctx, const Tensor& in, Tensor& out) { - // Lambda is syntactic sugar needed to workaround compilation on some older non-compatible distros - // where isnan is not defined for double, but either for float or for long double + // Lambda is syntactic sugar needed to workaround compilation on some older + // non-compatible distros where isnan is returning int rather than bool return internal::unary_ufunc_realhb_to_bool( [](double x) -> bool { return std::isnan(x); }, ctx, in, out); }