[PATCH 0/2] fortran: ALIGN attribute and ALLOCATE ALIGN directive

Magnus Weinmueller <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <[email protected]>
Hi,

this series adds a GNU extension that lets Fortran code request
over-aligned storage, which is currently only reachable from Fortran by
going through C or by relying on the OpenMP allocate directive.

  !GCC$ ATTRIBUTES ALIGN(n) :: x     ! alignment of the variable
  !GCC$ ALLOCATE (x) ALIGN(n)        ! alignment of one ALLOCATE

The spelling ALIGN(n) was chosen to match the align modifier of the
OpenMP allocate directive, which gfortran already supports, and C's
__attribute__((aligned(n))).  n has to be a positive power of two.

The series is split so that each patch is useful on its own:

  1/2  Parse the attribute and apply it to variables with automatic or
       static storage duration via SET_DECL_ALIGN.  This is the complete
       feature for everything that is not allocated on the heap.  The
       alignment value is stored in module files as an optional named
       attribute bit, so module files written by an unpatched compiler
       still parse correctly and no MOD_VERSION bump is needed.

  2/2  Make ALLOCATE honour the requested alignment by emitting a call
       to aligned_alloc instead of malloc, and add the per-statement
       !GCC$ ALLOCATE directive.  Where both an attribute and a
       directive apply to the same object, the larger alignment wins.

This came out of work on explicit SIMD types for gfortran, where I
needed over-aligned arrays for aligned vector loads and stores.  It
seems useful on its own, so here it is separately.

Three things I am unsure about:

ALIGN on a POINTER only does something for memory that ALLOCATE hands
out.  A pointer assignment p => t obviously cannot change how t is
aligned.  I kept the attribute as a pure allocation request and never
tell the optimiser to assume anything, so this cannot miscompile - the
request is just not honoured there.  Rejecting ALIGN on POINTER
outright would work too, if that is preferred.

Passing a non-contiguous actual to a contiguous dummy loses the
alignment, because the packed temporary comes from internal_pack and
that uses malloc.  Doing this properly needs either alignment-aware
temporaries in the front end or a new libgfortran entry point, which
seemed too much for this series.

ALIGN on a dummy argument is accepted and then ignored.

Bootstrapped and regression tested on x86_64-pc-linux-gnu, no
regressions.  Expected passes go from 75310 to 75344, the 34 extra ones
being the new tests; expected failures and unsupported are unchanged.

I do not have write access, so if these are approved I would need
someone to commit them for me.

Thanks,
Magnus Weinmueller

Magnus Weinmueller (2):
  fortran: add ALIGN attribute for variables
  fortran: honour ALIGN in ALLOCATE and add ALLOCATE ALIGN directive

 gcc/fortran/decl.cc                    | 139 +++++++++++++++++++++++++
 gcc/fortran/f95-lang.cc                |   5 +
 gcc/fortran/gfortran.h                 |  17 +++
 gcc/fortran/match.cc                   |  22 ++++
 gcc/fortran/match.h                    |   1 +
 gcc/fortran/module.cc                  |  17 ++-
 gcc/fortran/parse.cc                   |   8 ++
 gcc/fortran/trans-array.cc             |   9 +-
 gcc/fortran/trans-array.h              |   3 +-
 gcc/fortran/trans-decl.cc              |  19 ++++
 gcc/fortran/trans-stmt.cc              |  31 +++++-
 gcc/fortran/trans.cc                   |  32 +++++-
 gcc/fortran/trans.h                    |   4 +-
 gcc/testsuite/gfortran.dg/align_1.f90  |  38 +++++++
 gcc/testsuite/gfortran.dg/align_2.f90  |  19 ++++
 gcc/testsuite/gfortran.dg/align_2b.f90 |  12 +++
 gcc/testsuite/gfortran.dg/align_3.f90  |  41 ++++++++
 gcc/testsuite/gfortran.dg/align_4.f90  |  23 ++++
 18 files changed, 425 insertions(+), 15 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/align_1.f90
 create mode 100644 gcc/testsuite/gfortran.dg/align_2.f90
 create mode 100644 gcc/testsuite/gfortran.dg/align_2b.f90
 create mode 100644 gcc/testsuite/gfortran.dg/align_3.f90
 create mode 100644 gcc/testsuite/gfortran.dg/align_4.f90

-- 
2.50.1 (Apple Git-155)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.