Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 777
Mephisto Avatar asked Dec 08 '16 23:12

Mephisto


People also ask

What is used to optimize compiler?

Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources or executes faster.

What are the properties of optimizing compiler * 5 points?

Compiler optimizing process should meet the following objectives : The optimization must be correct, it must not, in any way, change the meaning of the program. Optimization should increase the speed and performance of the program. The compilation time must be kept reasonable.

How do I disable compiler optimization?

You can disable optimizations if you pass -O0 with the gcc command-line. -O0 is redundant as it is the default.

Do compilers Optimise code?

Compilers are free to optimize code so long as they can guarantee the semantics of the code are not changed.


1 Answers

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.

like image 183
Vladimir F Героям слава Avatar answered Sep 30 '22 13:09

Vladimir F Героям слава