[all-commits] [llvm/llvm-project] a1532e: [InstrProfiling] Make CountersPtr in __profd_ rela...

Fangrui Song via All-commits all-commits at lists.llvm.org
Fri Jul 30 11:52:33 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: a1532ed27582038e2d9588108ba0fe8237f01844
      https://github.com/llvm/llvm-project/commit/a1532ed27582038e2d9588108ba0fe8237f01844
  Author: Fangrui Song <i at maskray.me>
  Date:   2021-07-30 (Fri, 30 Jul 2021)

  Changed paths:
    M clang/test/Profile/c-linkage-available_externally.c
    M compiler-rt/include/profile/InstrProfData.inc
    M compiler-rt/lib/profile/InstrProfilingMerge.c
    M compiler-rt/lib/profile/InstrProfilingWriter.c
    M llvm/include/llvm/ProfileData/InstrProfData.inc
    M llvm/lib/ProfileData/InstrProfReader.cpp
    M llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
    M llvm/test/Instrumentation/InstrProfiling/icall.ll
    M llvm/test/Instrumentation/InstrProfiling/profiling.ll
    M llvm/test/Transforms/PGOProfile/comdat_internal.ll
    M llvm/test/Transforms/PGOProfile/indirect_call_profile.ll
    M llvm/test/Transforms/PGOProfile/memcpy.ll
    M llvm/test/tools/llvm-profdata/Inputs/c-general.profraw
    M llvm/test/tools/llvm-profdata/raw-32-bits-be.test
    M llvm/test/tools/llvm-profdata/raw-32-bits-le.test
    M llvm/test/tools/llvm-profdata/raw-64-bits-be.test
    M llvm/test/tools/llvm-profdata/raw-64-bits-le.test

  Log Message:
  -----------
  [InstrProfiling] Make CountersPtr in __profd_ relative

Change `CountersPtr` in `__profd_` to a label difference, which is a link-time
constant. On ELF, when linking a shared object, this requires that `__profc_` is
either private or linkonce/linkonce_odr hidden. On COFF, we need D104564 so that
`.quad a-b` (64-bit label difference) can lower to a 32-bit PC-relative relocation.

```
# ELF: R_X86_64_PC64 (PC-relative)
.quad .L__profc_foo-.L__profd_foo

# Mach-O: a pair of 8-byte X86_64_RELOC_UNSIGNED and X86_64_RELOC_SUBTRACTOR
.quad l___profc_foo-l___profd_foo

# COFF: we actually use IMAGE_REL_AMD64_REL32/IMAGE_REL_ARM64_REL32 so
# the high 32-bit value is zero even if .L__profc_foo < .L__profd_foo
# As compensation, we truncate CountersDelta in the header so that
# __llvm_profile_merge_from_buffer and llvm-profdata reader keep working.
.quad .L__profc_foo-.L__profd_foo
```

(Note: link.exe sorts `.lprfc` before `.lprfd` even if the object writer
has `.lprfd` before `.lprfc`, so we cannot work around by reordering
`.lprfc` and `.lprfd`.)

With this change, a stage 2 (`-DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_BUILD_INSTRUMENTED=IR`)
`ld -pie` linked clang is 1.74% smaller due to fewer R_X86_64_RELATIVE relocations.
```
% readelf -r pie | awk '$3~/R.*/{s[$3]++} END {for (k in s) print k, s[k]}'
R_X86_64_JUMP_SLO 331
R_X86_64_TPOFF64 2
R_X86_64_RELATIVE 476059  # was: 607712
R_X86_64_64 2616
R_X86_64_GLOB_DAT 31
```

The absolute function address (used by llvm-profdata to collect indirect call
targets) can be converted to relative as well, but is not done in this patch.

Differential Revision: https://reviews.llvm.org/D104556




More information about the All-commits mailing list