[compiler-rt] [ctx_profile] Integration test (PR #92456)
Mircea Trofin via llvm-commits
llvm-commits at lists.llvm.org
Fri May 17 11:08:00 PDT 2024
https://github.com/mtrofin updated https://github.com/llvm/llvm-project/pull/92456
>From 198443f6250f7818bbe71f2f9af31bb022487190 Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Wed, 15 May 2024 15:55:04 -0700
Subject: [PATCH 1/4] [ctx_profile] Integration test
Compile with clang a program that's instrumented for contextual profiling
and verify a profile can be collected.
---
compiler-rt/lib/ctx_profile/CMakeLists.txt | 9 +++
.../TestCases/generate-context.cpp | 72 +++++++++++++++++++
compiler-rt/test/ctx_profile/lit.cfg.py | 4 ++
3 files changed, 85 insertions(+)
create mode 100644 compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
diff --git a/compiler-rt/lib/ctx_profile/CMakeLists.txt b/compiler-rt/lib/ctx_profile/CMakeLists.txt
index 1fa70594b28a3..ab7bf3241fd69 100644
--- a/compiler-rt/lib/ctx_profile/CMakeLists.txt
+++ b/compiler-rt/lib/ctx_profile/CMakeLists.txt
@@ -18,3 +18,12 @@ append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ EXTRA_FLAGS)
if(COMPILER_RT_INCLUDE_TESTS)
add_subdirectory(tests)
endif()
+
+add_compiler_rt_runtime(clang_rt.ctx_profile
+ STATIC
+ ARCHS ${CTX_PROFILE_SUPPORTED_ARCH}
+ OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc
+ CFLAGS ${EXTRA_FLAGS}
+ SOURCES ${CTX_PROFILE_SOURCES}
+ ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS}
+ PARENT_TARGET ctx_profile)
\ No newline at end of file
diff --git a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
new file mode 100644
index 0000000000000..a6796df851997
--- /dev/null
+++ b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
@@ -0,0 +1,72 @@
+// Simple integration test for contextual instrumentation
+//
+// Copy the header defining ContextNode.
+// RUN: mkdir -p %t_include
+// RUN: cp %llvm_src/include/llvm/ProfileData/CtxInstrContextNode.h %t_include/
+//
+// Compile with ctx instrumentation "on". We treat "the_root" as callgraph root.
+// RUN: %clangxx %s -lclang_rt.ctx_profile -I%t_include -O2 -o %t.bin -mllvm -profile-context-root=the_root
+//
+// Run the binary, and observe the profile fetch handler's output.
+// RUN: %t.bin | FileCheck %s
+
+#include "CtxInstrContextNode.h"
+#include <cstdio>
+#include <iostream>
+
+using namespace llvm::ctx_profile;
+extern "C" bool __llvm_ctx_profile_fetch(void *Data,
+ bool (*Writer)(void *,
+ const ContextNode &));
+
+extern "C" {
+__attribute__((noinline)) void someFunction() { printf("check 2\n"); }
+
+// block inlining because the pre-inliner otherwise will inline this - it's
+// too small.
+__attribute__((noinline)) void the_root() {
+ printf("check 1\n");
+ someFunction();
+ someFunction();
+}
+}
+
+// Make sure the program actually ran correctly.
+// CHECK: check 1
+// CHECK: check 2
+// CHECK: check 2
+
+void printProfile(const ContextNode &Node, const std::string &Indent,
+ const std::string &Increment) {
+ std::cout << Indent << "Guid: " << Node.guid() << std::endl;
+ std::cout << Indent << "Entries: " << Node.entrycount() << std::endl;
+ for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
+ for (const auto *N = Node.subContexts()[I]; N; N = N->next()) {
+ std::cout << Indent << "At Index " << I << ":" << std::endl;
+ printProfile(*N, Indent + Increment, Increment);
+ }
+}
+
+// CHECK: Guid: 11065787667334760794
+// CHECK: Entries: 1
+// CHECK: At Index 1:
+// CHECK: Guid: 6759619411192316602
+// CHECK: Entries: 1
+// CHECK: At Index 2:
+// CHECK: Guid: 6759619411192316602
+// CHECK: Entries: 1
+
+bool profileWriter() {
+ return __llvm_ctx_profile_fetch(
+ nullptr, +[](void *, const ContextNode &Node) {
+ printProfile(Node, "", " ");
+ return true;
+ });
+}
+
+int main(int argc, char **argv) {
+ the_root();
+ // This would be implemented in a specific RPC handler, but here we just call
+ // it directly.
+ return !profileWriter();
+}
diff --git a/compiler-rt/test/ctx_profile/lit.cfg.py b/compiler-rt/test/ctx_profile/lit.cfg.py
index a56dabb8ebeb3..520552974f76a 100644
--- a/compiler-rt/test/ctx_profile/lit.cfg.py
+++ b/compiler-rt/test/ctx_profile/lit.cfg.py
@@ -29,3 +29,7 @@ def get_required_attr(config, attr_name):
config.test_source_root = os.path.dirname(__file__)
# Default test suffixes.
config.suffixes = [".c", ".cpp", ".test"]
+
+config.substitutions.append(
+ ("%clangxx ", " ".join([config.clang] + config.cxx_mode_flags) + " ")
+)
>From eff6221b70ff02d84651b466dd160218e89ac60e Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Thu, 16 May 2024 16:15:09 -0700
Subject: [PATCH 2/4] show counters.
---
.../TestCases/generate-context.cpp | 41 ++++++++++++++++---
1 file changed, 35 insertions(+), 6 deletions(-)
diff --git a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
index a6796df851997..0adac5b4c2730 100644
--- a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
+++ b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
@@ -20,26 +20,40 @@ extern "C" bool __llvm_ctx_profile_fetch(void *Data,
const ContextNode &));
extern "C" {
-__attribute__((noinline)) void someFunction() { printf("check 2\n"); }
+__attribute__((noinline)) void someFunction(int I) {
+ if (I % 2)
+ printf("check odd\n");
+ else
+ printf("check even\n");
+}
// block inlining because the pre-inliner otherwise will inline this - it's
// too small.
__attribute__((noinline)) void the_root() {
printf("check 1\n");
- someFunction();
- someFunction();
+ someFunction(1);
+#pragma nounroll
+ for (auto I = 0; I < 2; ++I) {
+ someFunction(I);
+ }
}
}
// Make sure the program actually ran correctly.
// CHECK: check 1
-// CHECK: check 2
-// CHECK: check 2
+// CHECK: check odd
+// CHECK: check even
void printProfile(const ContextNode &Node, const std::string &Indent,
const std::string &Increment) {
std::cout << Indent << "Guid: " << Node.guid() << std::endl;
std::cout << Indent << "Entries: " << Node.entrycount() << std::endl;
+ std::cout << Indent << Node.counters_size() << " counters and "
+ << Node.callsites_size() << " callsites" << std::endl;
+ std::cout << Indent << "Counter values: ";
+ for (uint32_t I = 0U; I < Node.counters_size(); ++I)
+ std::cout << Node.counters()[I] << " ";
+ std::cout << std::endl;
for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
for (const auto *N = Node.subContexts()[I]; N; N = N->next()) {
std::cout << Indent << "At Index " << I << ":" << std::endl;
@@ -47,14 +61,29 @@ void printProfile(const ContextNode &Node, const std::string &Indent,
}
}
+// 11065787667334760794 is the_root. We expect 2 callsites and 2 counters - one
+// for the entry basic block and one for the loop.
+// 6759619411192316602 is someFunction. We expect all context instances to show
+// the same nr of counters and callsites, but the counters will be different.
+// The first context is for the first callsite with the_root as parent, and the
+// second counter in someFunction will be 0 (we pass an odd nr, and the other
+// path gets instrumented).
+// The second context is in the loop. We expect 2 entries and each of the
+// branches would be taken once, so the second counter is 1.
// CHECK: Guid: 11065787667334760794
// CHECK: Entries: 1
+// CHECK: 2 counters and 3 callsites
+// CHECK: Counter values: 1 2
// CHECK: At Index 1:
// CHECK: Guid: 6759619411192316602
// CHECK: Entries: 1
+// CHECK: 2 counters and 2 callsites
+// CHECK: Counter values: 1 0
// CHECK: At Index 2:
// CHECK: Guid: 6759619411192316602
-// CHECK: Entries: 1
+// CHECK: Entries: 2
+// CHECK: 2 counters and 2 callsites
+// CHECK: Counter values: 2 1
bool profileWriter() {
return __llvm_ctx_profile_fetch(
>From 10c647fd173e1a9bf2d8497159fc42113fa09913 Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Thu, 16 May 2024 17:01:03 -0700
Subject: [PATCH 3/4] tightned the test
---
.../TestCases/generate-context.cpp | 33 ++++++++++---------
1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
index 0adac5b4c2730..78326971fc556 100644
--- a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
+++ b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
@@ -41,8 +41,9 @@ __attribute__((noinline)) void the_root() {
// Make sure the program actually ran correctly.
// CHECK: check 1
-// CHECK: check odd
-// CHECK: check even
+// CHECK-NEXT: check odd
+// CHECK-NEXT: check even
+// CHECK-NEXT: check odd
void printProfile(const ContextNode &Node, const std::string &Indent,
const std::string &Increment) {
@@ -70,20 +71,20 @@ void printProfile(const ContextNode &Node, const std::string &Indent,
// path gets instrumented).
// The second context is in the loop. We expect 2 entries and each of the
// branches would be taken once, so the second counter is 1.
-// CHECK: Guid: 11065787667334760794
-// CHECK: Entries: 1
-// CHECK: 2 counters and 3 callsites
-// CHECK: Counter values: 1 2
-// CHECK: At Index 1:
-// CHECK: Guid: 6759619411192316602
-// CHECK: Entries: 1
-// CHECK: 2 counters and 2 callsites
-// CHECK: Counter values: 1 0
-// CHECK: At Index 2:
-// CHECK: Guid: 6759619411192316602
-// CHECK: Entries: 2
-// CHECK: 2 counters and 2 callsites
-// CHECK: Counter values: 2 1
+// CHECK-NEXT: Guid: 11065787667334760794
+// CHECK-NEXT: Entries: 1
+// CHECK-NEXT: 2 counters and 3 callsites
+// CHECK-NEXT: Counter values: 1 2
+// CHECK-NEXT: At Index 1:
+// CHECK-NEXT: Guid: 6759619411192316602
+// CHECK-NEXT: Entries: 1
+// CHECK-NEXT: 2 counters and 2 callsites
+// CHECK-NEXT: Counter values: 1 0
+// CHECK-NEXT: At Index 2:
+// CHECK-NEXT: Guid: 6759619411192316602
+// CHECK-NEXT: Entries: 2
+// CHECK-NEXT: 2 counters and 2 callsites
+// CHECK-NEXT: Counter values: 2 1
bool profileWriter() {
return __llvm_ctx_profile_fetch(
>From af03222e2cc69efb7a88f5bb8273fd0618b41c80 Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Fri, 17 May 2024 10:32:55 -0700
Subject: [PATCH 4/4] s/the_root/theRoot and same for guid and comments
---
.../ctx_profile/TestCases/generate-context.cpp | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
index 78326971fc556..981d6170091c5 100644
--- a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
+++ b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
@@ -4,8 +4,8 @@
// RUN: mkdir -p %t_include
// RUN: cp %llvm_src/include/llvm/ProfileData/CtxInstrContextNode.h %t_include/
//
-// Compile with ctx instrumentation "on". We treat "the_root" as callgraph root.
-// RUN: %clangxx %s -lclang_rt.ctx_profile -I%t_include -O2 -o %t.bin -mllvm -profile-context-root=the_root
+// Compile with ctx instrumentation "on". We treat "theRoot" as callgraph root.
+// RUN: %clangxx %s -lclang_rt.ctx_profile -I%t_include -O2 -o %t.bin -mllvm -profile-context-root=theRoot
//
// Run the binary, and observe the profile fetch handler's output.
// RUN: %t.bin | FileCheck %s
@@ -19,6 +19,7 @@ extern "C" bool __llvm_ctx_profile_fetch(void *Data,
bool (*Writer)(void *,
const ContextNode &));
+// avoid name mangling
extern "C" {
__attribute__((noinline)) void someFunction(int I) {
if (I % 2)
@@ -29,7 +30,7 @@ __attribute__((noinline)) void someFunction(int I) {
// block inlining because the pre-inliner otherwise will inline this - it's
// too small.
-__attribute__((noinline)) void the_root() {
+__attribute__((noinline)) void theRoot() {
printf("check 1\n");
someFunction(1);
#pragma nounroll
@@ -62,16 +63,16 @@ void printProfile(const ContextNode &Node, const std::string &Indent,
}
}
-// 11065787667334760794 is the_root. We expect 2 callsites and 2 counters - one
+// 8657661246551306189 is theRoot. We expect 2 callsites and 2 counters - one
// for the entry basic block and one for the loop.
// 6759619411192316602 is someFunction. We expect all context instances to show
// the same nr of counters and callsites, but the counters will be different.
-// The first context is for the first callsite with the_root as parent, and the
+// The first context is for the first callsite with theRoot as parent, and the
// second counter in someFunction will be 0 (we pass an odd nr, and the other
// path gets instrumented).
// The second context is in the loop. We expect 2 entries and each of the
// branches would be taken once, so the second counter is 1.
-// CHECK-NEXT: Guid: 11065787667334760794
+// CHECK-NEXT: Guid: 8657661246551306189
// CHECK-NEXT: Entries: 1
// CHECK-NEXT: 2 counters and 3 callsites
// CHECK-NEXT: Counter values: 1 2
@@ -95,7 +96,7 @@ bool profileWriter() {
}
int main(int argc, char **argv) {
- the_root();
+ theRoot();
// This would be implemented in a specific RPC handler, but here we just call
// it directly.
return !profileWriter();
More information about the llvm-commits
mailing list