[AIX][PGO] Enable linux style PGO on AIX

This patch switches the PGO implementation on AIX from using the runtime
registration-based section tracking to the __start_SECNAME/__stop_SECNAME
based. In order to enable the recognition of __start_SECNAME/__stop_SECNAME
symbols in the AIX linker, the -bdbg:namedsects:ss needs to be used.

Reviewed By: jsji, MaskRay, davidxl

Differential Revision: https://reviews.llvm.org/D124857
This commit is contained in:
Wael Yehia 2022-05-03 10:27:15 -04:00
parent 58abe36ae7
commit 2407c13aa4
7 changed files with 51 additions and 14 deletions

View File

@ -117,7 +117,7 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
options::OPT_fno_profile_generate, false) ||
Args.hasArg(options::OPT_fcreate_profile) ||
Args.hasArg(options::OPT_coverage))
CmdArgs.push_back("-bdbg:namedsects");
CmdArgs.push_back("-bdbg:namedsects:ss");
// Specify linker output file.
assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");

View File

@ -664,7 +664,7 @@
// CHECK-PGO-NON-LTO: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
// CHECK-PGO-NON-LTO: "-isysroot" "[[SYSROOT:[^"]+]]"
// CHECK-PGO-NON-LTO: "{{.*}}ld{{(.exe)?}}"
// CHECK-PGO-NON-LTO: "-bdbg:namedsects"
// CHECK-PGO-NON-LTO: "-bdbg:namedsects:ss"
// CHECK-PGO-NON-LTO: "-b32"
// CHECK-PGO-NON-LTO: "-bpT:0x10000000" "-bpD:0x20000000"
// CHECK-PGO-NON-LTO: "[[SYSROOT]]/usr/lib{{/|\\\\}}crt0.o"
@ -692,7 +692,7 @@
// CHECK-PGO-LTO: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
// CHECK-PGO-LTO: "-isysroot" "[[SYSROOT:[^"]+]]"
// CHECK-PGO-LTO: "{{.*}}ld{{(.exe)?}}"
// CHECK-PGO-LTO: "-bdbg:namedsects"
// CHECK-PGO-LTO: "-bdbg:namedsects:ss"
// CHECK-PGO-LTO: "-b32"
// CHECK-PGO-LTO: "-bpT:0x10000000" "-bpD:0x20000000"
// CHECK-PGO-LTO: "[[SYSROOT]]/usr/lib{{/|\\\\}}crt0.o"

View File

@ -7,10 +7,13 @@
\*===----------------------------------------------------------------------===*/
#if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
(defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__)
(defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__) || \
defined(_AIX)
#if !defined(_AIX)
#include <elf.h>
#include <link.h>
#endif
#include <stdlib.h>
#include <string.h>
@ -227,4 +230,43 @@ COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
}
#endif
#if defined(_AIX)
// Empty stubs to allow linking object files using the registration-based scheme
COMPILER_RT_VISIBILITY
void __llvm_profile_register_function(void *Data_) {}
COMPILER_RT_VISIBILITY
void __llvm_profile_register_names_function(void *NamesStart,
uint64_t NamesSize) {}
// The __start_SECNAME and __stop_SECNAME symbols (for SECNAME \in
// {"__llvm_prf_cnts", "__llvm_prf_data", "__llvm_prf_name", "__llvm_prf_vnds"})
// are always live when linking on AIX, regardless if the .o's being linked
// reference symbols from the profile library (for example when no files were
// compiled with -fprofile-generate). That's because these symbols are kept
// alive through references in constructor functions that are always live in the
// default linking model on AIX (-bcdtors:all). The __start_SECNAME and
// __stop_SECNAME symbols are only resolved by the linker when the SECNAME
// section exists. So for the scenario where the user objects have no such
// section (i.e. when they are compiled with -fno-profile-generate), we always
// define these zero length variables in each of the above 4 sections.
COMPILER_RT_VISIBILITY int dummy_cnts[0] COMPILER_RT_SECTION(
COMPILER_RT_SEG INSTR_PROF_CNTS_SECT_NAME);
COMPILER_RT_VISIBILITY int dummy_data[0] COMPILER_RT_SECTION(
COMPILER_RT_SEG INSTR_PROF_DATA_SECT_NAME);
COMPILER_RT_VISIBILITY const int dummy_name[0] COMPILER_RT_SECTION(
COMPILER_RT_SEG INSTR_PROF_NAME_SECT_NAME);
COMPILER_RT_VISIBILITY int dummy_vnds[0] COMPILER_RT_SECTION(
COMPILER_RT_SEG INSTR_PROF_VNODES_SECT_NAME);
// Create a fake reference to avoid GC'ing of the dummy variables by the linker.
// Ideally, we create a ".ref" of each variable inside the function
// __llvm_profile_begin_counters(), but there's no source level construct
// that allows us to generate that.
__attribute__((destructor)) void keep() {
int volatile use = &dummy_cnts < &dummy_data && &dummy_name < &dummy_vnds;
(void)use;
}
#endif
#endif

