Search code examples
optimizationmakefilecompilationfortranintel-fortran

When does ifort use optimization flags?


Introduction

I understand that Intel's Fortran compiler ifort performs some amount of function inlining and interprocedural optimization (IPO), as well as other, standard optimizations. This can be done as in the following example:

ifort -O2 -finline-functions -ipo myProgram.f function.f subroutine.f -o MyProgram

Compiler Option Explanations:

  • The -O2 flag tells the compiler to perform significant optimizations (which for Fortran, includes function inlining).
  • The -finline-functions flag tells the compiler to allow function inlining. (This is the default behavior; I include the flag here for the sake of explicitness.)
  • The -ipo flag tells the compiler to perform IPO.

Suppose I break up the compilation process into two steps, as is often done when using a Makefile....

Step 1

I compile and assemble a bunch of files separately, turning source code into object files:

ifort -O2 -ipo -c   function.f -o   function.o
ifort -O2 -ipo -c subroutine.f -o subroutine.o
ifort -O2 -ipo -c  myProgram.f -o  myProgram.o

(I'm guessing no inlining is done at this step because the files are compiled independently....)

Step 2

I then link the object files together and create the executable:

  • ifort -O2 -finline-functions -ipo myProgram.o function.o subroutine.o -o MyProgram

My Question

If I do compile files individually, which of these optimization flags should be passed to the compiler at which stages? During which step are inlining and IPO performed?


Solution

  • When you compile with -ipo, the compiler generates intermediate code but does no optimization or inlining at all. You have .o files but they don't contain any machine instructions. When you then call ifort again to create the executable, the compiler then sucks up all the intermediate code and optimizes it as if you had compiled everything in one big source file. It can then see which routines are referenced and where and can do more optimization and inlining.

    You should pass the same optimization options for each compile - they are recorded along with the intermediate code.

    I'm pretty sure that other compilers with a similar feature do it much the same way. (I'm a former Intel compiler dev/support engineer.)