Skip to content

Commit a2e0368

Browse files
committedMar 12, 2014
Profile: Add a library for the instrumentation based profiling format
This provides a library to work with the instrumentation based profiling format that is used by clang's -fprofile-instr-* options and by the llvm-profdata tool. This is a binary format, rather than the textual one that's currently in use. The tests are in the subsequent commits that use this. llvm-svn: 203703
1 parent da6b4f0 commit a2e0368

12 files changed

+553
-2
lines changed
 
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//=-- ProfileData.h - Instrumented profiling format support -------*- C++ -*-=//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file contains support for instrumentation based PGO and coverage.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_PROFILE_PROFILEDATA_H__
15+
#define LLVM_PROFILE_PROFILEDATA_H__
16+
17+
#include "llvm/Support/DataTypes.h"
18+
#include "llvm/Support/system_error.h"
19+
20+
#include <vector>
21+
22+
namespace llvm {
23+
24+
const char PROFILEDATA_MAGIC[4] = {'L', 'P', 'R', 'F'};
25+
const uint32_t PROFILEDATA_VERSION = 1;
26+
27+
const error_category &profiledata_category();
28+
29+
struct profiledata_error {
30+
enum ErrorType {
31+
success = 0,
32+
bad_magic,
33+
unsupported_version,
34+
too_large,
35+
truncated,
36+
malformed,
37+
unknown_function
38+
};
39+
ErrorType V;
40+
41+
profiledata_error(ErrorType V) : V(V) {}
42+
operator ErrorType() const { return V; }
43+
};
44+
45+
inline error_code make_error_code(profiledata_error E) {
46+
return error_code(static_cast<int>(E), profiledata_category());
47+
}
48+
49+
template <> struct is_error_code_enum<profiledata_error> : std::true_type {};
50+
template <> struct is_error_code_enum<profiledata_error::ErrorType>
51+
: std::true_type {};
52+
53+
} // end namespace llvm
54+
55+
#endif // LLVM_PROFILE_PROFILEDATA_H__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//=-- ProfileDataReader.h - Instrumented profiling reader ---------*- C++ -*-=//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file contains support for reading profiling data for instrumentation
11+
// based PGO and coverage.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_PROFILE_PROFILEDATA_READER_H__
16+
#define LLVM_PROFILE_PROFILEDATA_READER_H__
17+
18+
#include "llvm/ADT/StringMap.h"
19+
#include "llvm/Support/Compiler.h"
20+
#include "llvm/Support/DataTypes.h"
21+
#include "llvm/Support/ErrorOr.h"
22+
#include "llvm/Support/MemoryBuffer.h"
23+
#include "llvm/Support/raw_ostream.h"
24+
25+
#include <vector>
26+
27+
namespace llvm {
28+
29+
class ProfileDataCursor;
30+
31+
/// Reader for the profile data that is used for instrumentation based PGO.
32+
class ProfileDataReader {
33+
private:
34+
/// The profile data file contents.
35+
std::unique_ptr<MemoryBuffer> DataBuffer;
36+
/// Offsets into DataBuffer for each function's counters.
37+
StringMap<uint32_t> DataOffsets;
38+
/// The maximal execution count among all functions.
39+
uint64_t MaxFunctionCount;
40+
41+
ProfileDataReader(const ProfileDataReader &) LLVM_DELETED_FUNCTION;
42+
ProfileDataReader &operator=(const ProfileDataReader &) LLVM_DELETED_FUNCTION;
43+
protected:
44+
ProfileDataReader(std::unique_ptr<MemoryBuffer> &DataBuffer)
45+
: DataBuffer(DataBuffer.release()) {}
46+
47+
/// Populate internal state using the profile data's index
48+
error_code readIndex();
49+
public:
50+
51+
class name_iterator {
52+
typedef StringMap<unsigned>::const_iterator IterTy;
53+
IterTy Ix;
54+
public:
55+
explicit name_iterator(const IterTy &Ix) : Ix(Ix) {}
56+
57+
StringRef operator*() const { return Ix->getKey(); }
58+
59+
bool operator==(const name_iterator &RHS) const { return Ix == RHS.Ix; }
60+
bool operator!=(const name_iterator &RHS) const { return Ix != RHS.Ix; }
61+
62+
inline name_iterator& operator++() { ++Ix; return *this; }
63+
};
64+
65+
/// Iterators over the names of indexed items
66+
name_iterator begin() const {
67+
return name_iterator(DataOffsets.begin());
68+
}
69+
name_iterator end() const {
70+
return name_iterator(DataOffsets.end());
71+
}
72+
73+
private:
74+
error_code findFunctionCounts(StringRef FuncName, uint64_t &FunctionHash,
75+
ProfileDataCursor &Cursor);
76+
public:
77+
/// The number of profiled functions
78+
size_t numProfiledFunctions() { return DataOffsets.size(); }
79+
/// Fill Counts with the profile data for the given function name.
80+
error_code getFunctionCounts(StringRef FuncName, uint64_t &FunctionHash,
81+
std::vector<uint64_t> &Counts);
82+
/// Get the frequency with which a function is called relative to the function
83+
/// that is called most often in the program.
84+
error_code getCallFrequency(StringRef FuncName, uint64_t &FunctionHash,
85+
double &F);
86+
87+
static error_code create(std::string Path,
88+
std::unique_ptr<ProfileDataReader> &Result);
89+
};
90+
91+
} // end namespace llvm
92+
93+
#endif // LLVM_PROFILE_PROFILEDATA_READER_H__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//=-- ProfileDataWriter.h - Instrumented profiling writer ---------*- C++ -*-=//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file contains support for writing profiling data for instrumentation
11+
// based PGO and coverage.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_PROFILE_PROFILEDATA_WRITER_H__
16+
#define LLVM_PROFILE_PROFILEDATA_WRITER_H__
17+
18+
#include "llvm/ADT/StringMap.h"
19+
#include "llvm/Support/DataTypes.h"
20+
#include "llvm/Support/raw_ostream.h"
21+
22+
#include <vector>
23+
24+
namespace llvm {
25+
26+
struct __attribute__((packed)) ProfileDataHeader {
27+
char Magic[4];
28+
uint32_t Version;
29+
uint32_t DataStart;
30+
uint32_t Padding;
31+
uint64_t MaxFunctionCount;
32+
};
33+
34+
/// Writer for instrumentation based profile data
35+
class ProfileDataWriter {
36+
StringMap<size_t> FunctionOffsets;
37+
std::vector<uint64_t> FunctionData;
38+
uint32_t DataStart;
39+
uint64_t MaxFunctionCount;
40+
41+
void write32(raw_ostream &OS, uint32_t Value);
42+
void write64(raw_ostream &OS, uint64_t Value);
43+
public:
44+
ProfileDataWriter()
45+
: DataStart(sizeof(ProfileDataHeader)), MaxFunctionCount(0) {}
46+
47+
void addFunctionCounts(StringRef FuncName, uint64_t FunctionHash,
48+
uint64_t NumCounters, const uint64_t *Counters);
49+
void write(raw_ostream &OS);
50+
};
51+
52+
} // end namespace llvm
53+
54+
#endif // LLVM_PROFILE_PROFILEDATA_WRITER_H__

