xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingWriter.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric /*===- InstrProfilingWriter.c - Write instrumentation to a file or buffer -===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric 
9e8d8bef9SDimitry Andric // Note: This is linked into the Darwin kernel, and must remain compatible
10e8d8bef9SDimitry Andric // with freestanding compilation. See `darwin_add_builtin_libraries`.
11e8d8bef9SDimitry Andric 
120b57cec5SDimitry Andric #ifdef _MSC_VER
130b57cec5SDimitry Andric /* For _alloca */
140b57cec5SDimitry Andric #include <malloc.h>
150b57cec5SDimitry Andric #endif
160b57cec5SDimitry Andric #include <string.h>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "InstrProfiling.h"
190b57cec5SDimitry Andric #include "InstrProfilingInternal.h"
20480093f4SDimitry Andric #include "InstrProfilingPort.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #define INSTR_PROF_VALUE_PROF_DATA
23480093f4SDimitry Andric #include "profile/InstrProfData.inc"
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void (*FreeHook)(void *) = NULL;
260b57cec5SDimitry Andric static ProfBufferIO TheBufferIO;
270b57cec5SDimitry Andric #define VP_BUFFER_SIZE 8 * 1024
280b57cec5SDimitry Andric static uint8_t BufferIOBuffer[VP_BUFFER_SIZE];
290b57cec5SDimitry Andric static InstrProfValueData VPDataArray[16];
300b57cec5SDimitry Andric static uint32_t VPDataArraySize = sizeof(VPDataArray) / sizeof(*VPDataArray);
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint8_t *DynamicBufferIOBuffer = 0;
330b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint32_t VPBufferSize = 0;
340b57cec5SDimitry Andric 
35349cc55cSDimitry Andric /* The buffer writer is responsible in keeping writer state
360b57cec5SDimitry Andric  * across the call.
370b57cec5SDimitry Andric  */
380b57cec5SDimitry Andric COMPILER_RT_VISIBILITY uint32_t lprofBufferWriter(ProfDataWriter *This,
390b57cec5SDimitry Andric                                                   ProfDataIOVec *IOVecs,
400b57cec5SDimitry Andric                                                   uint32_t NumIOVecs) {
410b57cec5SDimitry Andric   uint32_t I;
420b57cec5SDimitry Andric   char **Buffer = (char **)&This->WriterCtx;
430b57cec5SDimitry Andric   for (I = 0; I < NumIOVecs; I++) {
440b57cec5SDimitry Andric     size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
450b57cec5SDimitry Andric     if (IOVecs[I].Data)
460b57cec5SDimitry Andric       memcpy(*Buffer, IOVecs[I].Data, Length);
47480093f4SDimitry Andric     else if (IOVecs[I].UseZeroPadding) {
48480093f4SDimitry Andric       /* Allocating the buffer should zero fill. */
49480093f4SDimitry Andric     }
500b57cec5SDimitry Andric     *Buffer += Length;
510b57cec5SDimitry Andric   }
520b57cec5SDimitry Andric   return 0;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric static void llvmInitBufferIO(ProfBufferIO *BufferIO, ProfDataWriter *FileWriter,
560b57cec5SDimitry Andric                              uint8_t *Buffer, uint32_t BufferSz) {
570b57cec5SDimitry Andric   BufferIO->FileWriter = FileWriter;
580b57cec5SDimitry Andric   BufferIO->OwnFileWriter = 0;
590b57cec5SDimitry Andric   BufferIO->BufferStart = Buffer;
600b57cec5SDimitry Andric   BufferIO->BufferSz = BufferSz;
610b57cec5SDimitry Andric   BufferIO->CurOffset = 0;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric COMPILER_RT_VISIBILITY ProfBufferIO *
650b57cec5SDimitry Andric lprofCreateBufferIO(ProfDataWriter *FileWriter) {
660b57cec5SDimitry Andric   uint8_t *Buffer = DynamicBufferIOBuffer;
670b57cec5SDimitry Andric   uint32_t BufferSize = VPBufferSize;
680b57cec5SDimitry Andric   if (!Buffer) {
690b57cec5SDimitry Andric     Buffer = &BufferIOBuffer[0];
700b57cec5SDimitry Andric     BufferSize = sizeof(BufferIOBuffer);
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric   llvmInitBufferIO(&TheBufferIO, FileWriter, Buffer, BufferSize);
730b57cec5SDimitry Andric   return &TheBufferIO;
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric COMPILER_RT_VISIBILITY void lprofDeleteBufferIO(ProfBufferIO *BufferIO) {
770b57cec5SDimitry Andric   if (BufferIO->OwnFileWriter)
780b57cec5SDimitry Andric     FreeHook(BufferIO->FileWriter);
790b57cec5SDimitry Andric   if (DynamicBufferIOBuffer) {
800b57cec5SDimitry Andric     FreeHook(DynamicBufferIOBuffer);
810b57cec5SDimitry Andric     DynamicBufferIOBuffer = 0;
820b57cec5SDimitry Andric     VPBufferSize = 0;
830b57cec5SDimitry Andric   }
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int
870b57cec5SDimitry Andric lprofBufferIOWrite(ProfBufferIO *BufferIO, const uint8_t *Data, uint32_t Size) {
880b57cec5SDimitry Andric   /* Buffer is not large enough, it is time to flush.  */
890b57cec5SDimitry Andric   if (Size + BufferIO->CurOffset > BufferIO->BufferSz) {
900b57cec5SDimitry Andric     if (lprofBufferIOFlush(BufferIO) != 0)
910b57cec5SDimitry Andric       return -1;
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric   /* Special case, bypass the buffer completely. */
94480093f4SDimitry Andric   ProfDataIOVec IO[] = {{Data, sizeof(uint8_t), Size, 0}};
950b57cec5SDimitry Andric   if (Size > BufferIO->BufferSz) {
960b57cec5SDimitry Andric     if (BufferIO->FileWriter->Write(BufferIO->FileWriter, IO, 1))
970b57cec5SDimitry Andric       return -1;
980b57cec5SDimitry Andric   } else {
990b57cec5SDimitry Andric     /* Write the data to buffer */
1000b57cec5SDimitry Andric     uint8_t *Buffer = BufferIO->BufferStart + BufferIO->CurOffset;
1010b57cec5SDimitry Andric     ProfDataWriter BufferWriter;
1020b57cec5SDimitry Andric     initBufferWriter(&BufferWriter, (char *)Buffer);
1030b57cec5SDimitry Andric     lprofBufferWriter(&BufferWriter, IO, 1);
1040b57cec5SDimitry Andric     BufferIO->CurOffset =
1050b57cec5SDimitry Andric         (uint8_t *)BufferWriter.WriterCtx - BufferIO->BufferStart;
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric   return 0;
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int lprofBufferIOFlush(ProfBufferIO *BufferIO) {
1110b57cec5SDimitry Andric   if (BufferIO->CurOffset) {
1120b57cec5SDimitry Andric     ProfDataIOVec IO[] = {
113480093f4SDimitry Andric         {BufferIO->BufferStart, sizeof(uint8_t), BufferIO->CurOffset, 0}};
1140b57cec5SDimitry Andric     if (BufferIO->FileWriter->Write(BufferIO->FileWriter, IO, 1))
1150b57cec5SDimitry Andric       return -1;
1160b57cec5SDimitry Andric     BufferIO->CurOffset = 0;
1170b57cec5SDimitry Andric   }
1180b57cec5SDimitry Andric   return 0;
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric /* Write out value profile data for function specified with \c Data.
1220b57cec5SDimitry Andric  * The implementation does not use the method \c serializeValueProfData
1230b57cec5SDimitry Andric  * which depends on dynamic memory allocation. In this implementation,
1240b57cec5SDimitry Andric  * value profile data is written out to \c BufferIO piecemeal.
1250b57cec5SDimitry Andric  */
1260b57cec5SDimitry Andric static int writeOneValueProfData(ProfBufferIO *BufferIO,
1270b57cec5SDimitry Andric                                  VPDataReaderType *VPDataReader,
1280b57cec5SDimitry Andric                                  const __llvm_profile_data *Data) {
1290b57cec5SDimitry Andric   unsigned I, NumValueKinds = 0;
1300b57cec5SDimitry Andric   ValueProfData VPHeader;
1310b57cec5SDimitry Andric   uint8_t *SiteCountArray[IPVK_Last + 1];
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   for (I = 0; I <= IPVK_Last; I++) {
1340b57cec5SDimitry Andric     if (!Data->NumValueSites[I])
1350b57cec5SDimitry Andric       SiteCountArray[I] = 0;
1360b57cec5SDimitry Andric     else {
1370b57cec5SDimitry Andric       uint32_t Sz =
1380b57cec5SDimitry Andric           VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
1390b57cec5SDimitry Andric           offsetof(ValueProfRecord, SiteCountArray);
1400b57cec5SDimitry Andric       /* Only use alloca for this small byte array to avoid excessive
1410b57cec5SDimitry Andric        * stack growth.  */
1420b57cec5SDimitry Andric       SiteCountArray[I] = (uint8_t *)COMPILER_RT_ALLOCA(Sz);
1430b57cec5SDimitry Andric       memset(SiteCountArray[I], 0, Sz);
1440b57cec5SDimitry Andric     }
1450b57cec5SDimitry Andric   }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   /* If NumValueKinds returned is 0, there is nothing to write, report
1480b57cec5SDimitry Andric      success and return. This should match the raw profile reader's behavior. */
1490b57cec5SDimitry Andric   if (!(NumValueKinds = VPDataReader->InitRTRecord(Data, SiteCountArray)))
1500b57cec5SDimitry Andric     return 0;
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   /* First write the header structure. */
1530b57cec5SDimitry Andric   VPHeader.TotalSize = VPDataReader->GetValueProfDataSize();
1540b57cec5SDimitry Andric   VPHeader.NumValueKinds = NumValueKinds;
1550b57cec5SDimitry Andric   if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPHeader,
1560b57cec5SDimitry Andric                          sizeof(ValueProfData)))
1570b57cec5SDimitry Andric     return -1;
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   /* Make sure nothing else needs to be written before value profile
1600b57cec5SDimitry Andric    * records. */
1610b57cec5SDimitry Andric   if ((void *)VPDataReader->GetFirstValueProfRecord(&VPHeader) !=
1620b57cec5SDimitry Andric       (void *)(&VPHeader + 1))
1630b57cec5SDimitry Andric     return -1;
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   /* Write out the value profile record for each value kind
1660b57cec5SDimitry Andric    * one by one. */
1670b57cec5SDimitry Andric   for (I = 0; I <= IPVK_Last; I++) {
1680b57cec5SDimitry Andric     uint32_t J;
1690b57cec5SDimitry Andric     ValueProfRecord RecordHeader;
1700b57cec5SDimitry Andric     /* The size of the value prof record header without counting the
1710b57cec5SDimitry Andric      * site count array .*/
1720b57cec5SDimitry Andric     uint32_t RecordHeaderSize = offsetof(ValueProfRecord, SiteCountArray);
1730b57cec5SDimitry Andric     uint32_t SiteCountArraySize;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric     if (!Data->NumValueSites[I])
1760b57cec5SDimitry Andric       continue;
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric     /* Write out the record header.  */
1790b57cec5SDimitry Andric     RecordHeader.Kind = I;
1800b57cec5SDimitry Andric     RecordHeader.NumValueSites = Data->NumValueSites[I];
1810b57cec5SDimitry Andric     if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&RecordHeader,
1820b57cec5SDimitry Andric                            RecordHeaderSize))
1830b57cec5SDimitry Andric       return -1;
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric     /* Write out the site value count array including padding space. */
1860b57cec5SDimitry Andric     SiteCountArraySize =
1870b57cec5SDimitry Andric         VPDataReader->GetValueProfRecordHeaderSize(Data->NumValueSites[I]) -
1880b57cec5SDimitry Andric         RecordHeaderSize;
1890b57cec5SDimitry Andric     if (lprofBufferIOWrite(BufferIO, SiteCountArray[I], SiteCountArraySize))
1900b57cec5SDimitry Andric       return -1;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric     /* Write out the value profile data for each value site.  */
1930b57cec5SDimitry Andric     for (J = 0; J < Data->NumValueSites[I]; J++) {
1940b57cec5SDimitry Andric       uint32_t NRead, NRemain;
1950b57cec5SDimitry Andric       ValueProfNode *NextStartNode = 0;
1960b57cec5SDimitry Andric       NRemain = VPDataReader->GetNumValueDataForSite(I, J);
1970b57cec5SDimitry Andric       if (!NRemain)
1980b57cec5SDimitry Andric         continue;
1990b57cec5SDimitry Andric       /* Read and write out value data in small chunks till it is done. */
2000b57cec5SDimitry Andric       do {
2010b57cec5SDimitry Andric         NRead = (NRemain > VPDataArraySize ? VPDataArraySize : NRemain);
2020b57cec5SDimitry Andric         NextStartNode =
2030b57cec5SDimitry Andric             VPDataReader->GetValueData(I, /* ValueKind */
2040b57cec5SDimitry Andric                                        J, /* Site */
2050b57cec5SDimitry Andric                                        &VPDataArray[0], NextStartNode, NRead);
2060b57cec5SDimitry Andric         if (lprofBufferIOWrite(BufferIO, (const uint8_t *)&VPDataArray[0],
2070b57cec5SDimitry Andric                                NRead * sizeof(InstrProfValueData)))
2080b57cec5SDimitry Andric           return -1;
2090b57cec5SDimitry Andric         NRemain -= NRead;
2100b57cec5SDimitry Andric       } while (NRemain != 0);
2110b57cec5SDimitry Andric     }
2120b57cec5SDimitry Andric   }
2130b57cec5SDimitry Andric   /* All done report success.  */
2140b57cec5SDimitry Andric   return 0;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric static int writeValueProfData(ProfDataWriter *Writer,
2180b57cec5SDimitry Andric                               VPDataReaderType *VPDataReader,
2190b57cec5SDimitry Andric                               const __llvm_profile_data *DataBegin,
2200b57cec5SDimitry Andric                               const __llvm_profile_data *DataEnd) {
2210b57cec5SDimitry Andric   ProfBufferIO *BufferIO;
2220b57cec5SDimitry Andric   const __llvm_profile_data *DI = 0;
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   if (!VPDataReader)
2250b57cec5SDimitry Andric     return 0;
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   BufferIO = lprofCreateBufferIO(Writer);
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   for (DI = DataBegin; DI < DataEnd; DI++) {
2300b57cec5SDimitry Andric     if (writeOneValueProfData(BufferIO, VPDataReader, DI))
2310b57cec5SDimitry Andric       return -1;
2320b57cec5SDimitry Andric   }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   if (lprofBufferIOFlush(BufferIO) != 0)
2350b57cec5SDimitry Andric     return -1;
2360b57cec5SDimitry Andric   lprofDeleteBufferIO(BufferIO);
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric   return 0;
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int lprofWriteData(ProfDataWriter *Writer,
2420b57cec5SDimitry Andric                                           VPDataReaderType *VPDataReader,
2430b57cec5SDimitry Andric                                           int SkipNameDataWrite) {
2440b57cec5SDimitry Andric   /* Match logic in __llvm_profile_write_buffer(). */
2450b57cec5SDimitry Andric   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
2460b57cec5SDimitry Andric   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
24704eeddc0SDimitry Andric   const char *CountersBegin = __llvm_profile_begin_counters();
24804eeddc0SDimitry Andric   const char *CountersEnd = __llvm_profile_end_counters();
2490b57cec5SDimitry Andric   const char *NamesBegin = __llvm_profile_begin_names();
2500b57cec5SDimitry Andric   const char *NamesEnd = __llvm_profile_end_names();
2510b57cec5SDimitry Andric   return lprofWriteDataImpl(Writer, DataBegin, DataEnd, CountersBegin,
2520b57cec5SDimitry Andric                             CountersEnd, VPDataReader, NamesBegin, NamesEnd,
2530b57cec5SDimitry Andric                             SkipNameDataWrite);
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric COMPILER_RT_VISIBILITY int
2570b57cec5SDimitry Andric lprofWriteDataImpl(ProfDataWriter *Writer, const __llvm_profile_data *DataBegin,
2580b57cec5SDimitry Andric                    const __llvm_profile_data *DataEnd,
25904eeddc0SDimitry Andric                    const char *CountersBegin, const char *CountersEnd,
2600b57cec5SDimitry Andric                    VPDataReaderType *VPDataReader, const char *NamesBegin,
2610b57cec5SDimitry Andric                    const char *NamesEnd, int SkipNameDataWrite) {
2620eae32dcSDimitry Andric   int DebugInfoCorrelate =
2630eae32dcSDimitry Andric       (__llvm_profile_get_version() & VARIANT_MASK_DBG_CORRELATE) != 0ULL;
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   /* Calculate size of sections. */
266bdd1243dSDimitry Andric   const uint64_t DataSectionSize =
2670eae32dcSDimitry Andric       DebugInfoCorrelate ? 0 : __llvm_profile_get_data_size(DataBegin, DataEnd);
26804eeddc0SDimitry Andric   const uint64_t NumData =
26904eeddc0SDimitry Andric       DebugInfoCorrelate ? 0 : __llvm_profile_get_num_data(DataBegin, DataEnd);
270bdd1243dSDimitry Andric   const uint64_t CountersSectionSize =
27104eeddc0SDimitry Andric       __llvm_profile_get_counters_size(CountersBegin, CountersEnd);
27204eeddc0SDimitry Andric   const uint64_t NumCounters =
27304eeddc0SDimitry Andric       __llvm_profile_get_num_counters(CountersBegin, CountersEnd);
2740eae32dcSDimitry Andric   const uint64_t NamesSize = DebugInfoCorrelate ? 0 : NamesEnd - NamesBegin;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   /* Create the header. */
2770b57cec5SDimitry Andric   __llvm_profile_header Header;
2780b57cec5SDimitry Andric 
279480093f4SDimitry Andric   /* Determine how much padding is needed before/after the counters and after
280480093f4SDimitry Andric    * the names. */
281480093f4SDimitry Andric   uint64_t PaddingBytesBeforeCounters, PaddingBytesAfterCounters,
282480093f4SDimitry Andric       PaddingBytesAfterNames;
283480093f4SDimitry Andric   __llvm_profile_get_padding_sizes_for_counters(
284bdd1243dSDimitry Andric       DataSectionSize, CountersSectionSize, NamesSize,
285bdd1243dSDimitry Andric       &PaddingBytesBeforeCounters, &PaddingBytesAfterCounters,
286bdd1243dSDimitry Andric       &PaddingBytesAfterNames);
287480093f4SDimitry Andric 
28804eeddc0SDimitry Andric   {
28904eeddc0SDimitry Andric     // TODO: Unfortunately the header's fields are named DataSize and
29004eeddc0SDimitry Andric     // CountersSize when they should be named NumData and NumCounters,
29104eeddc0SDimitry Andric     // respectively.
29204eeddc0SDimitry Andric     const uint64_t CountersSize = NumCounters;
29304eeddc0SDimitry Andric     const uint64_t DataSize = NumData;
2940b57cec5SDimitry Andric /* Initialize header structure.  */
2950b57cec5SDimitry Andric #define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
296480093f4SDimitry Andric #include "profile/InstrProfData.inc"
29704eeddc0SDimitry Andric   }
2980b57cec5SDimitry Andric 
299349cc55cSDimitry Andric   /* On WIN64, label differences are truncated 32-bit values. Truncate
300349cc55cSDimitry Andric    * CountersDelta to match. */
301349cc55cSDimitry Andric #ifdef _WIN64
302349cc55cSDimitry Andric   Header.CountersDelta = (uint32_t)Header.CountersDelta;
303349cc55cSDimitry Andric #endif
304349cc55cSDimitry Andric 
3050eae32dcSDimitry Andric   /* The data and names sections are omitted in lightweight mode. */
3060eae32dcSDimitry Andric   if (DebugInfoCorrelate) {
3070eae32dcSDimitry Andric     Header.CountersDelta = 0;
3080eae32dcSDimitry Andric     Header.NamesDelta = 0;
3090eae32dcSDimitry Andric   }
3100eae32dcSDimitry Andric 
311fe6060f1SDimitry Andric   /* Write the profile header. */
312fe6060f1SDimitry Andric   ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1, 0}};
313fe6060f1SDimitry Andric   if (Writer->Write(Writer, IOVec, sizeof(IOVec) / sizeof(*IOVec)))
314fe6060f1SDimitry Andric     return -1;
315fe6060f1SDimitry Andric 
316fe6060f1SDimitry Andric   /* Write the binary id lengths and data. */
317fe6060f1SDimitry Andric   if (__llvm_write_binary_ids(Writer) == -1)
318fe6060f1SDimitry Andric     return -1;
319fe6060f1SDimitry Andric 
320fe6060f1SDimitry Andric   /* Write the profile data. */
321fe6060f1SDimitry Andric   ProfDataIOVec IOVecData[] = {
322bdd1243dSDimitry Andric       {DebugInfoCorrelate ? NULL : DataBegin, sizeof(uint8_t), DataSectionSize,
323bdd1243dSDimitry Andric        0},
324480093f4SDimitry Andric       {NULL, sizeof(uint8_t), PaddingBytesBeforeCounters, 1},
325bdd1243dSDimitry Andric       {CountersBegin, sizeof(uint8_t), CountersSectionSize, 0},
326480093f4SDimitry Andric       {NULL, sizeof(uint8_t), PaddingBytesAfterCounters, 1},
3270eae32dcSDimitry Andric       {(SkipNameDataWrite || DebugInfoCorrelate) ? NULL : NamesBegin,
3280eae32dcSDimitry Andric        sizeof(uint8_t), NamesSize, 0},
329480093f4SDimitry Andric       {NULL, sizeof(uint8_t), PaddingBytesAfterNames, 1}};
330fe6060f1SDimitry Andric   if (Writer->Write(Writer, IOVecData, sizeof(IOVecData) / sizeof(*IOVecData)))
3310b57cec5SDimitry Andric     return -1;
3320b57cec5SDimitry Andric 
333480093f4SDimitry Andric   /* Value profiling is not yet supported in continuous mode. */
334480093f4SDimitry Andric   if (__llvm_profile_is_continuous_mode_enabled())
335480093f4SDimitry Andric     return 0;
336480093f4SDimitry Andric 
3370b57cec5SDimitry Andric   return writeValueProfData(Writer, VPDataReader, DataBegin, DataEnd);
3380b57cec5SDimitry Andric }
339*06c3fb27SDimitry Andric 
340*06c3fb27SDimitry Andric /*
341*06c3fb27SDimitry Andric  * Write binary id length and then its data, because binary id does not
342*06c3fb27SDimitry Andric  * have a fixed length.
343*06c3fb27SDimitry Andric  */
344*06c3fb27SDimitry Andric COMPILER_RT_VISIBILITY
345*06c3fb27SDimitry Andric int lprofWriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,
346*06c3fb27SDimitry Andric                           const uint8_t *BinaryIdData,
347*06c3fb27SDimitry Andric                           uint64_t BinaryIdPadding) {
348*06c3fb27SDimitry Andric   ProfDataIOVec BinaryIdIOVec[] = {
349*06c3fb27SDimitry Andric       {&BinaryIdLen, sizeof(uint64_t), 1, 0},
350*06c3fb27SDimitry Andric       {BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0},
351*06c3fb27SDimitry Andric       {NULL, sizeof(uint8_t), BinaryIdPadding, 1},
352*06c3fb27SDimitry Andric   };
353*06c3fb27SDimitry Andric   if (Writer->Write(Writer, BinaryIdIOVec,
354*06c3fb27SDimitry Andric                     sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec)))
355*06c3fb27SDimitry Andric     return -1;
356*06c3fb27SDimitry Andric 
357*06c3fb27SDimitry Andric   /* Successfully wrote binary id, report success. */
358*06c3fb27SDimitry Andric   return 0;
359*06c3fb27SDimitry Andric }
360