From: "H.J. Lu" <hjl.tools@gmail.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>
Cc: Jakub Jelinek <jakub@redhat.com>,
Jason Merrill <jason@redhat.com>,
Richard Biener <richard.guenther@gmail.com>,
Markus Trippelsdorf <markus@trippelsdorf.de>
Subject: Re: PING^1: [PATCH] Add TYPE_EMPTY_RECORD for C++ empty class
Date: Wed, 27 Jan 2016 13:46:00 -0000 [thread overview]
Message-ID: <CAMe9rOqv4=QiW7USRc4hm=BE47bAX8MfK8J+8827xZkvjM8-VA@mail.gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.20.1601270953190.1966@laptop-mg.saclay.inria.fr>
On Wed, Jan 27, 2016 at 1:03 AM, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Wed, 27 Jan 2016, Jakub Jelinek wrote:
>
>> On Wed, Jan 27, 2016 at 09:10:14AM +0100, Marc Glisse wrote:
>>>>
>>>> Revised:
>>>>
>>>> /* Returns true if TYPE is POD of one-byte or less in size for the
>>>> purpose
>>>> of layout and an empty class or an class with empty classes. */
>>>>
>>>> static bool
>>>> is_empty_record (tree type)
>>>> {
>>>> if (type == error_mark_node)
>>>> return false;
>>>>
>>>> if (!CLASS_TYPE_P (type))
>>>> return false;
>>>>
>>>> if (CLASSTYPE_NON_LAYOUT_POD_P (type))
>>>> return false;
>>>>
>>>> gcc_assert (COMPLETE_TYPE_P (type));
>>>>
>>>> if (CLASSTYPE_EMPTY_P (type))
>>>> return true;
>>>>
>>>> if (int_size_in_bytes (type) > 1)
>>>> return false;
>>>
>>>
>>> That's completely arbitrary :-(
>>
>>
>> Yeah. Because (adapted to be compilable with C):
>> struct A1 {}; struct A2 {};
>> struct B1 { struct A1 a; struct A2 b; }; struct B2 { struct A1 a; struct
>> A2 b; };
>> struct C1 { struct B1 a; struct B2 b; }; struct C2 { struct B1 a; struct
>> B2 b; };
>> struct D1 { struct C1 a; struct C2 b; }; struct D2 { struct C1 a; struct
>> C2 b; };
>> struct E1 { struct D1 a; struct D2 b; }; struct E2 { struct D1 a; struct
>> D2 b; };
>> struct F1 { struct E1 a; struct E2 b; }; struct F2 { struct E1 a; struct
>> E2 b; };
>> struct G1 { struct F1 a; struct F2 b; }; struct G2 { struct F1 a; struct
>> F2 b; };
>> struct H1 { struct G1 a; struct G2 b; }; struct H2 { struct G1 a; struct
>> G2 b; };
>> struct I1 { struct H1 a; struct H2 b; }; struct I2 { struct H1 a; struct
>> H2 b; };
>> struct J1 { struct I1 a; struct I2 b; }; struct J2 { struct I1 a; struct
>> I2 b; };
>> struct K1 { struct J1 a; struct J2 b; };
>> int v;
>> __attribute__((noinline, noclone))
>> struct K1 foo (int a, struct K1 x, int b)
>> {
>> v = a + b;
>> return x;
>> }
>> struct K1 k, m;
>> void
>> bar (void)
>> {
>> m = foo (1, k, 2);
>> }
>> then would have a different calling convention between C and C++,
>> so where is the argument that we change anything just to make the two
>> compatible? Though, of course, those two will never be compatible,
>> it is enough to add struct L1 { int a; struct K1 b; int c; }; and
>> that structure has 1024+8 bytes in C++ and 8 bytes in C.
>
>
> I don't know how empty classes are used in C in practice, but it could make
> sense to have ABI compatibility as long as no empty struct is used as a
> member of another struct (I also suggested an attribute to let C++ use the
> same layout as C here: PR63579). But then the usual definition of empty
> would be sufficient.
>
>> As clang generates different code for the above between C and C++, it
>> clearly special cases for some reason just the most common case.
>> IMHO it is not worth to change GCC ABI...
>
>
> I was interested in this change because it improves C++, C compatibility was
> a convenient excuse ;-)
>
I opened a clang bug:
https://llvm.org/bugs/show_bug.cgi?id=26337
I propose the following definitions:
i. An empty record is:
1. A class without member. Or
2. An array of empty records. Or
3. A class with empty records.
ii. An empty record type for parameter passing is POD for the purpose of layout
and
1. A class without member. Or
2. A class with empty records.
/* An empty record is:
1. A class without member. Or
2. An array of empty records. Or
3. A class with empty records. */
/* Returns true if TYPE is an empty record or an array of empty records. */
static bool
is_empty_record_or_array_of_empty_record (tree type)
{
if (CLASS_TYPE_P (type))
{
if (CLASSTYPE_EMPTY_P (type))
return true;
tree field;
for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
if (TREE_CODE (field) == FIELD_DECL
&& !DECL_ARTIFICIAL (field)
&& !is_empty_record_or_array_of_empty_record (TREE_TYPE (field)))
return false;
return true;
}
else if (TREE_CODE (type) == ARRAY_TYPE)
return is_empty_record_or_array_of_empty_record (TREE_TYPE (type));
return false;
}
/* Returns true if TYPE is POD for the purpose of layout and
1. A class without member. Or
2. A class with empty records. */
static bool
is_empty_record_for_parm (tree type)
{
if (type == error_mark_node)
return false;
if (!CLASS_TYPE_P (type))
return false;
if (CLASSTYPE_NON_LAYOUT_POD_P (type))
return false;
gcc_assert (COMPLETE_TYPE_P (type));
if (CLASSTYPE_EMPTY_P (type))
return true;
tree field;
for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
if (TREE_CODE (field) == FIELD_DECL
&& !DECL_ARTIFICIAL (field)
&& !is_empty_record_or_array_of_empty_record (TREE_TYPE (field)))
return false;
return true;
}
--
H.J.
next prev parent reply other threads:[~2016-01-27 13:46 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-08 16:22 H.J. Lu
2015-12-09 14:05 ` Richard Biener
2015-12-09 18:53 ` H.J. Lu
2015-12-09 21:14 ` H.J. Lu
2015-12-09 21:31 ` Markus Trippelsdorf
2015-12-10 11:24 ` Richard Biener
2015-12-11 23:52 ` H.J. Lu
2015-12-12 14:51 ` Jason Merrill
2015-12-12 15:27 ` Jakub Jelinek
2015-12-12 16:45 ` H.J. Lu
2015-12-12 18:43 ` Marc Glisse
2015-12-14 20:16 ` Jason Merrill
2015-12-14 20:39 ` H.J. Lu
2015-12-14 20:44 ` Jason Merrill
2015-12-14 22:08 ` H.J. Lu
2016-01-26 19:27 ` Jason Merrill
2016-01-26 19:52 ` H.J. Lu
2016-01-26 20:23 ` Marc Glisse
2016-01-26 20:26 ` H.J. Lu
2016-01-26 20:44 ` Marc Glisse
2016-01-26 21:21 ` H.J. Lu
2016-01-26 21:40 ` Jakub Jelinek
2016-01-26 22:21 ` H.J. Lu
2016-01-27 8:10 ` Marc Glisse
2016-01-27 8:21 ` Jakub Jelinek
2016-01-27 9:03 ` Marc Glisse
2016-01-27 13:46 ` H.J. Lu [this message]
2016-01-27 15:39 ` H.J. Lu
2016-03-01 1:02 ` Jason Merrill
2016-03-01 22:44 ` H.J. Lu
2016-03-02 16:25 ` Ulrich Weigand
2016-03-02 17:34 ` H.J. Lu
2016-03-15 15:35 ` Jason Merrill
2016-03-15 16:00 ` H.J. Lu
2016-03-15 19:32 ` Jason Merrill
2016-03-16 12:38 ` H.J. Lu
2016-03-16 16:58 ` Jason Merrill
2016-03-16 17:02 ` H.J. Lu
2016-03-16 19:39 ` H.J. Lu
2016-03-16 19:43 ` Jason Merrill
2016-03-15 21:40 ` Joseph Myers
2016-03-15 22:31 ` H.J. Lu
2016-03-15 22:35 ` Joseph Myers
2016-03-16 0:23 ` H.J. Lu
2016-03-16 0:25 ` Joseph Myers
2016-03-16 2:17 ` H.J. Lu
2016-03-16 9:46 ` Bernhard Reutner-Fischer
2016-03-16 11:53 ` H.J. Lu
2016-03-16 2:51 ` Jason Merrill
2016-03-16 11:55 ` H.J. Lu
2016-03-16 14:33 ` Jason Merrill
2016-03-16 14:48 ` H.J. Lu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMe9rOqv4=QiW7USRc4hm=BE47bAX8MfK8J+8827xZkvjM8-VA@mail.gmail.com' \
--to=hjl.tools@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jakub@redhat.com \
--cc=jason@redhat.com \
--cc=markus@trippelsdorf.de \
--cc=richard.guenther@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).