From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id B0DE73858D20 for ; Thu, 2 May 2024 16:56:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B0DE73858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=cs.washington.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.washington.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B0DE73858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714669009; cv=none; b=P2PjsO9lN2kfOGmKvfWwLgczV7xo53oqjE2h0jLVfUJ32JPCoE1YhJLv9SxQtVpO+Xn+fUvuNDYiDLqitYUCLUU5UI7kNRpJdwECIsrJngj2bImadhho7rd6XTgygySfQm6NsQYwY/IjBpu4MlLTNikXeOYc/M4MVxmifCoeO7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714669009; c=relaxed/simple; bh=GrKlHZ7h6vbDjVnw5A3lQ27s5yIJ+JBp7W9+zJEi/U4=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=vhUUvH8Wnb3zIEc76SdWYry4gtlAwH5teg1nN1DYpNbQMDcRTxRpNIxtq+WrPSdvkkYfrbdvajFCnNMVOLwveHWFuo+N0UpSLGjz7gFNsCBBeceNzIL3Uex4WbvLABcDA9exzzli89IsWrCr4BOlNSLJEBjhhRnlZkg/xHmr7Rg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-a519e1b0e2dso1128546566b.2 for ; Thu, 02 May 2024 09:56:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; t=1714669005; x=1715273805; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=GE+V+OX64Sb8JbececKtaQDeIfwG5qczBHdmid1tie4=; b=FFFxtSbNx2IziMyshtqUw9hBgRTMtxd99mtFLST4O96998JBiI4UVqESuQFvSOrT9o HY49u5OXFioDSKJ/vw9twg2D0QRMPj3ij2YC76nyHtSgf32reZyvhHL+xMX+ipLvge/v XWcAUeiDki0GfWXfrjPLOTZ2yzgpzk35PcNm0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714669005; x=1715273805; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GE+V+OX64Sb8JbececKtaQDeIfwG5qczBHdmid1tie4=; b=Ho8GjspsrMAG3ZhuKbXvCRrfeBAzs3UQKRgu1fWOZpJi2INCVpX1a8Wl5s2qGqCSzJ 46bNZ/JCgQqpcszOYnDnkxPY4ED+uoxlDRULU0DbcY5kHXNp4GF+D/8088HK9A8sAXS1 s9Hm1rjVii6yDqDT3GOcIZ97GzWbhLK2UA0LO4+Q+oncRkwN/N5mPqjdB47c7DAOcnbU GSTqExYrjgy/mnDHwYjxvq2h1k1micv4F/wkH5lZ3J3y0ro+UibBwQoLewsbyEDb7Hrk m8Ffsok2cB+dwC+P1jHd8tGupgfNXjZwifJEZIUVS0aoKRuFQieARiP+f8reB70sFjbd YA3g== X-Forwarded-Encrypted: i=1; AJvYcCXEjX1so9mlJbnkvzOnxQoPupxa6E5aFzUt6k1alprcqhe3+6wr85AHxZjFweiU+kOpfvnbgfxOaSbFjO8y0PlvJtgQUAY= X-Gm-Message-State: AOJu0YzekkkcBijAxtdgbpKevSu4LrBTuDbyPqc5w4nb0+Q40xM97D4+ rGlySOFq0iRxc9fXznX+V5oE1g7u4nHMu5E+l+dUNGhoxiSrY6kKsmDbNqGivldC6r2zK8VoPU9 negY5IofrApaHRyxlrTQIgOpw/Lw6grj+b4cb X-Google-Smtp-Source: AGHT+IF4H8RGEsbNuzmZX8E8HUUfQSNmGZvhNNWUNPpWDTQ8vuOSQDQBBg7Q0Df2WRp3r8W9vty4Epfvv6i23H617oo= X-Received: by 2002:a17:906:f2d4:b0:a58:ea69:9629 with SMTP id gz20-20020a170906f2d400b00a58ea699629mr85143ejb.6.1714669005259; Thu, 02 May 2024 09:56:45 -0700 (PDT) MIME-Version: 1.0 References: <20240221093616.4001742-1-kmatsui@gcc.gnu.org> <20240228192843.188979-1-kmatsui@gcc.gnu.org> <20240228192843.188979-22-kmatsui@gcc.gnu.org> <8b32b64d-8411-4792-9ffc-b81dbc189e52@redhat.com> <21abf361-86be-4c67-a845-9abc3a88a061@redhat.com> In-Reply-To: <21abf361-86be-4c67-a845-9abc3a88a061@redhat.com> From: Ken Matsui Date: Thu, 2 May 2024 09:56:08 -0700 Message-ID: Subject: Re: [PATCH v14 21/26] c++: Implement __rank built-in trait To: Jason Merrill Cc: Patrick Palka , Ken Matsui , gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, May 2, 2024 at 9:45=E2=80=AFAM Jason Merrill wro= te: > > On 5/2/24 12:20, Ken Matsui wrote: > > On Thu, May 2, 2024 at 8:34=E2=80=AFAM Ken Matsui wrote: > >> > >> On Thu, May 2, 2024 at 8:16=E2=80=AFAM Patrick Palka wrote: > >>> > >>> On Tue, 30 Apr 2024, Jason Merrill wrote: > >>> > >>>> On 2/28/24 11:26, Ken Matsui wrote: > >>>>> This patch implements built-in trait for std::rank. > >>>> > >>>> __rank seems too short, maybe __array_rank? > >>>> > >>>> Actually, it occurs to me that perhaps we should have been adding __= builtin to > >>>> all of these rather than just __ and the library trait name. I gues= s it's too > >>>> late to do that for the GCC 14 traits, but we could do it for this g= roup? > >>> > >>> Clang already implements many of these built-in, without using > >>> "__builtin" in their name. Shouldn't we be consistent with Clang whe= re > >>> we can? > > > > Since I had already replaced the prefix locally with __builtin, I > > submitted patches addressing all other Jason's reviews with that. > > Please let me know if we want to use __ and __array_rank. > > If Clang already has a corresponding built-in (as with __array_rank, > apparently), let's use the same name; otherwise let's add __builtin to > the new ones. I see. It looks like Clang does not have built-ins for is_unbounded_array, is_invocable, and is_nothrow_invocable. I will revert other built-ins. > > >> Oh, then, would using __array_rank and keeping existing built-in > >> traits as-is sound like a better choice? > >> > >> https://github.com/llvm/llvm-project/blob/4aca302f5a82ee65847c88500b39= a2530dfeceb4/libcxx/include/__type_traits/rank.h#L23 > >> > >>> > >>>> > >>>>> gcc/cp/ChangeLog: > >>>>> > >>>>> * cp-trait.def: Define __rank. > >>>>> * constraint.cc (diagnose_trait_expr): Handle CPTK_RANK. > >>>>> * semantics.cc (trait_expr_value): Likewise. > >>>>> (finish_trait_expr): Likewise. > >>>>> > >>>>> gcc/testsuite/ChangeLog: > >>>>> > >>>>> * g++.dg/ext/has-builtin-1.C: Test existence of __rank. > >>>>> * g++.dg/ext/rank.C: New test. > >>>>> > >>>>> Signed-off-by: Ken Matsui > >>>>> --- > >>>>> gcc/cp/constraint.cc | 3 +++ > >>>>> gcc/cp/cp-trait.def | 1 + > >>>>> gcc/cp/semantics.cc | 23 ++++++++++++++++++= ++--- > >>>>> gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++ > >>>>> gcc/testsuite/g++.dg/ext/rank.C | 24 ++++++++++++++++++= ++++++ > >>>>> 5 files changed, 51 insertions(+), 3 deletions(-) > >>>>> create mode 100644 gcc/testsuite/g++.dg/ext/rank.C > >>>>> > >>>>> diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc > >>>>> index 000df847342..23ea66d9c12 100644 > >>>>> --- a/gcc/cp/constraint.cc > >>>>> +++ b/gcc/cp/constraint.cc > >>>>> @@ -3870,6 +3870,9 @@ diagnose_trait_expr (tree expr, tree args) > >>>>> case CPTK_IS_VOLATILE: > >>>>> inform (loc, " %qT is not a volatile type", t1); > >>>>> break; > >>>>> + case CPTK_RANK: > >>>>> + inform (loc, " %qT cannot yield a rank", t1); > >>>>> + break; > >>>>> case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY: > >>>>> inform (loc, " %qT is not a reference that binds to a tem= porary " > >>>>> "object of type %qT (direct-initialization)", t1, t2); > >>>>> diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def > >>>>> index 2d1cb7c227c..85056c8140b 100644 > >>>>> --- a/gcc/cp/cp-trait.def > >>>>> +++ b/gcc/cp/cp-trait.def > >>>>> @@ -99,6 +99,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, > >>>>> "__is_trivially_copyable", 1) > >>>>> DEFTRAIT_EXPR (IS_UNBOUNDED_ARRAY, "__is_unbounded_array", 1) > >>>>> DEFTRAIT_EXPR (IS_UNION, "__is_union", 1) > >>>>> DEFTRAIT_EXPR (IS_VOLATILE, "__is_volatile", 1) > >>>>> +DEFTRAIT_EXPR (RANK, "__rank", 1) > >>>>> DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, > >>>>> "__reference_constructs_from_temporary", 2) > >>>>> DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, > >>>>> "__reference_converts_from_temporary", 2) > >>>>> DEFTRAIT_TYPE (REMOVE_ALL_EXTENTS, "__remove_all_extents", 1) > >>>>> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc > >>>>> index 45dc509855a..7242db75248 100644 > >>>>> --- a/gcc/cp/semantics.cc > >>>>> +++ b/gcc/cp/semantics.cc > >>>>> @@ -12550,6 +12550,9 @@ trait_expr_value (cp_trait_kind kind, tree = type1, > >>>>> tree type2) > >>>>> case CPTK_IS_DEDUCIBLE: > >>>>> return type_targs_deducible_from (type1, type2); > >>>>> + /* __rank is handled in finish_trait_expr. */ > >>>>> + case CPTK_RANK: > >>>> > >>>> This should have a gcc_unreachable. > >>>> > >>>>> + > >>>>> #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \ > >>>>> case CPTK_##CODE: > >>>>> #include "cp-trait.def" > >>>>> @@ -12622,7 +12625,10 @@ finish_trait_expr (location_t loc, cp_trai= t_kind > >>>>> kind, tree type1, tree type2) > >>>>> if (processing_template_decl) > >>>>> { > >>>>> tree trait_expr =3D make_node (TRAIT_EXPR); > >>>>> - TREE_TYPE (trait_expr) =3D boolean_type_node; > >>>>> + if (kind =3D=3D CPTK_RANK) > >>>>> + TREE_TYPE (trait_expr) =3D size_type_node; > >>>>> + else > >>>>> + TREE_TYPE (trait_expr) =3D boolean_type_node; > >>>>> TRAIT_EXPR_TYPE1 (trait_expr) =3D type1; > >>>>> TRAIT_EXPR_TYPE2 (trait_expr) =3D type2; > >>>>> TRAIT_EXPR_KIND (trait_expr) =3D kind; > >>>>> @@ -12714,6 +12720,7 @@ finish_trait_expr (location_t loc, cp_trait= _kind > >>>>> kind, tree type1, tree type2) > >>>>> case CPTK_IS_UNBOUNDED_ARRAY: > >>>>> case CPTK_IS_UNION: > >>>>> case CPTK_IS_VOLATILE: > >>>>> + case CPTK_RANK: > >>>>> break; > >>>>> case CPTK_IS_LAYOUT_COMPATIBLE: > >>>>> @@ -12745,8 +12752,18 @@ finish_trait_expr (location_t loc, cp_trai= t_kind > >>>>> kind, tree type1, tree type2) > >>>>> gcc_unreachable (); > >>>>> } > >>>>> - tree val =3D (trait_expr_value (kind, type1, type2) > >>>>> - ? boolean_true_node : boolean_false_node); > >>>>> + tree val; > >>>>> + if (kind =3D=3D CPTK_RANK) > >>>>> + { > >>>>> + size_t rank =3D 0; > >>>>> + for (; TREE_CODE (type1) =3D=3D ARRAY_TYPE; type1 =3D TREE_T= YPE (type1)) > >>>>> + ++rank; > >>>>> + val =3D build_int_cst (size_type_node, rank); > >>>>> + } > >>>>> + else > >>>>> + val =3D (trait_expr_value (kind, type1, type2) > >>>>> + ? boolean_true_node : boolean_false_node); > >>>>> + > >>>>> return maybe_wrap_with_location (val, loc); > >>>>> } > >>>>> diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C > >>>>> b/gcc/testsuite/g++.dg/ext/has-builtin-1.C > >>>>> index 3aca273aad6..7f7b27f7aa7 100644 > >>>>> --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C > >>>>> +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C > >>>>> @@ -179,6 +179,9 @@ > >>>>> #if !__has_builtin (__is_volatile) > >>>>> # error "__has_builtin (__is_volatile) failed" > >>>>> #endif > >>>>> +#if !__has_builtin (__rank) > >>>>> +# error "__has_builtin (__rank) failed" > >>>>> +#endif > >>>>> #if !__has_builtin (__reference_constructs_from_temporary) > >>>>> # error "__has_builtin (__reference_constructs_from_temporary) f= ailed" > >>>>> #endif > >>>>> diff --git a/gcc/testsuite/g++.dg/ext/rank.C > >>>>> b/gcc/testsuite/g++.dg/ext/rank.C > >>>>> new file mode 100644 > >>>>> index 00000000000..28894184387 > >>>>> --- /dev/null > >>>>> +++ b/gcc/testsuite/g++.dg/ext/rank.C > >>>>> @@ -0,0 +1,24 @@ > >>>>> +// { dg-do compile { target c++11 } } > >>>>> + > >>>>> +#include > >>>>> + > >>>>> +#define SA(X) static_assert((X),#X) > >>>>> + > >>>>> +class ClassType { }; > >>>>> + > >>>>> +SA(__rank(int) =3D=3D 0); > >>>>> +SA(__rank(int[2]) =3D=3D 1); > >>>>> +SA(__rank(int[][4]) =3D=3D 2); > >>>>> +SA(__rank(int[2][2][4][4][6][6]) =3D=3D 6); > >>>>> +SA(__rank(ClassType) =3D=3D 0); > >>>>> +SA(__rank(ClassType[2]) =3D=3D 1); > >>>>> +SA(__rank(ClassType[][4]) =3D=3D 2); > >>>>> +SA(__rank(ClassType[2][2][4][4][6][6]) =3D=3D 6); > >>>>> + > >>>>> +template void f(T) =3D delete; > >>>>> +void f(size_t); > >>>>> + > >>>>> +template > >>>>> +void g() { f(__rank(T)); } > >>>>> + > >>>>> +template void g(); > >>>> > >>>> > >>> > > >