Mephisto
Mephisto

Reputation: 680

Do pointers inhibit compiler optimizations?

I was trying to pass an array to a subroutine, declared in the subroutine as an assumed shape array. That was giving me some problems that I have been able to solve by passing a pointer instead.

But some user with a high reputation tells me in a comment:

Adding pointer is also a reasonable way of telling the compilers optimiser that it doesn't have to do any work today.

Could anyone offer a short explanation on this? The language is Fortran 95, though I believe this applies to other languages.

Upvotes: 6

Views: 310

Answers (1)

Yes, Fortran compilers have to assume that pointers can alias with other pointers and with target variables.

If you have pointer arrays a and b then in

  a(i) = a(i) + b(i)

the compiler must assume that these two arrays may partially overlap and it must inhibit certain optimizations, because changing the value of a can change some value of b at some unknown index.

See also the C restrict keyword and a much more thorough discussion at Is Fortran easier to optimize than C for heavy calculations? . It is not worth repeating all points about pointer aliasing raised there.

IanH's comment was intentionally perhaps bit too strong, but there is a lot of truth in it.

Upvotes: 4

Related Questions