Skip to content

Commit 198443f

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 198443f

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 "CtxInstrContextNode.h"
14+
#include <cstdio>
15+
#include <iostream>
16+
17+
using namespace llvm::ctx_profile;
18+
extern "C" bool __llvm_ctx_profile_fetch(void *Data,
19+
bool (*Writer)(void *,
20+
const ContextNode &));
21+
22+
extern "C" {
23+
__attribute__((noinline)) void someFunction() { printf("check 2\n"); }
24+
25+
// block inlining because the pre-inliner otherwise will inline this - it's
26+
// too small.
27+
__attribute__((noinline)) void the_root() {
28+
printf("check 1\n");
29+
someFunction();
30+
someFunction();
31+
}
32+
}
33+
34+
// Make sure the program actually ran correctly.
35+
// CHECK: check 1
36+
// CHECK: check 2
37+
// CHECK: check 2
38+
39+
void printProfile(const ContextNode &Node, const std::string &Indent,
40+
const std::string &Increment) {
41+
std::cout << Indent << "Guid: " << Node.guid() << std::endl;
42+
std::cout << Indent << "Entries: " << Node.entrycount() << std::endl;
43+
for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
44+
for (const auto *N = Node.subContexts()[I]; N; N = N->next()) {
45+
std::cout << Indent << "At Index " << I << ":" << std::endl;
46+
printProfile(*N, Indent + Increment, Increment);
47+
}
48+
}
49+
50+
// CHECK: Guid: 11065787667334760794
51+
// CHECK: Entries: 1
52+
// CHECK: At Index 1:
53+
// CHECK: Guid: 6759619411192316602
54+
// CHECK: Entries: 1
55+
// CHECK: At Index 2:
56+
// CHECK: Guid: 6759619411192316602
57+
// CHECK: Entries: 1
58+
59+
bool profileWriter() {
60+
return __llvm_ctx_profile_fetch(
61+
nullptr, +[](void *, const ContextNode &Node) {
62+
printProfile(Node, "", " ");
63+
return true;
64+
});
65+
}
66+
67+
int main(int argc, char **argv) {
68+
the_root();
69+
// This would be implemented in a specific RPC handler, but here we just call
70+
// it directly.
71+
return !profileWriter();
72+
}

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)