public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jamie Lokier <jamie@shareable.org>
To: Richard Kenner <kenner@vlsi1.ultra.nyu.edu>
Cc: aoliva@redhat.com, gcc-patches@gcc.gnu.org, gcc@gcc.gnu.org
Subject: Re: DATA_ALIGNMENT vs. DECL_USER_ALIGNMENT
Date: Mon, 21 Apr 2003 18:14:00 -0000	[thread overview]
Message-ID: <20030421172537.GA15952@mail.jlokier.co.uk> (raw)
In-Reply-To: <10304211651.AA29704@vlsi1.ultra.nyu.edu>

Richard Kenner wrote:
>     So if you turn:
> 
>     T i __attribute__((align(2)));
>     T j __attribute__((align(2)));
> 
>     into
> 
>     typedef T T2 __attribute__((align(2)));
>     T2 i, j;
> 
>     you say we could get different code?  
> 
> I say so, yes.
> 
> And the reason is as I said: you specify alignment for a type both for
> interface and efficiency reasons, but for an object only for the latter.
> So there is a difference in meaning between these two constructs.

So, starting from the second (typedef) sample, if I write:

	__typeof__(i) * ptr = &i;
	use (*ptr);

That should be equivalent to:

	T2 * ptr = &i;
	use (*ptr);

The code for `use' may assume that the pointer has at least an
alignment of 2, i.e. the least significant bit of the pointer is zero?

That seems to imply that `i' must have an alignment of at least 2, if
it is declared using the typedef.

This is also true if `i' is declared with object alignment, as in the
first code sample, as `T i __attribute__ ((aligned (2)))' as well.

So what is the difference?

Robert Dewar, on behalf of Ada, seems to think that the object
alignment specification implies a _maximum_ alignment as well as a
minimum.  For fields in structures this is relevant.  But for data
variables?


I have always expected that a user-specific alignment attribute can
only increase the alignment of a type or object, not decrease it.
After all, an object with alignment 8 *is* an object with alignment 4,
for pointer dereferences and arithmetic purposes.

From a C programming point of view, I tend to think of the pointer
address constraint as the whole reason for specifying alignment.  (If
I want to control structure layout, I'll use
`__attribute__((packed))').  And, for this, any reason for a type or
object to have an alignment should only be able to _increase_ the value.

But now I see that decreasing alignment is sometimes useful, because
that influences how data is packed in memory, which is useful in some
data structures and things like variables in special sections.

It seems that in Ada, this "exact alignment" constraint is a language
requirement.  (Is this correct, Robert?)

When there is a mechanism for limiting the alignment of an object to
something below it's natural alignment, this affects the correctness
of compiler-generated code which dereferences pointers to it, and when
performing arithmetic with those pointers.dereference that type

This suggests two kinds of user alignment constraint:

	__attribute__((aligned(x)))
		- forces type or object or field to have AT LEAST alignment x

	__attribute__((exactly_aligned(x)))
		- forces type or object or field to have EXACTLY alignment x,
		  overriding natural alignment constraints.

Both of these should inherit from types to objects and fields, AND
they inherit from objects and fields back to types, when the user does
`__typeof__' or uses the address-of operator (`&' in C).

In other words, assuming that the `exactly_aligned' attribute has the
qualities that I belive Robert thinks `aligned' should have for objects:

	/* sizeof(int) == __alignof__(int) == 4. */

	typedef int T __attribute_((exactly_aligned(2)));
	int x __attribute_((exactly_aligned(2)));

	int __attribute((exactly_aligned(2))) * px1 = &x; /* Correct. */
	T * px2 = &x;	/* Correct. */
	int * px3 = &x;	/* Type error (just a warning?). */

	T y1 = *px1;	/* Fine, special code emitted by compiler. */
	T y3 = *px3;	/* Crashes due to access alignment fault... */

Whereas, if you replace each occurence of `exactly_aligned' with
`aligned' in the above, there will be no access alignment fault (or
compile time type error, whichever is implemented).

-- Jamie

  reply	other threads:[~2003-04-21 17:25 UTC|newest]

Thread overview: 101+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-21 17:25 Richard Kenner
2003-04-21 18:14 ` Jamie Lokier [this message]
2003-04-22  9:00 ` Alexandre Oliva
  -- strict thread matches above, loose matches on Subject: below --