‎llvm/lib/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ add_subdirectory(ExecutionEngine)
1616
add_subdirectory(Target)
1717
add_subdirectory(AsmParser)
1818
add_subdirectory(LineEditor)
19+
add_subdirectory(Profile)

‎llvm/lib/LLVMBuild.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
;===------------------------------------------------------------------------===;
1717

1818
[common]
19-
subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine LineEditor Linker IR IRReader LTO MC Object Option Support TableGen Target Transforms
19+
subdirectories = Analysis AsmParser Bitcode CodeGen DebugInfo ExecutionEngine LineEditor Linker IR IRReader LTO MC Object Option Profile Support TableGen Target Transforms
2020

2121
[component_0]
2222
type = Group

‎llvm/lib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ include $(LEVEL)/Makefile.config
1212

1313
PARALLEL_DIRS := IR AsmParser Bitcode Analysis Transforms CodeGen Target \
1414
ExecutionEngine Linker LTO MC Object Option DebugInfo \
15-
IRReader LineEditor
15+
IRReader LineEditor Profile
1616

1717
include $(LEVEL)/Makefile.common

‎llvm/lib/Profile/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_llvm_library(LLVMProfile
2+
ProfileData.cpp
3+
ProfileDataReader.cpp
4+
ProfileDataWriter.cpp
5+
)

