* Unaligned access to packed structs on ppc405
@ 2006-01-29 10:00 Yaro Pollak
0 siblings, 0 replies; 10+ messages in thread
From: Yaro Pollak @ 2006-01-29 10:00 UTC (permalink / raw)
To: gcc-help
Greetings,
I am cross-compiling on GCC 3.4.1 for the PowerPC 405, which can handle
unaligned accesses in hardware.
Whenever I am accessing members of a packed bit-struct by pointer, the
compiler produces byte accesses, instead of 4-byte acceses.
I've tried to use |-mstrict-align but to no avail.
Am I doing something wrong?
Thanks in advance,
Yaro
|
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Unaligned access to packed structs on ppc405
@ 2006-01-29 17:16 David Edelsohn
2006-01-30 12:03 ` Yaro Pollak
0 siblings, 1 reply; 10+ messages in thread
From: David Edelsohn @ 2006-01-29 17:16 UTC (permalink / raw)
To: Yaro Pollak; +Cc: gcc-help
>>>>> Yaro Pollak writes:
> I am cross-compiling on GCC 3.4.1 for the PowerPC 405, which can handle
> unaligned accesses in hardware.
> Whenever I am accessing members of a packed bit-struct by pointer, the
> compiler produces byte accesses, instead of 4-byte acceses.
> I've tried to use |-mstrict-align but to no avail.
What does "|-mstrict-align" mean? -mstrict-align means "do not
assume that unaligned accesses are handled by the system." You are
requesting that the compiler assume the target requires strict alignment
and then questioning why it is accessing a packed bit-struct assuming the
least alignment.
David
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Unaligned access to packed structs on ppc405
2006-01-29 17:16 David Edelsohn
@ 2006-01-30 12:03 ` Yaro Pollak
2006-02-02 16:13 ` David Edelsohn
0 siblings, 1 reply; 10+ messages in thread
From: Yaro Pollak @ 2006-01-30 12:03 UTC (permalink / raw)
To: David Edelsohn; +Cc: gcc-help
David Edelsohn wrote:
>>>>>>Yaro Pollak writes:
>>>>>>
>>>>>>
>
>
>
>>I am cross-compiling on GCC 3.4.1 for the PowerPC 405, which can handle
>>unaligned accesses in hardware.
>>Whenever I am accessing members of a packed bit-struct by pointer, the
>>compiler produces byte accesses, instead of 4-byte acceses.
>>I've tried to use |-mstrict-align but to no avail.
>>
>>
>
> What does "|-mstrict-align" mean? -mstrict-align means "do not
>assume that unaligned accesses are handled by the system." You are
>requesting that the compiler assume the target requires strict alignment
>and then questioning why it is accessing a packed bit-struct assuming the
>least alignment.
>
>David
>
>
>
Yes, I actually couldn't tell whether I should use -mno-strict-align or
-mstrict-align from the explanation in GCC documentation (thank you for
your explanation) so I tried both, but it didn't matter at all. I guess
I should have mentioned that before. Perhaps any other ideas? I feel it
is something fundamental that I keep missing.
Yaro
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Unaligned access to packed structs on ppc405
2006-01-30 12:03 ` Yaro Pollak
@ 2006-02-02 16:13 ` David Edelsohn
0 siblings, 0 replies; 10+ messages in thread
From: David Edelsohn @ 2006-02-02 16:13 UTC (permalink / raw)
To: Yaro Pollak; +Cc: gcc-help
>>>>> Yaro Pollak writes:
Yaro> Yes, I actually couldn't tell whether I should use -mno-strict-align or
Yaro> -mstrict-align from the explanation in GCC documentation (thank you for
Yaro> your explanation) so I tried both, but it didn't matter at all. I guess
Yaro> I should have mentioned that before. Perhaps any other ideas? I feel it
Yaro> is something fundamental that I keep missing.
Can you provide a small function and command-line option used to
compile the function that demonstrates the problem?
David
^ permalink raw reply [flat|nested] 10+ messages in thread
* RE: Unaligned access to packed structs on ppc405
@ 2006-01-30 16:18 Matthew Jones
2006-01-30 16:26 ` Yaro Pollak
0 siblings, 1 reply; 10+ messages in thread
From: Matthew Jones @ 2006-01-30 16:18 UTC (permalink / raw)
To: gcc-help
> From: Yaro Pollak
>
> Greetings,
> I am cross-compiling on GCC 3.4.1 for the PowerPC 405,
> which can handle unaligned accesses in hardware.
> Whenever I am accessing members of a packed bit-struct by
> pointer, the compiler produces byte accesses, instead of
> 4-byte acceses. I've tried to use |-mstrict-align but to
> no avail. Am I doing something wrong?
Are you sure it is the compiler? Are you looking at the generated
assembler, or inferring this from a logic analyser? If its the
second case, it could be the uP rather than gcc.
--
Matthew JONES
http://www.tandbergtv.com/
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Unaligned access to packed structs on ppc405
2006-01-30 16:18 Matthew Jones
@ 2006-01-30 16:26 ` Yaro Pollak
0 siblings, 0 replies; 10+ messages in thread
From: Yaro Pollak @ 2006-01-30 16:26 UTC (permalink / raw)
To: Matthew Jones; +Cc: gcc-help
Matthew Jones wrote:
>>From: Yaro Pollak
>>
>>Greetings,
>>I am cross-compiling on GCC 3.4.1 for the PowerPC 405,
>>which can handle unaligned accesses in hardware.
>>Whenever I am accessing members of a packed bit-struct by
>>pointer, the compiler produces byte accesses, instead of
>>4-byte acceses. I've tried to use |-mstrict-align but to
>>no avail. Am I doing something wrong?
>>
>>
>
>Are you sure it is the compiler? Are you looking at the generated
>assembler, or inferring this from a logic analyser? If its the
>second case, it could be the uP rather than gcc.
>
>
>
I'm sorry I failed to mention that as well, but I am in fact looking at
the generated assembler, vs. the assembler generated when I am accessing
the members of the struct directly (not through a pointer).
Yaro
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Re: Unaligned access to packed structs on ppc405]
@ 2006-02-01 12:03 Yaro Pollak
2006-02-04 4:25 ` Unaligned access to packed structs on ppc405 David Edelsohn
0 siblings, 1 reply; 10+ messages in thread
From: Yaro Pollak @ 2006-02-01 12:03 UTC (permalink / raw)
To: gcc-help
> Yaro Pollak writes:
> I am cross-compiling on GCC 3.4.1 for the PowerPC 405, which can handle
> unaligned accesses in hardware.
> Whenever I am accessing members of a packed bit-struct by pointer, the
> compiler produces byte accesses, instead of 4-byte accesses.
> I've tried to use -mstrict-align but to no avail.
Hi everyone,
I still didn't resolve this problem, and frankly, I am lost.
I was using GCC 3.4.1, so I've compiled GCC 4.0.1 and tried to compile the code with it, and the results were exactly the same.
I've tried to use -mno-strict-align and -mstrict-align and there was no effect on the assembly produced.
The code that I'm trying to compile is
struct Test {
unsigned b : 4;
unsigned c : 8;
unsigned d : 6;
unsigned e : 3;
unsigned f : 7;
unsigned g : 6;
unsigned h : 2;
}__attribute__((__packed__));
int main (void) {
Test* pt = (Test*)1234;
return pt->b + pt->c + pt->d;
}
and the resulting assembly from g++ -mcpu=405 -O3 - S -c -mno-strict-align looks like:
li 9,1234
lbz 11,0(9)
lbz 10,1(9)
rlwinm 8,11,4,24,27
srwi 0,10,4
lbz 3,2(9)
or 0,0,8
rlwinm 11,11,28,28,31
add 11,11,0
rlwinm 10,10,2,26,29
lwz 0,12(1)
srwi 3,3,6
or 3,3,10
add 3,11,3
mtlr 0
addi 1,1,8
blr
Note the 3 lbz.
If on the other hand I compile:
Test pt;
return pt.b + pt.c + pt.d;
The assembly looks like this:
lwz 3,8(1)
rlwinm 0,3,4,28,31
rlwinm 9,3,12,24,31
add 0,0,9
rlwinm 3,3,18,26,31
add 3,0,3
lwz 0,28(1)
addi 1,1,24
mtlr 0
blr
Does anyone have any idea about this? This is a rather trivial problem, I MUST be doing something wrong, please help me.
Thanks,
Yaro
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Unaligned access to packed structs on ppc405
2006-02-01 12:03 [Re: Unaligned access to packed structs on ppc405] Yaro Pollak
@ 2006-02-04 4:25 ` David Edelsohn
0 siblings, 0 replies; 10+ messages in thread
From: David Edelsohn @ 2006-02-04 4:25 UTC (permalink / raw)
To: Yaro Pollak; +Cc: gcc-help
>>>>> Yaro Pollak writes:
> If on the other hand I compile:
>
> Test pt;
> return pt.b + pt.c + pt.d;
>
> lwz 3,8(1)
That is an artifact of the example. If I declare "pt" as a global
variable, GCC 4.1 prerelease produces three lbz.
The lbz has to do with the size and the packed alignment. With
the packed structure, GCC chooses the smallest memory access that covers
the bitfield. Once GCC has chosen bytes, it cannot merge the loads
together. If the structure were not declared packed, GCC would use wider
loads with masking, and then determine that the loads refer to the same
object.
David
^ permalink raw reply [flat|nested] 10+ messages in thread
* RE: Unaligned access to packed structs on ppc405
@ 2006-02-05 18:35 John Yates
2006-02-05 21:02 ` David Edelsohn
0 siblings, 1 reply; 10+ messages in thread
From: John Yates @ 2006-02-05 18:35 UTC (permalink / raw)
To: David Edelsohn, Yaro Pollak; +Cc: gcc-help
David,
Do I read this correctly? Are you truly saying that two structs
with identical layout will trigger different code sequences just
because one was declared packed?
(This is just the sort of thing that drives me crazy when trying
to coax a compiler into producing good code.)
/john
-----Original Message-----
From: David Edelsohn [mailto:dje@watson.ibm.com]
Sent: Friday, February 03, 2006 11:26 PM
To: Yaro Pollak
Cc: gcc-help@gcc.gnu.org
Subject: Re: Unaligned access to packed structs on ppc405
>>>>> Yaro Pollak writes:
> If on the other hand I compile:
>
> Test pt;
> return pt.b + pt.c + pt.d;
>
> lwz 3,8(1)
That is an artifact of the example. If I declare "pt" as a global
variable, GCC 4.1 prerelease produces three lbz.
The lbz has to do with the size and the packed alignment. With
the packed structure, GCC chooses the smallest memory access that covers
the bitfield. Once GCC has chosen bytes, it cannot merge the loads
together. If the structure were not declared packed, GCC would use wider
loads with masking, and then determine that the loads refer to the same
object.
David
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Unaligned access to packed structs on ppc405
2006-02-05 18:35 John Yates
@ 2006-02-05 21:02 ` David Edelsohn
2006-02-06 10:18 ` Yaro Pollak
0 siblings, 1 reply; 10+ messages in thread
From: David Edelsohn @ 2006-02-05 21:02 UTC (permalink / raw)
To: John Yates; +Cc: Yaro Pollak, gcc-help
>>>>> John Yates writes:
John> Do I read this correctly? Are you truly saying that two structs
John> with identical layout will trigger different code sequences just
John> because one was declared packed?
Yes. Why is that strange? attribute packed assigns the smallest
possible alignment so that the compiler composes the layout of the
structure or bitfield in the more compact form possible. Even if the
layout produced is the same, the smaller alignment is carried around with
the fields and causes the compiler to use more conservative access
operations.
David
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Unaligned access to packed structs on ppc405
2006-02-05 21:02 ` David Edelsohn
@ 2006-02-06 10:18 ` Yaro Pollak
0 siblings, 0 replies; 10+ messages in thread
From: Yaro Pollak @ 2006-02-06 10:18 UTC (permalink / raw)
To: David Edelsohn, gcc-help
What seems odd to me is that packed structures accesses are inherently
less efficient than non-packed structures.
In my example, the 3 lbz instructions instead of one lwz require 3
memory accesses instead of 1, that is a penalty of 2 extra memory access
over the slow bus, and in addition to that there is extra penalty when
the bit field overlaps byte boundary (as in my example), where GCC must
generate extra code to "or" those bytes, which, BTW, in my opinion
contradicts what you wrote earlier:
David Edelsohn wrote:
" The lbz has to do with the size and the packed alignment. With the
packed structure, GCC chooses the smallest memory access that covers the
bitfield. Once GCC has chosen bytes, it cannot merge the loads together.
If the structure were not declared packed, GCC would use wider loads
with masking, and then determine that the loads refer to the same object."
In my case it shouldn't have chosen byte because it doesn't cover the bitfield that spans over byte boundary. I don't know whether what GCC does is "Right", and I guess if it was implemented in 4.1 somebody decided that it was "Right", but, if the code generated is 3 times the instruction count, and 3 times the memory accesses, for no apparent reason, then I can't see any reason why anyone would want this behavior. I mean the code produced in 4.0.1 for the same structure accessed not through a pointer is just fine, why break it like that? Something just doesn't seem right, I'm sorry.
I think I can summarize it by saying that if it's less efficient then
there is no justification for it.
Yaro
David Edelsohn wrote:
>>>>>> John Yates writes:
>>>>>>
>
> John> Do I read this correctly? Are you truly saying that two structs
> John> with identical layout will trigger different code sequences just
> John> because one was declared packed?
>
> Yes. Why is that strange? attribute packed assigns the smallest
> possible alignment so that the compiler composes the layout of the
> structure or bitfield in the more compact form possible. Even if the
> layout produced is the same, the smaller alignment is carried around with
> the fields and causes the compiler to use more conservative access
> operations.
>
> David
>
>
>
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2006-02-06 10:18 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-29 10:00 Unaligned access to packed structs on ppc405 Yaro Pollak
2006-01-29 17:16 David Edelsohn
2006-01-30 12:03 ` Yaro Pollak
2006-02-02 16:13 ` David Edelsohn
2006-01-30 16:18 Matthew Jones
2006-01-30 16:26 ` Yaro Pollak
2006-02-01 12:03 [Re: Unaligned access to packed structs on ppc405] Yaro Pollak
2006-02-04 4:25 ` Unaligned access to packed structs on ppc405 David Edelsohn
2006-02-05 18:35 John Yates
2006-02-05 21:02 ` David Edelsohn
2006-02-06 10:18 ` Yaro Pollak
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).