2003-04-23  3:29 Robert Dewar
2003-04-23  2:20 Richard Kenner
2003-04-23  5:31 ` Alexandre Oliva
2003-04-24  6:23   ` Jason Merrill
2003-04-24  8:43     ` Geert Bosch
2003-04-24 17:48       ` Jason Merrill
2003-04-24 18:04         ` Zack Weinberg
2003-04-24 19:31           ` Jason Merrill
2003-04-24 21:32         ` Geert Bosch
2003-04-24 23:24           ` Jason Merrill
2003-04-25  2:56     ` Richard Henderson
2003-04-22 15:08 Richard Kenner
2003-04-23  1:07 ` Alexandre Oliva
2003-04-22  0:15 Robert Dewar
2003-04-22  1:58 ` Jamie Lokier
2003-04-22  0:11 Robert Dewar
2003-04-22  1:35 ` Jamie Lokier
2003-04-21 21:25 Richard Kenner
2003-04-21 20:59 Richard Kenner
2003-04-21 21:14 ` Jamie Lokier
2003-04-21 20:45 Richard Kenner
2003-04-21 20:58 ` Jamie Lokier
2003-04-21 18:14 Richard Kenner
2003-04-21 20:33 ` Jamie Lokier
2003-04-20 21:28 Robert Dewar
2003-04-21 17:05 ` Alexandre Oliva
2003-04-20 21:19 Robert Dewar
2003-04-18 11:41 Richard Kenner
2003-04-18  8:06 Richard Kenner
2003-04-18  8:59 ` Richard Henderson
2003-04-17 21:41 Richard Kenner
2003-04-17 23:20 ` Alexandre Oliva
2003-04-18  1:16 ` Richard Henderson
2003-04-17 21:40 Richard Kenner
2003-04-17 11:56 Richard Kenner
2003-04-17 20:35 ` Alexandre Oliva
2003-04-17 22:41   ` Geert Bosch
2003-04-17 23:19     ` Alexandre Oliva
2003-04-17 10:44 Robert Dewar
2003-04-17 20:30 ` Alexandre Oliva
2003-04-17  8:12 Richard Kenner
2003-04-17  8:38 ` Alexandre Oliva
2003-04-11 22:26 Robert Dewar
2003-04-11 16:59 Robert Dewar
2003-04-11 18:49 ` Laurent Guerby
2003-04-11 16:51 Robert Dewar
2003-04-11 14:52 Robert Dewar
2003-04-11  4:45 Richard Kenner
2003-04-17  5:39 ` Alexandre Oliva
2003-04-11  0:45 Ulrich Weigand
2003-04-10 22:43 Richard Kenner
2003-04-11  0:31 ` Richard Henderson
2003-04-11  2:19   ` David Edelsohn
2003-04-10 22:23 Richard Kenner
2003-04-10 22:16 Richard Kenner
2003-04-10 22:07 Richard Kenner
2003-04-10 22:23 ` David Edelsohn
2003-04-10 22:05 Robert Dewar
2003-04-10 22:02 Richard Kenner
2003-04-10 22:24 ` Richard Henderson
2003-04-11 11:20   ` Laurent Guerby
2003-04-11 11:57     ` Arnaud Charlet
2003-04-11 14:16       ` Laurent Guerby
2003-04-10 21:56 Richard Kenner
2003-04-10 22:02 ` David Edelsohn
2003-04-10 21:45 Richard Kenner
2003-04-10 21:52 ` David Edelsohn
2003-04-10 21:57 ` Richard Henderson
2003-04-10 21:44 Richard Kenner
2003-04-10 21:32 Robert Dewar
2003-04-10 21:32 ` David Edelsohn
2003-04-10 21:31 Richard Kenner
2003-04-10 21:29 Robert Dewar
2003-04-10 21:10 Robert Dewar
2003-04-10 21:17 ` David Edelsohn
2003-04-10 21:10 Richard Kenner
2003-04-10 21:04 Richard Kenner
2003-04-10 21:41 ` Geoff Keating
2003-04-10 20:57 Robert Dewar
2003-04-10 21:07 ` Ulrich Weigand
2003-04-10 20:52 Robert Dewar
2003-04-10 20:39 Richard Kenner
2003-04-10 21:00 ` Geoff Keating
2003-04-10 21:01 ` Ulrich Weigand
2003-04-10 21:44 ` Richard Henderson
2003-04-10 20:13 Richard Kenner
2003-04-10 20:28 ` Ulrich Weigand
2003-04-10 19:20 Richard Kenner
2003-04-10  5:33 Richard Kenner
2003-04-10  7:20 ` Ulrich Weigand
2003-04-09 23:31 Ulrich Weigand
2003-04-10  2:27 ` Richard Henderson
2003-04-10  3:10   ` Ulrich Weigand
2003-04-10 16:20     ` Ulrich Weigand
2003-04-10 16:39       ` Geoff Keating
2003-04-10 16:44         ` Ulrich Weigand
2003-04-10 18:06       ` Richard Henderson
2003-04-08 17:47 Ulrich Weigand

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=20030421172537.GA15952@mail.jlokier.co.uk \
    --to=jamie@shareable.org \
    --cc=aoliva@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc@gcc.gnu.org \
    --cc=kenner@vlsi1.ultra.nyu.edu \
    /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).