[llvm] r252113 - [PGO] Use template file to define runtime structures

Xinliang David Li via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 4 16:47:26 PST 2015


Author: davidxl
Date: Wed Nov  4 18:47:26 2015
New Revision: 252113

URL: http://llvm.org/viewvc/llvm-project?rev=252113&view=rev
Log:
[PGO] Use template file to define runtime structures

With this change, instrumentation code and reader/write
code related to profile data structs are kept strictly
in-sync. THis will be extended to cfe and compile-rt 
references as well.

Differential Revision: http://reviews.llvm.org/D13843

Modified:
    llvm/trunk/include/llvm/ProfileData/InstrProf.h
    llvm/trunk/include/llvm/ProfileData/InstrProfData.inc
    llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp
    llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp

Modified: llvm/trunk/include/llvm/ProfileData/InstrProf.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/InstrProf.h?rev=252113&r1=252112&r2=252113&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/InstrProf.h (original)
+++ llvm/trunk/include/llvm/ProfileData/InstrProf.h Wed Nov  4 18:47:26 2015
@@ -431,11 +431,8 @@ inline uint64_t getMagic<uint32_t>() {
 
 template <class IntPtrT>
 struct ProfileData {
-  const uint32_t NameSize;
-  const uint32_t NumCounters;
-  const uint64_t FuncHash;
-  const IntPtrT NamePtr;
-  const IntPtrT CounterPtr;
+  #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Type Name;
+  #include "llvm/ProfileData/InstrProfData.inc"
 };
 
 // The definition should match the header referenced in
@@ -454,6 +451,18 @@ struct Header {
 
 }  // end namespace RawInstrProf
 
+namespace coverage {
+
+LLVM_PACKED_START
+template <class IntPtrT>
+struct CovMapFunctionRecord {
+  #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init) Type Name;
+  #include "llvm/ProfileData/InstrProfData.inc"
+};
+LLVM_PACKED_END
+
+}
+
 } // end namespace llvm
 
 namespace std {

Modified: llvm/trunk/include/llvm/ProfileData/InstrProfData.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/InstrProfData.inc?rev=252113&r1=252112&r2=252113&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/InstrProfData.inc (original)
+++ llvm/trunk/include/llvm/ProfileData/InstrProfData.inc Wed Nov  4 18:47:26 2015
@@ -17,48 +17,45 @@
 // 1. To declare a structure:
 // 
 // struct ProfData {
-// #define INSTR_PROF_DATA(Type, LLVMTypeVar, LLVMType, Name, Initializer) \
+// #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
 //    Type Name;
-// #include "ProfileData/InstrProfData.inc"
+// #include "llvm/ProfileData/InstrProfData.inc"
 // };
 //
-// 2. To define local variables for struct member's LLVM types"
-//
-// #define INSTR_PROF_DATA(Type, LLVMTypeVar, LLVMType, Name, Initializer) \
-//   LLVMTypeVar = LLVMType;
-// #include "ProfileData/InstrProfData.inc"
-//
-// 3. To construct LLVM type arrays for the struct type:
+// 2. To construct LLVM type arrays for the struct type:
 //
 // Type *DataTypes[] = {
-// #define INSTR_PROF_DATA(Type, LLVMTypeVar, LLVMType, Name, Initializer) \
-//   LLVMTypeVar,
-// #include "ProfileData/InstrProfData.inc"
+// #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
+//   LLVMType,
+// #include "llvm/ProfileData/InstrProfData.inc"
 // };
 //
 // 4. To construct constant array for the initializers:
-// #define INSTR_PROF_DATA(Type, LLVMTypeVar, LLVMType, Name, Initializer) \
+// #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
 //   Initializer,
 // Constant *ConstantVals[] = {
-// #include "ProfileData/InstrProfData.inc"
+// #include "llvm/ProfileData/InstrProfData.inc"
 // };
 //===----------------------------------------------------------------------===//
 
 #ifndef INSTR_PROF_DATA
-#define INSTR_PROF_DATA(Type, LLVMTypeVar, LLVMType, Name, Initializer)
+#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer)
 #endif
 
 // INSTR_PROF_DATA_START
-INSTR_PROF_DATA(const uint32_t, Int32Ty, llvm::Type::getInt32Ty(Ctx), NameSize, \
-                ConstantInt::get(Int32Ty, NameSize))
-INSTR_PROF_DATA(const uint32_t, Int32Ty, llvm::Type::getInt32Ty(Ctx), NumCounters, \
-                ConstantInt::get(Int32Ty, NumCounters))
-INSTR_PROF_DATA(const uint64_t, Int64Ty, llvm::Type::getInt64Ty(Ctx), FuncHash, \
-                ConstantInt::get(Int64Ty, FuncHash))
-INSTR_PROF_DATA(const IntPtrT, Int8PtrTy,llvm::Type::getInt8PtrTy(Ctx), NamePtr, \
-                ConstantExpr::getBitCast(Name, Int8PtrTy))
-INSTR_PROF_DATA(const IntPtrT, Int64PtrTy, llvm::Type::getInt64PtrTy(Ctx), CounterPtr, \
-                ConstantExpr::getBitCast(CounterPtr, Int8PtrTy))
+INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
+                ConstantInt::get(llvm::Type::getInt32Ty(Ctx), \
+                NamePtr->getType()->getPointerElementType()->getArrayNumElements()))
+INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumCounters, \
+                ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumCounters))
+INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
+                ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
+                Inc->getHash()->getZExtValue()))
+INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), NamePtr, \
+                ConstantExpr::getBitCast(NamePtr, llvm::Type::getInt8PtrTy(Ctx)))
+INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt64PtrTy(Ctx), CounterPtr, \
+                ConstantExpr::getBitCast(CounterPtr, \
+                llvm::Type::getInt64PtrTy(Ctx)))
 // INSTR_PROF_DATA_END
 
 #ifdef INSTR_PROF_DATA
