tl;dr: there is a bug in optimization options saving/restoring. See after the 
break for simple C testcase.


> Unfortunately I can't find if/where there is some explanation for
> these frontend_set_flag_* options -- how they're supposed to work and
> who's responsible for setting them.

It’s not them, but this got me looking into the right place.

In my case, opts.c’s set_fast_math_flags() is called three times:

  1. first, from toplev_main -> decode_options -> default_options_optimization 
-> handle_generated_option -> handle_option -> common_handle_option
  2. then, from toplev_main -> decode_options -> read_cmdline_option -> 
handle_option -> common_handle_option
  3. then, from my own build_function_decl -> parse_optimize_options -> 
decode_options -> default_options_optimization -> handle_generated_option -> 
handle_option -> common_handle_option

At 1 and 3, it’s called with value = 0, and at 2, with value = 1. So it is 
indeed a bug: because we re-parse the defaults at 3, we reset some of the flags 
dependent on -ffast-math (the ones quoted before), overwriting their earlier 
value.

PS: There is also something similar with 
align_functions/align_jumps/align_loops flags, but I don’t have time to chase 
it right now.

------------------------

This is easy to see with a simple C test case:

//__attribute__ ((optimize("strength-reduce")))
int foo (float x) { return __builtin_isnan(x); }

Compiled with -O3 -ffast-math, the isnan is simplified out (fast math means no 
NaNs). If you uncomment the attribute (chosen because it’s actually useless), 
and compile again with -O3 -ffast-math: the isnan test is not simplified any 
more. This is because the codepath through default_options_optimization() has 
overwritten the value of the flags handled in set_fast_math_flags(): 
flag_finite_math_only, flag_signed_zeros, flag_trapping_math and 
flag_unsafe_math_optimizations.

I’m CC’ing the maintainers who added the optimize attribute in the first place, 
as they might have an idea how to fix this. This is way beyond my league!


Thanks Steven for your help!
FX

Reply via email to