llvm-link: why link '@llvm.global_ctors' into dest file even it's not used in dest file?

Hi all,

Recently I do some jobs based on llvm-link tool.
I wonder why link ‘@llvm.global_ctors’ into dest file, even it’s not used in dest file?
And how can I remove it?

Thank you all in advance!
Fangqing
Xilinx Inc.

Hi,

Hi all,

Recently I do some jobs based on llvm-link tool.
I wonder why link ‘@llvm.global_ctors’ into dest file, even it’s not used in dest file?

This is a “magic” global variable: https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable

It is implicitly used by the loader of the program before entering main().

Think about how a C++ global variable constructor can register itself and the “destination file” you’re mentionning can query this registry.

And how can I remove it?

Like other global variables? You need to transform the module yourself before linking it (if you believe it is safe to do so).

Hi,

    > Hi all,

    > Recently I do some jobs based on llvm-link tool. I wonder
    > why link '@llvm.global_ctors' into dest file, even it's not
    > used in dest file?

    > This is a "magic" global variable:
    > LLVM Language Reference Manual — LLVM 18.0.0git documentation

    > It is implicitly used by the loader of the program before
    > entering main().

    > Think about how a C++ global variable constructor can
    > register itself and the "destination file" you're mentionning
    > can query this registry.

But it is true that the dead-code elimination phases could remove it
when not used...

    > And how can I remove it?

    > Like other global variables? You need to transform the module
    > yourself before linking it (if you believe it is safe to do
    > so).

In our SYCL compiler I wrote a pass to remove it:

Look at
https://github.com/triSYCL/triSYCL/blob/master/doc/architecture.rst for
some context.