public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc optimization options and lto (detailed info)
@ 2022-05-17  6:56 muzungu
  2022-05-17  8:30 ` Jonathan Wakely
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: muzungu @ 2022-05-17  6:56 UTC (permalink / raw)
  To: gcc-help

Hi

In order to attempt to get LTO implemented in Zephyr OS I need a better understanding of the GCC optimization options.


I do not yet have an understanding either where in the Zephyr OS build LTO is problematic and and how it could be solved.


I now start from the GCC side first, to understand the options I / we have.


From https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html I understand the following:

 

* -O<x> combines a collection of -f<xyz>


* There is a long list of single optimizations which probably could be set individually

 

What is not clear to me:


* does every -f<xyz> have a -fno-<xyz>?


* would I be able able to disable single optimizations activated via -O<x> using -fno-<xyz> after -O<x>?


* When LTO is enabled, are the optimizations only effective at link time and not during the compilation into IR (GIMPLE)?


* Or are the optimizations effective in different stages of the build process? Are there information which options is applied when?


* From armclang we have an experience, where a const parameter from a parameter block was optimized as const value embedded into the code rendering the flash parameter block useless.

We were able to solve that by declaring the const parameter pointer as volatile by good guessing and try and error methodology, but also leaves a good level of uncertainty.


Are there information available what the different optimizations do (which I know, is a long list) and how such unwanted optimization can be avoided respective the unwanted effects of the optimizations


  * by writing the code accordingly (e.g. delcaring variables volatile or similar)


  * by attributing the code section accordingly (suppressing certain optimizations for that code part)


  * by simply disabling the optimization via compiler/linker cmd-line (-fno-<xyz>) for the different code files/modules (would that have an effect on modules/files for LTO at all or will it be treated as one big "module" rendering single file options useless?


* Since there is a rather long list of optimization options which would very likely exceed the command line length, what is the solution to activate a custom set of optimzations? I could not find a --configfile option or similar to replace the command line.


Creating a header file full of pragma GCC (which then is not generic but compiler specific) and the header being included in each compiler unit (C-File) seems very ugly to me.


I would appreciate any help also pointing to documentation which would clarify.


Best Roman




⁣Get BlueMail for Android ​

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: gcc optimization options and lto (detailed info)
  2022-05-17  6:56 gcc optimization options and lto (detailed info) muzungu
@ 2022-05-17  8:30 ` Jonathan Wakely
       [not found]   ` <trinity-1d53f520-c371-471b-b6c2-0446d6ba8184-1652865880524@3c-app-gmx-bap04>
  2022-05-17  8:54 ` Stefan Ring
  2022-05-17 23:05 ` Segher Boessenkool
  2 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2022-05-17  8:30 UTC (permalink / raw)
  To: muzungu; +Cc: gcc-help

On Tue, 17 May 2022 at 07:57, muzungu--- via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
> * does every -f<xyz> have a -fno-<xyz>?

Nearly all, yes:

"Many options have long names starting with -f or with -W --- for
example, -fmove-loop-invariants, -Wformat and so on.  Most of these
have both positive and negative forms; the negative form of -ffoo is
-fno-foo.  This manual documents only one of these two forms,
whichever one is not the default."


> * would I be able able to disable single optimizations activated via -O<x> using -fno-<xyz> after -O<x>?

Yes.

> * When LTO is enabled, are the optimizations only effective at link time and not during the compilation into IR (GIMPLE)?

Both.

> * Or are the optimizations effective in different stages of the build process? Are there information which options is applied when?

If a particular optimization is only effective before/during LTO I
think that will be mentioned in the manual. If it isn't mentioned,
assume it is effective at both times.

> Are there information available what the different optimizations do (which I know, is a long list) and how such unwanted optimization can be avoided respective the unwanted effects of the optimizations

No, it would be an entire book, and rapidly be outdated. All the
available documentation is already in the GCC manual. Of course the
source code is the ultimate guide, but it's not clear to follow unless
you already know how GCC works.

> * Since there is a rather long list of optimization options which would very likely exceed the command line length, what is the solution to activate a custom set of optimzations? I could not find a --configfile option or similar to replace the command line.

See the @file option.

But if you need so many options that you exceed the command-line
limits, maybe you're doing something wrong. That would be unusual.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: gcc optimization options and lto (detailed info)
  2022-05-17  6:56 gcc optimization options and lto (detailed info) muzungu
  2022-05-17  8:30 ` Jonathan Wakely
@ 2022-05-17  8:54 ` Stefan Ring
  2022-05-17 23:05 ` Segher Boessenkool
  2 siblings, 0 replies; 6+ messages in thread
From: Stefan Ring @ 2022-05-17  8:54 UTC (permalink / raw)
  To: gcc-help

On Tue, May 17, 2022 at 8:56 AM muzungu--- via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> * does every -f<xyz> have a -fno-<xyz>?

I think that’s the idea.

> * would I be able able to disable single optimizations activated via -O<x> using -fno-<xyz> after -O<x>?

