common block alignment

Christopher D. Rickett crickett@lanl.gov
Mon Aug 6 16:47:00 GMT 2007


hi Jack,

thanks for raising this question; i had not had a chance to do so yet.

the mismatch in alignment also occurs on x86 linux, but the linux linker 
didn't warn about the problem.  the code snippet below does fix the issue, 
and i plan to submit it as a formal patch shortly.

Chris

On Mon, 6 Aug 2007, Jack Howarth wrote:

>   I haven't seen Christopher Rickett ask this yet on
> the list. Using a powerpc-apple-darwin9 cross-compiler
> (which adds an additional alignment field to the .comm
> assembly lines), we discovered a mismatch between the
> code generated in bind_c_coms .f90...
>
>>>        .size   main, .-main
>>>        .comm   com,16,8
>>>        .comm   single,8,8
>>>        .comm   mycom,4,4
>>>        .comm   mycom2,8,8
>>>        .comm   f03_com2,8,4
>>>        .section        .rodata
>>>        .align
>
> ...and bind_c_coms_driver.c...
>
>>>        .size   test_coms, .-test_coms
>>>        .comm   mycom,4,16
>>>        .comm   com3,8,16
>>>        .comm   com,16,16
>>>        .comm   f03_com2,8,16
>>>        .comm   single,8,16
>>>        .comm   mycom2,8,16
>>>        .section        .rodata
>>>        .align 8
>
> Chris fixed this issue for the iso c binding tests with
> the following patch...
>
> Index: gcc/fortran/trans-common.c
> ===================================================================
> --- gcc/fortran/trans-common.c	(revision 127182)
> +++ gcc/fortran/trans-common.c	(working copy)
> @@ -413,7 +413,20 @@ build_common_decl (gfc_common_head *com,
>       SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_common_id (com));
>       TREE_PUBLIC (decl) = 1;
>       TREE_STATIC (decl) = 1;
> -      DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
> +      if (!com->is_bind_c)
> +	DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
> +      else
> +        {
> +	  /* Do not set the alignment for bind(c) common blocks to
> +	     BIGGEST_ALIGNMENT because that won't match what C does.  Also,
> +	     for common blocks with one element, the alignment must be
> +	     that of the field within the common block in order to match
> +	     what C will do.  */
> +	  tree field = NULL_TREE;
> +	  field = TYPE_FIELDS (TREE_TYPE (decl));
> +	  if (TREE_CHAIN (field) == NULL_TREE)
> +	    DECL_ALIGN (decl) = TYPE_ALIGN (TREE_TYPE (field));
> +	}
>       DECL_USER_ALIGN (decl) = 0;
>       GFC_DECL_COMMON_OR_EQUIV (decl) = 1;
>
> However we still have the question of why the common blocks in fortran
> are aligned according to BIGGEST_ALIGNMENT and if we shouldn't just
> have all of fortran use the same alignment as in c? I assume we would
> have to do this with the soversion bump for libgfortran in gcc 4.3.
> Thanks in advance for any clarifications on this.
>                 Jack
>



More information about the Fortran mailing list