[LLVMdev] weak_odr constant versus weak_odr global

Duncan Sands
Wed, 02 Nov 2011 08:45:51 -0000

Hi Richard,

> This segfaults, because... in TU1, we get:
>
>    @_ZN1UI1SE1kE = weak_odr constant i32 42, align 4
>
> and in TU2, we get:
>
>    @_ZN1UI1SE1kE = weak_odr global i32 0, align 4

here one version has an initial value of 42, and the other an initial
value of 0.  Doesn't this break the One Definition Rule (the odr in
weak_odr)?  I realize that the constantness is the real issue here,
but I thought I should mention this.

> Is this a clang bug (should we be generating a weak_odr global, and losing
> optimization opportunities in TU1), or is this an llvm bug (should weak_odr
> constants be banned from read-only sections, since another module might
write
> to them)?

Dragonegg has the following comment in the analogous spot:

   // Allow loads from constants to be folded even if the constant has weak
   // linkage.  Do this by giving the constant weak_odr linkage rather than
   // weak linkage.  It is not clear whether this optimization is valid (see
   // gcc bug 36685), but mainline gcc chooses to do it, and fold may already
   // have done it, so we might as well join in with gusto.

Ciao, Duncan.