‎llvm/lib/Profile/LLVMBuild.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
;===- ./lib/Profile/LLVMBuild.txt ------------------------------*- Conf -*--===;
2+
;
3+
; The LLVM Compiler Infrastructure
4+
;
5+
; This file is distributed under the University of Illinois Open Source
6+
; License. See LICENSE.TXT for details.
7+
;
8+
;===------------------------------------------------------------------------===;
9+
;
10+
; This is an LLVMBuild description file for the components in this subdirectory.
11+
;
12+
; For more information on the LLVMBuild system, please see:
13+
;
14+
; http://llvm.org/docs/LLVMBuild.html
15+
;
16+
;===------------------------------------------------------------------------===;
17+
18+
[component_0]
19+
type = Library
20+
name = Profile
21+
parent = Libraries

‎llvm/lib/Profile/Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
##===- lib/Profile/Makefile --------------------------------*- Makefile -*-===##
2+
#
3+
# The LLVM Compiler Infrastructure
4+
#
5+
# This file is distributed under the University of Illinois Open Source
6+
# License. See LICENSE.TXT for details.
7+
#
8+
##===----------------------------------------------------------------------===##
9+
10+
LEVEL = ../..
11+
LIBRARYNAME = LLVMProfile
12+
BUILD_ARCHIVE := 1
13+
14+
include $(LEVEL)/Makefile.common

‎llvm/lib/Profile/ProfileData.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//=-- ProfileData.cpp - Instrumented profiling format support ---------------=//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file contains support for clang's instrumentation based PGO and
11+
// coverage.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "llvm/Profile/ProfileData.h"
16+
#include "llvm/Support/ErrorHandling.h"
17+
18+
using namespace llvm;
19+
20+
namespace {
21+
class ProfileDataErrorCategoryType : public _do_message {
22+
const char *name() const override { return "llvm.profiledata"; }
23+
std::string message(int IE) const {
24+
profiledata_error::ErrorType E =
25+
static_cast<profiledata_error::ErrorType>(IE);
26+
switch (E) {
27+
case profiledata_error::success: return "Success";
28+
case profiledata_error::bad_magic:
29+
return "Invalid file format (bad magic)";
30+
case profiledata_error::unsupported_version:
31+
return "Unsupported format version";
32+
case profiledata_error::too_large:
33+
return "Too much profile data";
34+
case profiledata_error::truncated:
35+
return "Truncated profile data";
36+
case profiledata_error::malformed:
37+
return "Malformed profile data";
38+
case profiledata_error::unknown_function:
39+
return "No profile data available for function";
40+
}
41+
llvm_unreachable("A value of profiledata_error has no message.");
42+
}
43+
error_condition default_error_condition(int EV) const {
44+
if (EV == profiledata_error::success)
45+
return errc::success;
46+
return errc::invalid_argument;
47+
}
48+
};
49+
}
50+
51+
const error_category &llvm::profiledata_category() {
52+
static ProfileDataErrorCategoryType C;
53+
return C;
54+
}

0 commit comments

Comments
 (0)
Please sign in to comment.