Currently, the PGO runtime only creates unique per process profile files when both of the following conditions are true
- __llvm_profile_initialize is triggered through global variable initialization (e.g. when exec starts an instrumented binary).
- When there are no existing file patterns set. See https://github.com/llvm/llvm-project/blob/bb6d60bf9dfea82814d9e50c5bb457c47d010611/compiler-rt/lib/profile/InstrProfilingFile.c#L795
This patch changes the behaviour so that a new profile file name is generated as soon as a fork system call creates a new process from an instrumented binary. The patch uses the pthread_atfork hook to register the initialization code, and modifies the file name pattern parsing logic so that when %p is present in the pattern, a unique file name can be generated even though the pattern already exists.
Additionally, the unique profile files are created before the profiling starts. If a process is not terminated gracefully through exit, an empty profile file for that process will exist, indicating that the profiling may be incomplete due to the process's abnormal termination.
Note that the unique profile per process is only generated when %p is present in the profile file name pattern. Additionally, this patch does not support Windows because the implementation relies on pthread.
The change in compiler-rt/lib/profile/InstrProfilingFile.c affects non-AIX platforms too, so won't they need -lpthreads too?