For this simple Fortran string: [hjl@gnu-6 pr45634]$ cat pr45634.f90 SUBROUTINE RCRDRD (VTYP) CHARACTER(4), INTENT(OUT) :: VTYP CHARACTER(1), SAVE :: DBL = "D" VTYP = DBL END GCC generates: [hjl@gnu-6 pr45634]$ cat pr45634.s .file "pr45634.f90" .text .p2align 4,,15 .globl rcrdrd_ .type rcrdrd_, @function rcrdrd_: .LFB0: .cfi_startproc movzbl dbl.1557(%rip), %eax movw $8224, 1(%rdi) movb $32, 3(%rdi) movb %al, (%rdi) ret .cfi_endproc .LFE0: .size rcrdrd_, .-rcrdrd_ .section .rodata .type dbl.1557, @object .size dbl.1557, 1 dbl.1557: .ascii "D" .ident "GCC: (GNU) 4.6.0 20100910 (experimental)" .section .note.GNU-stack,"",@progbits IFORT generates: [hjl@gnu-6 pr45634]$ cat icc.s # -- Machine type EFI2 # mark_description "Intel(R) Fortran Compiler XE for applications running on Intel(R) 64, Version 12.0.0 Beta Build 20100512"; # mark_description "-O3 -S"; .file "pr45634.f90" .text ..TXTST0: # -- Begin rcrdrd_ # mark_begin; .align 16,0x90 .globl rcrdrd_ rcrdrd_: # parameter 1: %rdi # parameter 2: %rsi ..B1.1: # Preds ..B1.0 ..___tag_value_rcrdrd_.1: #1.18 movl $538976324, (%rdi) #4.7 ret #5.7
I have a slightly different result with your code. troutmask:sgk[212] gfc4x -c -O g.f90 g.f90: In function 'rcrdrd': g.f90:1:0: internal compiler error: in build_int_cst_wide, at tree.c:1218 Please submit a full bug report, with preprocessed source if appropriate. See <http://gcc.gnu.org/bugs.html> for instructions. If I replace the SAVE attribute by PARAMETER (which will most likely cause the folding you want), everything compiles fine. The ICE also goes away if I increase the length of DBL from one to any larger value. troutmask:sgk[219] gfc4x -v Using built-in specs. COLLECT_GCC=/home/sgk/work/4x/bin/gfortran COLLECT_LTO_WRAPPER=/usr/home/sgk/work/4x/bin/../libexec/gcc/x86_64-unknown-freebsd9.0/4.6.0/lto-wrapper Target: x86_64-unknown-freebsd9.0 Configured with: ../gcc4x/configure --prefix=/home/sgk/work/4x --enable-languages=c,fortran --disable-libmudflap --disable-bootstrap --with-quad=/home/sgk/work Thread model: posix gcc version 4.6.0 20100909 (experimental) (GCC)
The -fdump-tree-original for HJ's original code look like rcrdrd (character(kind=1)[1:4] & restrict vtyp, integer(kind=4) _vtyp) { static character(kind=1) dbl[1:1] = "D"; (MEM[(c_char * {ref-all})vtyp] = MEM[(c_char * {ref-all})&dbl];, (void *) vtyp;); __builtin_memset ((void *) vtyp + 1, 32, 3); } If I increase the length of DBL to 2, then the dump looks like rcrdrd (character(kind=1)[1:4] & restrict vtyp, integer(kind=4) _vtyp) { static character(kind=1) dbl[1:2] = "D "; __builtin_memmove ((void *) vtyp, (void *) &dbl, 2); __builtin_memset ((void *) vtyp + 2, 32, 2); }
(In reply to comment #1) > I have a slightly different result with your code. > > troutmask:sgk[212] gfc4x -c -O g.f90 > g.f90: In function 'rcrdrd': > g.f90:1:0: internal compiler error: in build_int_cst_wide, at tree.c:1218 > Please submit a full bug report, > with preprocessed source if appropriate. > See <http://gcc.gnu.org/bugs.html> for instructions. > > It is fixed by http://gcc.gnu.org/ml/gcc-cvs/2010-09/msg00475.html
(In reply to comment #3) > (In reply to comment #1) > > I have a slightly different result with your code. > > > > troutmask:sgk[212] gfc4x -c -O g.f90 > > g.f90: In function 'rcrdrd': > > g.f90:1:0: internal compiler error: in build_int_cst_wide, at tree.c:1218 > > Please submit a full bug report, > > with preprocessed source if appropriate. > > See <http://gcc.gnu.org/bugs.html> for instructions. > > > > > > It is fixed by > > http://gcc.gnu.org/ml/gcc-cvs/2010-09/msg00475.html > HJ, Thanks. I updated by trunk right before your patch, so I missed it my most recent build.
For arbitrary lengths (both of the constant string and of the padding) the memmove (which will be optimized to memcpy as the source is read-only) + memset is the best thing to do, replacing say memmove (x, "900 bytes long string", 900); memset (x + 900, ' ', 100); would be very .rodata size unfriendly. So, the question is, do we want to optimize this for very small sizes of both (what sizes? Should we call can_store_by_pieces to determine that from the FE?) in the FE by transforming that say memmove (x, "ABCDE", 5); memset (x + 5, ' ', 3); into memcpy (x, "ABCDE ", 8); or should we do this generically in the middle-end, where we'd do this transformation for such cases even for other languages?
Created attachment 21891 [details] patch for comment#5 This patch fixes the issue in comment #5 in the Fortran front end. The constant for padding is arbitrary, I don't know a better way to set it.
(In reply to comment #6) > Created attachment 21891 [details] > patch for comment#5 > > This patch fixes the issue in comment #5 in the Fortran front end. ... but also causes regressions. I'll investigate. Confirmed, BTW.
Created attachment 21991 [details] gcc46-pr45636.patch So far untested patch to optimize memcpy + memset in forwprop. Works on the attached testcase, but ICEs on: ! { dg-do compile } ! { dg-options "-O2 -fdump-tree-forwprop2" } ! PR 45636 - make sure no memset is needed for a short right-hand side. program main character(len=2), parameter :: x='a ' character(len=4) :: a a = x call sub(a) end program main ! { dg-final { scan-tree-dump-times "memset" 0 "forwprop2" } } ! { dg-final { cleanup-tree-dump "forwprop2" } } I'm afraid I'm missing somewhere some magic call to update vops after the call removal. And, it unfortunately doesn't handle the case where the first memcpy is of size 1 (or memset is of size 1), because then folding changes the calls into assignments.
Author: jakub Date: Tue Oct 12 22:01:04 2010 New Revision: 165401 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=165401 Log: PR fortran/45636 * tree-ssa-forwprop.c: Include expr.h. (constant_pointer_difference, simplify_builtin_call): New functions. (tree_ssa_forward_propagate_single_use_vars): Call simplify_builtin_call on builtin calls. * gcc.c-torture/execute/pr45636.c: New test. * gfortran.dg/pr45636.f90: New test. Added: trunk/gcc/testsuite/gcc.c-torture/execute/pr45636.c trunk/gcc/testsuite/gfortran.dg/pr45636.f90 Modified: trunk/gcc/ChangeLog trunk/gcc/Makefile.in trunk/gcc/testsuite/ChangeLog trunk/gcc/tree-ssa-forwprop.c
Created attachment 22064 [details] Tree dump. gfortran.dg/pr45636.f90 fails on hppa-unknown-linux-gnu. Tree dump attached.
In addition, although gcc.c-torture/execute/pr45636.c doesn't fail on hppa-unknown-linux-gnu, it does fail on hpux: Executing on host: /test/gnu/gcc/objdir/gcc/xgcc -B/test/gnu/gcc/objdir/gcc/ /test/gnu/gcc/gcc/gcc/testsuite/gcc.c-torture/execute/pr45636.c -w -O0 -lm -o /test/gnu/gcc/objdir/gcc/testsuite/gcc/pr45636.x0 (timeout = 300) /usr/ccs/bin/ld: Unsatisfied symbols: mempcpy (first referenced in /var/tmp//ccT7jpSk.o) (code) Looking at the hppa-linux .s, there are still many calls to various 'mem*' functions. I don't see any on x86. Dave
gcc.c-torture/execute/pr45636.c also fails on Solaris 2/SPARC: FAIL: gcc.c-torture/execute/pr45636.c compilation, -Os UNRESOLVED: gcc.c-torture/execute/pr45636.c execution, -Os Undefined first referenced symbol in file mempcpy /var/tmp//ccJduPWN.o ld: fatal: Symbol referencing errors. No output written to /var/gcc/regression/trunk/10-gcc-gas/build/gcc/testsuite/gcc2/pr45636.x7 collect2: ld returned 1 exit status
I have moved gcc.c-torture/execute/pr45636.c to gcc.dg/torture/pr45636.c and added a check for the mempcpy function so this should not fail on HP-UX or Solaris which don't have a mempcpy function. The change was r166378 and if the test failures are the only reason to keep this bug report open then it we should be able to close it now.
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/7/2010 4:12:41 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/7/2010 8:18:00 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/8/2010 12:26:28 AM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/8/2010 4:35:13 AM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/8/2010 8:43:46 AM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/8/2010 12:52:18 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/8/2010 5:00:28 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/8/2010 5:46:34 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/8/2010 9:50:00 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
Delivery is delayed to these recipients or distribution lists: fdebgev4jr1c@hiauly1.hia.nrc.ca<mailto:fdebgev4jr1c@hiauly1.hia.nrc.ca> Subject: [Bug fortran/45636] Failed to fold simple Fortran string This message has not yet been delivered. Microsoft Exchange will continue to try delivering the message on your behalf. Delivery of this message will be attempted until 11/8/2010 9:08:26 PM (GMT-05:00) Eastern Time (US & Canada). Microsoft Exchange will notify you if the message can't be delivered by that time. ________________________________ Sent by Microsoft Exchange Server 2007
remove Danglin to avoid mail spamage
> The change was r166378 and if the test failures are the only reason to keep > this bug report open then it we should be able to close it now. Closing would be ok if there is a reason the call to mempcpy shouldn't be eliminated. On darwin, it was eliminated. Dave
The mempcpy is not inlined with -Os. Presumbably because that would increase the size of the resulting object.
(In reply to comment #26) > The mempcpy is not inlined with -Os. Presumbably because that would increase > the size of the resulting object. Possibly, but based on Rainer's comment, the issue would appear to affect targets with strict alignment. Sorry for the mail spamage. It was caused by a hardware failure at NRC.
Sorry, I didn't intend to reopen. The web interface seems to unexpected change fields sometimes.