Skip to content

Commit a6c14fb

Browse files
petrhosektstellar
authored andcommitted
[profile] Fix profile merging with binary IDs
This fixes support for merging profiles which broke as a consequence of e50a388. The issue was missing adjustment in merge logic to account for the binary IDs which are now included in the raw profile just after header. In addition, this change also: * Includes the version in module signature that's used for merging to avoid accidental attempts to merge incompatible profiles. * Moves the binary IDs size field after version field in the header as was suggested in the review. Differential Revision: https://reviews.llvm.org/D107143 (cherry picked from commit 83302c8)
1 parent 8dcdfc0 commit a6c14fb

File tree

15 files changed

+43
-25
lines changed

15 files changed

+43
-25
lines changed

compiler-rt/include/profile/InstrProfData.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ INSTR_PROF_VALUE_NODE(PtrToNodeT, llvm::Type::getInt8PtrTy(Ctx), Next, \
129129
#endif
130130
INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
131131
INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
132+
INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
132133
INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSize)
133134
INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesBeforeCounters, PaddingBytesBeforeCounters)
134135
INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSize)
@@ -137,7 +138,6 @@ INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
137138
INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta, (uintptr_t)CountersBegin)
138139
INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
139140
INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
140-
INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
141141
#undef INSTR_PROF_RAW_HEADER
142142
/* INSTR_PROF_RAW_HEADER end */
143143

@@ -646,7 +646,7 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
646646
(uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
647647

648648
/* Raw profile format version (start from 1). */
649-
#define INSTR_PROF_RAW_VERSION 6
649+
#define INSTR_PROF_RAW_VERSION 7
650650
/* Indexed profile format version (start from 1). */
651651
#define INSTR_PROF_INDEX_VERSION 7
652652
/* Coverage mapping format version (start from 0). */

compiler-rt/lib/profile/InstrProfilingBuffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ uint64_t __llvm_profile_get_size_for_buffer_internal(
116116
DataSize, CountersSize, NamesSize, &PaddingBytesBeforeCounters,
117117
&PaddingBytesAfterCounters, &PaddingBytesAfterNames);
118118

119-
return sizeof(__llvm_profile_header) +
119+
return sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) +
120120
(DataSize * sizeof(__llvm_profile_data)) + PaddingBytesBeforeCounters +
121121
(CountersSize * sizeof(uint64_t)) + PaddingBytesAfterCounters +
122122
NamesSize + PaddingBytesAfterNames;

