Skip to content

Commit a0f30c6

Browse files
committed
update port
1 parent 29ac528 commit a0f30c6

14 files changed

+275
-102
lines changed

system/lib/compiler-rt/include/profile/InstrProfData.inc

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,18 @@ COVMAP_HEADER(uint32_t, Int32Ty, Version, \
303303
#undef COVMAP_HEADER
304304
/* COVMAP_HEADER end. */
305305

306+
/* COVINIT_FUNC start */
307+
#ifndef COVINIT_FUNC
308+
#define COVINIT_FUNC(Type, LLVMType, Name, Initializer)
309+
#else
310+
#define INSTR_PROF_DATA_DEFINED
311+
#endif
312+
COVINIT_FUNC(IntPtrT, llvm::PointerType::getUnqual(Ctx), WriteoutFunction, \
313+
WriteoutF)
314+
COVINIT_FUNC(IntPtrT, llvm::PointerType::getUnqual(Ctx), ResetFunction, \
315+
ResetF)
316+
#undef COVINIT_FUNC
317+
/* COVINIT_FUNC end */
306318

307319
#ifdef INSTR_PROF_SECT_ENTRY
308320
#define INSTR_PROF_DATA_DEFINED
@@ -345,6 +357,9 @@ INSTR_PROF_SECT_ENTRY(IPSK_covdata, \
345357
INSTR_PROF_SECT_ENTRY(IPSK_covname, \
346358
INSTR_PROF_QUOTE(INSTR_PROF_COVNAME_COMMON), \
347359
INSTR_PROF_COVNAME_COFF, "__LLVM_COV,")
360+
INSTR_PROF_SECT_ENTRY(IPSK_covinit, \
361+
INSTR_PROF_QUOTE(INSTR_PROF_COVINIT_COMMON), \
362+
INSTR_PROF_COVINIT_COFF, "__LLVM_COV,")
348363

349364
#undef INSTR_PROF_SECT_ENTRY
350365
#endif
@@ -715,10 +730,12 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
715730
#define INSTR_PROF_COVMAP_VERSION 6
716731

717732
/* Profile version is always of type uint64_t. Reserve the upper 32 bits in the
718-
* version for other variants of profile. We set the 8th most significant bit
733+
* version for other variants of profile. We set the 8th most significant bit
719734
* (i.e. bit 56) to 1 to indicate if this is an IR-level instrumentation
720735
* generated profile, and 0 if this is a Clang FE generated profile.
721736
* 1 in bit 57 indicates there are context-sensitive records in the profile.
737+
* The 54th bit indicates whether to always instrument loop entry blocks.
738+
* The 58th bit indicates whether to always instrument function entry blocks.
722739
* The 59th bit indicates whether to use debug info to correlate profiles.
723740
* The 60th bit indicates single byte coverage instrumentation.
724741
* The 61st bit indicates function entry instrumentation only.
@@ -727,6 +744,7 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
727744
*/
728745
#define VARIANT_MASKS_ALL 0xffffffff00000000ULL
729746
#define GET_VERSION(V) ((V) & ~VARIANT_MASKS_ALL)
747+
#define VARIANT_MASK_INSTR_LOOP_ENTRIES (0x1ULL << 55)
730748
#define VARIANT_MASK_IR_PROF (0x1ULL << 56)
731749
#define VARIANT_MASK_CSIR_PROF (0x1ULL << 57)
732750
#define VARIANT_MASK_INSTR_ENTRY (0x1ULL << 58)
@@ -738,6 +756,7 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
738756
#define INSTR_PROF_RAW_VERSION_VAR __llvm_profile_raw_version
739757
#define INSTR_PROF_PROFILE_RUNTIME_VAR __llvm_profile_runtime
740758
#define INSTR_PROF_PROFILE_COUNTER_BIAS_VAR __llvm_profile_counter_bias
759+
#define INSTR_PROF_PROFILE_BITMAP_BIAS_VAR __llvm_profile_bitmap_bias
741760
#define INSTR_PROF_PROFILE_SET_TIMESTAMP __llvm_profile_set_timestamp
742761
#define INSTR_PROF_PROFILE_SAMPLING_VAR __llvm_profile_sampling
743762

@@ -760,6 +779,8 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
760779
#define INSTR_PROF_COVDATA_COMMON __llvm_covdata
761780
#define INSTR_PROF_COVNAME_COMMON __llvm_covnames
762781
#define INSTR_PROF_ORDERFILE_COMMON __llvm_orderfile
782+
#define INSTR_PROF_COVINIT_COMMON __llvm_covinit
783+
763784
/* Windows section names. Because these section names contain dollar characters,
764785
* they must be quoted.
765786
*/
@@ -780,6 +801,10 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
780801
#define INSTR_PROF_COVNAME_COFF ".lcovn"
781802
#define INSTR_PROF_ORDERFILE_COFF ".lorderfile$M"
782803

804+
// FIXME: Placeholder for Windows. Windows currently does not initialize
805+
// the GCOV functions in the runtime.
806+
#define INSTR_PROF_COVINIT_COFF ".lcovd$M"
807+
783808
#ifdef _WIN32
784809
/* Runtime section names and name strings. */
785810
#define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COFF
@@ -799,6 +824,7 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
799824
#define INSTR_PROF_COVDATA_SECT_NAME INSTR_PROF_COVDATA_COFF
800825
#define INSTR_PROF_COVNAME_SECT_NAME INSTR_PROF_COVNAME_COFF
801826
#define INSTR_PROF_ORDERFILE_SECT_NAME INSTR_PROF_ORDERFILE_COFF
827+
#define INSTR_PROF_COVINIT_SECT_NAME INSTR_PROF_COVINIT_COFF
802828
#else
803829
/* Runtime section names and name strings. */
804830
#define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON)
@@ -820,6 +846,7 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
820846
/* Order file instrumentation. */
821847
#define INSTR_PROF_ORDERFILE_SECT_NAME \
822848
INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COMMON)
849+
#define INSTR_PROF_COVINIT_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVINIT_COMMON)
823850
#endif
824851

825852
#define INSTR_PROF_ORDERFILE_BUFFER_NAME _llvm_order_file_buffer

system/lib/compiler-rt/lib/profile/GCDAProfiling.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ void llvm_reset_counters(void) {
584584
}
585585
}
586586

