This patch subsumes https://reviews.llvm.org/D134253.
It addresses a couple of problems:
- The problem addressed by D134253; namely, incorrect behavior when linking a shared library that has PGO into an executable with PGO (see LIT test below). The symbol keep was seen as duplicate, and one copy was thrown away; we want both to stay as keep was creating a reference to other internal symbols that we want the linker to keep.
- The keep symbol was visible to the user and would conflict with a user symbol with the same name.
- When compiling and linking with -ffunction-sections -Wl,-bcdtors:mbr, the PGO runtime was not initialized.
- When compiling and linking with -ffunction-sections -Wl,-bcdtors:csect, the PGO runtime was not initialized. The constructor function that does the runtime registration must be associated to a live variable, otherwise under the semantics of -Wl,-bcdtors:csect it would get GC'ed by the linker.
The following changes are made:
- Convert the library-internal function keep into an array (__llvm_profile_keep) that references the dummy variables that we want to retain.
- associate the liveness of __llvm_profile_keep with the runtime registration variable since we want that array to be live when the profile write out code is.
- Preserve the runtime hook variable (__llvm_profile_runtime) explicitly on AIX, same as on Linux.
- On AIX only, perform the runtime registration from a constructor of a variable that is guaranteed to be live; so we chose the runtime hook as that variable. This is needed to allow linking PGO rt with -Wl,-bcdtors:csect.
- Refactor the runtime registration logic to accomodate for AIX.