Skip to content

Commit f214737

Browse files
committed
[InstrProf][NFC] Do not assume size of counter type
Existing code tended to assume that counters had type `uint64_t` and computed size from the number of counters. Fix this code to directly compute the counters size in number of bytes where possible. When the number of counters is needed, use `__llvm_profile_counter_entry_size()` or `getCounterTypeSize()`. In a later diff these functions will depend on the profile mode. Change the meaning of `DataSize` and `CountersSize` to make them more clear. * `DataSize` (`CountersSize`) - the size of the data (counter) section in bytes. * `NumData` (`NumCounters`) - the number of data (counter) entries. Reviewed By: kyulee Differential Revision: https://reviews.llvm.org/D116179
1 parent acb8de5 commit f214737

18 files changed

+211
-200
lines changed

compiler-rt/include/profile/InstrProfData.inc

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ INSTR_PROF_VALUE_NODE(PtrToNodeT, llvm::Type::getInt8PtrTy(Ctx), Next, \
128128
INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
129129
INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
130130
INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
131-
INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSize)
131+
INSTR_PROF_RAW_HEADER(uint64_t, NumData, NumData)
132132
INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesBeforeCounters, PaddingBytesBeforeCounters)
133-
INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSize)
133+
INSTR_PROF_RAW_HEADER(uint64_t, NumCounters, NumCounters)
134134
INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesAfterCounters, PaddingBytesAfterCounters)
135135
INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
136136
INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta,

compiler-rt/lib/profile/InstrProfiling.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ COMPILER_RT_VISIBILITY uint64_t __llvm_profile_get_version(void) {
4242
}
4343

4444
COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
45-
uint64_t *I = __llvm_profile_begin_counters();
46-
uint64_t *E = __llvm_profile_end_counters();
45+
char *I = __llvm_profile_begin_counters();
46+
char *E = __llvm_profile_end_counters();
4747

48-
memset(I, 0, sizeof(uint64_t) * (E - I));
48+
memset(I, 0, E - I);
4949

5050
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
5151
const __llvm_profile_data *DataEnd = __llvm_profile_end_data();

compiler-rt/lib/profile/InstrProfiling.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ const __llvm_profile_data *__llvm_profile_begin_data(void);
8686
const __llvm_profile_data *__llvm_profile_end_data(void);
8787
const char *__llvm_profile_begin_names(void);
8888
const char *__llvm_profile_end_names(void);
89-
uint64_t *__llvm_profile_begin_counters(void);
90-
uint64_t *__llvm_profile_end_counters(void);
89+
char *__llvm_profile_begin_counters(void);
90+
char *__llvm_profile_end_counters(void);
9191
ValueProfNode *__llvm_profile_begin_vnodes();
9292
ValueProfNode *__llvm_profile_end_vnodes();
9393
uint32_t *__llvm_profile_begin_orderfile();
@@ -260,17 +260,26 @@ uint64_t __llvm_profile_get_magic(void);
260260
uint64_t __llvm_profile_get_version(void);
261261

262262
/*! \brief Get the number of entries in the profile data section. */
263+
uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
264+
const __llvm_profile_data *End);
265+
266+
/*! \brief Get the size of the profile data section in bytes. */
263267
uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
264268
const __llvm_profile_data *End);
265269