587-
#if !defined(_WIN32)
587+
#if !defined(_WIN32) && !defined(__wasm__)
588588
COMPILER_RT_VISIBILITY
589589
pid_t __gcov_fork() {
590590
pid_t parent_pid = getpid();
@@ -617,13 +617,32 @@ void llvm_gcov_init(fn_ptr wfn, fn_ptr rfn) {
617617
atexit_ran = 1;
618618

619619
/* Make sure we write out the data and delete the data structures. */
620-
atexit(llvm_delete_reset_function_list);
620+
lprofAtExit(llvm_delete_reset_function_list);
621621
#ifdef _WIN32
622-
atexit(llvm_writeout_and_clear);
622+
lprofAtExit(llvm_writeout_and_clear);
623623
#endif
624624
}
625625
}
626626

627+
#if defined(_AIX)
628+
COMPILER_RT_VISIBILITY __attribute__((constructor)) void
629+
__llvm_profile_gcov_initialize() {
630+
const __llvm_gcov_init_func_struct *InitFuncStart =
631+
__llvm_profile_begin_covinit();
632+
const __llvm_gcov_init_func_struct *InitFuncEnd =
633+
__llvm_profile_end_covinit();
634+
635+
for (const __llvm_gcov_init_func_struct *Ptr = InitFuncStart;
636+
Ptr != InitFuncEnd; ++Ptr) {
637+
fn_ptr wfn = (fn_ptr)Ptr->WriteoutFunction;
638+
fn_ptr rfn = (fn_ptr)Ptr->ResetFunction;
639+
if (!(wfn && rfn))
640+
continue;
641+
llvm_gcov_init(wfn, rfn);
642+
}
643+
}
644+
#endif
645+
627646
void __gcov_dump(void) {
628647
for (struct fn_node *f = writeout_fn_list.head; f; f = f->next)
629648
f->fn();

system/lib/compiler-rt/lib/profile/InstrProfiling.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT) VTableProfData {
5454
#include "profile/InstrProfData.inc"
5555
} VTableProfData;
5656

