Fix memory leaks in Fortran TRANSFER simplification
Fortran front-end memory leaks fixed during TRANSFER simplification.
This commit addresses memory leaks in GCC’s Fortran front-end related to the simplification of the TRANSFER intrinsic function. It corrects how array expressions are generated and avoids redundant initialization of numerical components, preventing leaks and improving stability.
In Details
The Fortran front-end's gfc_simplify_transfer function in simplify.cc suffered from GMP memory leaks, particularly when simplifying TRANSFER operations that resulted in arrays. The issue stemmed from creating a scalar expression and then changing its type, losing track of allocated memory. This patch reorganizes the expression generation to directly handle the known rank of the result (0 or 1) and also modifies gfc_target_interpret_expr in target-memory.cc to properly clear GMP integers and reals before reinitialization, fixing PR127892.
- TRANSFER
- A Fortran intrinsic function used to transfer the representation of data from one data type to another, potentially changing the shape and interpretation of the data without changing its underlying bits.
- front-end
- The part of a compiler that parses the source code, performs semantic analysis, and typically generates an intermediate representation. For GCC Fortran, this is the
gfortranpart. - GMP
- The GNU Multiple Precision Arithmetic Library, used by GCC for arbitrary-precision arithmetic, often in the Fortran front-end for handling large numbers or specific mathematical operations.
- array expression
- In Fortran, an expression that evaluates to an array, which is a collection of elements of the same data type, organized in one or more dimensions.
- scalar
- In programming, a single data item, as opposed to an array or other collection. In Fortran, this refers to a single number, character, or logical value.