View File

@ -8,7 +8,7 @@
#if !defined(__APPLE__) && !defined(__linux__) && !defined(__FreeBSD__) && \
!defined(__Fuchsia__) && !(defined(__sun__) && defined(__svr4__)) && \
!defined(__NetBSD__) && !defined(_WIN32)
!defined(__NetBSD__) && !defined(_WIN32) && !defined(_AIX)
#include <stdlib.h>
#include <stdio.h>

View File

@ -855,7 +855,7 @@ static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
if (TT.isOSDarwin())
return false;
// Use linker script magic to get data/cnts/name start/end.
if (TT.isOSLinux() || TT.isOSFreeBSD() || TT.isOSNetBSD() ||
if (TT.isOSAIX() || TT.isOSLinux() || TT.isOSFreeBSD() || TT.isOSNetBSD() ||
TT.isOSSolaris() || TT.isOSFuchsia() || TT.isPS4() || TT.isOSWindows())
return false;

View File

@ -50,7 +50,7 @@ declare void @llvm.instrprof.increment(i8*, i64, i32, i32)
; SOLARIS-NOT: define internal void @__llvm_profile_register_functions
; PS4-NOT: define internal void @__llvm_profile_register_functions
; WINDOWS-NOT: define internal void @__llvm_profile_register_functions
; AIX: define internal void @__llvm_profile_register_functions
; AIX-NOT: define internal void @__llvm_profile_register_functions
;; PR38340: When dynamic registration is used, we had a bug where we'd register
;; something that's not a __profd_* variable.
@ -61,4 +61,4 @@ declare void @llvm.instrprof.increment(i8*, i64, i32, i32)
; SOLARIS-NOT: define internal void @__llvm_profile_init
; PS4-NOT: define internal void @__llvm_profile_init
; WINDOWS-NOT: define internal void @__llvm_profile_init
; AIX: define internal void @__llvm_profile_init
; AIX-NOT: define internal void @__llvm_profile_init

View File

@ -119,9 +119,4 @@ declare void @llvm.instrprof.increment(i8*, i64, i32, i32)
; ELF_GENERIC-NEXT: ret void
; ELF_GENERIC-NEXT: }
; XCOFF: define internal void @__llvm_profile_register_functions() unnamed_addr {
; XCOFF-NEXT: call void @__llvm_profile_register_function(i8* bitcast ({ i64, i64, i64, i8*, i8*, i32, [{{.*}} x i16] }* @__profd_foo to i8*))
; XCOFF-NEXT: call void @__llvm_profile_register_function(i8* bitcast ({ i64, i64, i64, i8*, i8*, i32, [{{.*}} x i16] }* @__profd_foo_weak to i8*))
; XCOFF: call void @__llvm_profile_register_names_function(i8* getelementptr inbounds {{.*}} @__llvm_prf_nm
; XCOFF-NEXT: ret void
; XCOFF-NEXT: }
; XCOFF-NOT: internal void @__llvm_profile_register_functions()