Index: include/llvm/ProfileData/InstrProf.h =================================================================== --- include/llvm/ProfileData/InstrProf.h +++ include/llvm/ProfileData/InstrProf.h @@ -19,6 +19,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/IR/GlobalValue.h" +#include "llvm/ProfileData/InstrProfData.inc" #include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" @@ -36,19 +37,28 @@ /// Return the name of data section containing profile counter variables. inline StringRef getInstrProfCountersSectionName(bool AddSegment) { - return AddSegment ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts"; + return AddSegment ? "__DATA," INSTR_PROF_CNTS_SECT_NAME_STR + : INSTR_PROF_CNTS_SECT_NAME_STR; } /// Return the name of data section containing names of instrumented /// functions. inline StringRef getInstrProfNameSectionName(bool AddSegment) { - return AddSegment ? "__DATA,__llvm_prf_names" : "__llvm_prf_names"; + return AddSegment ? "__DATA," INSTR_PROF_NAME_SECT_NAME_STR + : INSTR_PROF_NAME_SECT_NAME_STR; } /// Return the name of the data section containing per-function control /// data. inline StringRef getInstrProfDataSectionName(bool AddSegment) { - return AddSegment ? "__DATA,__llvm_prf_data" : "__llvm_prf_data"; + return AddSegment ? "__DATA," INSTR_PROF_DATA_SECT_NAME_STR + : INSTR_PROF_DATA_SECT_NAME_STR; +} + +/// Return the name profile runtime entry point to do value profiling +/// for a given site. +inline StringRef getInstrProfValueProfFuncName() { + return INSTR_PROF_VALUE_PROF_FUNC_STR; } /// Return the name of the section containing function coverage mapping @@ -168,13 +178,6 @@ return std::error_code(static_cast(E), instrprof_category()); } -enum InstrProfValueKind : uint32_t { - IPVK_IndirectCallTarget = 0, - - IPVK_First = IPVK_IndirectCallTarget, - IPVK_Last = IPVK_IndirectCallTarget -}; - struct InstrProfStringTable { // Set of string values in profiling data. StringSet<> StringValueSet; @@ -551,7 +554,7 @@ namespace RawInstrProf { -const uint64_t Version = 2; +const uint64_t Version = INSTR_PROF_RAW_VERSION; // Magic number to detect file format and endianness. // Use 255 at one end, since no UTF-8 file can use that character. Avoid 0, @@ -562,20 +565,13 @@ // The magic and version need to be kept in sync with // projects/compiler-rt/lib/profile/InstrProfiling.c -template -inline uint64_t getMagic(); -template <> -inline uint64_t getMagic() { - return uint64_t(255) << 56 | uint64_t('l') << 48 | uint64_t('p') << 40 | - uint64_t('r') << 32 | uint64_t('o') << 24 | uint64_t('f') << 16 | - uint64_t('r') << 8 | uint64_t(129); +template inline uint64_t getMagic(); +template <> inline uint64_t getMagic() { + return INSTR_PROF_RAW_MAGIC_64; } -template <> -inline uint64_t getMagic() { - return uint64_t(255) << 56 | uint64_t('l') << 48 | uint64_t('p') << 40 | - uint64_t('r') << 32 | uint64_t('o') << 24 | uint64_t('f') << 16 | - uint64_t('R') << 8 | uint64_t(129); +template <> inline uint64_t getMagic() { + return INSTR_PROF_RAW_MAGIC_32; } // Per-function profile data header/control structure. @@ -593,19 +589,11 @@ // compiler-rt/lib/profile/InstrProfilingFile.c and // InstrProfilingBuffer.c. struct Header { - const uint64_t Magic; - const uint64_t Version; - const uint64_t DataSize; - const uint64_t CountersSize; - const uint64_t NamesSize; - const uint64_t CountersDelta; - const uint64_t NamesDelta; - const uint64_t ValueKindLast; - const uint64_t ValueDataSize; - const uint64_t ValueDataDelta; +#define INSTR_PROF_RAW_HEADER(Type, Name, Init) Type Name; +#include "llvm/ProfileData/InstrProfData.inc" }; -} // end namespace RawInstrProf +} // end namespace RawInstrProf namespace coverage { Index: include/llvm/ProfileData/InstrProfData.inc =================================================================== --- include/llvm/ProfileData/InstrProfData.inc +++ include/llvm/ProfileData/InstrProfData.inc @@ -1,48 +1,70 @@ -//===-- InstrProfData.inc - instr profiling runtime structures-----===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines templates for core runtime data structures used by -// instrumentation based profiling and coverage mapping. The instrumentation -// runtime library, the compiler IR lowering, and profile reader/writer need -// to use the same template to make sure the same data structure is defined -// consistently. -// -// Examples of how the template is used: -// 1. To declare a structure: -// -// struct ProfData { -// #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \ -// Type Name; -// #include "llvm/ProfileData/InstrProfData.inc" -// }; -// -// 2. To construct LLVM type arrays for the struct type: -// -// Type *DataTypes[] = { -// #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, LLVMType, Name, Initializer) \ -// Initializer, -// Constant *ConstantVals[] = { -// #include "llvm/ProfileData/InstrProfData.inc" -// }; -//===----------------------------------------------------------------------===// +/*===-- InstrProfData.inc - instr profiling runtime structures -----------=== *\ +|* +|* The LLVM Compiler Infrastructure +|* +|* This file is distributed under the University of Illinois Open Source +|* License. See LICENSE.TXT for details. +|* +\*===----------------------------------------------------------------------===*/ +/* + * This is the master file that defines all the data structure, signature, + * constant literals that are shared across profiling runtime library, + * compiler (instrumentation), and host tools (reader/writer). The entities + * defined in this file affect the profile runtime ABI, the raw profile format, + * or both. + * + * The file has two identical copies. The master copy lives in LLVM and + * the other one sits in compiler-rt/lib/profile directory. Changes can only + * be made directly made in the master copy. Whenever the master copy changes, + * the compiler-rt copy needs to be kept in sync with the master. + * + * The first part of the file includes macros that defines types, names, and + * initializers for the member fields of the core data structures. The field + * declarations for one structure is enabled by defining the field activation + * macro associated with that structure. Only one field activation record + * can be defined at one time and the rest definitions will be filtered out by + * the preprocessor. + * + * Examples of how the template is used to instantiate structure definition: + * 1. To declare a structure: + * + * struct ProfData { + * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \ + * Type Name; + * #include "llvm/ProfileData/InstrProfData.inc" + * }; + * + * 2. To construct LLVM type arrays for the struct type: + * + * Type *DataTypes[] = { + * #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, LLVMType, Name, Initializer) \ + * Initializer, + * Constant *ConstantVals[] = { + * #include "llvm/ProfileData/InstrProfData.inc" + * }; + * + * + * The second part of the file includes definitions all other entities that + * are related to runtime ABI and format. When no field activation macro is + * defined, this file can be included to introduce the definitions. + * +\*===----------------------------------------------------------------------===*/ + +/* INSTR_PROF_DATA start */ +/* Definition of member fields of the per-function control structure. */ #ifndef INSTR_PROF_DATA #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) +#else +#define INSTR_PROF_DATA_DEFINED #endif -// INSTR_PROF_DATA_START INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \ ConstantInt::get(llvm::Type::getInt32Ty(Ctx), \ NamePtr->getType()->getPointerElementType()->getArrayNumElements())) @@ -58,22 +80,63 @@ llvm::Type::getInt64PtrTy(Ctx))) INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), FunctionPointer, \ FunctionAddr) -INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), Values, \ +INSTR_PROF_DATA(IntPtrT, llvm::Type::getInt8PtrTy(Ctx), Values, \ ConstantPointerNull::get(Int8PtrTy)) INSTR_PROF_DATA(const uint16_t, Int16ArrayTy, NumValueSites[IPVK_Last+1], \ ConstantArray::get(Int16ArrayTy, Int16ArrayVals)) -// INSTR_PROF_DATA_END - -#ifdef INSTR_PROF_DATA #undef INSTR_PROF_DATA +/* INSTR_PROF_DATA end */ + + +/* INSTR_PROF_RAW_HEADER start */ +/* Definition of member fields of the raw profile header data structure. */ +#ifndef INSTR_PROF_RAW_HEADER +#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer) +#else +#define INSTR_PROF_DATA_DEFINED #endif +INSTR_PROF_RAW_HEADER(const uint64_t, Magic, __llvm_profile_get_magic()) +INSTR_PROF_RAW_HEADER(const uint64_t, Version, __llvm_profile_get_version()) +INSTR_PROF_RAW_HEADER(const uint64_t, DataSize, DataSize) +INSTR_PROF_RAW_HEADER(const uint64_t, CountersSize, CountersSize) +INSTR_PROF_RAW_HEADER(const uint64_t, NamesSize, NameSize) +INSTR_PROF_RAW_HEADER(const uint64_t, CountersDelta, (uintptr_t)CountersBegin) +INSTR_PROF_RAW_HEADER(const uint64_t, NamesDelta, (uintptr_t)NamesBegin) +INSTR_PROF_RAW_HEADER(const uint64_t, ValueKindLast, IPVK_Last) +INSTR_PROF_RAW_HEADER(const uint64_t, ValueDataSize, ValueDataSize) +INSTR_PROF_RAW_HEADER(const uint64_t, ValueDataDelta, (uintptr_t)ValueDataBegin) +#undef INSTR_PROF_RAW_HEADER +/* INSTR_PROF_RAW_HEADER end */ + +/* VALUE_PROF_FUNC_PARAM start */ +/* Definition of parameter types of the runtime API used to do value profiling + * for a given value site. + */ +#ifndef VALUE_PROF_FUNC_PARAM +#define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType) +#define INSTR_PROF_COMMA +#else +#define INSTR_PROF_DATA_DEFINED +#define INSTR_PROF_COMMA , +#endif +VALUE_PROF_FUNC_PARAM(uint64_t, TargetValue, Type::getInt64Ty(Ctx)) INSTR_PROF_COMMA +VALUE_PROF_FUNC_PARAM(void *, Data, Type::getInt8PtrTy(Ctx)) INSTR_PROF_COMMA +VALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx)) +#undef VALUE_PROF_FUNC_PARAM +#undef INSTR_PROF_COMMA +/* VALUE_PROF_FUNC_PARAM end */ +/* COVMAP_FUNC_RECORD start */ +/* Definition of member fields of the function record structure in coverage + * map. + */ #ifndef COVMAP_FUNC_RECORD #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Initializer) +#else +#define INSTR_PROF_DATA_DEFINED #endif -// COVMAP_FUNC_RECORD_START COVMAP_FUNC_RECORD(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), \ NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, \ llvm::Type::getInt8PtrTy(Ctx))) @@ -85,10 +148,83 @@ CoverageMapping.size())) 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 #undef COVMAP_FUNC_RECORD +/* COVMAP_FUNC_RECORD end */ + + +/*============================================================================*/ + + +#ifndef INSTR_PROF_DATA_DEFINED + +#ifndef INSTR_PROF_DATA_INC_ +#define INSTR_PROF_DATA_INC_ + +/* Helper macros. */ +#define INSTR_PROF_SIMPLE_QUOTE(x) #x +#define INSTR_PROF_QUOTE(x) INSTR_PROF_SIMPLE_QUOTE(x) +#define INSTR_PROF_SIMPLE_CONCAT(x,y) x ## y +#define INSTR_PROF_CONCAT(x,y) INSTR_PROF_SIMPLE_CONCAT(x,y) + +/* Magic number to detect file format and endianness. + * Use 255 at one end, since no UTF-8 file can use that character. Avoid 0, + * so that utilities, like strings, don't grab it as a string. 129 is also + * invalid UTF-8, and high enough to be interesting. + * Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR" + * for 32-bit platforms. + * The magic and version need to be kept in sync with + * projects/compiler-rt/lib/profile/InstrProfiling.c + */ +#define INSTR_PROF_RAW_MAGIC_64 uint64_t(255) << 56 | uint64_t('l') << 48 | \ + uint64_t('p') << 40 | uint64_t('r') << 32 | uint64_t('o') << 24 | \ + uint64_t('f') << 16 | uint64_t('r') << 8 | uint64_t(129) +#define INSTR_PROF_RAW_MAGIC_32 uint64_t(255) << 56 | uint64_t('l') << 48 | \ + uint64_t('p') << 40 | uint64_t('r') << 32 | uint64_t('o') << 24 | \ + uint64_t('f') << 16 | uint64_t('R') << 8 | uint64_t(129) + +/* Raw profile format version. */ +#define INSTR_PROF_RAW_VERSION 2 + +/* Runtime section names and name strings. */ +#define INSTR_PROF_DATA_SECT_NAME __llvm_prf_data +#define INSTR_PROF_NAME_SECT_NAME __llvm_prf_names +#define INSTR_PROF_CNTS_SECT_NAME __llvm_prf_cnts + +#define INSTR_PROF_DATA_SECT_NAME_STR \ + INSTR_PROF_QUOTE(INSTR_PROF_DATA_SECT_NAME) +#define INSTR_PROF_NAME_SECT_NAME_STR \ + INSTR_PROF_QUOTE(INSTR_PROF_NAME_SECT_NAME) +#define INSTR_PROF_CNTS_SECT_NAME_STR \ + INSTR_PROF_QUOTE(INSTR_PROF_CNTS_SECT_NAME) + +/* Macros to define start/stop section symbol for a given + * section on Linux. For instance + * INSTR_PROF_SECT_START(INSTR_PROF_DATA_SECT_NAME) will + * expand to __start___llvm_prof_data + */ +#define INSTR_PROF_SECT_START(Sect) \ + INSTR_PROF_CONCAT(__start_,Sect) +#define INSTR_PROF_SECT_STOP(Sect) \ + INSTR_PROF_CONCAT(__stop_,Sect) + +/* Value Profiling API linkage name. */ +#define INSTR_PROF_VALUE_PROF_FUNC __llvm_profile_instrument_target +#define INSTR_PROF_VALUE_PROF_FUNC_STR \ + INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_FUNC) + +/* Value profiling kinds. */ +#ifdef __cplusplus +enum InstrProfValueKind : uint32_t { +#else +enum InstrProfValueKind { #endif + IPVK_IndirectCallTarget = 0, + IPVK_First = IPVK_IndirectCallTarget, + IPVK_Last = IPVK_IndirectCallTarget +}; +#endif +#else +#undef INSTR_PROF_DATA_DEFINED +#endif Index: lib/Transforms/Instrumentation/InstrProfiling.cpp =================================================================== --- lib/Transforms/Instrumentation/InstrProfiling.cpp +++ lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -173,14 +173,16 @@ } static Constant *getOrInsertValueProfilingCall(Module &M) { - auto *VoidTy = Type::getVoidTy(M.getContext()); - auto *VoidPtrTy = Type::getInt8PtrTy(M.getContext()); - auto *Int32Ty = Type::getInt32Ty(M.getContext()); - auto *Int64Ty = Type::getInt64Ty(M.getContext()); - Type *ArgTypes[] = {Int64Ty, VoidPtrTy, Int32Ty}; + LLVMContext &Ctx = M.getContext(); + auto *ReturnTy = Type::getVoidTy(M.getContext()); + Type *ParamTypes[] = { +#define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType +#include "llvm/ProfileData/InstrProfData.inc" + }; auto *ValueProfilingCallTy = - FunctionType::get(VoidTy, makeArrayRef(ArgTypes), false); - return M.getOrInsertFunction("__llvm_profile_instrument_target", + FunctionType::get(ReturnTy, + makeArrayRef(ParamTypes), false); + return M.getOrInsertFunction(getInstrProfValueProfFuncName(), ValueProfilingCallTy); }