270+
/*! \brief Get the size in bytes of a single counter entry. */
271+
size_t __llvm_profile_counter_entry_size(void);
272+
273+
/*! \brief Get the number of entries in the profile counters section. */
274+
uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End);
275+
276+
/*! \brief Get the size of the profile counters section in bytes. */
277+
uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End);
278+
266279
/* ! \brief Given the sizes of the data and counter information, return the
267280
* number of padding bytes before and after the counters, and after the names,
268281
* in the raw profile.
269282
*
270-
* Note: In this context, "size" means "number of entries", i.e. the first two
271-
* arguments must be the result of __llvm_profile_get_data_size() and of
272-
* (__llvm_profile_end_counters() - __llvm_profile_begin_counters()) resp.
273-
*
274283
* Note: When mmap() mode is disabled, no padding bytes before/after counters
275284
* are needed. However, in mmap() mode, the counter section in the raw profile
276285
* must be page-aligned: this API computes the number of padding bytes

compiler-rt/lib/profile/InstrProfilingBuffer.c

+38-18
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ COMPILER_RT_VISIBILITY
4141
uint64_t __llvm_profile_get_size_for_buffer(void) {
4242
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
4343
const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
44-
const uint64_t *CountersBegin = __llvm_profile_begin_counters();
45-
const uint64_t *CountersEnd = __llvm_profile_end_counters();
44+
const char *CountersBegin = __llvm_profile_begin_counters();
45+
const char *CountersEnd = __llvm_profile_end_counters();
4646
const char *NamesBegin = __llvm_profile_begin_names();
4747
const char *NamesEnd = __llvm_profile_end_names();
4848

@@ -51,13 +51,36 @@ uint64_t __llvm_profile_get_size_for_buffer(void) {
5151
}
5252

5353
COMPILER_RT_VISIBILITY
54-
uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
55-
const __llvm_profile_data *End) {
54+
uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
55+
const __llvm_profile_data *End) {
5656
intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
5757
return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
5858
sizeof(__llvm_profile_data);
5959
}
6060

61+
COMPILER_RT_VISIBILITY
62+
uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
63+
const __llvm_profile_data *End) {
64+
return __llvm_profile_get_num_data(Begin, End) * sizeof(__llvm_profile_data);
65+
}
66+
67+
COMPILER_RT_VISIBILITY size_t __llvm_profile_counter_entry_size(void) {
68+
return sizeof(uint64_t);
69+
}
70+
71+
COMPILER_RT_VISIBILITY
72+
uint64_t __llvm_profile_get_num_counters(const char *Begin, const char *End) {
73+
intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
74+
return ((EndI + __llvm_profile_counter_entry_size() - 1) - BeginI) /
75+
__llvm_profile_counter_entry_size();
76+
}
77+
78+
COMPILER_RT_VISIBILITY
79+
uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End) {
80+
return __llvm_profile_get_num_counters(Begin, End) *
81+
__llvm_profile_counter_entry_size();
82+
}
83+
6184
/// Calculate the number of padding bytes needed to add to \p Offset in order
6285
/// for (\p Offset + Padding) to be page-aligned.
6386
static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset) {
@@ -89,24 +112,22 @@ void __llvm_profile_get_padding_sizes_for_counters(
89112

90113
// In continuous mode, the file offsets for headers and for the start of
91114
// counter sections need to be page-aligned.
92-
uint64_t DataSizeInBytes = DataSize * sizeof(__llvm_profile_data);
93-
uint64_t CountersSizeInBytes = CountersSize * sizeof(uint64_t);
94-
*PaddingBytesBeforeCounters = calculateBytesNeededToPageAlign(
95-
sizeof(__llvm_profile_header) + DataSizeInBytes);
96-
*PaddingBytesAfterCounters =
97-
calculateBytesNeededToPageAlign(CountersSizeInBytes);
115+
*PaddingBytesBeforeCounters =
116+
calculateBytesNeededToPageAlign(sizeof(__llvm_profile_header) + DataSize);
117+
*PaddingBytesAfterCounters = calculateBytesNeededToPageAlign(CountersSize);
98118
*PaddingBytesAfterNames = calculateBytesNeededToPageAlign(NamesSize);
99119
}
100120

101121
COMPILER_RT_VISIBILITY
102122
uint64_t __llvm_profile_get_size_for_buffer_internal(
103123
const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
104-
const uint64_t *CountersBegin, const uint64_t *CountersEnd,
105-
const char *NamesBegin, const char *NamesEnd) {
124+
const char *CountersBegin, const char *CountersEnd, const char *NamesBegin,
125+
const char *NamesEnd) {
106126
/* Match logic in __llvm_profile_write_buffer(). */
107127
const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
108128
uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
109-
uint64_t CountersSize = CountersEnd - CountersBegin;
129+
uint64_t CountersSize =
130+
__llvm_profile_get_counters_size(CountersBegin, CountersEnd);
110131

111132
/* Determine how much padding is needed before/after the counters and after
112133
* the names. */
@@ -117,9 +138,8 @@ uint64_t __llvm_profile_get_size_for_buffer_internal(
117138
&PaddingBytesAfterCounters, &PaddingBytesAfterNames);
118139

