Skip to content

Commit 32bddad

Browse files
committed
[profile] Make atexit hook a no-op on Fuchsia
On Fuchsia, we always use the continuous mode with runtime counter relocation, so there's no need for atexit hook or support for dumping the profile manually. Differential Revision: https://reviews.llvm.org/D76556
1 parent d6fb02b commit 32bddad

File tree

6 files changed

+54
-67
lines changed

6 files changed

+54
-67
lines changed

compiler-rt/lib/profile/InstrProfiling.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ int __llvm_profile_register_write_file_atexit(void);
218218
/*! \brief Initialize file handling. */
219219
void __llvm_profile_initialize_file(void);
220220

221+
/*! \brief Initialize the profile runtime. */
222+
void __llvm_profile_initialize(void);
223+
221224
/*!
222225
* \brief Return path prefix (excluding the base filename) of the profile data.
223226
* This is useful for users using \c -fprofile-generate=./path_prefix who do

compiler-rt/lib/profile/InstrProfilingBuffer.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include "InstrProfilingInternal.h"
1111
#include "InstrProfilingPort.h"
1212

13-
/* When counters are being relocated at runtime, this parameter is set to 1. */
14-
COMPILER_RT_VISIBILITY int RuntimeCounterRelocation = 0;
15-
1613
/* When continuous mode is enabled (%c), this parameter is set to 1.
1714
*
1815
* This parameter is defined here in InstrProfilingBuffer.o, instead of in
@@ -66,7 +63,7 @@ void __llvm_profile_get_padding_sizes_for_counters(
6663
uint64_t *PaddingBytesBeforeCounters, uint64_t *PaddingBytesAfterCounters,
6764
uint64_t *PaddingBytesAfterNames) {
6865
if (!__llvm_profile_is_continuous_mode_enabled() ||
69-
RuntimeCounterRelocation) {
66+
lprofRuntimeCounterRelocation()) {
7067
*PaddingBytesBeforeCounters = 0;
7168
*PaddingBytesAfterCounters = 0;
7269
*PaddingBytesAfterNames = __llvm_profile_get_num_padding_bytes(NamesSize);

compiler-rt/lib/profile/InstrProfilingFile.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
#include "InstrProfilingPort.h"
3636
#include "InstrProfilingUtil.h"
3737

38+
static int RuntimeCounterRelocation = 0;
39+
40+
COMPILER_RT_VISIBILITY unsigned lprofRuntimeCounterRelocation(void) {
41+
return RuntimeCounterRelocation;
42+
}
43+
44+
COMPILER_RT_VISIBILITY void lprofSetRuntimeCounterRelocation(void) {
45+
RuntimeCounterRelocation = 1;
46+
}
47+
3848
/* From where is profile name specified.
3949
* The order the enumerators define their
4050
* precedence. Re-order them may lead to
@@ -477,7 +487,8 @@ static int writeMMappedFile(FILE *OutputFile, char **Profile) {
477487
}
478488

479489
static void relocateCounters(void) {
480-
if (!__llvm_profile_is_continuous_mode_enabled() || !RuntimeCounterRelocation)
490+
if (!__llvm_profile_is_continuous_mode_enabled() ||
491+
!lprofRuntimeCounterRelocation())
481492
return;
482493

483494
/* Get the sizes of various profile data sections. Taken from
@@ -808,7 +819,7 @@ static void parseAndSetFilename(const char *FilenamePat,
808819

809820
truncateCurrentFile();
810821
if (__llvm_profile_is_continuous_mode_enabled()) {
811-
if (RuntimeCounterRelocation)
822+
if (lprofRuntimeCounterRelocation())
812823
relocateCounters();
813824
else
814825
initializeProfileForContinuousMode();
@@ -951,10 +962,10 @@ const char *__llvm_profile_get_filename(void) {
951962
return FilenameBuf;
952963
}
953964

954-
/* This method is invoked by the runtime initialization hook
955-
* InstrProfilingRuntime.o if it is linked in. Both user specified
965+
/* This API initializes the file handling, both user specified
956966
* profile path via -fprofile-instr-generate= and LLVM_PROFILE_FILE
957-
* environment variable can override this default value. */
967+
* environment variable can override this default value.
968+
*/
958969
COMPILER_RT_VISIBILITY
959970
void __llvm_profile_initialize_file(void) {
960971
const char *EnvFilenamePat;
@@ -963,7 +974,7 @@ void __llvm_profile_initialize_file(void) {
963974
int hasCommandLineOverrider = (INSTR_PROF_PROFILE_NAME_VAR[0] != 0);
964975

965976
if (__llvm_profile_counter_bias != -1)
966-
RuntimeCounterRelocation = 1;
977+
lprofSetRuntimeCounterRelocation();
967978

968979
EnvFilenamePat = getFilenamePatFromEnv();
969980
if (EnvFilenamePat) {
@@ -982,6 +993,16 @@ void __llvm_profile_initialize_file(void) {
982993
parseAndSetFilename(SelectedPat, PNS, 0);
983994
}
984995

996+
/* This method is invoked by the runtime initialization hook
997+
* InstrProfilingRuntime.o if it is linked in.
998+
*/
999+
COMPILER_RT_VISIBILITY
1000+
void __llvm_profile_initialize(void) {
1001+
__llvm_profile_initialize_file();
1002+
if (!__llvm_profile_is_continuous_mode_enabled())
1003+
__llvm_profile_register_write_file_atexit();
1004+
}
1005+
9851006
/* This API is directly called by the user application code. It has the
9861007
* highest precedence compared with LLVM_PROFILE_FILE environment variable
9871008
* and command line option -fprofile-instr-generate=<profile_name>.

compiler-rt/lib/profile/InstrProfilingInternal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ uint64_t lprofGetLoadModuleSignature();
184184
unsigned lprofProfileDumped();
185185
void lprofSetProfileDumped();
186186

187-
COMPILER_RT_VISIBILITY extern int RuntimeCounterRelocation;
187+
/* Return non zero value if counters are being relocated at runtime. */
188+
unsigned lprofRuntimeCounterRelocation(void);
189+
void lprofSetRuntimeCounterRelocation(void);
190+
188191
COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
189192
COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;
190193
COMPILER_RT_VISIBILITY extern uint32_t VPBufferSize;

compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c

Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
#include "InstrProfilingInternal.h"
3535
#include "InstrProfilingUtil.h"
3636

37-
/* VMO that contains the coverage data shared across all modules. */
37+
COMPILER_RT_VISIBILITY unsigned lprofRuntimeCounterRelocation(void) {
38+
return 1;
39+
}
40+
41+
/* VMO that contains the profile data for this module. */
3842
static zx_handle_t __llvm_profile_vmo;
3943
/* Current offset within the VMO where data should be written next. */
4044
static uint64_t __llvm_profile_offset;
@@ -92,43 +96,23 @@ static void initVMOWriter(ProfDataWriter *This) {
9296
This->WriterCtx = NULL;
9397
}
9498

95-
static int dump(void) {
96-
if (lprofProfileDumped()) {
97-
lprofWrite("LLVM Profile: data not published: already written.\n");
98-
return 0;
99-
}
100-
101-
/* Check if there is llvm/runtime version mismatch. */
102-
if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
103-
lprofWrite("LLVM Profile: runtime and instrumentation version mismatch: "
104-
"expected %d, but got %d\n",
105-
INSTR_PROF_RAW_VERSION,
106-
(int)GET_VERSION(__llvm_profile_get_version()));
107-
return -1;
108-
}
109-
110-
/* Write the profile data into the mapped region. */
111-
ProfDataWriter VMOWriter;
112-
initVMOWriter(&VMOWriter);
113-
if (lprofWriteData(&VMOWriter, lprofGetVPDataReader(), 0) != 0)
114-
return -1;
115-
116-
return 0;
117-
}
118-
99+
/* This method is invoked by the runtime initialization hook
100+
* InstrProfilingRuntime.o if it is linked in. */
119101
COMPILER_RT_VISIBILITY
120-
int __llvm_profile_dump(void) {
121-
int rc = dump();
122-
lprofSetProfileDumped();
123-
return rc;
124-
}
125-
126-
static void dumpWithoutReturn(void) { dump(); }
102+
void __llvm_profile_initialize(void) {
103+
/* This symbol is defined as weak and initialized to -1 by the runtimer, but
104+
* compiler will generate a strong definition initialized to 0 when runtime
105+
* counter relocation is used. */
106+
if (__llvm_profile_counter_bias == -1) {
107+
lprofWrite("LLVM Profile: counter relocation at runtime is required\n");
108+
return;
109+
}
127110

128-
static void createVMO(void) {
129111
/* Don't create VMO if it has been alread created. */
130-
if (__llvm_profile_vmo != ZX_HANDLE_INVALID)
112+
if (__llvm_profile_vmo != ZX_HANDLE_INVALID) {
113+
lprofWrite("LLVM Profile: VMO has already been created\n");
131114
return;
115+
}
132116

133117
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
134118
const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
@@ -211,23 +195,4 @@ static void createVMO(void) {
211195
(uintptr_t)__llvm_profile_begin_counters() + CountersOffset;
212196
}
213197

214-
/* This method is invoked by the runtime initialization hook
215-
* InstrProfilingRuntime.o if it is linked in.
216-
*/
217-
COMPILER_RT_VISIBILITY
218-
void __llvm_profile_initialize_file(void) { createVMO(); }
219-
220-
COMPILER_RT_VISIBILITY
221-
int __llvm_profile_register_write_file_atexit(void) {
222-
static bool HasBeenRegistered = false;
223-
224-
if (HasBeenRegistered)
225-
return 0;
226-
227-
lprofSetupValueProfiler();
228-
229-
HasBeenRegistered = true;
230-
return atexit(dumpWithoutReturn);
231-
}
232-
233198
#endif

compiler-rt/lib/profile/InstrProfilingRuntime.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ namespace {
1919
class RegisterRuntime {
2020
public:
2121
RegisterRuntime() {
22-
__llvm_profile_initialize_file();
23-
if (!__llvm_profile_is_continuous_mode_enabled())
24-
__llvm_profile_register_write_file_atexit();
22+
__llvm_profile_initialize();
2523
}
2624
};
2725

0 commit comments

Comments
 (0)