From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ua1-x929.google.com (mail-ua1-x929.google.com [IPv6:2607:f8b0:4864:20::929]) by sourceware.org (Postfix) with ESMTPS id 938B63858C53 for ; Fri, 16 Feb 2024 02:22:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 938B63858C53 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 938B63858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::929 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708050160; cv=none; b=Mk1sN2HIFCdXC2NSCcsoxApomFBveKAON+UVvlhAUPmvnUa7r2Q+BWAslbTuICLt+PEBSS76VnW/wHQCrZdfCyWBco283N/gB9Aj744wzc6mTEL34PWlksLsp4dO+X0OQcKWvLNoHXm0BDepwNUUTWkW6AsyqaXoK1jr9BlvQCM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708050160; c=relaxed/simple; bh=I5T9QcusWU6Hws1e0SS94abDbn7ND7KkI08/l0SK+zc=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=lPCc6QZxopJfUC1lTnNcuM7rd6iNejX8vSPGVe8cD2KLMpyg07U+N2iBaShMOedpVe3R4lNBGVOfQv382VRkrs0ecBYxcUmAx7kcosUlmuzAkrCdNA2YdbEULsH5So1HS0cI7Ht/Co9EGZflenWZLwb2uB6IbWCKw24XWZcaIlY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ua1-x929.google.com with SMTP id a1e0cc1a2514c-7cedcea89a0so960157241.1 for ; Thu, 15 Feb 2024 18:22:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cs.washington.edu; s=goo201206; t=1708050157; x=1708654957; 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=o3OVpwTsRClweFMaQszGZ1+MCNWpcdtCDQg99Zfowas=; b=CX7r7Xnyn+tUsVVlqMO05rdvSwqvcgGDJoWATt2G0/9kBtZKc/3nnyXS75aljnhnE/ 2vpJA2/vs0TPWCqZ9scD1vslgBVbtPyC2KStVi3ykHztc+pS8hgq8xSpoZC8U5vBqS1V XtN0HQRXkNNgad7eXRVb2OIrlnP+t+bYj6VAk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708050157; x=1708654957; 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=o3OVpwTsRClweFMaQszGZ1+MCNWpcdtCDQg99Zfowas=; b=T6BPpLAwOjD1GqZ9vsbhlzXBzrkiPvIl1NXX82sSeYo6ZK9+hWQ0BFE/E696hRq37z HBHjMFYSAnXodXJEB+8jVrNn1CKP0uGE8qypCN9ft6/44ThfXS0tvhpjTng/FJTR52BW 4fj3d8/DtWfSg4KOVWuemvEVyE2bxg54QEVkMwCOdkVhxduDVxlSgTIbbD0cji3DGXCs 3qgqKps1AnFh8jNN/gipmcu6Z6dcCf0soX4qPz6qbtgpsgw0KLYm8H4653rw9v2j+1To S4Hw+i3R8SW/iYWkrZBMptZmziBxLIAPHOOinV9+5cX+tQRtAinRsQNDqcWkNxbhxIQG /7HQ== X-Forwarded-Encrypted: i=1; AJvYcCUuX0l3inSJEahKJesxtPy61TUCADexVKZM+eznI3oB+wQlKG3mXyBgW9kasMhiEWIovqZS3gKNBWw7w2RqlqOXZ03PQR0= X-Gm-Message-State: AOJu0YzU/k30HuhPKPBqjeYeCaLRVcvAbmQ9+U2AtcPBG9vGx+FhMVI0 Vvy55RuGpFp8RjAR16TS1kDhCHwMmXYwwgMur/g2jn6Ffwm+tlh34UHOh7BN4dp2dZMMffgKhlU EYmu1G6Oa033HceemLFnK95yLc09IFWO+zuST X-Google-Smtp-Source: AGHT+IHkJpP1ek5RrqH0HalMAUgzGMQQFLSOFSzWWj8FY5I1ZxvkHxLuzWPRt4RqLb6nYppKtLoaFYjIe+hedCNrJOs= X-Received: by 2002:a1f:df04:0:b0:4c0:240b:89de with SMTP id w4-20020a1fdf04000000b004c0240b89demr3672691vkg.7.1708050156829; Thu, 15 Feb 2024 18:22:36 -0800 (PST) MIME-Version: 1.0 References: <20240215045258.3025806-1-kmatsui@gcc.gnu.org> <20240215152037.720162-1-kmatsui@gcc.gnu.org> <20240215152037.720162-13-kmatsui@gcc.gnu.org> <4c427657-fdac-6d2c-bd99-9480e33554e2@idea> In-Reply-To: From: Ken Matsui Date: Thu, 15 Feb 2024 18:22:01 -0800 Message-ID: Subject: Re: [PATCH v5 13/14] c++: Implement __rank built-in trait To: Patrick Palka Cc: 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.7 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,T_SCC_BODY_TEXT_LINE 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, Feb 15, 2024 at 6:14=E2=80=AFPM Ken Matsui wrote: > > On Thu, Feb 15, 2024 at 1:48=E2=80=AFPM Patrick Palka = wrote: > > > > On Thu, 15 Feb 2024, Ken Matsui wrote: > > > > > This patch implements built-in trait for std::rank. > > > > > > gcc/cp/ChangeLog: > > > > > > * cp-trait.def: Define __rank. > > > * semantics.cc (trait_expr_value): Handle CPTK_RANK. > > > (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/cp-trait.def | 1 + > > > gcc/cp/semantics.cc | 18 ++++++++++++++++-- > > > gcc/testsuite/g++.dg/ext/has-builtin-1.C | 3 +++ > > > gcc/testsuite/g++.dg/ext/rank.C | 14 ++++++++++++++ > > > 4 files changed, 34 insertions(+), 2 deletions(-) > > > create mode 100644 gcc/testsuite/g++.dg/ext/rank.C > > > > > > diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def > > > index 11270f3ae6b..3d5a7970563 100644 > > > --- a/gcc/cp/cp-trait.def > > > +++ b/gcc/cp/cp-trait.def > > > @@ -95,6 +95,7 @@ DEFTRAIT_EXPR (IS_TRIVIALLY_ASSIGNABLE, "__is_trivi= ally_assignable", 2) > > > DEFTRAIT_EXPR (IS_TRIVIALLY_CONSTRUCTIBLE, "__is_trivially_construct= ible", -1) > > > DEFTRAIT_EXPR (IS_TRIVIALLY_COPYABLE, "__is_trivially_copyable", 1) > > > DEFTRAIT_EXPR (IS_UNION, "__is_union", 1) > > > +DEFTRAIT_EXPR (RANK, "__rank", 1) > > > DEFTRAIT_EXPR (REF_CONSTRUCTS_FROM_TEMPORARY, "__reference_construct= s_from_temporary", 2) > > > DEFTRAIT_EXPR (REF_CONVERTS_FROM_TEMPORARY, "__reference_converts_fr= om_temporary", 2) > > > DEFTRAIT_TYPE (REMOVE_ALL_EXTENTS, "__remove_all_extents", 1) > > > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc > > > index 256e7ef8166..4f285909b83 100644 > > > --- a/gcc/cp/semantics.cc > > > +++ b/gcc/cp/semantics.cc > > > @@ -12538,6 +12538,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: > > > + > > > #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \ > > > case CPTK_##CODE: > > > #include "cp-trait.def" > > > @@ -12698,6 +12701,7 @@ finish_trait_expr (location_t loc, cp_trait_k= ind kind, tree type1, tree type2) > > > case CPTK_IS_SAME: > > > case CPTK_IS_SCOPED_ENUM: > > > case CPTK_IS_UNION: > > > + case CPTK_RANK: > > > break; > > > > > > case CPTK_IS_LAYOUT_COMPATIBLE: > > > @@ -12729,8 +12733,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); > > > > So this will be the first expression-yielding trait that's not a bool. > > That's no problem conceptually, but I think we hardcode their bool-ness > > near the top of finish_trait_expr when returning a templated version of > > the trait. We should instead give templated __rank the type size_type_= node. > > > > > + } > > > + 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 5b590db1cf6..a00193c1a81 100644 > > > --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C > > > +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C > > > @@ -167,6 +167,9 @@ > > > #if !__has_builtin (__is_union) > > > # error "__has_builtin (__is_union) 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) faile= d" > > > #endif > > > diff --git a/gcc/testsuite/g++.dg/ext/rank.C b/gcc/testsuite/g++.dg/e= xt/rank.C > > > new file mode 100644 > > > index 00000000000..bab062d776e > > > --- /dev/null > > > +++ b/gcc/testsuite/g++.dg/ext/rank.C > > > @@ -0,0 +1,14 @@ > > > +// { dg-do compile { target c++11 } } > > > + > > > +#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); > > > > We should have a test that the __rank inside a template has the correct > > type, something like (this should currently fail with your patch as-is > > due to the hardcoded bool type): > > > > template void f(T) =3D delete; > > void f(bool); > > > > template > > void g() { f(__rank(T)); } > > > > template void g(); > > > > Thank you! I think this test should be the following, to fail with my > patch as-is: > > - void f(bool); > + void f(size_t); > > since we are expecting size_t from __rank. The current patch passed > this test and failed with a test with change, and my updated patch > failed with this test and passed a test with this change. > Sorry, the wording was a bit confusing. Just in case, here is what I meant= . v5 passed yours (expect bool) failed with this change (expect size_t) v6 failed with yours (expect bool) passed this change (expect size_t) Again, thank you for your review, Patrick! > I will submit the updated patch soon. Thank you for pointing this out! > > > > -- > > > 2.43.0 > > > > > > > >