On preserving unused file-local definitions at -O0

Clang fails a bunch of the GDB testsuite because clang optimizes* away
file-local (namespace scope static) functions and variables at -O0.

I sent a patch to the GDB mailing list to 'fix' these tests by using
non-static functions/variables, or marking them used with
__attribute__((used)). While there's precedent for both, I've had one
reply pushing back a little that Clang should change behavior here and
produce code for these unused definitions.

Before I go back to the GDB mailing list and say that Clang will not
change its behavior here, I figured I'd just put it out there and ask.
If it comes to it I can file a bug, resolve it as "won't fix"/"by
design"/"invalid", and then mark these GDB tests as KFAIL against that
bug if the two communities can't come to an agreement about the
correct behavior here.

It's only been hours since I posted to GDB so the opinion expressed
isn't the only one or necessarily the will of the GDB community, but I
wanted to cover all my bases by having the conversation here too
before I go representing the Clang community opinion over there.

For myself I think there's some merit to this and realize there's
continuous tension between -O0 used for debugging ("don't make my code
hard to debug") and -O0 used for JIT or similar situations ("give me
code as quickly as possible"). At some point these things may need to
be split (-Og or some other approach) I would guess, though so long as
Clang's -O0 leans towards "give me code quickly" we'll need to do
something about the GDB tests that assume -O0 for debugging fidelity.

* it's a very cheap optimization - in the sense that we just don't do
any IRGen for them because we see they're unreferenced

From: cfe-dev-bounces@cs.uiuc.edu [mailto:cfe-dev-bounces@cs.uiuc.edu]
On Behalf Of David Blaikie
Sent: Friday, April 11, 2014 8:59 AM
To: cfe-dev Developers; Eric Christopher
Subject: [cfe-dev] On preserving unused file-local definitions at -O0

Clang fails a bunch of the GDB testsuite because clang optimizes* away
file-local (namespace scope static) functions and variables at -O0.

I sent a patch to the GDB mailing list to 'fix' these tests by using
non-static functions/variables, or marking them used with
__attribute__((used)). While there's precedent for both, I've had one
reply pushing back a little that Clang should change behavior here and
produce code for these unused definitions.

Before I go back to the GDB mailing list and say that Clang will not
change its behavior here, I figured I'd just put it out there and ask.
If it comes to it I can file a bug, resolve it as "won't fix"/"by
design"/"invalid", and then mark these GDB tests as KFAIL against that
bug if the two communities can't come to an agreement about the
correct behavior here.

It's only been hours since I posted to GDB so the opinion expressed
isn't the only one or necessarily the will of the GDB community, but I
wanted to cover all my bases by having the conversation here too
before I go representing the Clang community opinion over there.

For myself I think there's some merit to this and realize there's
continuous tension between -O0 used for debugging ("don't make my code
hard to debug") and -O0 used for JIT or similar situations ("give me
code as quickly as possible"). At some point these things may need to
be split (-Og or some other approach) I would guess, though so long as
Clang's -O0 leans towards "give me code quickly" we'll need to do
something about the GDB tests that assume -O0 for debugging fidelity.

* it's a very cheap optimization - in the sense that we just don't do
any IRGen for them because we see they're unreferenced

For Clang, run the GDB suite with -femit_all_decls ?
--paulr

For Clang, run the GDB suite with -femit_all_decls ?
--paulr

Speling it korecly, -femit-all-decls (duh).
--paulr

Huh - I think I vaguely knew about that flag at some point & forgot
again (or never knew about it in the first place).

Neat-ish. Poor name of the flag (given it's about definitions, not
declarations).

Still want to try to get the GDB test suite clean with clang's default
configuration, ideally, one way or another. But actually the
difference in behavior between GCC's default and Clang's default or
Clang with -femit-all-decls is an interesting point. Clang either
emits 'everything' (unused static definitions and unused inline
definitions) or nothing. GCC is somewhere in the middle (emits unused
static definitions, but doesn't emit unused inline definitions) - so
perhaps that's a useful point to make to the GDB community that GCC
doesn't have a strong principle here... maybe. (I can see some
possible counterarguments already, though)

- David

Right, and you start straying into questions of whether the GDB test
suite is more properly verifying GDB's behavior or the compiler's
behavior. I think it's a highly desirable goal to ensure that Clang's
output can be consumed by GDB, which is not quite the same thing as
ensuring that Clang's default behavior satisfies the assumptions made
by the GDB test suite.

All good fodder for an LLVM-social discussion, sometime.
--paulr

Yep, all a bit vague/tricky.

Clang fails a bunch of the GDB testsuite because clang optimizes* away
file-local (namespace scope static) functions and variables at -O0.

I sent a patch to the GDB mailing list to 'fix' these tests by using
non-static functions/variables, or marking them used with
__attribute__((used)). While there's precedent for both, I've had one
reply pushing back a little that Clang should change behavior here and
produce code for these unused definitions.

Before I go back to the GDB mailing list and say that Clang will not
change its behavior here, I figured I'd just put it out there and ask.
If it comes to it I can file a bug, resolve it as "won't fix"/"by
design"/"invalid", and then mark these GDB tests as KFAIL against that
bug if the two communities can't come to an agreement about the
correct behavior here.

It's only been hours since I posted to GDB so the opinion expressed
isn't the only one or necessarily the will of the GDB community, but I
wanted to cover all my bases by having the conversation here too
before I go representing the Clang community opinion over there.

Mostly this is what __attribute__((used)) is to be used for, anything
else is just relying on iffy behavior. That said, the attribute is a
compiler specific workaround to say "please keep this dead code" and
most compilers will have something similar - I don't think the
debugger should be relying upon it for tests. :stuck_out_tongue:

-eric