Yes.

> * When LTO is enabled, are the optimizations only effective at link time and not during the compilation into IR (GIMPLE)?

I would expect them to be recorded in the intermediate object file and
becoming effective during code generation, but I am not sure about
that.

> * Since there is a rather long list of optimization options which would very likely exceed the command line length, what is the solution to activate a custom set of optimzations? I could not find a --configfile option or similar to replace the command line.

What do you think is your maximum command line length? This should not
be a problem on any sane OS.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: gcc optimization options and lto (detailed info)
  2022-05-17  6:56 gcc optimization options and lto (detailed info) muzungu
  2022-05-17  8:30 ` Jonathan Wakely
  2022-05-17  8:54 ` Stefan Ring
@ 2022-05-17 23:05 ` Segher Boessenkool
  2 siblings, 0 replies; 6+ messages in thread
From: Segher Boessenkool @ 2022-05-17 23:05 UTC (permalink / raw)
  To: muzungu; +Cc: gcc-help

On Tue, May 17, 2022 at 08:56:20AM +0200, muzungu--- via Gcc-help wrote:
> * -O<x> combines a collection of -f<xyz>

It does more than that.  Each -O<x> enables many things that can be
separately enabled and disabled with -f<xxx> flags, but there are
hundreds if not thousands other things that are sirectly controlled by
optimisation level.


Segher

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Re: gcc optimization options and lto (detailed info)
       [not found]   ` <trinity-1d53f520-c371-471b-b6c2-0446d6ba8184-1652865880524@3c-app-gmx-bap04>
@ 2022-05-18 10:23     ` Jonathan Wakely
       [not found]       ` <trinity-dba8f838-b88d-491b-a582-c9be885cb09e-1652951565751@3c-app-gmx-bs42>
  0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2022-05-18 10:23 UTC (permalink / raw)
  To: Roman Kellner; +Cc: Stefan Ring, gcc-help

On Wed, 18 May 2022 at 10:24, Roman Kellner wrote:
>
> Hi Jonathan, hi Stefan, hi all
>
> Thanks for your support.
>
> Since no detailed documenation on coding do's and dont's related to optimizations is available, except reading the code itself which will take me probably ages to get into,
> is there any common good practice how to avoid effects of optimizations:

Why do you want to avoid the effects of optimizations?

If your code is correct, then it will behave the same optimized or
unoptimized, LTO or not. I don't understand what you're so concerned
about that you think you need to tweak optimization options in precise
detail.

If you think LTO might cause problems for your OS, you can just not
use it. Don't contort the code in strange ways to solve hypothetical
problems.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Re: Re: gcc optimization options and lto (detailed info)
       [not found]       ` <trinity-dba8f838-b88d-491b-a582-c9be885cb09e-1652951565751@3c-app-gmx-bs42>
@ 2022-05-19  9:33         ` Jonathan Wakely
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Wakely @ 2022-05-19  9:33 UTC (permalink / raw)
  To: Roman Kellner; +Cc: Stefan Ring, gcc-help

On Thu, 19 May 2022 at 10:12, Roman Kellner wrote:
> What I try to understand is how to write "correct" code, if I need to prevent some dedicated code parts or data structures (typically in flash) from being optimized away or moved around, because the program relies on them.
> Since __attribute__((used)), compare https://www.keil.com/support/man/docs/armclang_ref/armclang_ref_chr1359124978363.htm , volatile etc. are compiler and language features, all I try to further understand is, which influence such declarations and attributes do have on the different optimization features / strategies.
>
> It is not that the optimizers do things wrong, but they do not understand the programmers notion.
> I am looking for ways to tell the optimizers what I need ("This data structure/code part needs to stay where it is / how it is. Do not touch").
> And if others have gathered experience with it, providing some hints (do's and dont's).

Thank you, that makes it clearer. I agree you should be fixing the
code to correctly express the intent, and not adding hundreds of
command-line options to try and micro-manage the optimizers, which is
what you seemed to be asking about (which optimizations are active at
which stages, what they're documented to do, how to avoid very long
command lines etc).

It seems what you're really asking about is how to write correct
embedded code, so that it works correctly with different compilers,
even when optimized. Asking about GCC's LTO optimizations is the wrong
goal, I think.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-05-19  9:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-17  6:56 gcc optimization options and lto (detailed info) muzungu
2022-05-17  8:30 ` Jonathan Wakely
     [not found]   ` <trinity-1d53f520-c371-471b-b6c2-0446d6ba8184-1652865880524@3c-app-gmx-bap04>
2022-05-18 10:23     ` Jonathan Wakely
     [not found]       ` <trinity-dba8f838-b88d-491b-a582-c9be885cb09e-1652951565751@3c-app-gmx-bs42>
2022-05-19  9:33         ` Jonathan Wakely
2022-05-17  8:54 ` Stefan Ring
2022-05-17 23:05 ` Segher Boessenkool

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).