Where to look for profiling runtime initialization hooks?

The following is an excerpt from Source based code coverage documentation:

By default the compiler runtime uses a static initializer to determine the
profile output path and to register a writer function. To collect profiles
without using static initializers, do this manually:

  • Export a int __llvm_profile_runtime symbol from each instrumented shared
    ┊ library and executable. When the linker finds a definition of this symbol, it
    ┊ knows to skip loading the object which contains the profiling runtime’s
    ┊ static initializer.

Can anyone point me to source code/documentation/discussion on how the the static initializer works for profiling? Ultimately, I’m curious how __llvm_profile_initialize is called in InstrProfilingFile.c to set up the filename/write at exit. I saw the RegisterRuntime function defined in InstrProfilingRuntime.cpp, but didn’t find any function in llvm itself that called the function.

By default, instrumented builds are linked with -u__llvm_profile_runtime which tells the linker that if __llvm_profile_runtime is undefined, then it must try to link it from a static library.

So if you don’t export a int __llvm_profile_runtime in your source like it says in the docs, then the linker will load InstrProfilingRuntime.cpp. (I just now noticed this file has changed. Hopefully it has the same behavior.)

So __llvm_profile_initialize() is called in RegisterRuntime(), which will be called when constructing the global INSTR_PROF_PROFILE_RUNTIME_VAR (__llvm_profile_runtime).

1 Like

Thanks for the reply; I’m working with an older llvm version where the RegisterRuntime function is part of a class and isn’t called in the file (i.e. pre-commit 461a183). Your explanation and the new code makes sense to me.

The old code actually works almost the same way.

When this file is linked in (depending on __llvm_profile_runtime) the constructor for the Registration global is implicitly called which in turn calls __llvm_profile_initialize().

Right! Haha I’m too used to looking for () when searching for function calls. Thanks again.