[Fwd: question on optimizing calls to library functions]

Richard Guenther richard.guenther@gmail.com
Wed Dec 10 16:22:00 GMT 2008


On Wed, Dec 10, 2008 at 4:17 PM, Tobias Burnus <burnus@net-b.de> wrote:
> Hi all,
>
> does someone see why
>   REAL, DIMENSION(1, 1), PARAMETER :: a = 1.0
>   ... = SIN(a)
> is not compile-time evaluated? Or does it make sense for constant arrays
> that they are not compile-time simplified?

Depends what the frontend presents to the middle-end here.

> (The question about optimizing matmul way I leave to the middle enders)
>
> Tobias
>
> Hi all.
>
> While looking at PR fortran/22572, I wondered where the difference between the
> following two programs might be:
>
> $> cat matmul.f90
>  REAL, DIMENSION(1,1), PARAMETER :: a = 1.0, b = 2.0
>  REAL, DIMENSION(1,1) :: c
>  c = MATMUL(a, b)
>  c = MATMUL(a, b)
> end
>
> $> cat sin.f90
>  REAL, DIMENSION(1, 1), PARAMETER :: a = 1.0
>  REAL, DIMENSION(1, 1) :: b, c
>  b = SIN(a)
>  c = SIN(a)
> end
>
> Compiling both with "-Wall -O3 -S -fdump-tree-original -fdump-tree-optimized",
> one finds that the calls to SIN in sin.f90 have been optimized into
> nothingness, while MATMUL in matmul.f90 is spelled out twice in the optimized
> tree dump.
>
> The main difference that springs to mind: SIN is built-in, MATMUL is a library
> function. In gcc/builtin.defs, one finds
>
>        DEF_LIB_BUILTIN (BUILT_IN_SIN, "sin", BT_FN_DOUBLE_DOUBLE,
>                         ATTR_MATHFN_FPROUNDING)
>
> with
>
>        #define ATTR_MATHFN_FPROUNDING (flag_rounding_math ? \
>                ATTR_PURE_NOTHROW_NOVOPS_LIST : ATTR_CONST_NOTHROW_LIST)
>
> Grep'ing the fortran sources, hardly any ATTR_* are used. Would the
> application of ATTR_MATHFN_FPROUNDING or any other ATTR_* (e.g. ATTR_PURE?)
> make any difference for the optimizer? If yes, where and how should these
> attributes be applied to the function symbol?
>
> Are these the right questions to ask or am I barking up the wrong tree?

The compiler doesn't have a builtin for matmul, so it doesn't know what a call
to matmul does.  You could teach it such knowledge and provide constant-folders
for it (and inline expanders, etc.).  Note that matmul in its full
Fortran generality
is hard to map to a gcc builtin function - the middle-end arrays may make this
easier.

Richard.



More information about the Fortran mailing list