Just Curious: When has anyone "HAD" to use {$OPTIMIZATION OFF} lazarus / 21-General ---------------------------------------------------------------------- Ten_Mile_Hike · 2025-03-27 14:59 I'm curious as to if and in what circumstances anyone here has had to actually use {$OPTIMIZATION OFF} when compiling optimized source code. Was it because the compiler produced buggy code; if so was this in old compiler releases or just recently? {$OPTIMIZATION OFF} // Disable optimizations procedure My_Procedure; begin  Some Code Here end; {$OPTIMIZATION ON} //Reenable optimizations ---------------------------------------------------------------------- Martin_fr · 2025-03-27 15:15 Not exactly in the way of your example, but I have... Several cases of Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IF FPC_Fullversion=30202}{$Optimization NOPEEPHOLE}{$ENDIF}To prevent a bug in the peephole optimizer from generating bad machine code for the units in question. I also have several Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IFDEF NOINLINE}{$INLINE OFF}so I can define that for debugging. I haven't run into any issue where I needed a debug session with some optimised, and some debug-able code. But if that would happen, it might lead to exactly your case. I could also imagine specific other optimisation to be turned off (unconditionally) for some sections of code. Maybe Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$Optimization noORDERFIELDS} Though I would likely prefer other methods (like embedding the relevant data in a record) ---------------------------------------------------------------------- n7800 · 2025-03-27 16:26 Searching in Lazarus sources gives few results in debugger tests. Searching in OPM gives a lot of results. Here is one example in BGRABitmap: 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 {$PUSH}{$OPTIMIZATION OFF} // avoids internal error 2012090607procedure TCustomRenderer3D.SetProjection(const AValue: TProjection3D);begin  FProjection := AValue;  FProjectionDefined := true;end;{$POP}  I also remembered seeing optimization disabled to speed up execution in LazUtils: 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 function SameMethod(const m1, m2: TMethod): boolean;begin{$PUSH}  {$BOOLEVAL ON}// With a full evaluation of the boolean expression the generated code will not// contain conditional statements, which is more efficient on modern processors  Result:=(m1.Code=m2.Code) and (m1.Data=m2.Data);{$POP}end; ---------------------------------------------------------------------- Paolo · 2025-03-27 17:43 this could be a problem for some mathematical calculations, see for example wiki "Kahan summation". the "smart" optimizer could oversimplify the expression completely defeating the purpose of the algorithm ---------------------------------------------------------------------- Martin_fr · 2025-03-27 17:46 Actually I saw this procedure TLazMonitor.Acquire; 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       {$OPTIMIZATION OFF}      for j := 0 to Waitcount-1 do        begin      end;      {$POP}  Part of a spin-lock. So the thread needs to do a couple of cycles of nothing on the CPU. It must not use sleep, as it does not want to give up its runtime. But an optimizer could replace an empty loop with nothing. ---------------------------------------------------------------------- Mr.Madguy · 2025-03-28 07:55 I have never done it in Delphi/FPC, but I've done it in C++. But it's mostly connected to using inline asm and complete lack of runtime code. For example code like this: 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 for I := 0 to 9 do A[I] := B[I]; is turned into memcpy call. And as runtime code is stripped, it causes unresolved references. Sometimes complier removes code, if it thinks, that it does nothing. For example if data is passed to function as pointer, compiler may think, that it does nothing. ---------------------------------------------------------------------- circular · 2025-12-02 04:52 It may be counterintuitive, but $OPTIMIZATION ON is only a synonym of $OPTIMIZATION LEVEL2, as explained in FPC documentation. So it would enable optimizations even if they are turned on in the package/project options. I've confirmed this experimentally. To locally turn off optimizations while respecting project options, one can do: 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     {$OPTIMIZATION OFF}    SetLastCoord(ArcEndPoint(arcDef));    {$OPTIMIZATION DEFAULT} This examples comes from BGRABitmap. ---------------------------------------------------------------------- Thaddy · 2025-12-02 05:38 Imho, level2 optimizations are still - as documented - debugger friendly, but higher optimizations can - as documented - have side effects and that is in some cases an issue. There are still bugs in the optimizer, which is another reason to turn them off if the results - not in speed - between O- and O4 differ. And that will happen. Also, like many here, there is the matter of specific optimizations, which need care during development. So in general there is always the case to turn optimizations off during development. You need to compare both. Never write software with max optimization on ever. You would be fooling yourself. ---------------------------------------------------------------------- BeniBela · 2025-12-02 17:11 Quote from: circular on December 02, 2025, 05:52:35 amIt may be counterintuitive, but $OPTIMIZATION ON is only a synonym of $OPTIMIZATION LEVEL2, as explained in FPC documentation. So it would enable optimizations even if they are turned on in the package/project options. I've confirmed this experimentally. Does this mean that if Level 4 optimizations are enabled on the command-line parameter and you use $OPTIMIZATION ON, it would disable them to revert to Level 2? ---------------------------------------------------------------------- creaothceann · 2025-12-02 22:42 Quote from: BeniBela on December 02, 2025, 06:11:46 pmDoes this mean that if Level 4 optimizations are enabled on the command-line parameter and you use $OPTIMIZATION ON, it would disable them to revert to Level 2? Apparently it does... Quote ON     Switches on optimizations, corresponding to level 2 optimizations. OFF     Switches of [sic] all kinds of optimizations. DEFAULT     Returns to default (i.e. command-line or config file) specified optimizations. ---------------------------------------------------------------------- PascalDragon · 2025-12-04 20:34 Quote from: Thaddy on December 02, 2025, 06:38:41 amImho, level2 optimizations are still - as documented - debugger friendly, but higher optimizations can - as documented - have side effects and that is in some cases an issue. Level 2 optimizations can already lead to code that might lead to issues in the debugger, but don't have side effects. E.g. the REGVAR optimization might be enabled there which can lead to less stack usage or reuse of the same register for different variables without the variable being stored anywhere if the lifetime permits it. After all Lazarus only declares -O1 as more or less debugger friendly for a reason... ---------------------------------------------------------------------- Martin_fr · 2025-12-04 22:28 Quote from: PascalDragon on December 04, 2025, 09:34:40 pmQuote from: Thaddy on December 02, 2025, 06:38:41 amImho, level2 optimizations are still - as documented - debugger friendly, but higher optimizations can - as documented - have side effects and that is in some cases an issue. Level 2 optimizations can already lead to code that might lead to issues in the debugger, but don't have side effects. E.g. the REGVAR optimization might be enabled there which can lead to less stack usage or reuse of the same register for different variables without the variable being stored anywhere if the lifetime permits it. After all Lazarus only declares -O1 as more or less debugger friendly for a reason... Indeed. Level 2 opts provide still mostly intact line-info. Of course - inlined code can't be stepped, or have breakpoints - break, exit statements may merge with the line before them (though that happens in level 1 already), but level 2 may optimize more lines away But, as for variables, values can't be trusted much (well at least not local vars). If they are not in registers, they may still delay updating their memory. So the value may show only once stepped an extra line away. But if they are in registers, they show the value of that register, regardless if it currently is that value. That is, a single register is often used for several vars (in different lines of code), yet the compiler just says "variable foo is in reg x" as if for the whole time. ---------------------------------------------------------------------- ad1mt · 2025-12-05 20:22 I had a problem where inlining of functions plus -O3 optimisation caused the compiler to crash. So I had to either disable inlining of functions, or set -O2 to get an optimised executable. CORRECTION... I had a problem where inlining of functions plus -O2 optimisation caused the compiler to crash. So I had to either disable inlining of functions, or set -O1 to get an optimised executable. ---------------------------------------------------------------------- n7800 · 2025-12-09 23:15 This was already mentioned above, but I think it needs to be made more prominent. Using $PUSH/$POP directives for optimization directives doesn't work, see the FPC issue. Just for information, for anyone who finds this thread.