@@ -67,18 +64,19 @@ INSTR_PROF_DATA(const IntPtrT, Int64PtrT
 
 
 #ifndef COVMAP_FUNC_RECORD
-#define COVMAP_FUNC_RECORD(Type, LLVMTypeVar, LLVMType, Name, Initializer)
+#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Initializer)
 #endif
 
 // COVMAP_FUNC_RECORD_START
-COVMAP_FUNC_RECORD(const IntPtrT, Int8PtrTy, llvm::Type::getInt8PtrTy(Ctx), \
-                   NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, Int8PtrTy))
-COVMAP_FUNC_RECORD(const uint32_t, Int32Ty, llvm::Type::getInt32Ty(Ctx), \
-                   NameSize, llvm::ConstantInt::get(Int32Ty, NameSize))
-COVMAP_FUNC_RECORD(const uint32_t, Int32Ty, llvm::Type::getInt32Ty(Ctx), \
-                   DataSize, llvm::ConstantInt::get(Int32Ty, DataSize))
-COVMAP_FUNC_RECORD(const uint64_t, Int64Ty, llvm::Type::getInt64Ty(Ctx), \
-                   FuncHash, llvm::ConstantInt::get(Int64Ty, FuncSize))
+COVMAP_FUNC_RECORD(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), \
+                   NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, \
+                   llvm::Type::getInt8PtrTy(Ctx))) 
+COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
+                   llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NameSize))
+COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), DataSize, \
+                   llvm::ConstantInt::get(llvm::getInt32Ty(Ctx), DataSize))
+COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
+                   llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), FuncHash))
 // COVMAP_FUNC_RECORD_END
 
 #ifdef COVMAP_FUNC_RECORD

