@@ -639,6 +639,27 @@ static Error writeMemProf(ProfOStream &OS,
639639 memprof::MaximumSupportedVersion));
640640}
641641
642+ uint64_t InstrProfWriter::writeHeader (const IndexedInstrProf::Header &Header,
643+ const bool WritePrevVersion,
644+ ProfOStream &OS) {
645+ // Only write out the first four fields.
646+ for (int I = 0 ; I < 4 ; I++)
647+ OS.write (reinterpret_cast <const uint64_t *>(&Header)[I]);
648+
649+ // Remember the offset of the remaining fields to allow back patching later.
650+ auto BackPatchStartOffset = OS.tell ();
651+
652+ // Reserve the space for back patching later.
653+ OS.write (0 ); // HashOffset
654+ OS.write (0 ); // MemProfOffset
655+ OS.write (0 ); // BinaryIdOffset
656+ OS.write (0 ); // TemporalProfTracesOffset
657+ if (!WritePrevVersion)
658+ OS.write (0 ); // VTableNamesOffset
659+
660+ return BackPatchStartOffset;
661+ }
662+
642663Error InstrProfWriter::writeImpl (ProfOStream &OS) {
643664 using namespace IndexedInstrProf ;
644665 using namespace support ;
@@ -651,7 +672,7 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
651672 InfoObj->CSSummaryBuilder = &CSISB;
652673
653674 // Populate the hash table generator.
654- SmallVector<std::pair<StringRef, const ProfilingData *>, 0 > OrderedData;
675+ SmallVector<std::pair<StringRef, const ProfilingData *>> OrderedData;
655676 for (const auto &I : FunctionData)
656677 if (shouldEncodeData (I.getValue ()))
657678 OrderedData.emplace_back ((I.getKey ()), &I.getValue ());
@@ -693,35 +714,8 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
693714 Header.TemporalProfTracesOffset = 0 ;
694715 Header.VTableNamesOffset = 0 ;
695716
696- // Only write out the first four fields. We need to remember the offset of the
697- // remaining fields to allow back patching later.
698- for (int I = 0 ; I < 4 ; I++)
699- OS.write (reinterpret_cast <uint64_t *>(&Header)[I]);
700-
701- // Save the location of Header.HashOffset field in \c OS.
702- uint64_t HashTableStartFieldOffset = OS.tell ();
703- // Reserve the space for HashOffset field.
704- OS.write (0 );
705-
706- // Save the location of MemProf profile data. This is stored in two parts as
707- // the schema and as a separate on-disk chained hashtable.
708- uint64_t MemProfSectionOffset = OS.tell ();
709- // Reserve space for the MemProf table field to be patched later if this
710- // profile contains memory profile information.
711- OS.write (0 );
712-
713- // Save the location of binary ids section.
714- uint64_t BinaryIdSectionOffset = OS.tell ();
715- // Reserve space for the BinaryIdOffset field to be patched later if this
716- // profile contains binary ids.
717- OS.write (0 );
718-
719- uint64_t TemporalProfTracesOffset = OS.tell ();
720- OS.write (0 );
721-
722- uint64_t VTableNamesOffset = OS.tell ();
723- if (!WritePrevVersion)
724- OS.write (0 );
717+ const uint64_t BackPatchStartOffset =
718+ writeHeader (Header, WritePrevVersion, OS);
725719
726720 // Reserve space to write profile summary data.
727721 uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size ();
@@ -850,16 +844,20 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
850844 }
851845 InfoObj->CSSummaryBuilder = nullptr ;
852846
847+ const size_t MemProfOffset = BackPatchStartOffset + sizeof (uint64_t );
848+ const size_t BinaryIdOffset = MemProfOffset + sizeof (uint64_t );
849+ const size_t TemporalProfTracesOffset = BinaryIdOffset + sizeof (uint64_t );
850+ const size_t VTableNamesOffset = TemporalProfTracesOffset + sizeof (uint64_t );
853851 if (!WritePrevVersion) {
854852 // Now do the final patch:
855853 PatchItem PatchItems[] = {
856854 // Patch the Header.HashOffset field.
857- {HashTableStartFieldOffset , &HashTableStart, 1 },
855+ {BackPatchStartOffset , &HashTableStart, 1 },
858856 // Patch the Header.MemProfOffset (=0 for profiles without MemProf
859857 // data).
860- {MemProfSectionOffset , &MemProfSectionStart, 1 },
858+ {MemProfOffset , &MemProfSectionStart, 1 },
861859 // Patch the Header.BinaryIdSectionOffset.
862- {BinaryIdSectionOffset , &BinaryIdSectionStart, 1 },
860+ {BinaryIdOffset , &BinaryIdSectionStart, 1 },
863861 // Patch the Header.TemporalProfTracesOffset (=0 for profiles without
864862 // traces).
865863 {TemporalProfTracesOffset, &TemporalProfTracesSectionStart, 1 },
@@ -875,12 +873,12 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
875873 // Now do the final patch:
876874 PatchItem PatchItems[] = {
877875 // Patch the Header.HashOffset field.
878- {HashTableStartFieldOffset , &HashTableStart, 1 },
876+ {BackPatchStartOffset , &HashTableStart, 1 },
879877 // Patch the Header.MemProfOffset (=0 for profiles without MemProf
880878 // data).
881- {MemProfSectionOffset , &MemProfSectionStart, 1 },
879+ {MemProfOffset , &MemProfSectionStart, 1 },
882880 // Patch the Header.BinaryIdSectionOffset.
883- {BinaryIdSectionOffset , &BinaryIdSectionStart, 1 },
881+ {BinaryIdOffset , &BinaryIdSectionStart, 1 },
884882 // Patch the Header.TemporalProfTracesOffset (=0 for profiles without
885883 // traces).
886884 {TemporalProfTracesOffset, &TemporalProfTracesSectionStart, 1 },
0 commit comments