GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
fortran Performance Win

Use memcpy/memmove for contiguous Fortran array slice copies.

Improves efficiency of Fortran array assignments by using built-in memcpy/memmove.

This patch enhances the efficiency of copying contiguous array slices in Fortran by leveraging the memcpy and memmove built-in functions. It extends the conditions under which these optimized functions are generated to handle variable-sized arrays and slices, and introduces memmove for cases where overlapping regions might occur, reducing temporary variable usage and improving performance.

In Details

The GCC Fortran front-end (gfc) generates code for array assignments. This commit refactors gfc_trans_array_copy to use __builtin_memcpy and __builtin_memmove for contiguous array slices, including those with dynamic extents. Previously, these copies might have been generated less efficiently. The new copyable_slice_p helper function determines if a slice can be copied using these optimized routines. This change also addresses PRs fortran/128542 and fortran/40598.

For Context
Fortran
A high-level programming language primarily used for numerical and scientific computing. GCC includes a Fortran compiler named GFortran.
memcpy
A standard C library function that copies a block of memory from a source location to a destination location. It is highly optimized for performance when copying non-overlapping memory regions.
memmove
A standard C library function similar to memcpy, but it correctly handles cases where the source and destination memory regions may overlap.
array slice
A contiguous portion of a Fortran array, defined by a starting index, ending index, and a stride. For example, array(1:10:2) refers to a slice of array.
scalarizer
A compiler optimization pass that transforms array operations (like element-wise assignment or reductions) into loops that operate on individual elements (scalars).
PR fortran/128542
A bug report identifier for the Fortran component of GCC, indicating an issue related to array temporaries and memory copying.
PR fortran/40598
Another bug report identifier for the Fortran component of GCC, likely related to array assignment or memory operations.
Filed Under: fortranoptimizationmemcpymemmove