Compiler options to report implicit creation of temporary variables

Hello,

Gfortran has an option to report implicit creation of temporary arrays (or array temporaries):

-fcheck=array-temps (Code Gen Options)

-fcheck=<keyword>
Enable the generation of run-time checks (…)

keyword = ‘all’
Enable all run-time test of -fcheck.

keyword = ‘array-temps’
Warns at run time when for passing an actual argument a temporary array had to be generated. The information generated by this warning is sometimes useful in optimization, in order to avoid such temporaries.

Here, I mean by “implicit creation” that compilers create temporary arrays internally for some reason (contiguity of memory, optimization, etc). I am wondering if other Fortran compilers also have similar options? Also, are there possibly some options to report implicit creation of scalar temporary variables, particularly for derived-type objects? (I checked the manual for Gfortran-12, but could not find related ones.)

If you might know similar options in other languages (either for scalar derived types or array temporaries), I would appreciate it if you share the information. I think it would be possible to implement such a feature somehow (e.g., by user-defined automatic constructors if it is supported by the language), but here I am wondering whether compiler options or pragmas or tools (?) are available for making a report of temporary objects (particularly large ones).

Thanks very much!

1 Like

The info from Perplexity was something like this, which suggests that similar options are available for arrays, though no specific options for scalar variables. (FWIW, the last paragraph mentions a challenge for determining “unnecessary” temporary objects, but I am more interested in knowing whether implicit copies of (possibly large) objects occur in a given routine, for example. According to the info below, it might be useful to check general optimization reports also.)

Gfortran’s -fcheck=array-temps option is indeed useful for reporting implicit creation of temporary arrays. Some other Fortran compilers offer similar options:

  1. Intel Fortran Compiler (ifort):
  • -check arg_temp_created: Reports when temporary arrays are created for array >arguments.
  1. NAG Fortran Compiler:
  • -C=array: Checks array bounds and reports temporary array creation.

For scalar temporary variables, especially derived-type objects, there are no widely available compiler options specifically for reporting their creation. However, some compilers provide general optimization reports that may include information about temporary object creation:

  1. Intel Fortran Compiler:
  • -qopt-report: Generates optimization reports that may include information about temporary objects.
  1. PGI/NVIDIA HPC Compiler:
  • -Minfo=all: Provides detailed optimization information.

It’s worth noting that implementing such features for scalar derived types or more complex objects is challenging due to the difficulty in determining what constitutes an “unnecessary” temporary object in all cases. Compiler developers often focus on optimizing these cases rather than reporting them.

(more details on other languages) In other programming languages:
  1. C++:

    • GCC and Clang offer -Wextra which can sometimes warn about temporary object creation.
    • Some static analysis tools like PVS-Studio can detect unnecessary temporary objects.
  2. Rust:

    • The Rust compiler provides detailed borrowing and lifetime information, which can indirectly indicate temporary object creation.
  3. Java:

    • The JVM’s JIT compiler doesn’t typically provide options for reporting temporary object creation, but profiling tools like VisualVM can help identify object allocation patterns.
1 Like