public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/40693] New: atomic built-ins malfunction with 64-bit and optimization
@ 2009-07-08 23:10 m dot rosellini at f5 dot com
2009-07-09 0:10 ` [Bug c/40693] " m dot rosellini at f5 dot com
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: m dot rosellini at f5 dot com @ 2009-07-08 23:10 UTC (permalink / raw)
To: gcc-bugs
#include <stdint.h>
#include <stdio.h>
int
main(int ac, char *av[])
{
int64_t x;
int64_t like_a_constant = -1;
int64_t unlike_a_constant = -1;
if (ac == 0) unlike_a_constant = 5;
x = 0;
__sync_add_and_fetch(&x, like_a_constant);
printf("%016llx\n", x);
x = 0;
__sync_add_and_fetch(&x, unlike_a_constant);
printf("%016llx\n", x);
}
Expected result:
ffffffffffffffff
ffffffffffffffff
Actual result:
00000000ffffffff
ffffffffffffffff
--
Summary: atomic built-ins malfunction with 64-bit and
optimization
Product: gcc
Version: 4.1.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: m dot rosellini at f5 dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40693
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/40693] atomic built-ins malfunction with 64-bit and optimization
2009-07-08 23:10 [Bug c/40693] New: atomic built-ins malfunction with 64-bit and optimization m dot rosellini at f5 dot com
@ 2009-07-09 0:10 ` m dot rosellini at f5 dot com
2009-07-09 5:54 ` [Bug target/40693] atomic built-ins malfunction with 64-bit signed ptrs and negative constants jakub at gcc dot gnu dot org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: m dot rosellini at f5 dot com @ 2009-07-09 0:10 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from m dot rosellini at f5 dot com 2009-07-09 00:10 -------
I forgot to add: You need to compile this with -O2 and -march=pentium.
The way that negative constants are handled in the code emitted for
__sync_blah_and_blah is incorrect when the pointer type is 64-bits and the
platform is 32-bit x86. That's the real issue. Calling
__sync_blah_and_blah(ptr, -1) is sufficient (where ptr is a pointer to a 64-bit
signed type). Then you do not need to turn on optimization.
This doesn't really have anything to do with optimization... it's just that
there is a strange interplay with optimization whereby the function has the bad
behavior exhibited with constants even when it doesn't look like one is passing
a constant (such as when calling this through an inline function).
--
m dot rosellini at f5 dot com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |m dot rosellini at f5 dot
| |com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40693
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug target/40693] atomic built-ins malfunction with 64-bit signed ptrs and negative constants
2009-07-08 23:10 [Bug c/40693] New: atomic built-ins malfunction with 64-bit and optimization m dot rosellini at f5 dot com
2009-07-09 0:10 ` [Bug c/40693] " m dot rosellini at f5 dot com
@ 2009-07-09 5:54 ` jakub at gcc dot gnu dot org
2009-07-09 20:29 ` m dot rosellini at f5 dot com
2009-10-14 4:00 ` pinskia at gcc dot gnu dot org
3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu dot org @ 2009-07-09 5:54 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from jakub at gcc dot gnu dot org 2009-07-09 05:54 -------
Can't reproduce.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40693
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug target/40693] atomic built-ins malfunction with 64-bit signed ptrs and negative constants
2009-07-08 23:10 [Bug c/40693] New: atomic built-ins malfunction with 64-bit and optimization m dot rosellini at f5 dot com
2009-07-09 0:10 ` [Bug c/40693] " m dot rosellini at f5 dot com
2009-07-09 5:54 ` [Bug target/40693] atomic built-ins malfunction with 64-bit signed ptrs and negative constants jakub at gcc dot gnu dot org
@ 2009-07-09 20:29 ` m dot rosellini at f5 dot com
2009-10-14 4:00 ` pinskia at gcc dot gnu dot org
3 siblings, 0 replies; 5+ messages in thread
From: m dot rosellini at f5 dot com @ 2009-07-09 20:29 UTC (permalink / raw)
To: gcc-bugs
------- Comment #3 from m dot rosellini at f5 dot com 2009-07-09 20:29 -------
OK, maybe some assembly will make the issue clearer. Or perhaps you just want
to close this bug if you're not fixing bugs in the 4.1 compiler. I verified
that the problem does occur with 4.1 and that it does not occur with 4.2. The
relevant built-ins seem not to exist in 4.0.
Below is the output from a 4.1 compiler and a 4.2 compiler. I have annotated
the relevant instructions with "<<<". In the first example, from a 4.1
compiler, you can see that there is a constant of "0" passed to adcl (which is
incorrect), whereas in the second example, from a 4.2 compiler, there is a
constant of "-1" passed to adcl (which is correct).
seapddev(catalan) ~/ 503$ cat t.c
int
main(int ac, char **av)
{
volatile long long x;
__sync_fetch_and_add(&x, -1);
return x;
}
seapddev(catalan) ~/ 504$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
seapddev(catalan) ~/ 505$ gcc -march=pentium t.c
seapddev(catalan) ~/ 506$ cat t.s
.file "t.c"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %edi
pushl %esi
pushl %ebx
pushl %ecx
subl $40, %esp
leal -24(%ebp), %eax
movl %eax, -36(%ebp)
movl -36(%ebp), %edx
movl (%edx), %eax
movl 4(%edx), %edx
movl %eax, -48(%ebp)
movl %edx, -44(%ebp)
.L2:
movl -48(%ebp), %eax
movl -44(%ebp), %edx
movl %eax, -56(%ebp)
movl %edx, -52(%ebp)
movl -56(%ebp), %ecx
movl -52(%ebp), %ebx
addl $-1, %ecx
adcl $0, %ebx <<<
movl %ecx, %esi
movl %ebx, %ecx
movl -36(%ebp), %edi
movl -56(%ebp), %eax
movl -52(%ebp), %edx
movl %esi, %ebx
lock
cmpxchg8b (%edi)
movl %eax, -48(%ebp)
movl %edx, -44(%ebp)
jne .L2
movl -24(%ebp), %eax
movl -20(%ebp), %edx
addl $40, %esp
popl %ecx
popl %ebx
popl %esi
popl %edi
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.1.2 20080704 (Red Hat 4.1.2-44)"
.section .note.GNU-stack,"",@progbits
seapddev(catalan) ~/ 507$
wayward ~/ 537$ cat t.c
int
main(int ac, char **av)
{
volatile long long x;
__sync_fetch_and_add(&x, -1);
return x;
}
wayward ~/ 538$ gcc-4.2 --version
i686-apple-darwin9-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5574)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
wayward ~/ 539$ rm t.s
rm: t.s: No such file or directory
wayward ~/ 540$ gcc-4.2 -march=pentium -S t.c
wayward ~/ 541$ cat t.s
.text
.globl _main
_main:
pushl %ebp
movl %esp, %ebp
pushl %edi
pushl %esi
subl $64, %esp
leal -16(%ebp), %eax
movl %eax, -28(%ebp)
movl -28(%ebp), %edi
movl (%edi), %esi
movl 4(%edi), %edi
movl %esi, -40(%ebp)
movl %edi, -36(%ebp)
L2:
movl -40(%ebp), %eax
movl -36(%ebp), %edx
movl %eax, -64(%ebp)
movl %edx, -60(%ebp)
movl -64(%ebp), %esi
movl -60(%ebp), %edi
addl $-1, %esi
adcl $-1, %edi <<<
movl %esi, -44(%ebp)
movl %edi, %ecx
movl -28(%ebp), %esi
movl -64(%ebp), %eax
movl -60(%ebp), %edx
movl -44(%ebp), %edi
xchgl %ebx, %edi
lock
cmpxchg8b (%esi)
xchgl %ebx, %edi
movl %eax, -40(%ebp)
movl %edx, -36(%ebp)
jne L2
movl -16(%ebp), %eax
movl -12(%ebp), %edx
addl $64, %esp
popl %esi
popl %edi
popl %ebp
ret
.subsections_via_symbols
wayward ~/ 542$
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40693
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug target/40693] atomic built-ins malfunction with 64-bit signed ptrs and negative constants
2009-07-08 23:10 [Bug c/40693] New: atomic built-ins malfunction with 64-bit and optimization m dot rosellini at f5 dot com
` (2 preceding siblings ...)
2009-07-09 20:29 ` m dot rosellini at f5 dot com
@ 2009-10-14 4:00 ` pinskia at gcc dot gnu dot org
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-10-14 4:00 UTC (permalink / raw)
To: gcc-bugs
------- Comment #4 from pinskia at gcc dot gnu dot org 2009-10-14 04:00 -------
Fixed in 4.2.0 so closing.
--
pinskia at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |FIXED
Target Milestone|--- |4.2.0
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40693
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-10-14 4:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-08 23:10 [Bug c/40693] New: atomic built-ins malfunction with 64-bit and optimization m dot rosellini at f5 dot com
2009-07-09 0:10 ` [Bug c/40693] " m dot rosellini at f5 dot com
2009-07-09 5:54 ` [Bug target/40693] atomic built-ins malfunction with 64-bit signed ptrs and negative constants jakub at gcc dot gnu dot org
2009-07-09 20:29 ` m dot rosellini at f5 dot com
2009-10-14 4:00 ` pinskia at gcc dot gnu dot org
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).