@@ -810,6 +810,45 @@ uint64_t InstrProfWriter::writeHeader(const IndexedInstrProf::Header &Header,
810810 return BackPatchStartOffset;
811811}
812812
813+ Error InstrProfWriter::writeBinaryIds (ProfOStream &OS) {
814+ // BinaryIdSection has two parts:
815+ // 1. uint64_t BinaryIdsSectionSize
816+ // 2. list of binary ids that consist of:
817+ // a. uint64_t BinaryIdLength
818+ // b. uint8_t BinaryIdData
819+ // c. uint8_t Padding (if necessary)
820+ // Calculate size of binary section.
821+ uint64_t BinaryIdsSectionSize = 0 ;
822+
823+ // Remove duplicate binary ids.
824+ llvm::sort (BinaryIds);
825+ BinaryIds.erase (llvm::unique (BinaryIds), BinaryIds.end ());
826+
827+ for (const auto &BI : BinaryIds) {
828+ // Increment by binary id length data type size.
829+ BinaryIdsSectionSize += sizeof (uint64_t );
830+ // Increment by binary id data length, aligned to 8 bytes.
831+ BinaryIdsSectionSize += alignToPowerOf2 (BI.size (), sizeof (uint64_t ));
832+ }
833+ // Write binary ids section size.
834+ OS.write (BinaryIdsSectionSize);
835+
836+ for (const auto &BI : BinaryIds) {
837+ uint64_t BILen = BI.size ();
838+ // Write binary id length.
839+ OS.write (BILen);
840+ // Write binary id data.
841+ for (unsigned K = 0 ; K < BILen; K++)
842+ OS.writeByte (BI[K]);
843+ // Write padding if necessary.
844+ uint64_t PaddingSize = alignToPowerOf2 (BILen, sizeof (uint64_t )) - BILen;
845+ for (unsigned K = 0 ; K < PaddingSize; K++)
846+ OS.writeByte (0 );
847+ }
848+
849+ return Error::success ();
850+ }
851+
813852Error InstrProfWriter::writeVTableNames (ProfOStream &OS) {
814853 std::vector<std::string> VTableNameStrs;
815854 for (StringRef VTableName : VTableNames.keys ())
@@ -920,41 +959,9 @@ Error InstrProfWriter::writeImpl(ProfOStream &OS) {
920959 return E;
921960 }
922961
923- // BinaryIdSection has two parts:
924- // 1. uint64_t BinaryIdsSectionSize
925- // 2. list of binary ids that consist of:
926- // a. uint64_t BinaryIdLength
927- // b. uint8_t BinaryIdData
928- // c. uint8_t Padding (if necessary)
929962 uint64_t BinaryIdSectionStart = OS.tell ();
930- // Calculate size of binary section.
931- uint64_t BinaryIdsSectionSize = 0 ;
932-
933- // Remove duplicate binary ids.
934- llvm::sort (BinaryIds);
935- BinaryIds.erase (llvm::unique (BinaryIds), BinaryIds.end ());
936-
937- for (const auto &BI : BinaryIds) {
938- // Increment by binary id length data type size.
939- BinaryIdsSectionSize += sizeof (uint64_t );
940- // Increment by binary id data length, aligned to 8 bytes.
941- BinaryIdsSectionSize += alignToPowerOf2 (BI.size (), sizeof (uint64_t ));
942- }
943- // Write binary ids section size.
944- OS.write (BinaryIdsSectionSize);
945-
946- for (const auto &BI : BinaryIds) {
947- uint64_t BILen = BI.size ();
948- // Write binary id length.
949- OS.write (BILen);
950- // Write binary id data.
951- for (unsigned K = 0 ; K < BILen; K++)
952- OS.writeByte (BI[K]);
953- // Write padding if necessary.
954- uint64_t PaddingSize = alignToPowerOf2 (BILen, sizeof (uint64_t )) - BILen;
955- for (unsigned K = 0 ; K < PaddingSize; K++)
956- OS.writeByte (0 );
957- }
963+ if (auto E = writeBinaryIds (OS))
964+ return E;
958965
959966 uint64_t VTableNamesSectionStart = OS.tell ();
960967
0 commit comments