* Getting rid of Unaligned Accesses (UA)
@ 2008-05-22 18:53 Jay.Estabrook
2008-05-22 19:10 ` macro
0 siblings, 1 reply; 6+ messages in thread
From: Jay.Estabrook @ 2008-05-22 18:53 UTC (permalink / raw)
Hi, Alpha folk,
I hope there's someone(s) out there that might have comments to offer
on how best to coerce the GCC compiler into generating the code WE want,
rather than what GCC wants.
To fix an unaligned access (UA) problem in "dbus", a union pointer
address which was not always aligned (gcc assumes they are, by default,
it seems), I changed something like the following, which appears in the
file: dbus-marshal-basic.c:dbus_marshal_read_basic():
vp->byt = _dbus_get_byte(str, pos);
to:
*((char*)&vp->byt) = _dbus_get_byte(str, pos);
Under GCC-4.1.2-23, this generated code that did the appropriate things
to prevent a UA.
HOWEVER, when I built it under GCC-4.1.2-37, it reverted to the original
bad code (always assuming alignment). I had to change it yet again, to
the following:
*((volatile char*)&vp->byt) = _dbus_get_byte(str, pos);
to get it to produce the good code again.
So, my question: is the final version (using "volatile") the only real
way to guarantee good code in this (and similar) instance?
Or is there another more (or less) obvious way to do so?
I certainly didn't expect the behavior to change like this between
releases and not major/minor versions... :-\
Thanks for any insights,
--Jay++
^ permalink raw reply [flat|nested] 6+ messages in thread
* Getting rid of Unaligned Accesses (UA)
2008-05-22 18:53 Getting rid of Unaligned Accesses (UA) Jay.Estabrook
@ 2008-05-22 19:10 ` macro
2008-05-22 19:13 ` Jay.Estabrook
0 siblings, 1 reply; 6+ messages in thread
From: macro @ 2008-05-22 19:10 UTC (permalink / raw)
On Thu, 22 May 2008, Jay Estabrook wrote:
> I hope there's someone(s) out there that might have comments to offer
> on how best to coerce the GCC compiler into generating the code WE want,
> rather than what GCC wants.
It might be a good question to ask at a GCC list.
> To fix an unaligned access (UA) problem in "dbus", a union pointer
> address which was not always aligned (gcc assumes they are, by default,
> it seems), I changed something like the following, which appears in the
> file: dbus-marshal-basic.c:dbus_marshal_read_basic():
>
> vp->byt = _dbus_get_byte(str, pos);
> to:
> *((char*)&vp->byt) = _dbus_get_byte(str, pos);
How's vp defined?
> Under GCC-4.1.2-23, this generated code that did the appropriate things
> to prevent a UA.
>
> HOWEVER, when I built it under GCC-4.1.2-37, it reverted to the original
> bad code (always assuming alignment). I had to change it yet again, to
> the following:
>
> *((volatile char*)&vp->byt) = _dbus_get_byte(str, pos);
>
> to get it to produce the good code again.
>
> So, my question: is the final version (using "volatile") the only real
> way to guarantee good code in this (and similar) instance?
>
> Or is there another more (or less) obvious way to do so?
Well, __attribute__((packed)) used to be the usual way to ask GCC to
assume the worst when it comes to alignment while keeping the data size
intact. It has worked for years and if it got broken at any point, then
it is a bug in GCC.
> I certainly didn't expect the behavior to change like this between
> releases and not major/minor versions... :-\
Well, from time to time distributors add their own breakage on top of
what's there in the respective pieces of software already.
Maciej
^ permalink raw reply [flat|nested] 6+ messages in thread
* Getting rid of Unaligned Accesses (UA)
2008-05-22 19:10 ` macro
@ 2008-05-22 19:13 ` Jay.Estabrook
2008-05-22 20:24 ` macro
0 siblings, 1 reply; 6+ messages in thread
From: Jay.Estabrook @ 2008-05-22 19:13 UTC (permalink / raw)
Maciej W. Rozycki wrote:
> On Thu, 22 May 2008, Jay Estabrook wrote:
>
>> I hope there's someone(s) out there that might have comments to offer
>> on how best to coerce the GCC compiler into generating the code WE want,
>> rather than what GCC wants.
>
> It might be a good question to ask at a GCC list.
Well, I tried asking one Alpha GCC guru, but never got a reply; I've
been reluctant to try them all, so I tried here first... ;-}
>> To fix an unaligned access (UA) problem in "dbus", a union pointer
>> address which was not always aligned (gcc assumes they are, by default,
>> it seems), I changed something like the following, which appears in the
>> file: dbus-marshal-basic.c:dbus_marshal_read_basic():
>>
>> vp->byt = _dbus_get_byte(str, pos);
>> to:
>> *((char*)&vp->byt) = _dbus_get_byte(str, pos);
>
> How's vp defined?
Pointer to a UNION of different datatypes, so the pointer may be asked
to point to anything from a char to a double.
>> Under GCC-4.1.2-23, this generated code that did the appropriate things
>> to prevent a UA.
>>
>> HOWEVER, when I built it under GCC-4.1.2-37, it reverted to the original
>> bad code (always assuming alignment). I had to change it yet again, to
>> the following:
>>
>> *((volatile char*)&vp->byt) = _dbus_get_byte(str, pos);
>>
>> to get it to produce the good code again.
>>
>> So, my question: is the final version (using "volatile") the only real
>> way to guarantee good code in this (and similar) instance?
>>
>> Or is there another more (or less) obvious way to do so?
>
> Well, __attribute__((packed)) used to be the usual way to ask GCC to
> assume the worst when it comes to alignment while keeping the data size
> intact. It has worked for years and if it got broken at any point, then
> it is a bug in GCC.
Doesn't the "packed" normally get applied to a struct?
In this case, since it's a UNION, what would "packed' mean?
>> I certainly didn't expect the behavior to change like this between
>> releases and not major/minor versions... :-\
>
> Well, from time to time distributors add their own breakage on top of
> what's there in the respective pieces of software already.
Understood; the GCC versions mentioned above were released during
the normal Fedora-8 ship (-23) and update (-37) cycles.
Thanks.
--Jay++
^ permalink raw reply [flat|nested] 6+ messages in thread
* Getting rid of Unaligned Accesses (UA)
2008-05-22 19:13 ` Jay.Estabrook
@ 2008-05-22 20:24 ` macro
2008-05-22 22:30 ` netbeans
0 siblings, 1 reply; 6+ messages in thread
From: macro @ 2008-05-22 20:24 UTC (permalink / raw)
On Thu, 22 May 2008, Jay Estabrook wrote:
> > It might be a good question to ask at a GCC list.
>
> Well, I tried asking one Alpha GCC guru, but never got a reply; I've
> been reluctant to try them all, so I tried here first... ;-}
This is what the list is for -- if somebody feels like willing to
investigate the problem, they will reply. At the very worst you will be
ignored, which may be annoying, but not exactly a disaster either (it
happened for me in the past too, yet I am still alive :-) ).
> > How's vp defined?
>
> Pointer to a UNION of different datatypes, so the pointer may be asked
> to point to anything from a char to a double.
Then GCC is allowed to assume the alignment of the data pointed to will
be that of the member of the union requiring the largest. And will
eagerly do so. ;-)
> > Well, __attribute__((packed)) used to be the usual way to ask GCC to
> > assume the worst when it comes to alignment while keeping the data size
> > intact. It has worked for years and if it got broken at any point, then
> > it is a bug in GCC.
>
> Doesn't the "packed" normally get applied to a struct?
Both struct and union definitions.
> In this case, since it's a UNION, what would "packed' mean?
With such a definition in scope:
union { int i; } __attribute__((packed)) *foo;
accesses to foo->i will be treated as unaligned by GCC. Verified to work
correctly with my Alpha/Linux-targeted cross-compiler which is 4.1.2 too,
except self-built, as you can imagine. ;-)
Maciej
^ permalink raw reply [flat|nested] 6+ messages in thread
* Getting rid of Unaligned Accesses (UA)
2008-05-22 20:24 ` macro
@ 2008-05-22 22:30 ` netbeans
2008-05-23 13:54 ` Jay.Estabrook
0 siblings, 1 reply; 6+ messages in thread
From: netbeans @ 2008-05-22 22:30 UTC (permalink / raw)
I suppose I should ask, why do u care?
early cpu's could only store/fetch certain types ( int's/long's ) on
aligned boundaries. fetching a byte, storing a byte, required an
int/long fetch, and shifting.
Overcoming such a problem, would mean expanding the byte storage into 4
byte, aligned on int boundaries. For structs, there is/was a pragma to
force byte alignment, short alignment on int boundaries. You pay for
this by wasted memory space.
I think the 21264 cpu's can fetch/store bytes anywhere without faulting
to external fixup routines.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Getting rid of Unaligned Accesses (UA)
2008-05-22 22:30 ` netbeans
@ 2008-05-23 13:54 ` Jay.Estabrook
0 siblings, 0 replies; 6+ messages in thread
From: Jay.Estabrook @ 2008-05-23 13:54 UTC (permalink / raw)
U. George wrote:
> I suppose I should ask, why do u care?
The example I initially gave was perhaps not the best to show the
reason why eliminating UAs is a goodness.
Recently, I happened to try printing a webpage from
firefox; the ghostscript interpreter was used to render it for the
print job. It seemed to take a really long time for something that
was almost entirely text.
When I could, I looked at /proc/cpuinfo before and after the render;
it indicated that over 200 MILLION UAs had occurred during that single
operation. When I fixed it, the render time dropped from more than
30 seconds to only a second or two.
Was that not enough to care? :-)
> early cpu's could only store/fetch certain types ( int's/long's ) on
> aligned boundaries. fetching a byte, storing a byte, required an
> int/long fetch, and shifting.
In fact, the later CPUs have the same restriction, they just support
more data types, like shorts and bytes, and their lesser requirements.
> Overcoming such a problem, would mean expanding the byte storage into 4
> byte, aligned on int boundaries. For structs, there is/was a pragma to
> force byte alignment, short alignment on int boundaries. You pay for
> this by wasted memory space.
And in fact, that's the default for structs - fields aligned on natural
boundary offsets from the start of the struct. Only by adding the pragma
to pack it will that not be done.
And in most cases, the defaults work well for generating generic Alpha
code. It's when the code does tricks, or the programmer doesn't realize
the consequences of a choice, that a price must be paid on Alpha.
> I think the 21264 cpu's can fetch/store bytes anywhere without faulting
> to external fixup routines.
Indeed, IFF the compiler is told to generate those instructions when
needed, via "-mcpu=ev6" or such switch to GCC. But if it's NOT told,
so that the resulting code generated can/will run on ALL Alpha CPUs, it
does not have that flexibility.
--Jay++
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-05-23 13:54 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-22 18:53 Getting rid of Unaligned Accesses (UA) Jay.Estabrook
2008-05-22 19:10 ` macro
2008-05-22 19:13 ` Jay.Estabrook
2008-05-22 20:24 ` macro
2008-05-22 22:30 ` netbeans
2008-05-23 13:54 ` Jay.Estabrook
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox