Skip to content

Commit bfbf3a6

Browse files
committed
[ctx_profile] Integration test
Compile with clang a program that's instrumented for contextual profiling and verify a profile can be collected.
1 parent d311a62 commit bfbf3a6

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

compiler-rt/lib/ctx_profile/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ EXTRA_FLAGS)
1818
if(COMPILER_RT_INCLUDE_TESTS)
1919
add_subdirectory(tests)
2020
endif()
21+
22+
add_compiler_rt_runtime(clang_rt.ctx_profile
23+
STATIC
24+
ARCHS ${CTX_PROFILE_SUPPORTED_ARCH}
25+
OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc
26+
CFLAGS ${EXTRA_FLAGS}
27+
SOURCES ${CTX_PROFILE_SOURCES}
28+
ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS}
29+
PARENT_TARGET ctx_profile)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Simple integration test for contextual instrumentation
2+
//
3+
// Copy the header defining ContextNode.
4+
// RUN: mkdir -p %t_include
5+
// RUN: cp %llvm_src/include/llvm/ProfileData/CtxInstrContextNode.h %t_include/
6+
//
7+
// Compile with ctx instrumentation "on". We treat "the_root" as callgraph root.
8+
// RUN: %clangxx %s -lclang_rt.ctx_profile -I%t_include -O2 -o %t.bin -mllvm -profile-context-root=the_root
9+
//
10+
// Run the binary, and observe the profile fetch handler's output.
11+
// RUN: %t.bin | FileCheck %s
12+
13+
#include <cstdio>
14+
#include <iostream>
15+
#include "CtxInstrContextNode.h"
16+
17+
using namespace llvm::ctx_profile;
18+
extern "C" bool __llvm_ctx_profile_fetch(void *Data,
19+
bool (*Writer)(void *, const ContextNode &));
20+
21+
extern "C" {
22+
__attribute__((noinline)) void someFunction() { printf("check 2\n"); }
23+
24+
// block inlining because the pre-inliner otherwise will inline this - it's
25+
// too small.
26+
__attribute__((noinline)) void the_root() {
27+
printf("check 1\n");
28+
someFunction();
29+
someFunction();
30+
}
31+
}
32+
33+
// Make sure the program actually ran correctly.
34+
// CHECK: check 1
35+
// CHECK: check 2
36+
// CHECK: check 2
37+
38+
void printProfile(const ContextNode &Node, const std::string &Indent,
39+
const std::string &Increment) {
40+
std::cout << Indent << "Guid: " << Node.guid() << std::endl;
41+
std::cout << Indent << "Entries: " << Node.entrycount() << std::endl;
42+
for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
43+
for (const auto *N = Node.subContexts()[I]; N; N = N->next()) {
44+
std::cout << Indent << "At Index " << I << ":" << std::endl;
45+
printProfile(*N, Indent + Increment, Increment);
46+
}
47+
}
48+
49+
// CHECK: Guid: 11065787667334760794
50+
// CHECK: Entries: 1
51+
// CHECK: At Index 1:
52+
// CHECK: Guid: 6759619411192316602
53+
// CHECK: Entries: 1
54+
// CHECK: At Index 2:
55+
// CHECK: Guid: 6759619411192316602
56+
// CHECK: Entries: 1
57+
58+
bool profileWriter() {
59+
return __llvm_ctx_profile_fetch(
60+
nullptr, +[](void *, const ContextNode &Node) {
61+
printProfile(Node, "", " ");
62+
return true;
63+
});
64+
}
65+
66+
int main(int argc, char **argv) {
67+
the_root();
68+
// This would be implemented in a specific RPC handler, but here we just call
69+
// it directly.
70+
return !profileWriter();
71+
}

compiler-rt/test/ctx_profile/lit.cfg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ def get_required_attr(config, attr_name):
2929
config.test_source_root = os.path.dirname(__file__)
3030
# Default test suffixes.
3131
config.suffixes = [".c", ".cpp", ".test"]
32+
33+
config.substitutions.append(
34+
("%clangxx ", " ".join([config.clang] + config.cxx_mode_flags) + " ")
35+
)

0 commit comments

Comments
 (0)