compiler-rt/lib/profile/InstrProfilingMerge.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void (*VPMergeHook)(ValueProfData *, __llvm_profile_data *);
2222
COMPILER_RT_VISIBILITY
2323
uint64_t lprofGetLoadModuleSignature() {
2424
/* A very fast way to compute a module signature. */
25+
uint64_t Version = __llvm_profile_get_version();
2526
uint64_t CounterSize = (uint64_t)(__llvm_profile_end_counters() -
2627
__llvm_profile_begin_counters());
2728
uint64_t DataSize = __llvm_profile_get_data_size(__llvm_profile_begin_data(),
@@ -33,7 +34,7 @@ uint64_t lprofGetLoadModuleSignature() {
3334
const __llvm_profile_data *FirstD = __llvm_profile_begin_data();
3435

3536
return (NamesSize << 40) + (CounterSize << 30) + (DataSize << 20) +
36-
(NumVnodes << 10) + (DataSize > 0 ? FirstD->NameRef : 0);
37+
(NumVnodes << 10) + (DataSize > 0 ? FirstD->NameRef : 0) + Version;
3738
}
3839

3940
/* Returns 1 if profile is not structurally compatible. */
@@ -44,7 +45,8 @@ int __llvm_profile_check_compatibility(const char *ProfileData,
4445
__llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
4546
__llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
4647
SrcDataStart =
47-
(__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header));
48+
(__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
49+
Header->BinaryIdsSize);
4850
SrcDataEnd = SrcDataStart + Header->DataSize;
4951

5052
if (ProfileSize < sizeof(__llvm_profile_header))
@@ -63,7 +65,7 @@ int __llvm_profile_check_compatibility(const char *ProfileData,
6365
Header->ValueKindLast != IPVK_Last)
6466
return 1;
6567

66-
if (ProfileSize < sizeof(__llvm_profile_header) +
68+
if (ProfileSize < sizeof(__llvm_profile_header) + Header->BinaryIdsSize +
6769
Header->DataSize * sizeof(__llvm_profile_data) +
6870
Header->NamesSize + Header->CountersSize)
6971
return 1;
@@ -91,7 +93,8 @@ int __llvm_profile_merge_from_buffer(const char *ProfileData,
9193
const char *SrcValueProfDataStart, *SrcValueProfData;
9294

9395
SrcDataStart =
94-
(__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header));
96+
(__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
97+
Header->BinaryIdsSize);
9598
SrcDataEnd = SrcDataStart + Header->DataSize;
9699
SrcCountersStart = (uint64_t *)SrcDataEnd;
97100
SrcNameStart = (const char *)(SrcCountersStart + Header->CountersSize);

compiler-rt/test/profile/Linux/binary-id.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
// RUN: llvm-profdata show --binary-ids %t.profraw > %t.profraw.out
1111
// RUN: FileCheck %s --check-prefix=BINARY-ID-RAW-PROF < %t.profraw.out
1212

13+
// RUN: rm -rf %t.profdir
14+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
15+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
16+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
17+
// RUN: llvm-profdata show --binary-ids %t.profdir/default_*.profraw > %t.profraw.out
18+
// RUN: FileCheck %s --check-prefix=BINARY-ID-MERGE-PROF < %t.profraw.out
19+
1320
void foo() {
1421
}
1522

@@ -34,3 +41,10 @@ int main() {
3441
// BINARY-ID-RAW-PROF-NEXT: Maximum internal block count: 0
3542
// BINARY-ID-RAW-PROF-NEXT: Binary IDs:
3643
// BINARY-ID-RAW-PROF-NEXT: {{[0-9a-f]+}}
44+
45+
// BINARY-ID-MERGE-PROF: Instrumentation level: Front-end
46+
// BINARY-ID-MERGE-PROF-NEXT: Total functions: 3
47+
// BINARY-ID-MERGE-PROF-NEXT: Maximum function count: 3
48+
// BINARY-ID-MERGE-PROF-NEXT: Maximum internal block count: 0
49+
// BINARY-ID-MERGE-PROF-NEXT: Binary IDs:
50+
// BINARY-ID-MERGE-PROF-NEXT: {{[0-9a-f]+}}

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ namespace RawInstrProf {
11041104
// Version 5: Bit 60 of FuncHash is reserved for the flag for the context
11051105
// sensitive records.
11061106
// Version 6: Added binary id.
1107+
// Version 7: Reorder binary id and include version in signature.
11071108
const uint64_t Version = INSTR_PROF_RAW_VERSION;
11081109

11091110
template <class IntPtrT> inline uint64_t getMagic();

llvm/include/llvm/ProfileData/InstrProfData.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ INSTR_PROF_VALUE_NODE(PtrToNodeT, llvm::Type::getInt8PtrTy(Ctx), Next, \
129129
#endif
130130
INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
131131
INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
132+
INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
132133
INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSize)
133134
INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesBeforeCounters, PaddingBytesBeforeCounters)
134135
INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSize)
@@ -137,7 +138,6 @@ INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
137138
INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta, (uintptr_t)CountersBegin)
138139
INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
139140
INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
140-
INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
141141
#undef INSTR_PROF_RAW_HEADER
142142
/* INSTR_PROF_RAW_HEADER end */
143143

@@ -646,7 +646,7 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
646646
(uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
647647

648648
/* Raw profile format version (start from 1). */
649-
#define INSTR_PROF_RAW_VERSION 6
649+
#define INSTR_PROF_RAW_VERSION 7
650650
/* Indexed profile format version (start from 1). */
651651
#define INSTR_PROF_INDEX_VERSION 7
652652
/* Coverage mapping format version (start from 0). */

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ Error RawInstrProfReader<IntPtrT>::readHeader(
366366
if (GET_VERSION(Version) != RawInstrProf::Version)
367367
return error(instrprof_error::unsupported_version);
368368

369+
BinaryIdsSize = swap(Header.BinaryIdsSize);
369370
CountersDelta = swap(Header.CountersDelta);
370371
NamesDelta = swap(Header.NamesDelta);
371372
auto DataSize = swap(Header.DataSize);
@@ -374,7 +375,6 @@ Error RawInstrProfReader<IntPtrT>::readHeader(
374375
auto PaddingBytesAfterCounters = swap(Header.PaddingBytesAfterCounters);
375376
NamesSize = swap(Header.NamesSize);
376377
ValueKindLast = swap(Header.ValueKindLast);
377-
BinaryIdsSize = swap(Header.BinaryIdsSize);
378378

379379
auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
380380
auto PaddingSize = getNumPaddingBytes(NamesSize);
Binary file not shown.
Binary file not shown.

llvm/test/tools/llvm-profdata/malformed-ptr-to-counter-array.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
//
33
// INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
44
// INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
5+
// INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
56
// INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSize)
67
// INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSize)
78
// INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
89
// INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta, (uintptr_t)CountersBegin)
910
// INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
1011
// INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
11-
// INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
1212

1313
RUN: printf '\201rforpl\377' > %t.profraw
14-
RUN: printf '\6\0\0\0\0\0\0\0' >> %t.profraw
14+
RUN: printf '\7\0\0\0\0\0\0\0' >> %t.profraw
15+
RUN: printf '\0\0\0\0\0\0\0\0' >> %t.profraw
1516
RUN: printf '\1\0\0\0\0\0\0\0' >> %t.profraw
1617
RUN: printf '\0\0\0\0\0\0\0\0' >> %t.profraw
1718
RUN: printf '\2\0\0\0\0\0\0\0' >> %t.profraw
@@ -20,7 +21,6 @@ RUN: printf '\10\0\0\0\0\0\0\0' >> %t.profraw
2021
RUN: printf '\0\0\6\0\1\0\0\0' >> %t.profraw
2122
RUN: printf '\0\0\6\0\2\0\0\0' >> %t.profraw
2223
RUN: printf '\0\0\0\0\0\0\0\0' >> %t.profraw
23-
RUN: printf '\0\0\0\0\0\0\0\0' >> %t.profraw
2424

2525
// Data Section
2626
//

llvm/test/tools/llvm-profdata/raw-32-bits-be.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
RUN: printf '\377lprofR\201' > %t
2-
RUN: printf '\0\0\0\0\0\0\0\6' >> %t
2+
RUN: printf '\0\0\0\0\0\0\0\7' >> %t
3+
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
34
RUN: printf '\0\0\0\0\0\0\0\2' >> %t
45
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
56
RUN: printf '\0\0\0\0\0\0\0\3' >> %t
@@ -8,7 +9,6 @@ RUN: printf '\0\0\0\0\0\0\0\20' >> %t
89
RUN: printf '\0\0\0\0\1\0\0\0' >> %t
910
RUN: printf '\0\0\0\0\2\0\0\0' >> %t
1011
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
11-
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
1212

1313
RUN: printf '\134\370\302\114\333\030\275\254' >> %t
1414
RUN: printf '\0\0\0\0\0\0\0\1' >> %t

llvm/test/tools/llvm-profdata/raw-32-bits-le.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
RUN: printf '\201Rforpl\377' > %t
2-
RUN: printf '\6\0\0\0\0\0\0\0' >> %t
2+
RUN: printf '\7\0\0\0\0\0\0\0' >> %t
3+
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
34
RUN: printf '\2\0\0\0\0\0\0\0' >> %t
45
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
56
RUN: printf '\3\0\0\0\0\0\0\0' >> %t
@@ -8,7 +9,6 @@ RUN: printf '\20\0\0\0\0\0\0\0' >> %t
89
RUN: printf '\0\0\0\1\0\0\0\0' >> %t
910
RUN: printf '\0\0\0\2\0\0\0\0' >> %t
1011
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
11-
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
1212

1313
RUN: printf '\254\275\030\333\114\302\370\134' >> %t
1414
RUN: printf '\1\0\0\0\0\0\0\0' >> %t

llvm/test/tools/llvm-profdata/raw-64-bits-be.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
RUN: printf '\377lprofr\201' > %t
2-
RUN: printf '\0\0\0\0\0\0\0\6' >> %t
2+
RUN: printf '\0\0\0\0\0\0\0\7' >> %t
3+
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
34
RUN: printf '\0\0\0\0\0\0\0\2' >> %t
45
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
56
RUN: printf '\0\0\0\0\0\0\0\3' >> %t
@@ -8,7 +9,6 @@ RUN: printf '\0\0\0\0\0\0\0\20' >> %t
89
RUN: printf '\0\0\0\1\0\4\0\0' >> %t
910
RUN: printf '\0\0\0\2\0\4\0\0' >> %t
1011
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
11-
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
1212

1313
RUN: printf '\134\370\302\114\333\030\275\254' >> %t
1414
RUN: printf '\0\0\0\0\0\0\0\1' >> %t

llvm/test/tools/llvm-profdata/raw-64-bits-le.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
RUN: printf '\201rforpl\377' > %t
2-
RUN: printf '\6\0\0\0\0\0\0\0' >> %t
2+
RUN: printf '\7\0\0\0\0\0\0\0' >> %t
3+
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
34
RUN: printf '\2\0\0\0\0\0\0\0' >> %t
45
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
56
RUN: printf '\3\0\0\0\0\0\0\0' >> %t
@@ -8,7 +9,6 @@ RUN: printf '\20\0\0\0\0\0\0\0' >> %t
89
RUN: printf '\0\0\4\0\1\0\0\0' >> %t
910
RUN: printf '\0\0\4\0\2\0\0\0' >> %t
1011
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
11-
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
1212

1313
RUN: printf '\254\275\030\333\114\302\370\134' >> %t
1414
RUN: printf '\1\0\0\0\0\0\0\0' >> %t

llvm/test/tools/llvm-profdata/raw-two-profiles.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
RUN: printf '\201rforpl\377' > %t-foo.profraw
2-
RUN: printf '\6\0\0\0\0\0\0\0' >> %t-foo.profraw
2+
RUN: printf '\7\0\0\0\0\0\0\0' >> %t-foo.profraw
3+
RUN: printf '\0\0\0\0\0\0\0\0' >> %t-foo.profraw
34
RUN: printf '\1\0\0\0\0\0\0\0' >> %t-foo.profraw
45
RUN: printf '\0\0\0\0\0\0\0\0' >> %t-foo.profraw
56
RUN: printf '\1\0\0\0\0\0\0\0' >> %t-foo.profraw
@@ -8,7 +9,6 @@ RUN: printf '\10\0\0\0\0\0\0\0' >> %t-foo.profraw
89
RUN: printf '\0\0\4\0\1\0\0\0' >> %t-foo.profraw
910
RUN: printf '\0\0\4\0\2\0\0\0' >> %t-foo.profraw
1011
RUN: printf '\0\0\0\0\0\0\0\0' >> %t-foo.profraw
11-
RUN: printf '\0\0\0\0\0\0\0\0' >> %t-foo.profraw
1212

1313
RUN: printf '\254\275\030\333\114\302\370\134' >> %t-foo.profraw
1414
RUN: printf '\1\0\0\0\0\0\0\0' >> %t-foo.profraw
@@ -21,7 +21,8 @@ RUN: printf '\023\0\0\0\0\0\0\0' >> %t-foo.profraw
2121
RUN: printf '\3\0foo\0\0\0' >> %t-foo.profraw
2222

2323
RUN: printf '\201rforpl\377' > %t-bar.profraw
24-
RUN: printf '\6\0\0\0\0\0\0\0' >> %t-bar.profraw
24+
RUN: printf '\7\0\0\0\0\0\0\0' >> %t-bar.profraw
25+
RUN: printf '\0\0\0\0\0\0\0\0' >> %t-bar.profraw
2526
RUN: printf '\1\0\0\0\0\0\0\0' >> %t-bar.profraw
2627
RUN: printf '\0\0\0\0\0\0\0\0' >> %t-bar.profraw
2728
RUN: printf '\2\0\0\0\0\0\0\0' >> %t-bar.profraw
@@ -30,7 +31,6 @@ RUN: printf '\10\0\0\0\0\0\0\0' >> %t-bar.profraw
3031
RUN: printf '\0\0\6\0\1\0\0\0' >> %t-bar.profraw
3132
RUN: printf '\0\0\6\0\2\0\0\0' >> %t-bar.profraw
3233
RUN: printf '\0\0\0\0\0\0\0\0' >> %t-bar.profraw
33-
RUN: printf '\0\0\0\0\0\0\0\0' >> %t-bar.profraw
3434

3535
RUN: printf '\067\265\035\031\112\165\023\344' >> %t-bar.profraw
3636
RUN: printf '\02\0\0\0\0\0\0\0' >> %t-bar.profraw

0 commit comments

Comments
 (0)