[cfe-dev] [LLVMdev] weak_odr constant versus weak_odr global

John McCall rjmccall at apple.com
Wed Nov 9 13:01:22 PST 2011


On Nov 9, 2011, at 11:34 AM, Rafael EspĂ­ndola wrote:
>>> 1) [Requires ABI change] We emit dynamic initialization code for weak globals
>>> (even in TUs where static initialization is required to be performed), unless
>>> we can prove that every translation unit will use static initialization. We
>>> emit the global plus its guard variable as a single object so the linker can't
>>> separate them (this is the ABI change). If we can perform static
>>> initialization in any translation unit, then that TU emits a constant weak
>>> object (in .rodata if we want) containing the folded value and with the guard
>>> variable set to 1 (per Eli's proposal).
>> 
>> The ABI actually suggests doing exactly this, except using multiple
>> symbols linked with a COMDAT group.  Unfortunately, LLVM doesn't
>> support that COMDAT feature yet, but it could certainly be taught to.
>> This guarantees correctness as long as every translation unit emits the
>> code the same way, which is exactly what we'd get from an ABI change,
>> except without actually breaking ABI conformance.
> 
> I like this. We already have basic support for COMDATs, but yes, it
> needs to be extended. So far we just create trivial COMDATs in codegen
> for weak objects.
> 
> We also need the IL linker itself needs to work on COMDATs too
> otherwise this bug would still exist when doing LTO.
> 
> In the "extended" example we would output
> 
> @_ZN1UI1SE1kE = weak_odr constant i32 42, align 4, comdat _ZN1UI1SE1kE
> 
> for TU1 and
> 
> @_ZN1UI1SE1kE = weak_odr global i32 0, align 4, comdat _ZN1UI1SE1kE
> ...
> define internal void @_GLOBAL__I_a() nounwind section ".text.startup"
> comdat _ZN1UI1SE1kE  {
> ....
> }
> 
> for TU2.

Unfortunately, making the comdat be for the entire function is not
conformant with the ABI, which says that you either put the variable
and its guard in different comdats or you put them in a single comdat
named for the variable.  It also doesn't actually help unless we disable
inlining.

So we still need to emit a guard variable (initialized to 1) into the
comdat for constant-initialized static locals, unless we can somehow
prove to our satisfaction that all translation units don't need this.
And we'd need LLVM to not throw away unused weak_odr globals
that are in a comdat with a used symbol.

John.



More information about the cfe-dev mailing list