From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb31.google.com (mail-yb1-xb31.google.com [IPv6:2607:f8b0:4864:20::b31]) by sourceware.org (Postfix) with ESMTPS id 38C13385840A for ; Thu, 2 May 2024 15:35:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 38C13385840A 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 38C13385840A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b31 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714664117; cv=none; b=lppm9N5B/VwAhRIvBKgmT1RkMFt6gujU9a49M7j5o4KPaagYI4KFYztYwT4/s3R9HjCmMgH1YFG0EgOcbaPjx/j+WjYdwkfmBxoE2JKYaLdRVXblkZ2K0McsIN/gKPhjoAW3on02xAP7MFUozFTwaqjwZl2c1PxdC04GG/Nw+z8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714664117; c=relaxed/simple; bh=Aa5zx2uU+TikNAeLFJZyrV1QE8AVICGnzP4y29j2oic=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=GvWq+3Phf9N2GhOtzJxpdTLL/+ts05zbZqQfDN985rmad2lOhRFCzO2n0xoqrGQb8Aile/Pr+KUwMcIGmR/I4LotjVMyx5oOggrGLR9H9xy2gXu0iDGqdU9iB94i4JA85ssglcI/VgHpsd+pBt+crKgccY4PJy2niAN3Exnh8IM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb31.google.com with SMTP id 3f1490d57ef6-de477949644so9092756276.1 for ; Thu, 02 May 2024 08:35:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; t=1714664113; x=1715268913; 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=bj7c/HZdeloB1kcbiQ2GvB8xWMMR4G6s6GchNFPUNNg=; b=AFISXMuNtAZoIa5tbYsJ/dWIUdsGJmjTA0ybvMQ+NEPh/YhYFxY74tCJyfo7IO0/PY Nv5KB+i8mktrfuEANPKnPwCNol0EsWcDgwDPKy9zBXZSxBli2iSgyyKK5v+dLpsebZBT z8NdsvGsKB05+a2Rs/zg5OmpkN2VFKm0LZkuc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714664113; x=1715268913; 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=bj7c/HZdeloB1kcbiQ2GvB8xWMMR4G6s6GchNFPUNNg=; b=GBYITJa2vxutRu4YIsSsUTO4128ct6aftRaNAMmg4raUoYEQvLZHXLUpK6y55h6iSg AbtA1OanMYAyrrh6SlumAi9cVyc2ixlp77uSSJTNuWUXBBqPxg0+irWne6Q0Xwnioh/N KCfyRUJC5nseXpvPy0OQy8HL16tTYx3vBnLhZcxdQ61OOE0iF+V3RDV1zQUEIXTtfhXj Z6cxOnfEcrKqQsBZdBH3uBgJOAwidCkF7NJKrqMNo+yc01p83YFS5onpblDoPwb72GZA iQP+saqzXxEqP2PUkSqNdCeRxkN1pPRLCdP+Nd9VS0tKdRVq/QFTWedolhVgQAVsOpmo HP0Q== X-Forwarded-Encrypted: i=1; AJvYcCX3ySXAOL1k4eMxYGwSEXKpmD9GTYe+3T4YS4nl4LMN964giK9LMSmzhObARsO8iz1VClhbWJL12DyLcuKGk3L17s73X6Q= X-Gm-Message-State: AOJu0Yz5GvO2T5DLeCldjs6i9I8Hna5OWXDm26SDenJK6gvQ0o13/u5F ODR4a9Ok01nazcBx5HdTSomlDOX/cnlNXtVbkShjVgWl9brV8XuFbK/4c7SeZEtqVLpcfrV9Y9U 1ddOln6WST9bgJCm6CHKAH3ObaRl8q9wHmCtG X-Google-Smtp-Source: AGHT+IHYgcx+6RcFl7pcyR7V8bSzhbzSGkQuQxKa3ZFD5ggJWpw/atoNf3tNlR6qLXWhWo/sULjSrTB93akDFAhNyPE= X-Received: by 2002:a5b:f8c:0:b0:de4:680c:1a16 with SMTP id q12-20020a5b0f8c000000b00de4680c1a16mr191688ybh.50.1714664113565; Thu, 02 May 2024 08:35:13 -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> In-Reply-To: From: Ken Matsui Date: Thu, 2 May 2024 08:34:37 -0700 Message-ID: Subject: Re: [PATCH v14 21/26] c++: Implement __rank built-in trait To: Patrick Palka Cc: Jason Merrill , 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=-9.9 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 8:16=E2=80=AFAM Patrick Palka wr= ote: > > 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 __bui= ltin to > > all of these rather than just __ and the library trait name. I guess i= t's too > > late to do that for the GCC 14 traits, but we could do it for this grou= p? > > Clang already implements many of these built-in, without using > "__builtin" in their name. Shouldn't we be consistent with Clang where > we can? 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/4aca302f5a82ee65847c88500b39a2530= dfeceb4/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 tempor= ary " > > > "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 ty= pe1, > > > 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_trait_= 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_k= ind > > > 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_trait_= 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_TYP= E (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) fail= ed" > > > #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(); > > > > >