57+
typedef struct COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
58+
__llvm_gcov_init_func_struct {
59+
#define COVINIT_FUNC(Type, LLVMType, Name, Initializer) Type Name;
60+
#include "profile/InstrProfData.inc"
61+
} __llvm_gcov_init_func_struct;
62+
5763
/*!
5864
* \brief Return 1 if profile counters are continuously synced to the raw
5965
* profile via an mmap(). This is in contrast to the default mode, in which
@@ -114,11 +120,11 @@ char *__llvm_profile_begin_counters(void);
114120
char *__llvm_profile_end_counters(void);
115121
char *__llvm_profile_begin_bitmap(void);
116122
char *__llvm_profile_end_bitmap(void);
117-
ValueProfNode *__llvm_profile_begin_vnodes();
118-
ValueProfNode *__llvm_profile_end_vnodes();
119-
const VTableProfData *__llvm_profile_begin_vtables();
120-
const VTableProfData *__llvm_profile_end_vtables();
121-
uint32_t *__llvm_profile_begin_orderfile();
123+
ValueProfNode *__llvm_profile_begin_vnodes(void);
124+
ValueProfNode *__llvm_profile_end_vnodes(void);
125+
const VTableProfData *__llvm_profile_begin_vtables(void);
126+
const VTableProfData *__llvm_profile_end_vtables(void);
127+
uint32_t *__llvm_profile_begin_orderfile(void);
122128

123129
/*!
124130
* \brief Merge profile data from buffer.
@@ -208,6 +214,9 @@ void __llvm_profile_initialize_file(void);
208214
/*! \brief Initialize the profile runtime. */
209215
void __llvm_profile_initialize(void);
210216

217+
/*! \brief Initialize the gcov profile runtime. */
218+
void __llvm_profile_gcov_initialize(void);
219+
211220
/*!
212221
* \brief Return path prefix (excluding the base filename) of the profile data.
213222
* This is useful for users using \c -fprofile-generate=./path_prefix who do
@@ -216,7 +225,7 @@ void __llvm_profile_initialize(void);
216225
* merge mode is turned on for instrumented programs with shared libs).
217226
* Side-effect: this API call will invoke malloc with dynamic memory allocation.
218227
*/
219-
const char *__llvm_profile_get_path_prefix();
228+
const char *__llvm_profile_get_path_prefix(void);
220229

221230
/*!
222231
* \brief Return filename (including path) of the profile data. Note that if the
@@ -229,7 +238,7 @@ const char *__llvm_profile_get_path_prefix();
229238
* instrumented image/DSO). This API only retrieves the filename from the copy
230239
* of the runtime available to the calling image.
231240
*/
232-
const char *__llvm_profile_get_filename();
241+
const char *__llvm_profile_get_filename(void);
233242

234243
/*! \brief Get the magic token for the file format. */
235244
uint64_t __llvm_profile_get_magic(void);
@@ -293,7 +302,7 @@ int __llvm_profile_get_padding_sizes_for_counters(
293302
* certain processes in case the processes don't have permission to write to
294303
* the disks, and trying to do so would result in side effects such as crashes.
295304
*/
296-
void __llvm_profile_set_dumped();
305+
void __llvm_profile_set_dumped(void);
297306

298307
/*!
299308
* This variable is defined in InstrProfilingRuntime.cpp as a hidden
@@ -324,4 +333,6 @@ COMPILER_RT_VISIBILITY extern uint64_t
324333
*/
325334
extern char INSTR_PROF_PROFILE_NAME_VAR[1]; /* __llvm_profile_filename. */
326335

336+
const __llvm_gcov_init_func_struct *__llvm_profile_begin_covinit();
337+
const __llvm_gcov_init_func_struct *__llvm_profile_end_covinit();
327338
#endif /* PROFILE_INSTRPROFILING_H_ */

system/lib/compiler-rt/lib/profile/InstrProfilingFile.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ static const int UseBiasVar = 0;
101101
static const char *FileOpenMode = "a+b";
102102
static void *BiasAddr = NULL;
103103
static void *BiasDefaultAddr = NULL;
104+
static void *BitmapBiasAddr = NULL;
105+
static void *BitmapBiasDefaultAddr = NULL;
104106
static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
105107
/* Get the sizes of various profile data sections. Taken from
106108
* __llvm_profile_get_size_for_buffer(). */
@@ -194,16 +196,20 @@ static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
194196
}
195197
return 0;
196198
}
197-
#elif defined(__ELF__) || defined(_WIN32)
199+
#elif defined(__ELF__) || defined(_WIN32) || defined(_AIX)
198200

