Thread overview
Switch -g disables optimizations.
Oct 09, 2015
Marco Leise
Oct 09, 2015
Joakim
Oct 09, 2015
David Nadlinger
Oct 09, 2015
Marco Leise
Oct 10, 2015
Kai Nacke
October 09, 2015
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

October 09, 2015
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
October 09, 2015
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

October 09, 2015
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 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

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.

-- 
Marco

October 10, 2015
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