Recent

Author Topic: Just Curious: When has anyone "HAD" to use {$OPTIMIZATION OFF}  (Read 591 times)

Ten_Mile_Hike

  • Full Member
  • ***
  • Posts: 100
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

When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11006
  • Debugger - SynEdit - and more
    • wiki
Re: Just Curious: When has anyone "HAD" to use {$OPTIMIZATION OFF}
« Reply #1 on: March 27, 2025, 04:15:08 pm »
Not exactly in the way of your example, but I have...

Several cases of
Code: Text  [Select][+][-]
  1. {$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][+][-]
  1. {$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][+][-]
  1. {$Optimization noORDERFIELDS}

Though I would likely prefer other methods (like embedding the relevant data in a record)

n7800

  • Sr. Member
  • ****
  • Posts: 280
Re: Just Curious: When has anyone "HAD" to use {$OPTIMIZATION OFF}
« Reply #2 on: March 27, 2025, 05:26:01 pm »
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][+][-]
  1. {$PUSH}{$OPTIMIZATION OFF} // avoids internal error 2012090607
  2. procedure TCustomRenderer3D.SetProjection(const AValue: TProjection3D);
  3. begin
  4.   FProjection := AValue;
  5.   FProjectionDefined := true;
  6. end;
  7. {$POP}
  8.  

I also remembered seeing optimization disabled to speed up execution in LazUtils:

Code: Pascal  [Select][+][-]
  1. function SameMethod(const m1, m2: TMethod): boolean;
  2. begin
  3. {$PUSH}  {$BOOLEVAL ON}
  4. // With a full evaluation of the boolean expression the generated code will not
  5. // contain conditional statements, which is more efficient on modern processors
  6.   Result:=(m1.Code=m2.Code) and (m1.Data=m2.Data);
  7. {$POP}
  8. end;
  9.  

Paolo

  • Hero Member
  • *****
  • Posts: 580
Re: Just Curious: When has anyone "HAD" to use {$OPTIMIZATION OFF}
« Reply #3 on: March 27, 2025, 06:43:25 pm »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 11006
  • Debugger - SynEdit - and more
    • wiki
Re: Just Curious: When has anyone "HAD" to use {$OPTIMIZATION OFF}
« Reply #4 on: March 27, 2025, 06:46:04 pm »
Actually I saw this
procedure TLazMonitor.Acquire;
Code: Pascal  [Select][+][-]
  1.       {$OPTIMIZATION OFF}
  2.       for j := 0 to Waitcount-1 do
  3.         begin
  4.         end;
  5.       {$POP}
  6.  

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

  • Hero Member
  • *****
  • Posts: 869
Re: Just Curious: When has anyone "HAD" to use {$OPTIMIZATION OFF}
« Reply #5 on: March 28, 2025, 08:55:57 am »
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][+][-]
  1. for I := 0 to 9 do A[I] := B[I];
  2.  
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.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

 

TinyPortal © 2005-2018