Index: include/llvm/ProfileData/InstrProf.h =================================================================== --- include/llvm/ProfileData/InstrProf.h +++ include/llvm/ProfileData/InstrProf.h @@ -132,6 +132,15 @@ } }; +namespace coverage { + +template +struct __attribute__((packed)) CovMapFunctionRecord { +#define COVMAP_FUNC_RECORD(T, TV, LLVMT, N, I) T N; +#include "llvm/ProfileData/InstrProfData.inc" +}; +} + } // end namespace llvm namespace std { Index: include/llvm/ProfileData/InstrProfData.inc =================================================================== --- include/llvm/ProfileData/InstrProfData.inc +++ include/llvm/ProfileData/InstrProfData.inc @@ -19,28 +19,28 @@ // struct ProfData { // #define INSTR_PROF_DATA(Type, LLVMTypeVar, 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" +// auto LLVMTypeVar = LLVMType; +// #include "llvm/ProfileData/InstrProfData.inc" // // 3. 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" +// #include "llvm/ProfileData/InstrProfData.inc" // }; // // 4. To construct constant array for the initializers: // #define INSTR_PROF_DATA(Type, LLVMTypeVar, LLVMType, Name, Initializer) \ // Initializer, // Constant *ConstantVals[] = { -// #include "ProfileData/InstrProfData.inc" +// #include "llvm/ProfileData/InstrProfData.inc" // }; //===----------------------------------------------------------------------===// @@ -51,14 +51,14 @@ // 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 uint32_t, Int32Ty2, llvm::Type::getInt32Ty(Ctx), NumCounters, \ + ConstantInt::get(Int32Ty2, 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, Int8PtrTy, llvm::Type::getInt8PtrTy(Ctx), NamePtr, \ + ConstantExpr::getBitCast(NamePtr, Int8PtrTy)) INSTR_PROF_DATA(const IntPtrT, Int64PtrTy, llvm::Type::getInt64PtrTy(Ctx), CounterPtr, \ - ConstantExpr::getBitCast(CounterPtr, Int8PtrTy)) + ConstantExpr::getBitCast(CounterPtr, Int64PtrTy)) // INSTR_PROF_DATA_END #ifdef INSTR_PROF_DATA @@ -75,10 +75,10 @@ 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 uint32_t, Int32Ty2, llvm::Type::getInt32Ty(Ctx), \ + DataSize, llvm::ConstantInt::get(Int32Ty2, DataSize)) COVMAP_FUNC_RECORD(const uint64_t, Int64Ty, llvm::Type::getInt64Ty(Ctx), \ - FuncHash, llvm::ConstantInt::get(Int64Ty, FuncSize)) + FuncHash, llvm::ConstantInt::get(Int64Ty, FuncHash)) // COVMAP_FUNC_RECORD_END #ifdef COVMAP_FUNC_RECORD Index: include/llvm/ProfileData/InstrProfReader.h =================================================================== --- include/llvm/ProfileData/InstrProfReader.h +++ include/llvm/ProfileData/InstrProfReader.h @@ -133,12 +133,10 @@ /// The profile data file contents. std::unique_ptr DataBuffer; struct ProfileData { - const uint32_t NameSize; - const uint32_t NumCounters; - const uint64_t FuncHash; - const IntPtrT NamePtr; - const IntPtrT CounterPtr; +#define INSTR_PROF_DATA(T, TV, LLVMT, N, I) T N; +#include "llvm/ProfileData/InstrProfData.inc" }; + struct RawHeader { const uint64_t Magic; const uint64_t Version; Index: lib/ProfileData/CoverageMappingReader.cpp =================================================================== --- lib/ProfileData/CoverageMappingReader.cpp +++ lib/ProfileData/CoverageMappingReader.cpp @@ -343,7 +343,7 @@ // 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); const char *FunEnd = Buf; // Get the filenames. @@ -366,12 +366,16 @@ // before reading the next map. Buf += alignmentAdjustment(Buf, 8); - while (FunBuf < FunEnd) { + const coverage::CovMapFunctionRecord *CFR = + reinterpret_cast *>(FunBuf); + while ((const char *)CFR < FunEnd) { // Read the function information - T NamePtr = endian::readNext(FunBuf); - uint32_t NameSize = endian::readNext(FunBuf); - uint32_t DataSize = endian::readNext(FunBuf); - uint64_t FuncHash = endian::readNext(FunBuf); + + T NamePtr = endian::byte_swap(CFR->NamePtr); + uint32_t NameSize = endian::byte_swap(CFR->NameSize); + uint32_t DataSize = endian::byte_swap(CFR->DataSize); + uint64_t FuncHash = endian::byte_swap(CFR->FuncHash); + CFR++; // Now use that to read the coverage data. if (CovBuf + DataSize > CovEnd) Index: lib/Transforms/Instrumentation/InstrProfiling.cpp =================================================================== --- lib/Transforms/Instrumentation/InstrProfiling.cpp +++ lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -224,20 +224,28 @@ RegionCounters[Inc->getName()] = Counters; // 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}; +// Define local variables for llvm types used +#define INSTR_PROF_DATA(T, TV, LLVMT, N, I) auto TV = LLVMT; +#include "llvm/ProfileData/InstrProfData.inc" + + Type *DataTypes[] = { +#define INSTR_PROF_DATA(T, TV, LLVMT, N, I) TV, +#include "llvm/ProfileData/InstrProfData.inc" + }; auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes)); + + auto *NameArrayTy = Name->getType()->getPointerElementType(); + auto NameSize = NameArrayTy->getArrayNumElements(); + auto FuncHash = Inc->getHash()->getZExtValue(); + auto NamePtr = Name; + auto CounterPtr = Counters; + 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)}; +#define INSTR_PROF_DATA(T, TV, LLVMT, N, I) I, +#include "llvm/ProfileData/InstrProfData.inc" + }; + auto *Data = new GlobalVariable(*M, DataTy, true, Name->getLinkage(), ConstantStruct::get(DataTy, DataVals), getVarName(Inc, "data"));