199201
#define INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR \
200202
INSTR_PROF_CONCAT(INSTR_PROF_PROFILE_COUNTER_BIAS_VAR, _default)
201-
COMPILER_RT_VISIBILITY intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR = 0;
203+
COMPILER_RT_VISIBILITY int64_t INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR = 0;
204+
#define INSTR_PROF_PROFILE_BITMAP_BIAS_DEFAULT_VAR \
205+
INSTR_PROF_CONCAT(INSTR_PROF_PROFILE_BITMAP_BIAS_VAR, _default)
206+
COMPILER_RT_VISIBILITY int64_t INSTR_PROF_PROFILE_BITMAP_BIAS_DEFAULT_VAR = 0;
202207

203208
/* This variable is a weak external reference which could be used to detect
204209
* whether or not the compiler defined this symbol. */
205210
#if defined(_MSC_VER)
206-
COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR;
211+
COMPILER_RT_VISIBILITY extern int64_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR;
212+
COMPILER_RT_VISIBILITY extern int64_t INSTR_PROF_PROFILE_BITMAP_BIAS_VAR;
207213
#if defined(_M_IX86) || defined(__i386__)
208214
#define WIN_SYM_PREFIX "_"
209215
#else
@@ -213,10 +219,17 @@ COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR;
213219
linker, "/alternatename:" WIN_SYM_PREFIX INSTR_PROF_QUOTE( \
214220
INSTR_PROF_PROFILE_COUNTER_BIAS_VAR) "=" WIN_SYM_PREFIX \
215221
INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR))
222+
#pragma comment( \
223+
linker, "/alternatename:" WIN_SYM_PREFIX INSTR_PROF_QUOTE( \
224+
INSTR_PROF_PROFILE_BITMAP_BIAS_VAR) "=" WIN_SYM_PREFIX \
225+
INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_BITMAP_BIAS_DEFAULT_VAR))
216226
#else
217-
COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR
227+
COMPILER_RT_VISIBILITY extern int64_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR
218228
__attribute__((weak, alias(INSTR_PROF_QUOTE(
219229
INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR))));
230+
COMPILER_RT_VISIBILITY extern int64_t INSTR_PROF_PROFILE_BITMAP_BIAS_VAR
231+
__attribute__((weak, alias(INSTR_PROF_QUOTE(
232+
INSTR_PROF_PROFILE_BITMAP_BIAS_DEFAULT_VAR))));
220233
#endif
221234
static const int ContinuousModeSupported = 1;
222235
static const int UseBiasVar = 1;
@@ -227,6 +240,9 @@ static const char *FileOpenMode = "w+b";
227240
* used and runtime provides a weak alias so we can check if it's defined. */
228241
static void *BiasAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_VAR;
229242
static void *BiasDefaultAddr = &INSTR_PROF_PROFILE_COUNTER_BIAS_DEFAULT_VAR;
243+
static void *BitmapBiasAddr = &INSTR_PROF_PROFILE_BITMAP_BIAS_VAR;
244+
static void *BitmapBiasDefaultAddr =
245+
&INSTR_PROF_PROFILE_BITMAP_BIAS_DEFAULT_VAR;
230246
static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
231247
/* Get the sizes of various profile data sections. Taken from
232248
* __llvm_profile_get_size_for_buffer(). */
@@ -237,12 +253,18 @@ static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
237253
const char *BitmapBegin = __llvm_profile_begin_bitmap();
238254
const char *BitmapEnd = __llvm_profile_end_bitmap();
239255
uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
256+
uint64_t CountersSize =
257+
__llvm_profile_get_counters_size(CountersBegin, CountersEnd);
258+
uint64_t NumBitmapBytes =
259+
__llvm_profile_get_num_bitmap_bytes(BitmapBegin, BitmapEnd);
240260
/* Get the file size. */
241261
uint64_t FileSize = 0;
242262
if (getProfileFileSizeForMerging(File, &FileSize))
243263
return 1;
244264

245265
int Fileno = fileno(File);
266+
uint64_t PaddingBytesAfterCounters =
267+
__llvm_profile_get_num_padding_bytes(CountersSize);
246268
uint64_t FileOffsetToCounters =
247269
sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) + DataSize;
248270

@@ -260,7 +282,17 @@ static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
260282
/* Return the memory allocated for counters to OS. */
261283
lprofReleaseMemoryPagesToOS((uintptr_t)CountersBegin, (uintptr_t)CountersEnd);
262284