119140
return sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) +
120-
(DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters +
121-
(CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters +
122-
NamesSize + PaddingBytesAfterNames;
141+
DataSize + PaddingBytesBeforeCounters + CountersSize +
142+
PaddingBytesAfterCounters + NamesSize + PaddingBytesAfterNames;
123143
}
124144

125145
COMPILER_RT_VISIBILITY
@@ -136,8 +156,8 @@ COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {
136156

137157
COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer_internal(
138158
char *Buffer, const __llvm_profile_data *DataBegin,
139-
const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
140-
const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
159+
const __llvm_profile_data *DataEnd, const char *CountersBegin,
160+
const char *CountersEnd, const char *NamesBegin, const char *NamesEnd) {
141161
ProfDataWriter BufferWriter;
142162
initBufferWriter(&BufferWriter, Buffer);
143163
return lprofWriteDataImpl(&BufferWriter, DataBegin, DataEnd, CountersBegin,

compiler-rt/lib/profile/InstrProfilingFile.c

+13-15
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
106106
* __llvm_profile_get_size_for_buffer(). */
107107
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
108108
const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
109-
const uint64_t *CountersBegin = __llvm_profile_begin_counters();
110-
const uint64_t *CountersEnd = __llvm_profile_end_counters();
109+
const char *CountersBegin = __llvm_profile_begin_counters();
110+
const char *CountersEnd = __llvm_profile_end_counters();
111111
const char *NamesBegin = __llvm_profile_begin_names();
112112
const char *NamesEnd = __llvm_profile_end_names();
113113
const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
114114
uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
115-
uint64_t CountersSize = CountersEnd - CountersBegin;
115+
uint64_t CountersSize =
116+
__llvm_profile_get_counters_size(CountersBegin, CountersEnd);
116117

117118
/* Check that the counter and data sections in this image are
118119
* page-aligned. */
@@ -136,11 +137,10 @@ static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
136137
DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
137138
&PaddingBytesAfterCounters, &PaddingBytesAfterNames);
138139

139-
uint64_t PageAlignedCountersLength =
140-
(CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters;
141-
uint64_t FileOffsetToCounters =
142-
CurrentFileOffset + sizeof(__llvm_profile_header) +
143-
(DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters;
140+
uint64_t PageAlignedCountersLength = CountersSize + PaddingBytesAfterCounters;
141+
uint64_t FileOffsetToCounters = CurrentFileOffset +
142+
sizeof(__llvm_profile_header) + DataSize +
143+
PaddingBytesBeforeCounters;
144144
uint64_t *CounterMmap = (uint64_t *)mmap(
145145
(void *)CountersBegin, PageAlignedCountersLength, PROT_READ | PROT_WRITE,
146146
MAP_FIXED | MAP_SHARED, Fileno, FileOffsetToCounters);
@@ -195,8 +195,8 @@ static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
195195
* __llvm_profile_get_size_for_buffer(). */
196196
const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
197197
const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
198-
const uint64_t *CountersBegin = __llvm_profile_begin_counters();
199-
const uint64_t *CountersEnd = __llvm_profile_end_counters();
198+
const char *CountersBegin = __llvm_profile_begin_counters();
199+
const char *CountersEnd = __llvm_profile_end_counters();
200200
uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
201201
/* Get the file size. */
202202
uint64_t FileSize = 0;
@@ -211,8 +211,7 @@ static int mmapForContinuousMode(uint64_t CurrentFileOffset, FILE *File) {
211211
return 1;
212212
}
213213
const uint64_t CountersOffsetInBiasMode =
214-
sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) +
215-
(DataSize * sizeof(__llvm_profile_data));
214+
sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) + DataSize;
216215
/* Update the profile fields based on the current mapping. */
217216
INSTR_PROF_PROFILE_COUNTER_BIAS_VAR =
218217
(intptr_t)Profile - (uintptr_t)CountersBegin + CountersOffsetInBiasMode;
@@ -578,9 +577,8 @@ static void initializeProfileForContinuousMode(void) {
578577
}
579578

580579
/* Get the sizes of counter section. */
581-
const uint64_t *CountersBegin = __llvm_profile_begin_counters();
582-
const uint64_t *CountersEnd = __llvm_profile_end_counters();
583-
uint64_t CountersSize = CountersEnd - CountersBegin;
580+
uint64_t CountersSize = __llvm_profile_get_counters_size(
581+
__llvm_profile_begin_counters(), __llvm_profile_end_counters());
584582

585583
int Length = getCurFilenameLength();
586584
char *FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);

compiler-rt/lib/profile/InstrProfilingInternal.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
*/
2222
uint64_t __llvm_profile_get_size_for_buffer_internal(
2323
const __llvm_profile_data *DataBegin, const __llvm_profile_data *DataEnd,
24-
const uint64_t *CountersBegin, const uint64_t *CountersEnd,
25-
const char *NamesBegin, const char *NamesEnd);
24+
const char *CountersBegin, const char *CountersEnd, const char *NamesBegin,
25+
const char *NamesEnd);
2626

2727
/*!
2828
* \brief Write instrumentation data to the given buffer, given explicit
@@ -35,8 +35,8 @@ uint64_t __llvm_profile_get_size_for_buffer_internal(
3535
*/
3636
int __llvm_profile_write_buffer_internal(
3737
char *Buffer, const __llvm_profile_data *DataBegin,
38-
const __llvm_profile_data *DataEnd, const uint64_t *CountersBegin,
39-
const uint64_t *CountersEnd, const char *NamesBegin, const char *NamesEnd);
38+
const __llvm_profile_data *DataEnd, const char *CountersBegin,
39+
const char *CountersEnd, const char *NamesBegin, const char *NamesEnd);
4040

4141
/*!
4242
* The data structure describing the data to be written by the
@@ -152,8 +152,7 @@ int lprofWriteData(ProfDataWriter *Writer, VPDataReaderType *VPDataReader,
152152
int lprofWriteDataImpl(ProfDataWriter *Writer,
153153
const __llvm_profile_data *DataBegin,
154154
const __llvm_profile_data *DataEnd,
155-
const uint64_t *CountersBegin,
156-
const uint64_t *CountersEnd,
155+
const char *CountersBegin, const char *CountersEnd,
157156
VPDataReaderType *VPDataReader, const char *NamesBegin,
158157
const char *NamesEnd, int SkipNameDataWrite);
159158

0 commit comments

Comments
 (0)