Question about -O4 optimizations (and ungrouped) lazarus / 62-General ---------------------------------------------------------------------- 440bx · 2020-05-01 23:50 Hello, I've been researching the FPC optimizations.  Particularly, -O4 and the ungrouped ones. There is a page in the Wiki about optimizations that states:QuoteThe ungrouped and -O4 group have potential side effects and may break your code, so use responsibly."use responsibly" is very good advice but, I can't find anywhere a mention of what kind of code would cause problems when asking for those optimizations. That's my question: what code causes -O4 and ungrouped to produce problematic code ? Thank you for your help. ---------------------------------------------------------------------- jamie · 2020-05-02 01:53 I already displayed what happens in -04 when initializing a series of variables... it seems to be a failure of code generation.  But who cares I guess. ---------------------------------------------------------------------- PascalDragon · 2020-05-02 09:27 Quote from: jamie on May 02, 2020, 03:53:52 amI already displayed what happens in -04 when initializing a series of variables... it seems to be a failure of code generation.  But who cares I guess. You did not provide a full example. You only showed a line of code and the result and we showed you that it does compile correctly. So unless you provide a full example that we can reproduce of course we're not going to care. Quote from: 440bx on May 02, 2020, 01:50:28 amHello, I've been researching the FPC optimizations.  Particularly, -O4 and the ungrouped ones. There is a page in the Wiki about optimizations that states:QuoteThe ungrouped and -O4 group have potential side effects and may break your code, so use responsibly."use responsibly" is very good advice but, I can't find anywhere a mention of what kind of code would cause problems when asking for those optimizations. That's my question: what code causes -O4 and ungrouped to produce problematic code ? Thank you for your help. There are two optimizations of which I'm currently aware of that might have side effects: -OoDeadValues and -OoOrderFields (this does not mean that others don't have side effects, it's just that I don't know them right now). The former has this mentioned in the compiler sources: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i          { Allow removing expressions whose result is not used, even when this         can change program behaviour (range check errors disappear,         access violations due to invalid pointer derefences disappear, ...).           Note: it does not (and must not) remove expressions that have             explicit side-effects, only implicit side- effects (like the ones             mentioned before) can disappear.       }         cs_opt_dead_values,  The latter does not really affect you (after all you don't use classes ), but take this example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyClass = class    SomeField: Byte;    SomeField2: LongInt;    SomeField3: Byte;  end; For classes, unlike records the order of fields is not defined. -OoOrderFields makes use of this and internally reorders the fields like this to avoid padding: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyClass = class    SomeField2: LongInt;    SomeField: Byte;    SomeField2: Byte;  end; In normal cases this won't lead to problems. However there are so called "cracker classes" that are used to access private fields of preexisting classes (e.g. in the RTL or LCL) to implement something that's not possible otherwise. For example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyClass = class  strict private    fSomeImportantField: LongInt;  end;   TMyCracker = class // does *not* inherit from TMyClass  public    fSomeImportantField: LongInt;  end; var  t: TMyClass;begin  t := TMyClass.Create;  //t.fSomeImportantField := 42; // this will fail as TMyClass.fSomeImportantField is private  TMyCracker(t).fSomeImportantField := 42; // this works now :Dend. If you now have class A compiled in a unit with field reordering and class B compiled in a unit without then you might access an invalid value. This is also the case if you're lazy and you only use those fields in your cracker up to the on you're interested in. ---------------------------------------------------------------------- 440bx · 2020-05-02 11:16 @PascalDragon, Thank you very much, very useful information. ---------------------------------------------------------------------- JiaXing · 2020-05-07 14:23 To our dear 440bx: Are you a researcher? From what you behaved I think you are more in tune with researcher but not programmer. Because programmer only want their program to be done fast and published it but researcher cares about others very small and minor things (in the mind of the programmer, though.) ---------------------------------------------------------------------- Thaddy · 2020-05-07 15:47 You may want to read the manuals. Simply specifying -O4 is not enough to get the most out of the compiler. You also need to specify the base line instruction set for the CPU and FPU. FreePascal is very conservative with these. I usually specify -CpCOREAVX2 and -CfAVX2 and -OpCOREAVX2 for my average machines on Intel platforms. I even build the compiler with that and -O3 Also note side effects by too strong optimization levels. It makes life hard during debugging. This  goes for any compiler, btw, not just FreePascal. ---------------------------------------------------------------------- 440bx · 2020-05-07 16:52 Quote from: JiaXing on May 07, 2020, 04:23:58 pmTo our dear 440bx: Are you a researcher? From what you behaved I think you are more in tune with researcher but not programmer.No, I am not a researcher.  I research to have sufficient knowledge to write a decent program.  That's all. Quote from: JiaXing on May 07, 2020, 04:23:58 pm Because programmer only want their program to be done fast and published Yes, that does seem to often be the case.  It's the McDonald's paradigm of programming.  Is that form for here or to go ?... would you like a listbox with your form ?...  how about a sprinkling of TLabels on it... ? ---------------------------------------------------------------------- lucamar · 2020-05-07 18:25 Quote from: JiaXing on May 07, 2020, 04:23:58 pmBecause programmer only want their program to be done fast and published it but researcher cares about others very small and minor things (in the mind of the programmer, though.) No. At least not an even mildly experienced programmers. A programmer, like a mechanic or a chef, likes (in fact, needs) to take care of and fully understand the tools of his trade. It's beginners, dabblers and script-kids that just want to do it fast and mean and never care what it's all about.