[PGO] Allow value profile writer interface to allocated target buffer
authorXinliang David Li <davidxl@google.com>
Sat, 28 Nov 2015 05:37:01 +0000 (05:37 +0000)
committerXinliang David Li <davidxl@google.com>
Sat, 28 Nov 2015 05:37:01 +0000 (05:37 +0000)
Raw profile writer needs to write all data of one kind in one continuous block,
so the buffer needs to be pre-allocated and passed to the writer method in
pieces for function profile data. The change adds the support for raw value data
writing.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254219 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ProfileData/InstrProf.h
lib/ProfileData/InstrProf.cpp
unittests/ProfileData/InstrProfTest.cpp

index 3a1226662007fce351f5ec55de80384127fcf408..8fd3d7f8ffd67116746a69fff3a68a806c3de1ed 100644 (file)
@@ -513,7 +513,8 @@ typedef struct ValueProfData {
   /// All data in the instance are properly byte swapped. The input
   /// data is assumed to be in little endian order.
   static ErrorOr<std::unique_ptr<ValueProfData>>
   /// All data in the instance are properly byte swapped. The input
   /// data is assumed to be in little endian order.
   static ErrorOr<std::unique_ptr<ValueProfData>>
-  getValueProfData(const unsigned char *D, const unsigned char *const BufferEnd,
+  getValueProfData(const unsigned char *SrcBuffer,
+                   const unsigned char *const SrcBufferEnd,
                    support::endianness SrcDataEndianness);
   /// Swap byte order from \c Endianness order to host byte order.
   void swapBytesToHost(support::endianness Endianness);
                    support::endianness SrcDataEndianness);
   /// Swap byte order from \c Endianness order to host byte order.
   void swapBytesToHost(support::endianness Endianness);
@@ -596,8 +597,8 @@ uint32_t getValueProfDataSizeRT(const ValueProfRuntimeRecord *Record);
 
 /* Return a ValueProfData instance that stores the data collected at runtime. */
 ValueProfData *
 
 /* Return a ValueProfData instance that stores the data collected at runtime. */
 ValueProfData *
-serializeValueProfDataFromRT(const ValueProfRuntimeRecord *Record);
-
+serializeValueProfDataFromRT(const ValueProfRuntimeRecord *Record,
+                             ValueProfData *Dst);
 
 /*! \brief Return the \c ValueProfRecord header size including the
  * padding bytes.
 
 /*! \brief Return the \c ValueProfRecord header size including the
  * padding bytes.
index 54c37e30dba3f0df39b1fe6918a7886485a88b84..3bbc8249c3d0de9b1c905c1fdbd6732b2bfd70d5 100644 (file)
@@ -170,10 +170,12 @@ void serializeValueProfRecordFrom(ValueProfRecord *This,
   }
 }
 
   }
 }
 
-ValueProfData *serializeValueProfDataFrom(ValueProfRecordClosure *Closure) {
+ValueProfData *serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
+                                          ValueProfData *DstData) {
   uint32_t TotalSize = getValueProfDataSize(Closure);
 
   uint32_t TotalSize = getValueProfDataSize(Closure);
 
-  ValueProfData *VPD = Closure->AllocValueProfData(TotalSize);
+  ValueProfData *VPD =
+      DstData ? DstData : Closure->AllocValueProfData(TotalSize);
 
   VPD->TotalSize = TotalSize;
   VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record);
 
   VPD->TotalSize = TotalSize;
   VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record);
@@ -259,7 +261,7 @@ ValueProfData::serializeFrom(const InstrProfRecord &Record) {
   InstrProfRecordClosure.Record = &Record;
 
   std::unique_ptr<ValueProfData> VPD(
   InstrProfRecordClosure.Record = &Record;
 
   std::unique_ptr<ValueProfData> VPD(
-      serializeValueProfDataFrom(&InstrProfRecordClosure));
+      serializeValueProfDataFrom(&InstrProfRecordClosure, 0));
   return VPD;
 }
 
   return VPD;
 }
 
@@ -367,16 +369,18 @@ uint32_t getValueProfDataSizeRT(const ValueProfRuntimeRecord *Record) {
 }
 
 /* Return a ValueProfData instance that stores the data collected
 }
 
 /* Return a ValueProfData instance that stores the data collected
-   from runtime. */
+ * from runtime. If \c DstData is provided by the caller, the value
+ * profile data will be store in *DstData and DstData is returned,
+ * otherwise the method will allocate space for the value data and
+ * return pointer to the newly allocated space.
+ */
 ValueProfData *
 ValueProfData *
-serializeValueProfDataFromRT(const ValueProfRuntimeRecord *Record) {
+serializeValueProfDataFromRT(const ValueProfRuntimeRecord *Record,
+                             ValueProfData *DstData) {
   RTRecordClosure.Record = Record;
   RTRecordClosure.Record = Record;
-  return serializeValueProfDataFrom(&RTRecordClosure);
+  return serializeValueProfDataFrom(&RTRecordClosure, DstData);
 }
 
 }
 
-
-
-
 void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
                                     InstrProfRecord::ValueMapType *VMap) {
   Record.reserveSites(Kind, NumValueSites);
 void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
                                     InstrProfRecord::ValueMapType *VMap) {
   Record.reserveSites(Kind, NumValueSites);
index 051bf391b9531fa76fb0e0299f2167eb3e94939a..d50944ff77fd7489f28c1d568926f0f8fa0c5863 100644 (file)
@@ -409,7 +409,7 @@ TEST_F(InstrProfTest, runtime_value_prof_data_read_write) {
   initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0],
                                    &ValueProfNodes[0]);
 
   initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0],
                                    &ValueProfNodes[0]);
 
-  ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord);
+  ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord, 0);
 
   InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
 
 
   InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});