[PATCH 5/5] Remove const attribute on get_log2

Florian Weimer fweimer@redhat.com
Wed May 15 07:44:00 GMT 2013


On 05/13/2013 07:26 PM, Peter Collingbourne wrote:
> On Mon, May 13, 2013 at 6:29 PM, Richard Henderson <rth@twiddle.net> wrote:
>>
>> On 05/13/2013 08:36 AM, Peter Collingbourne wrote:
>>> This function is not const, as it can modify log2_m and log2_m_inited.
>>
>> So?  The const attribute gets gcc to treat it as const, which means
>> that the call of get_log2 can be deleted if unused, or the call moved
>> outside of a loop.  IMO this would be a bug in LLVM if it errors for this.
>
> Those aren't necessarily the only optimisations that can be performed on a
> const function call.  The issue in this particular case is that LLVM's
> optimiser will
> replace the call to the function with a reference to its return value, log2_m
> (thereby skipping its initialisation).  Given that the source code is
> lying to the
> compiler about the constness of the function, this isn't entirely unreasonable.
> While one could in theory detect such cases in LLVM, I consider it to be a bug
> in glibc that it is lying to the compiler.

I think that at one point, it was fully acceptable to label functions 
which perform memoization as "const".  (The attribute is *way* older 
than IPA.)  I'm not sure if this true anymore.  The attribute would 
certainly more useful if such usage is permitted.

I'll ask on the GCC mailing list for advice.  The documentation could 
probably be more clear.

-- 
Florian Weimer / Red Hat Product Security Team



More information about the Libc-alpha mailing list