From e849b3424a46ac1c742cd5a9340deaaa790f12e3 Mon Sep 17 00:00:00 2001 From: Cheng Date: Thu, 21 Mar 2024 13:28:05 +0900 Subject: [PATCH] Do not use static constexpr in header (#863) Doing so results in each compilation unit (.cpp file) having its own copy of the variable, while inline constexpr makes sure there is only one copy. --- mlx/dtype.h | 26 +++++++++++++------------- mlx/types/complex.h | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/mlx/dtype.h b/mlx/dtype.h index fec1725f3..cb5f79849 100644 --- a/mlx/dtype.h +++ b/mlx/dtype.h @@ -46,22 +46,22 @@ struct Dtype { }; }; -static constexpr Dtype bool_{Dtype::Val::bool_, sizeof(bool)}; +inline constexpr Dtype bool_{Dtype::Val::bool_, sizeof(bool)}; -static constexpr Dtype uint8{Dtype::Val::uint8, sizeof(uint8_t)}; -static constexpr Dtype uint16{Dtype::Val::uint16, sizeof(uint16_t)}; -static constexpr Dtype uint32{Dtype::Val::uint32, sizeof(uint32_t)}; -static constexpr Dtype uint64{Dtype::Val::uint64, sizeof(uint64_t)}; +inline constexpr Dtype uint8{Dtype::Val::uint8, sizeof(uint8_t)}; +inline constexpr Dtype uint16{Dtype::Val::uint16, sizeof(uint16_t)}; +inline constexpr Dtype uint32{Dtype::Val::uint32, sizeof(uint32_t)}; +inline constexpr Dtype uint64{Dtype::Val::uint64, sizeof(uint64_t)}; -static constexpr Dtype int8{Dtype::Val::int8, sizeof(int8_t)}; -static constexpr Dtype int16{Dtype::Val::int16, sizeof(int16_t)}; -static constexpr Dtype int32{Dtype::Val::int32, sizeof(int32_t)}; -static constexpr Dtype int64{Dtype::Val::int64, sizeof(int64_t)}; +inline constexpr Dtype int8{Dtype::Val::int8, sizeof(int8_t)}; +inline constexpr Dtype int16{Dtype::Val::int16, sizeof(int16_t)}; +inline constexpr Dtype int32{Dtype::Val::int32, sizeof(int32_t)}; +inline constexpr Dtype int64{Dtype::Val::int64, sizeof(int64_t)}; -static constexpr Dtype float16{Dtype::Val::float16, sizeof(uint16_t)}; -static constexpr Dtype float32{Dtype::Val::float32, sizeof(float)}; -static constexpr Dtype bfloat16{Dtype::Val::bfloat16, sizeof(uint16_t)}; -static constexpr Dtype complex64{Dtype::Val::complex64, sizeof(complex64_t)}; +inline constexpr Dtype float16{Dtype::Val::float16, sizeof(uint16_t)}; +inline constexpr Dtype float32{Dtype::Val::float32, sizeof(float)}; +inline constexpr Dtype bfloat16{Dtype::Val::bfloat16, sizeof(uint16_t)}; +inline constexpr Dtype complex64{Dtype::Val::complex64, sizeof(complex64_t)}; Dtype promote_types(const Dtype& t1, const Dtype& t2); diff --git a/mlx/types/complex.h b/mlx/types/complex.h index 539974d33..dad0b322f 100644 --- a/mlx/types/complex.h +++ b/mlx/types/complex.h @@ -10,7 +10,7 @@ struct complex64_t; struct complex128_t; template -static constexpr bool can_convert_to_complex128 = +inline constexpr bool can_convert_to_complex128 = !std::is_same_v && std::is_convertible_v; struct complex128_t : public std::complex { @@ -28,7 +28,7 @@ struct complex128_t : public std::complex { }; template -static constexpr bool can_convert_to_complex64 = +inline constexpr bool can_convert_to_complex64 = !std::is_same_v && std::is_convertible_v; struct complex64_t : public std::complex {