[all-commits] [llvm/llvm-project] f9d078: [PGO] Initialize GCOV Writeout and Reset Functions...

Qiongsi Wu via All-commits all-commits at lists.llvm.org
Thu Oct 17 06:32:36 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: f9d07890640434a4be0e7f651dd295478598b36d
      https://github.com/llvm/llvm-project/commit/f9d07890640434a4be0e7f651dd295478598b36d
  Author: Qiongsi Wu <274595+qiongsiwu at users.noreply.github.com>
  Date:   2024-10-17 (Thu, 17 Oct 2024)

  Changed paths:
    M clang/test/CodeGen/code-coverage.c
    M compiler-rt/include/profile/InstrProfData.inc
    M compiler-rt/lib/profile/GCDAProfiling.c
    M compiler-rt/lib/profile/InstrProfiling.h
    M compiler-rt/lib/profile/InstrProfilingPlatformAIX.c
    M compiler-rt/lib/profile/InstrProfilingPlatformLinux.c
    A compiler-rt/test/profile/AIX/gcov-undef-sym.test
    M llvm/include/llvm/ProfileData/InstrProfData.inc
    M llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
    M llvm/lib/Transforms/Instrumentation/GCOVProfiling.cpp
    A llvm/test/CodeGen/PowerPC/gcov_ctr_ref_init.ll
    M llvm/test/Transforms/GCOVProfiling/kcfi-normalize.ll
    M llvm/test/Transforms/GCOVProfiling/kcfi.ll
    M llvm/test/Transforms/GCOVProfiling/module-flags.ll

  Log Message:
  -----------
  [PGO] Initialize GCOV Writeout and Reset Functions in the Runtime on AIX (#108570)

This PR registers the writeout and reset functions for `gcov` for all
modules in the PGO runtime, instead of registering them
using global constructors in each module. The change is made for AIX
only, but the same mechanism works on Linux on Power.

When registering such functions using global constructors in each module
without `-ffunction-sections`, the AIX linker cannot garbage collect
unused undefined symbols, because such symbols are grouped in the same
section as the `__sinit` symbol. Keeping such undefined symbols causes
link errors (see test case
https://github.com/llvm/llvm-project/pull/108570/files#diff-500a7e1ba871e1b6b61b523700d5e30987900002add306e1b5e4972cf6d5a4f1R1
for this scenario). This PR implements the initialization in the
runtime, hence avoiding introducing `__sinit` into each module.

The implementation adds a new global variable `__llvm_covinit_functions`
to each module. This new global variable contains the function pointers
to the `Writeout` and `Reset` functions. `__llvm_covinit_functions`'s
section is the named section `__llvm_covinit`. The linker will aggregate
all the `__llvm_covinit` sections from each module
to form one single named section in the final binary. The pair of
functions
```
const __llvm_gcov_init_func_struct *__llvm_profile_begin_covinit();
const __llvm_gcov_init_func_struct *__llvm_profile_end_covinit();
```
are implemented to return the start and end address of this named
section in the final binary, and they are used in function
```
__llvm_profile_gcov_initialize()
```
(which is a constructor function in the runtime) so the runtime knows
the addresses of all the `Writeout` and `Reset` functions from all the
modules.

One noticeable implementation detail relevant to AIX is that to preserve
the `__llvm_covinit` from the linker's garbage collection, a `.ref`
pseudo instruction is inserted into them, referring to the section that
contains the `__llvm_gcov_ctr` variables, which are used in the
instrumented code. The `__llvm_gcov_ctr` variables did not belong to
named sections before, but this PR added them to the
`__llvm_gcov_ctr_section` named section, so we can add a `.ref` pseudo
instruction that refers to them in the `__llvm_covinit` section.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list