Modified: llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp?rev=252113&r1=252112&r2=252113&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp (original)
+++ llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp Wed Nov  4 18:47:26 2015
@@ -343,7 +343,7 @@ std::error_code readCoverageMappingData(
 
     // Skip past the function records, saving the start and end for later.
     const char *FunBuf = Buf;
-    Buf += NRecords * (sizeof(T) + 2 * sizeof(uint32_t) + sizeof(uint64_t));
+    Buf += NRecords * sizeof(coverage::CovMapFunctionRecord<T>);
     const char *FunEnd = Buf;
 
     // Get the filenames.
@@ -366,12 +366,15 @@ std::error_code readCoverageMappingData(
     // before reading the next map.
     Buf += alignmentAdjustment(Buf, 8);
 
-    while (FunBuf < FunEnd) {
+    auto CFR =
+        reinterpret_cast<const coverage::CovMapFunctionRecord<T> *>(FunBuf);
+    while ((const char *)CFR < FunEnd) {
       // Read the function information
-      T NamePtr = endian::readNext<T, Endian, unaligned>(FunBuf);
-      uint32_t NameSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf);
-      uint32_t DataSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf);
-      uint64_t FuncHash = endian::readNext<uint64_t, Endian, unaligned>(FunBuf);
+      T NamePtr = endian::byte_swap<T, Endian>(CFR->NamePtr);
+      uint32_t NameSize = endian::byte_swap<uint32_t, Endian>(CFR->NameSize);
+      uint32_t DataSize = endian::byte_swap<uint32_t, Endian>(CFR->DataSize);
+      uint64_t FuncHash = endian::byte_swap<uint64_t, Endian>(CFR->FuncHash);
+      CFR++;
 
       // Now use that to read the coverage data.
       if (CovBuf + DataSize > CovEnd)

Modified: llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp?rev=252113&r1=252112&r2=252113&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/InstrProfiling.cpp Wed Nov  4 18:47:26 2015
@@ -193,8 +193,8 @@ static std::string getVarName(InstrProfI
 
 GlobalVariable *
 InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) {
-  GlobalVariable *Name = Inc->getName();
-  auto It = RegionCounters.find(Name);
+  GlobalVariable *NamePtr = Inc->getName();
+  auto It = RegionCounters.find(NamePtr);
   if (It != RegionCounters.end())
     return It->second;
 
@@ -207,45 +207,43 @@ InstrProfiling::getOrCreateRegionCounter
   if (Fn->hasComdat())
     ProfileVarsComdat = M->getOrInsertComdat(
         StringRef(getVarName(Inc, getInstrProfComdatPrefix())));
-  Name->setSection(getNameSection());
-  Name->setAlignment(1);
-  Name->setComdat(ProfileVarsComdat);
+  NamePtr->setSection(getNameSection());
+  NamePtr->setAlignment(1);
+  NamePtr->setComdat(ProfileVarsComdat);
 
   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
   LLVMContext &Ctx = M->getContext();
   ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
 
   // Create the counters variable.
-  auto *Counters =
-      new GlobalVariable(*M, CounterTy, false, Name->getLinkage(),
+  auto *CounterPtr =
+      new GlobalVariable(*M, CounterTy, false, NamePtr->getLinkage(),
                          Constant::getNullValue(CounterTy),
                          getVarName(Inc, getInstrProfCountersVarPrefix()));
-  Counters->setVisibility(Name->getVisibility());
-  Counters->setSection(getCountersSection());
-  Counters->setAlignment(8);
-  Counters->setComdat(ProfileVarsComdat);
+  CounterPtr->setVisibility(NamePtr->getVisibility());
+  CounterPtr->setSection(getCountersSection());
+  CounterPtr->setAlignment(8);
+  CounterPtr->setComdat(ProfileVarsComdat);
 
-  RegionCounters[Inc->getName()] = Counters;
+  RegionCounters[Inc->getName()] = CounterPtr;
 
   // Create data variable.
-  auto *NameArrayTy = Name->getType()->getPointerElementType();
-  auto *Int32Ty = Type::getInt32Ty(Ctx);
-  auto *Int64Ty = Type::getInt64Ty(Ctx);
-  auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
-  auto *Int64PtrTy = Type::getInt64PtrTy(Ctx);
 
-  Type *DataTypes[] = {Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int64PtrTy};
+  Type *DataTypes[] = {
+    #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType,
+    #include "llvm/ProfileData/InstrProfData.inc"
+  };
   auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes));
+
   Constant *DataVals[] = {
-      ConstantInt::get(Int32Ty, NameArrayTy->getArrayNumElements()),
-      ConstantInt::get(Int32Ty, NumCounters),
-      ConstantInt::get(Int64Ty, Inc->getHash()->getZExtValue()),
-      ConstantExpr::getBitCast(Name, Int8PtrTy),
-      ConstantExpr::getBitCast(Counters, Int64PtrTy)};
-  auto *Data = new GlobalVariable(*M, DataTy, true, Name->getLinkage(),
+    #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init,
+    #include "llvm/ProfileData/InstrProfData.inc"
+  };
+
+  auto *Data = new GlobalVariable(*M, DataTy, true, NamePtr->getLinkage(),
                                   ConstantStruct::get(DataTy, DataVals),
                                   getVarName(Inc, getInstrProfDataVarPrefix()));
-  Data->setVisibility(Name->getVisibility());
+  Data->setVisibility(NamePtr->getVisibility());
   Data->setSection(getDataSection());
   Data->setAlignment(8);
   Data->setComdat(ProfileVarsComdat);
@@ -253,7 +251,7 @@ InstrProfiling::getOrCreateRegionCounter
   // Mark the data variable as used so that it isn't stripped out.
   UsedVars.push_back(Data);
 
-  return Counters;
+  return CounterPtr;
 }
 
 void InstrProfiling::emitRegistration() {




More information about the llvm-commits mailing list