Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ class ContextNode final {
uint64_t entrycount() const { return counters()[0]; }
};

/// The internal structure of FunctionData. This makes sure that changes to
/// the fields of FunctionData either get automatically captured on the llvm
/// side, or force a manual corresponding update.
///
/// The macro arguments (see CtxInstrProfiling.h for example):
///
/// PTRDECL is a macro taking 2 parameters: a type and the name of the field.
/// The field is a pointer of that type;
///
/// VOLATILE_PTRDECL is the same as above, but for volatile pointers;
///
/// MUTEXDECL takes one parameter, the name of a field that is a mutex.
#define CTXPROF_FUNCTION_DATA(PTRDECL, VOLATILE_PTRDECL, MUTEXDECL) \
PTRDECL(FunctionData, Next) \
VOLATILE_PTRDECL(ContextRoot, CtxRoot) \
VOLATILE_PTRDECL(ContextNode, FlatCtx) \
MUTEXDECL(Mutex)

Comment on lines +128 to +133

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This macro should go in InstrProfData.inc in compiler-rt and llvm. I believe we have some tests to make sure they are kept in sync.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would create a dependency between ctxprof and instrprof, which is intentionally not there.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then perhaps create a new .inc file for ctxprof which is kept in sync with a similar test?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out that this is exactly what is mentioned in the description.

/// Abstraction for the parameter passed to `__llvm_ctx_profile_fetch`.
/// `startContextSection` is called before any context roots are sent for
/// writing. Then one or more `writeContextual` calls are made; finally,
Expand Down
16 changes: 9 additions & 7 deletions compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,19 @@ struct ContextRoot {
// The current design trades off a bit of overhead at the first time a function
// is encountered *for flat profiling* for avoiding size penalties.
struct FunctionData {
#define _PTRDECL(T, N) T *N = nullptr;
#define _VOLATILE_PTRDECL(T, N) T *volatile N = nullptr;
#define _MUTEXDECL(N) ::__sanitizer::SpinMutex N;
CTXPROF_FUNCTION_DATA(_PTRDECL, _VOLATILE_PTRDECL, _MUTEXDECL)
#undef _PTRDECL
#undef _VOLATILE_PTRDECL
#undef _MUTEXDECL

// Constructor for test only - since this is expected to be
// initialized by the compiler.
FunctionData() { Mutex.Init(); }

FunctionData *Next = nullptr;
ContextRoot *volatile CtxRoot = nullptr;
ContextNode *volatile FlatCtx = nullptr;

FunctionData() = default;
ContextRoot *getOrAllocateContextRoot();

::__sanitizer::StaticSpinMutex Mutex;
// If (unlikely) StaticSpinMutex internals change, we need to modify the LLVM
// instrumentation lowering side because it is responsible for allocating and
// zero-initializing ContextRoots.
Expand Down
18 changes: 18 additions & 0 deletions llvm/include/llvm/ProfileData/CtxInstrContextNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ class ContextNode final {
uint64_t entrycount() const { return counters()[0]; }
};

/// The internal structure of FunctionData. This makes sure that changes to
/// the fields of FunctionData either get automatically captured on the llvm
/// side, or force a manual corresponding update.
///
/// The macro arguments (see CtxInstrProfiling.h for example):
///
/// PTRDECL is a macro taking 2 parameters: a type and the name of the field.
/// The field is a pointer of that type;
///
/// VOLATILE_PTRDECL is the same as above, but for volatile pointers;
///
/// MUTEXDECL takes one parameter, the name of a field that is a mutex.
#define CTXPROF_FUNCTION_DATA(PTRDECL, VOLATILE_PTRDECL, MUTEXDECL) \
PTRDECL(FunctionData, Next) \
VOLATILE_PTRDECL(ContextRoot, CtxRoot) \
VOLATILE_PTRDECL(ContextNode, FlatCtx) \
MUTEXDECL(Mutex)

/// Abstraction for the parameter passed to `__llvm_ctx_profile_fetch`.
/// `startContextSection` is called before any context roots are sent for
/// writing. Then one or more `writeContextual` calls are made; finally,
Expand Down
18 changes: 11 additions & 7 deletions llvm/lib/Transforms/Instrumentation/PGOCtxProfLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/ProfileData/CtxInstrContextNode.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/CommandLine.h"
#include <utility>
Expand Down Expand Up @@ -113,13 +114,16 @@ CtxInstrumentationLowerer::CtxInstrumentationLowerer(Module &M,
auto *I32Ty = Type::getInt32Ty(M.getContext());
auto *I64Ty = Type::getInt64Ty(M.getContext());

FunctionDataTy =
StructType::get(M.getContext(), {
PointerTy, /*Next*/
PointerTy, /*CtxRoot*/
PointerTy, /*FlatCtx*/
SanitizerMutexType, /*Mutex*/
});
#define _PTRDECL(_, __) PointerTy,
#define _VOLATILE_PTRDECL(_, __) PointerTy,
#define _MUTEXDECL(_) SanitizerMutexType,

FunctionDataTy = StructType::get(
M.getContext(),
{CTXPROF_FUNCTION_DATA(_PTRDECL, _VOLATILE_PTRDECL, _MUTEXDECL)});
#undef _PTRDECL
#undef _VOLATILE_PTRDECL
#undef _MUTEXDECL

// The Context header.
ContextNodeTy = StructType::get(M.getContext(), {
Expand Down