PGO: Add explicit static initialization

Instead of relying on explicit static initialization from translation
units, create a new file, InstrProfilingRuntime.cc, with an
__llvm_pgo_runtime variable.  After this commit (and its pair in clang),
the driver will create a use of this variable.  Unless the user defines
their own version, the new object file will get pulled in, including
that C++ static initialization that calls
__llvm_pgo_register_write_atexit.

The result is that, at least on Darwin, static initialization typically
consists of a single function call, which registers a writeout functino
atexit.  Furthermore, users can skip even this behaviour by defining
their own __llvm_pgo_runtime.

<rdar://problem/15943240>

llvm-svn: 204380
This commit is contained in:
Duncan P. N. Exon Smith 2014-03-20 19:23:53 +00:00
parent 5188e91cd4
commit 775c178a90
5 changed files with 34 additions and 8 deletions

View File

@ -7,6 +7,7 @@ if(APPLE)
GCDAProfiling.c
InstrProfiling.c
InstrProfilingPlatformDarwin.c
InstrProfilingRuntime.cc
InstrProfilingExtras.c)
add_compiler_rt_osx_static_runtime(clang_rt.profile_osx
@ -18,6 +19,7 @@ else()
GCDAProfiling.c
InstrProfiling.c
InstrProfilingPlatformOther.c
InstrProfilingRuntime.cc
InstrProfilingExtras.c)
foreach(arch ${PROFILE_SUPPORTED_ARCH})

View File

@ -9,7 +9,6 @@
#include "InstrProfiling.h"
/*! \brief Write instrumentation data to the given file. */
void __llvm_pgo_write_file(const char *OutputName) {
/* TODO: Requires libc: move to separate translation unit. */
FILE *OutputFile;
@ -26,7 +25,6 @@ void __llvm_pgo_write_file(const char *OutputName) {
fclose(OutputFile);
}
/*! \brief Write instrumentation data to the default file. */
void __llvm_pgo_write_default_file() {
/* TODO: Requires libc: move to separate translation unit. */
const char *OutputName = getenv("LLVM_PROFILE_FILE");
@ -35,9 +33,6 @@ void __llvm_pgo_write_default_file() {
__llvm_pgo_write_file(OutputName);
}
/*!
* \brief Register to write instrumentation data to the default file at exit.
*/
void __llvm_pgo_register_write_atexit() {
/* TODO: Requires libc: move to separate translation unit. */
static int HasBeenRegistered = 0;

View File

@ -0,0 +1,28 @@
//===- InstrProfilingRuntime.cpp - PGO runtime initialization -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
extern "C" {
#include "InstrProfilingExtras.h"
extern int __llvm_pgo_runtime;
int __llvm_pgo_runtime;
}
namespace {
class RegisterAtExit {
public:
RegisterAtExit() { __llvm_pgo_register_write_atexit(); }
};
RegisterAtExit Registration;
}

View File

@ -10,8 +10,8 @@
ModuleName := profile
SubDirs :=
Sources := $(foreach file,$(wildcard $(Dir)/*.c),$(notdir $(file)))
ObjNames := $(Sources:%.c=%.o)
Sources := $(foreach file,$(wildcard $(Dir)/*.c $(Dir)/*.cc),$(notdir $(file)))
ObjNames := $(patsubst %.c,%.o,$(patsubst %.cc,%.o,$(Sources)))
Implementation := Generic
# FIXME: use automatic dependencies?

View File

@ -223,7 +223,8 @@ FUNCTIONS.ios.x86_64h := $(FUNCTIONS.ios.x86_64)
FUNCTIONS.osx := mulosi4 mulodi4 muloti4
FUNCTIONS.profile_osx := GCDAProfiling InstrProfiling \
InstrProfilingPlatformDarwin InstrProfilingExtras
InstrProfilingPlatformDarwin InstrProfilingRuntime \
InstrProfilingExtras
FUNCTIONS.profile_ios := $(FUNCTIONS.profile_osx)
FUNCTIONS.asan_osx_dynamic := $(AsanFunctions) $(InterceptionFunctions) \