1

I am using g++ 4.8.4 on Linux 14.04 x64. I am compiling my project with -O3 and -flto flags both during compilation and linking i.e., both CFLAGS and LDFLAGS in my Makefile have these options.

I just discovered that the optimized binary behaves differently (i.e., wrongly) when it comes to computations involving float/double. Removing the -O3 from LDFLAGS alone seems to be solving the problem.

I remember reading somewhere that it is generally safe to specify the same set of optimization flags to both the compiler and the linker. Am I wrong in my understanding? Thanks in advance.

1
  • If you are doing things with LTO and some wrong optimization applies on O3 only, then -- bingo -- removing O3 from last step will force to recompile whole code to O0 or something and will not trigger erroneous option. Commented Aug 10, 2016 at 11:00

1 Answer 1

1

While using LTO, compiler during compilation step dumps intermediate code representation (a.k.a. GIMPLE) into special sections of the object files.

Actual compilation to machine codes and optimization happens during linking stage.

So yes, if your code is causing UB or just mis-optimized with -O3 option, it might behave differently (as expected) if you change it to -O0, -Og etc

So you have to find out whether it is your code misbehaving, or actual GCC LTO bug

Sign up to request clarification or add additional context in comments.

3 Comments

got it. Let me try without the "-flto" alone and see. But, am I correct in giving the "-O3" flag to both the compiler and linker?
@SrivathsanM yes, should give same flag(s) to both compiler and linker
Flagging @"Severin Pappadeux" response as the answer since my project hasn't crashed from the time I removed the "-flto" flag. Even the "-O3" optimizaiton works fine without "-flto". So, it indeed seems to be an LTO bug in GCC 4.8.4 on Ubuntu 14.04 x64. Haven't experimented with GCC 5.x and the latest 6.2, though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.