[llvm-dev] [RFC] Usage of NDEBUG as a guard for non-assert debug code
Michael Kruse via llvm-dev
llvm-dev at lists.llvm.org
Thu Apr 9 22:00:13 PDT 2020
#ifndef NDEBUG in the LLVM source and assert() are at least somewhat
linked. For instance, consider
https://github.com/llvm/llvm-project/blob/8423a6f36386aabced2a367c0ea53487901d31ca/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp#L2668
The #ifndef NDEBUG is used to guard the value that checked in an
assert() later. Only enabling assert() will cause the build to fail
because the value is not defined. Another example is a loop only for
the assert, eg.
#ifndef NDEBUG
for (auto &Val : Container)
assert(Val && "all");
#endif
where the loop would loop over nothing with assertions disable. We
cannot have a 'change all 'NDEBUG to LLVM_NO_DEBUG_CHECKS'. Even if we
manage to leave all NDEBUG that are linked to some use of assert(), it
doubles the number of configurations that might break in some
configuration such as IndVarSimplify. I understand NDEBUG as `remove
all the code only useful for developers`, independent of whether we
also want debug symbols.
I'd find it more useful to discuss what should NOT be covered under
the blanket term LLVM_ENABLE_ASSERTIONS. Example from the past are
LLVM_ENABLE_STATS and LLVM_ENABLE_DUMP that once was also using
NDEBUG. As was mentioned, Support/Debug.h might be another candidate.
But IMHO the compile-time/execution-time/code-size impact of these are
too small to justify the increase combinatorial complexity of
configuration. At the last LLVM DevMtg we actually discussed how to
*reduce* the number of independent configuration parameters.
Michael
Am Do., 9. Apr. 2020 um 09:26 Uhr schrieb David Truby via llvm-dev
<llvm-dev at lists.llvm.org>:
>
> Hi all,
>
> During discussions about assertions in the Flang project, we noticed that there are a lot of cases in LLVM that #ifndef NDEBUG is used as a guard for non-assert code that we want enabled in debug builds.
> This works fine on its own, however it affects the behaviour of LLVM_ENABLE_ASSERTIONS; since NDEBUG controls whether assertions are enabled or not, a lot of debug code gets enabled in addition to asserts if you specify this flag. This goes contrary to the name of the flag I believe also its intention. Specifically in Flang we have a case where someone wants to ship a build with assertions enabled, but doesn't want to drag in all the extra things that are controlled by NDEBUG in LLVM.
>
> In my opinion we ideally want LLVM_ENABLE_ASSERTIONS to _only_ enable assertions and do nothing else. I don't think this is possible without changing the use of NDEBUG elsewhere as NDEBUG controls whether assert is enabled.
> I propose we should be using another macro (something like LLVM_DEBUG_CHECKS ?) that is enabled in Debug builds, and possibly controlled by another cmake flag (LLVM_ENABLE_DEBUG_CHECKS ?) for code that we want enabled for debugging but not in releases. This would allow LLVM_ENABLE_ASSERTIONS to do what it says on the tin and actually enable assertions only.
>
> Does anyone else have any thoughts on this?
>
> Thanks
> David Truby
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
More information about the llvm-dev
mailing list