www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - Switch -g disables optimizations.

reply Marco Leise <Marco.Leise gmx.de> writes:
I found the root for the horrible codegen with ldc2 I was
experiencing. As soon as I added -g to get some line numbers
in disassemblies, one or more optimizations steps got disabled.

  int main(string[])
  {
    return 0;
  }

compiles with -O to:

  xor eax,eax
  ret

and with -O -g to:

  push   rbp
  mov    rbp,rsp
  xor    eax,eax
  pop    rbp
  ret    

ldc2 -version:
LDC - the LLVM D compiler (0.16.0):
  based on DMD v2.067.1 and LLVM 3.6.2

Is that expected to improve the debugging experience and make
local variables always available or a bug? clang's codegen is
not influenced in this way by the -g flag.

-- 
Marco
Oct 08 2015
parent reply Joakim <dlang joakim.fea.st> writes:
On Friday, 9 October 2015 at 04:22:51 UTC, Marco Leise wrote:
 I found the root for the horrible codegen with ldc2 I was 
 experiencing. As soon as I added -g to get some line numbers in 
 disassemblies, one or more optimizations steps got disabled.

   int main(string[])
   {
     return 0;
   }

 compiles with -O to:

   xor eax,eax
   ret

 and with -O -g to:

   push   rbp
   mov    rbp,rsp
   xor    eax,eax
   pop    rbp
   ret

 ldc2 -version:
 LDC - the LLVM D compiler (0.16.0):
   based on DMD v2.067.1 and LLVM 3.6.2

 Is that expected to improve the debugging experience and make 
 local variables always available or a bug? clang's codegen is 
 not influenced in this way by the -g flag.
Optimization is turned off in debug mode, with a comment claiming debug info doesn't work with optimization: https://github.com/ldc-developers/ldc/blob/9d3dd1f609e08f001aeec2cacb1c55707f37884b/gen/optimizer.cpp#L179
Oct 08 2015
parent reply David Nadlinger via digitalmars-d-ldc <digitalmars-d-ldc puremagic.com> writes:
On 9 Oct 2015, at 7:46, Joakim via digitalmars-d-ldc wrote:
 On Friday, 9 October 2015 at 04:22:51 UTC, Marco Leise wrote:
 push   rbp
 mov    rbp,rsp
 xor    eax,eax
 pop    rbp
 ret
Optimization is turned off in debug mode, with a comment claiming debug info doesn't work with optimization: https://github.com/ldc-developers/ldc/blob/9d3dd1f609e08f001aeec2cacb1c55707f37884b/gen/optimizer.cpp#L179
We should actually look into re-enabling that. I don't know the story behind that comment, and I'm not sure who does. In this particular case, though, the reason for the "ugly" codegen is that LDC enables -disable-fp-elim (the equivalent of GCC's -fno-omit-frame-pointer) by default when -g is passed. I suppose the reason for this was that all the druntime backtracing code (inherited from DMD) relies on a full call frame being set up for each function. — David
Oct 09 2015
next sibling parent Marco Leise <Marco.Leise gmx.de> writes:
Am Fri, 09 Oct 2015 09:24:05 +0200
schrieb David Nadlinger via digitalmars-d-ldc
<digitalmars-d-ldc puremagic.com>:

 On 9 Oct 2015, at 7:46, Joakim via digitalmars-d-ldc wrote:
 On Friday, 9 October 2015 at 04:22:51 UTC, Marco Leise wrote:
 push   rbp
 mov    rbp,rsp
 xor    eax,eax
 pop    rbp
 ret
Optimization is turned off in debug mode, with a comment claiming=20 debug info doesn't work with optimization: https://github.com/ldc-developers/ldc/blob/9d3dd1f609e08f001aeec2cacb1c=
55707f37884b/gen/optimizer.cpp#L179
=20
 We should actually look into re-enabling that. I don't know the story=20
 behind that comment, and I'm not sure who does.
=20
 In this particular case, though, the reason for the "ugly" codegen is=20
 that LDC enables -disable-fp-elim (the equivalent of GCC's=20
 -fno-omit-frame-pointer) by default when -g is passed. I suppose the=20
 reason for this was that all the druntime backtracing code (inherited=20
 from DMD) relies on a full call frame being set up for each function.
=20
   =E2=80=94 David
=20 Then this was all intentional. It's just that neither Clang nor GDC do this and I generalized "-g" to be adding debug info to an otherwise unchanged compile. Maybe the other compilers also change to some saner defaults when -g is used. The stack unwinding code in druntime used to blow up anyways as soon as you call through GCC compiled functions, with -fomit-frame-pointer as default (on amd64). It's rather surprising when the default system ABI causes unexpected runtime crashes. --=20 Marco
Oct 09 2015
prev sibling parent Kai Nacke <kai redstar.de> writes:
On Friday, 9 October 2015 at 07:24:17 UTC, David Nadlinger wrote:
 On 9 Oct 2015, at 7:46, Joakim via digitalmars-d-ldc wrote:
 On Friday, 9 October 2015 at 04:22:51 UTC, Marco Leise wrote:
 push   rbp
 mov    rbp,rsp
 xor    eax,eax
 pop    rbp
 ret
Optimization is turned off in debug mode, with a comment claiming debug info doesn't work with optimization: https://github.com/ldc-developers/ldc/blob/9d3dd1f609e08f001aeec2cacb1c55707f37884b/gen/optimizer.cpp#L179
We should actually look into re-enabling that. I don't know the story behind that comment, and I'm not sure who does.
There were once problems with debug info and code optimization. We should re-check this when we drop support for older LLVM versions. Regards, Kai
Oct 10 2015