summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-03-20 19:23:53 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-03-20 19:23:53 +0000
commitad53c076409f8b6d37de8514b0bcd80432ee342b (patch)
tree820c1e4354e2745269536b3e1d2d4031042442f6
parent4bf72cf3f5f753ff76d06aa38fef14b9aefb083f (diff)
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> git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@204380 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/profile/CMakeLists.txt2
-rw-r--r--lib/profile/InstrProfilingExtras.c5
-rw-r--r--lib/profile/InstrProfilingRuntime.cc28
-rw-r--r--lib/profile/Makefile.mk4
-rw-r--r--make/platform/clang_darwin.mk3
5 files changed, 34 insertions, 8 deletions
diff --git a/lib/profile/CMakeLists.txt b/lib/profile/CMakeLists.txt
index 7de9c5e4b..949e174b4 100644
--- a/lib/profile/CMakeLists.txt
+++ b/lib/profile/CMakeLists.txt
@@ -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})
diff --git a/lib/profile/InstrProfilingExtras.c b/lib/profile/InstrProfilingExtras.c
index 03399a347..45a7f8ce5 100644
--- a/lib/profile/InstrProfilingExtras.c
+++ b/lib/profile/InstrProfilingExtras.c
@@ -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;
diff --git a/lib/profile/InstrProfilingRuntime.cc b/lib/profile/InstrProfilingRuntime.cc
new file mode 100644
index 000000000..e6ef4c941
--- /dev/null
+++ b/lib/profile/InstrProfilingRuntime.cc
@@ -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;
+
+}
diff --git a/lib/profile/Makefile.mk b/lib/profile/Makefile.mk
index 7689c9a06..dd3a36faf 100644
--- a/lib/profile/Makefile.mk
+++ b/lib/profile/Makefile.mk
@@ -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?
diff --git a/make/platform/clang_darwin.mk b/make/platform/clang_darwin.mk
index 0feb55341..2fdf051bc 100644
--- a/make/platform/clang_darwin.mk
+++ b/make/platform/clang_darwin.mk
@@ -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) \