admin管理员组

文章数量:1201413

Consider the following minimal example:

subroutine f(x,a,b,c)
    integer::x,i,j,k
    integer,optional::a,b,c
    ! depending on present arguments, some expressions and variables are redundant
    i=42*x
    j=137*x
    k=142857*x
    ! potentially more `if(present(...))...` statements in the procedure
    if(present(a))a=i*x
    if(present(b))b=j*x
    if(present(c))c=k*x
end

Will a Fortran compiler (GNU or Intel) with the -O3 compiler option generate 8 (2^3) specialized versions of this subroutine depending on the list of present arguments and eliminate dead code (redundant expressions) for better performance? Or will it generate only one subroutine with conditional statements? The second case can be significantly slower for larger procedures with a lot of if(present(...))... statements (e.g. in loops) and potentially redundant expressions. However, the first case will increase the size of the compiled binary file. And is there a simple way to check, which choice a compiler makes?

(If specialized versions are generated and dead code is eliminated, I consider using this behavior to improve the readability of the code that generates overloaded versions of a large procedure with different lists of arguments and currently uses conditional preprocessor statements.)

本文标签: compiler optimizationSpecialized versions of a Fortran procedureStack Overflow