263-
/* BIAS MODE not supported yet for Bitmap (MCDC). */
285+
/* Also mmap MCDC bitmap bytes. If there aren't any bitmap bytes, mmap()
286+
* will fail with EINVAL. */
287+
if (NumBitmapBytes == 0)
288+
return 0;
289+
290+
/* Update profbm_bias. */
291+
uint64_t FileOffsetToBitmap =
292+
FileOffsetToCounters + CountersSize + PaddingBytesAfterCounters;
293+
/* Update the profile fields based on the current mapping. */
294+
INSTR_PROF_PROFILE_BITMAP_BIAS_VAR =
295+
(uintptr_t)Profile - (uintptr_t)BitmapBegin + FileOffsetToBitmap;
264296

265297
/* Return the memory allocated for counters to OS. */
266298
lprofReleaseMemoryPagesToOS((uintptr_t)BitmapBegin, (uintptr_t)BitmapEnd);
@@ -272,6 +304,8 @@ static const int UseBiasVar = 0;
272304
static const char *FileOpenMode = "a+b";
273305
static void *BiasAddr = NULL;
274306
static void *BiasDefaultAddr = NULL;
307+
static void *BitmapBiasAddr = NULL;
308+
static void *BitmapBiasDefaultAddr = NULL;
275309
static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
276310
return 0;
277311
}
@@ -619,8 +653,10 @@ static void initializeProfileForContinuousMode(void) {
619653
PROF_ERR("%s\n", "continuous mode is unsupported on this platform");
620654
return;
621655
}
622-
if (UseBiasVar && BiasAddr == BiasDefaultAddr) {
623-
PROF_ERR("%s\n", "__llvm_profile_counter_bias is undefined");
656+
if (UseBiasVar && BiasAddr == BiasDefaultAddr &&
657+
BitmapBiasAddr == BitmapBiasDefaultAddr) {
658+
PROF_ERR("%s\n", "Neither __llvm_profile_counter_bias nor "
659+
"__llvm_profile_bitmap_bias is defined");
624660
return;
625661
}
626662

@@ -826,7 +862,7 @@ static int parseFilenamePattern(const char *FilenamePat,
826862
__llvm_profile_disable_continuous_mode();
827863
return -1;
828864
}
829-
#if defined(__APPLE__) || defined(__ELF__) || defined(_WIN32)
865+
#if defined(__APPLE__) || defined(__ELF__) || defined(_WIN32) || defined(_AIX)
830866
__llvm_profile_set_page_size(getpagesize());
831867
__llvm_profile_enable_continuous_mode();
832868
#else
@@ -1223,7 +1259,7 @@ int __llvm_profile_register_write_file_atexit(void) {
12231259
lprofSetupValueProfiler();
12241260

12251261
HasBeenRegistered = 1;
1226-
return atexit(writeFileWithoutReturn);
1262+
return lprofAtExit(writeFileWithoutReturn);
12271263
}
12281264

12291265
COMPILER_RT_VISIBILITY int __llvm_profile_set_file_object(FILE *File,

system/lib/compiler-rt/lib/profile/InstrProfilingInternal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,20 @@ int lprofWriteDataImpl(ProfDataWriter *Writer,
167167
void lprofMergeValueProfData(struct ValueProfData *SrcValueProfData,
168168
__llvm_profile_data *DstData);
169169

170-
VPDataReaderType *lprofGetVPDataReader();
170+
VPDataReaderType *lprofGetVPDataReader(void);
171171

172172
/* Internal interface used by test to reset the max number of
173173
* tracked values per value site to be \p MaxVals.
174174
*/
175175
void lprofSetMaxValsPerSite(uint32_t MaxVals);
176-
void lprofSetupValueProfiler();
176+
void lprofSetupValueProfiler(void);
177177

178178
/* Return the profile header 'signature' value associated with the current
179179
* executable or shared library. The signature value can be used to for
180180
* a profile name that is unique to this load module so that it does not
181181
* collide with profiles from other binaries. It also allows shared libraries
182182
* to dump merged profile data into its own profile file. */
183-
uint64_t lprofGetLoadModuleSignature();
183+
uint64_t lprofGetLoadModuleSignature(void);
184184

185185
/*
186186
* Return non zero value if the profile data has already been

0 commit comments

Comments
 (0)