Recent

Author Topic: How to detect -Os (smaller rather then faster) optimization in code?  (Read 3464 times)

avra

  • Hero Member
  • *****
  • Posts: 2226
    • Additional info
I would like optional inlining of some methods depending on -Os optimization setting. How can I detect in code if this optimization is active? I failed to find relevant info.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9522
  • FPC developer.
Re: How to detect -Os (smaller rather then faster) optimization in code?
« Reply #1 on: July 12, 2021, 11:02:39 am »
Afaik all inline depends on the complexity metric of a function being lower than some threshold. I assume for size optimization that threshold is lower and zero.

Short answer: besides on or off, inlining is not under the user's influence afaik

Only short options are testable with $ifopt, and that is only level 2 optimization
« Last Edit: July 12, 2021, 11:05:32 am by marcov »

avra

  • Hero Member
  • *****
  • Posts: 2226
    • Additional info
Re: How to detect -Os (smaller rather then faster) optimization in code?
« Reply #2 on: July 12, 2021, 11:26:28 am »
besides on or off, inlining is not under the user's influence afaik
I meant my own inlining, not compiler decided inlining. I have a type helper where I would like to have a specific method inlined or not, depending on -Os active or not.

Only short options are testable with $ifopt, and that is only level 2 optimization
So there is no short option for $OPTIMIZATION SIZE that I can use in my type helper method to detect if specific optimization is active? Something like:
Code: Pascal  [Select][+][-]
  1. procedure SetBit(const aIndex: TQuadwordBits; const aNewValue: boolean); {$IFOPT SOME_SHORT_NAME_FOR_OPT_SIZE_SWITCH+} {$ELSE} inline;{$ENDIF}
Is it sensible/reasonable to ask for it in bug tracker?

Thanks! I appreciate your input.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9522
  • FPC developer.
Re: How to detect -Os (smaller rather then faster) optimization in code?
« Reply #3 on: July 12, 2021, 11:44:12 am »
I don't think there is one.

You can file a bugreport of course, though personally I think this is a bit over the top. Even if people agree, I suspect it wouldn't be the fastest resolved bug in history :-)

Simply add a define to your build modes seems a way more logical way of going about it.

jamie

  • Hero Member
  • *****
  • Posts: 4841
Re: How to detect -Os (smaller rather then faster) optimization in code?
« Reply #4 on: July 12, 2021, 12:34:01 pm »
I think it depends if this is a Runtime option or a compile time option ?

For me what I do is use the custom options in the IDE and add a define for compile options..

You won't see the define in your source code but the compiler will.

 As for runtime, if you are dealing with helpers and want to diverge an operation due to how it's getting called then you may have issues.

The only true wisdom is knowing you know nothing

avra

  • Hero Member
  • *****
  • Posts: 2226
    • Additional info
Re: How to detect -Os (smaller rather then faster) optimization in code?
« Reply #5 on: July 12, 2021, 01:53:40 pm »
Simply add a define to your build modes seems a way more logical way of going about it.
I do not find that practical. I was hoping to get my code intentionally and controllably smaller or faster (by controlling inlining optimization by me and not letting compiler decide), based just on a check if the user has set -Os or appropriate checkbox in IDE or command line. If that wouldn't be a complete misunderstanding or misuse of -Os on my side then I will file a bugreport anyway. I do see a usecase for such a feature.

I think it depends if this is a Runtime option or a compile time option ?
Compile time. User does or does not set this checkbox: Lazarus / Project / Project Options / Compiler Options / Compilation and Linking / Smaller rather then faster (-Os). I want to detect it to decide if my code should use inline or not.

Thanks!
« Last Edit: July 12, 2021, 01:58:20 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9522
  • FPC developer.
Re: How to detect -Os (smaller rather then faster) optimization in code?
« Reply #6 on: July 12, 2021, 04:37:09 pm »
Simply add a define to your build modes seems a way more logical way of going about it.
I do not find that practical. I was hoping to get my code intentionally and controllably smaller or faster (by controlling inlining optimization by me and not letting compiler decide),

Inline is always a hint only, never a guarantee.

jamie

  • Hero Member
  • *****
  • Posts: 4841
Re: How to detect -Os (smaller rather then faster) optimization in code?
« Reply #7 on: July 12, 2021, 05:56:27 pm »
I see that Borland C++ has the _Always_Inline option..

So the Borland boys must of thought being like the others was important!  :D
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018