[compiler-rt] r204380 - PGO: Add explicit static initialization

Duncan P. N. Exon Smith dexonsmith at apple.com
Thu Mar 20 12:23:53 PDT 2014


Author: dexonsmith
Date: Thu Mar 20 14:23:53 2014
New Revision: 204380

URL: http://llvm.org/viewvc/llvm-project?rev=204380&view=rev
Log:
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>

Added:
    compiler-rt/trunk/lib/profile/InstrProfilingRuntime.cc
Modified:
    compiler-rt/trunk/lib/profile/CMakeLists.txt
    compiler-rt/trunk/lib/profile/InstrProfilingExtras.c
    compiler-rt/trunk/lib/profile/Makefile.mk
    compiler-rt/trunk/make/platform/clang_darwin.mk

Modified: compiler-rt/trunk/lib/profile/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/CMakeLists.txt?rev=204380&r1=204379&r2=204380&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/profile/CMakeLists.txt Thu Mar 20 14:23:53 2014
@@ -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})

Modified: compiler-rt/trunk/lib/profile/InstrProfilingExtras.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingExtras.c?rev=204380&r1=204379&r2=204380&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingExtras.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingExtras.c Thu Mar 20 14:23:53 2014
@@ -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 *O
   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;

Added: compiler-rt/trunk/lib/profile/InstrProfilingRuntime.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingRuntime.cc?rev=204380&view=auto
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingRuntime.cc (added)
+++ compiler-rt/trunk/lib/profile/InstrProfilingRuntime.cc Thu Mar 20 14:23:53 2014
@@ -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;
+
+}

Modified: compiler-rt/trunk/lib/profile/Makefile.mk
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/Makefile.mk?rev=204380&r1=204379&r2=204380&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/Makefile.mk (original)
+++ compiler-rt/trunk/lib/profile/Makefile.mk Thu Mar 20 14:23:53 2014
@@ -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?

Modified: compiler-rt/trunk/make/platform/clang_darwin.mk
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/make/platform/clang_darwin.mk?rev=204380&r1=204379&r2=204380&view=diff
==============================================================================
--- compiler-rt/trunk/make/platform/clang_darwin.mk (original)
+++ compiler-rt/trunk/make/platform/clang_darwin.mk Thu Mar 20 14:23:53 2014
@@ -223,7 +223,8 @@ FUNCTIONS.ios.x86_64h := $(FUNCTIONS.ios
 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) \





More information about the llvm-commits mailing list