* [PATCH 0/2] Optimize is_scoped_enum trait performance @ 2023-09-12 0:16 Ken Matsui 2023-09-12 0:16 ` [PATCH 1/2] c++: Implement __is_scoped_enum built-in trait Ken Matsui 2023-09-12 0:16 ` [PATCH 2/2] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui 0 siblings, 2 replies; 4+ messages in thread From: Ken Matsui @ 2023-09-12 0:16 UTC (permalink / raw) To: gcc-patches; +Cc: libstdc++, Ken Matsui This patch series optimizes is_scoped_enum trait performance. The first patch implements __is_scoped_enum built-in trait. The second patch optimizes is_scoped_enum trait performance by using __is_scoped_enum built-in trait if available. The performance improvement is shown in the following benchmark: https://github.com/ken-matsui/gsoc23/blob/main/is_scoped_enum_v.md#mon-sep-11-050411-pm-pdt-2023 Time: -32.717% +/- 1.67315% Peak Memory Usage: -17.3805% +/- 0.00931763% Total Memory Usage: -19.0881% +/- 0% Ken Matsui (2): c++: Implement __is_scoped_enum built-in trait libstdc++: Optimize is_scoped_enum trait performance gcc/cp/constraint.cc | 3 + gcc/cp/cp-trait.def | 1 + gcc/cp/semantics.cc | 4 ++ gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 + gcc/testsuite/g++.dg/ext/is_scoped_enum.C | 67 +++++++++++++++++++++++ libstdc++-v3/include/std/type_traits | 12 ++++ 6 files changed, 90 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_scoped_enum.C -- 2.42.0 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] c++: Implement __is_scoped_enum built-in trait 2023-09-12 0:16 [PATCH 0/2] Optimize is_scoped_enum trait performance Ken Matsui @ 2023-09-12 0:16 ` Ken Matsui 2023-09-12 0:16 ` [PATCH 2/2] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui 1 sibling, 0 replies; 4+ messages in thread From: Ken Matsui @ 2023-09-12 0:16 UTC (permalink / raw) To: gcc-patches; +Cc: libstdc++, Ken Matsui This patch implements built-in trait for std::is_scoped_enum. gcc/cp/ChangeLog: * cp-trait.def (IS_SCOPED_ENUM): Define __is_scoped_enum. * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_SCOPED_ENUM. * semantics.cc (trait_expr_value): Likewise. (finish_trait_expr): Likewise. gcc/testsuite/ChangeLog: * g++.dg/ext/has-builtin-1.C: Test existence of is_scoped_enum. * g++.dg/ext/is_scoped_enum.C: New test. Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org> --- gcc/cp/constraint.cc | 3 + gcc/cp/cp-trait.def | 1 + gcc/cp/semantics.cc | 4 ++ gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 + gcc/testsuite/g++.dg/ext/is_scoped_enum.C | 67 +++++++++++++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 gcc/testsuite/g++.dg/ext/is_scoped_enum.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index c9e4e7043cd..ba387cb5eb5 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3760,6 +3760,9 @@ diagnose_trait_expr (tree expr, tree args) case CPTK_IS_ASSIGNABLE: inform (loc, " %qT is not assignable from %qT", t1, t2); break; + case CPTK_IS_SCOPED_ENUM: + inform (loc, " %qT is not a scoped enum", t1); + break; case CPTK_IS_TRIVIALLY_ASSIGNABLE: inform (loc, " %qT is not trivially assignable from %qT", t1, t2); break; diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def index 8b7fece0cc8..8caabe4c289 100644 --- a/gcc/cp/cp-trait.def +++ b/gcc/cp/cp-trait.def @@ -76,6 +76,7 @@ DEFTRAIT_EXPR (IS_POINTER_INTERCONVERTIBLE_BASE_OF, "__is_pointer_interconvertib DEFTRAIT_EXPR (IS_POD, "__is_pod", 1) DEFTRAIT_EXPR (IS_POLYMORPHIC, "__is_polymorphic", 1) DEFTRAIT_EXPR (IS_SAME, "__is_same", 2) +DEFTRAIT_EXPR (IS_SCOPED_ENUM, "__is_scoped_enum", 1) DEFTRAIT_EXPR (IS_STD_LAYOUT, "__is_standard_layout", 1) DEFTRAIT_EXPR (IS_TRIVIAL, "__is_trivial", 1) DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivially_assignable", 2) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 0f7f4e87ae4..7e10d81fa34 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -12163,6 +12163,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_SAME: return same_type_p (type1, type2); + case CPTK_IS_SCOPED_ENUM: + return SCOPED_ENUM_P (type1); + case CPTK_IS_STD_LAYOUT: return std_layout_type_p (type1); @@ -12359,6 +12362,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2) case CPTK_IS_ENUM: case CPTK_IS_UNION: case CPTK_IS_SAME: + case CPTK_IS_SCOPED_ENUM: break; case CPTK_IS_LAYOUT_COMPATIBLE: diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C index f343e153e56..bc79e50054a 100644 --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C @@ -146,3 +146,6 @@ #if !__has_builtin (__remove_cvref) # error "__has_builtin (__remove_cvref) failed" #endif +#if !__has_builtin (__is_scoped_enum) +# error "__has_builtin (__is_scoped_enum) failed" +#endif diff --git a/gcc/testsuite/g++.dg/ext/is_scoped_enum.C b/gcc/testsuite/g++.dg/ext/is_scoped_enum.C new file mode 100644 index 00000000000..a563b6ee67d --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/is_scoped_enum.C @@ -0,0 +1,67 @@ +// { dg-do compile { target c++11 } } + +#include <testsuite_tr1.h> + +using namespace __gnu_test; + +#define SA(X) static_assert((X),#X) + +#define SA_TEST_FN(TRAIT, TYPE, EXPECT) \ + SA(TRAIT(TYPE) == EXPECT); \ + SA(TRAIT(const TYPE) == EXPECT); + +#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT) \ + SA(TRAIT(TYPE) == EXPECT); \ + SA(TRAIT(const TYPE) == EXPECT); \ + SA(TRAIT(volatile TYPE) == EXPECT); \ + SA(TRAIT(const volatile TYPE) == EXPECT) + +enum class E { e1, e2 }; +SA_TEST_CATEGORY(__is_scoped_enum, E, true); +enum class Ec : char { e1, e2 }; +SA_TEST_CATEGORY(__is_scoped_enum, Ec, true); + +// negative tests +enum U { u1, u2 }; +SA_TEST_CATEGORY(__is_scoped_enum, U, false); +enum F : int { f1, f2 }; +SA_TEST_CATEGORY(__is_scoped_enum, F, false); +struct S; +SA_TEST_CATEGORY(__is_scoped_enum, S, false); +struct S { }; +SA_TEST_CATEGORY(__is_scoped_enum, S, false); + +SA_TEST_CATEGORY(__is_scoped_enum, int, false); +SA_TEST_CATEGORY(__is_scoped_enum, int[], false); +SA_TEST_CATEGORY(__is_scoped_enum, int[2], false); +SA_TEST_CATEGORY(__is_scoped_enum, int[][2], false); +SA_TEST_CATEGORY(__is_scoped_enum, int[2][3], false); +SA_TEST_CATEGORY(__is_scoped_enum, int*, false); +SA_TEST_CATEGORY(__is_scoped_enum, int&, false); +SA_TEST_CATEGORY(__is_scoped_enum, int*&, false); +SA_TEST_FN(__is_scoped_enum, int(), false); +SA_TEST_FN(__is_scoped_enum, int(*)(), false); +SA_TEST_FN(__is_scoped_enum, int(&)(), false); + +enum opaque_unscoped : short; +enum class opaque_scoped; +enum class opaque_scoped_with_base : long; + +SA_TEST_CATEGORY(__is_scoped_enum, opaque_unscoped, false); +SA_TEST_CATEGORY(__is_scoped_enum, opaque_scoped, true); +SA_TEST_CATEGORY(__is_scoped_enum, opaque_scoped_with_base, true); + +enum unscoped { + u_is_scoped = __is_scoped_enum(unscoped), +}; +SA( ! unscoped::u_is_scoped ); + +enum unscoped_fixed : char { + uf_is_scoped = __is_scoped_enum(unscoped_fixed), +}; +SA( ! unscoped_fixed::uf_is_scoped ); + +enum class scoped { + is_scoped = __is_scoped_enum(scoped), +}; +SA( (bool) scoped::is_scoped ); -- 2.42.0 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/2] libstdc++: Optimize is_scoped_enum trait performance 2023-09-12 0:16 [PATCH 0/2] Optimize is_scoped_enum trait performance Ken Matsui 2023-09-12 0:16 ` [PATCH 1/2] c++: Implement __is_scoped_enum built-in trait Ken Matsui @ 2023-09-12 0:16 ` Ken Matsui 2023-09-12 6:50 ` Jonathan Wakely 1 sibling, 1 reply; 4+ messages in thread From: Ken Matsui @ 2023-09-12 0:16 UTC (permalink / raw) To: gcc-patches; +Cc: libstdc++, Ken Matsui This patch optimizes the performance of the is_scoped_enum trait by dispatching to the new __is_scoped_enum built-in trait. libstdc++-v3/ChangeLog: * include/std/type_traits (is_scoped_enum): Use __is_scoped_enum built-in trait. (is_scoped_enum_v): Likewise. Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org> --- libstdc++-v3/include/std/type_traits | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 677cd934b94..26ae491fa69 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -3585,6 +3585,12 @@ template<typename _Ret, typename _Fn, typename... _Args> /// True if the type is a scoped enumeration type. /// @since C++23 +# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum) + template<typename _Tp> + struct is_scoped_enum + : bool_constant<__is_scoped_enum(_Tp)> + { }; +# else template<typename _Tp> struct is_scoped_enum : false_type @@ -3596,11 +3602,17 @@ template<typename _Ret, typename _Fn, typename... _Args> struct is_scoped_enum<_Tp> : bool_constant<!requires(_Tp __t, void(*__f)(int)) { __f(__t); }> { }; +# endif /// @ingroup variable_templates /// @since C++23 +# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum) + template<typename _Tp> + inline constexpr bool is_scoped_enum_v = __is_scoped_enum(_Tp); +# else template<typename _Tp> inline constexpr bool is_scoped_enum_v = is_scoped_enum<_Tp>::value; +# endif #endif #ifdef __cpp_lib_reference_from_temporary // C++ >= 23 && ref_{converts,constructs}_from_temp -- 2.42.0 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] libstdc++: Optimize is_scoped_enum trait performance 2023-09-12 0:16 ` [PATCH 2/2] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui @ 2023-09-12 6:50 ` Jonathan Wakely 0 siblings, 0 replies; 4+ messages in thread From: Jonathan Wakely @ 2023-09-12 6:50 UTC (permalink / raw) To: Ken Matsui; +Cc: gcc-patches, libstdc++ [-- Attachment #1: Type: text/plain, Size: 1931 bytes --] On Tue, 12 Sept 2023, 01:21 Ken Matsui via Libstdc++, <libstdc++@gcc.gnu.org> wrote: > This patch optimizes the performance of the is_scoped_enum trait > by dispatching to the new __is_scoped_enum built-in trait. > OK for trunk (after the built-in is in the front end), thanks. > libstdc++-v3/ChangeLog: > > * include/std/type_traits (is_scoped_enum): Use > __is_scoped_enum built-in trait. > (is_scoped_enum_v): Likewise. > > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org> > --- > libstdc++-v3/include/std/type_traits | 12 ++++++++++++ > 1 file changed, 12 insertions(+) > > diff --git a/libstdc++-v3/include/std/type_traits > b/libstdc++-v3/include/std/type_traits > index 677cd934b94..26ae491fa69 100644 > --- a/libstdc++-v3/include/std/type_traits > +++ b/libstdc++-v3/include/std/type_traits > @@ -3585,6 +3585,12 @@ template<typename _Ret, typename _Fn, typename... > _Args> > /// True if the type is a scoped enumeration type. > /// @since C++23 > > +# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum) > + template<typename _Tp> > + struct is_scoped_enum > + : bool_constant<__is_scoped_enum(_Tp)> > + { }; > +# else > template<typename _Tp> > struct is_scoped_enum > : false_type > @@ -3596,11 +3602,17 @@ template<typename _Ret, typename _Fn, typename... > _Args> > struct is_scoped_enum<_Tp> > : bool_constant<!requires(_Tp __t, void(*__f)(int)) { __f(__t); }> > { }; > +# endif > > /// @ingroup variable_templates > /// @since C++23 > +# if _GLIBCXX_USE_BUILTIN_TRAIT(__is_scoped_enum) > + template<typename _Tp> > + inline constexpr bool is_scoped_enum_v = __is_scoped_enum(_Tp); > +# else > template<typename _Tp> > inline constexpr bool is_scoped_enum_v = is_scoped_enum<_Tp>::value; > +# endif > #endif > > #ifdef __cpp_lib_reference_from_temporary // C++ >= 23 && > ref_{converts,constructs}_from_temp > -- > 2.42.0 > > ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-09-12 6:50 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-09-12 0:16 [PATCH 0/2] Optimize is_scoped_enum trait performance Ken Matsui 2023-09-12 0:16 ` [PATCH 1/2] c++: Implement __is_scoped_enum built-in trait Ken Matsui 2023-09-12 0:16 ` [PATCH 2/2] libstdc++: Optimize is_scoped_enum trait performance Ken Matsui 2023-09-12 6:50 ` Jonathan Wakely
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).