1 //===- PGOCtxProfWriter.h - Contextual Profile Writer -----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file declares a utility for writing a contextual profile to bitstream. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_PROFILEDATA_PGOCTXPROFWRITER_H_ 14 #define LLVM_PROFILEDATA_PGOCTXPROFWRITER_H_ 15 16 #include "llvm/Bitstream/BitCodeEnums.h" 17 #include "llvm/Bitstream/BitstreamWriter.h" 18 #include "llvm/ProfileData/CtxInstrContextNode.h" 19 20 namespace llvm { 21 enum PGOCtxProfileRecords { Invalid = 0, Version, Guid, CalleeIndex, Counters }; 22 23 enum PGOCtxProfileBlockIDs { 24 ProfileMetadataBlockID = bitc::FIRST_APPLICATION_BLOCKID, 25 ContextNodeBlockID = ProfileMetadataBlockID + 1 26 }; 27 28 /// Write one or more ContextNodes to the provided raw_fd_stream. 29 /// The caller must destroy the PGOCtxProfileWriter object before closing the 30 /// stream. 31 /// The design allows serializing a bunch of contexts embedded in some other 32 /// file. The overall format is: 33 /// 34 /// [... other data written to the stream...] 35 /// SubBlock(ProfileMetadataBlockID) 36 /// Version 37 /// SubBlock(ContextNodeBlockID) 38 /// [RECORDS] 39 /// SubBlock(ContextNodeBlockID) 40 /// [RECORDS] 41 /// [... more SubBlocks] 42 /// EndBlock 43 /// EndBlock 44 /// 45 /// The "RECORDS" are bitsream records. The IDs are in CtxProfileCodes (except) 46 /// for Version, which is just for metadata). All contexts will have Guid and 47 /// Counters, and all but the roots have CalleeIndex. The order in which the 48 /// records appear does not matter, but they must precede any subcontexts, 49 /// because that helps keep the reader code simpler. 50 /// 51 /// Subblock containment captures the context->subcontext relationship. The 52 /// "next()" relationship in the raw profile, between call targets of indirect 53 /// calls, are just modeled as peer subblocks where the callee index is the 54 /// same. 55 /// 56 /// Versioning: the writer may produce additional records not known by the 57 /// reader. The version number indicates a more structural change. 58 /// The current version, in particular, is set up to expect optional extensions 59 /// like value profiling - which would appear as additional records. For 60 /// example, value profiling would produce a new record with a new record ID, 61 /// containing the profiled values (much like the counters) 62 class PGOCtxProfileWriter final { 63 BitstreamWriter Writer; 64 65 void writeCounters(const ctx_profile::ContextNode &Node); 66 void writeImpl(std::optional<uint32_t> CallerIndex, 67 const ctx_profile::ContextNode &Node); 68 69 public: 70 PGOCtxProfileWriter(raw_ostream &Out, 71 std::optional<unsigned> VersionOverride = std::nullopt) 72 : Writer(Out, 0) { 73 Writer.EnterSubblock(PGOCtxProfileBlockIDs::ProfileMetadataBlockID, 74 CodeLen); 75 const auto Version = VersionOverride ? *VersionOverride : CurrentVersion; 76 Writer.EmitRecord(PGOCtxProfileRecords::Version, 77 SmallVector<unsigned, 1>({Version})); 78 } 79 ~PGOCtxProfileWriter()80 ~PGOCtxProfileWriter() { Writer.ExitBlock(); } 81 82 void write(const ctx_profile::ContextNode &); 83 84 // constants used in writing which a reader may find useful. 85 static constexpr unsigned CodeLen = 2; 86 static constexpr uint32_t CurrentVersion = 1; 87 static constexpr unsigned VBREncodingBits = 6; 88 }; 89 90 } // namespace llvm 91 #endif 92