[compiler-rt] [ctx_profile] Integration test (PR #92456)
Mircea Trofin via llvm-commits
llvm-commits at lists.llvm.org
Thu May 16 14:05:54 PDT 2024
https://github.com/mtrofin created https://github.com/llvm/llvm-project/pull/92456
Compile with clang a program that's instrumented for contextual profiling and verify a profile can be collected.
>From bfbf3a647d8c4852cbf303f1ab2b0462fd3df773 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] [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 | 71 +++++++++++++++++++
compiler-rt/test/ctx_profile/lit.cfg.py | 4 ++
3 files changed, 84 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..42497b83c8cc4
--- /dev/null
+++ b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
@@ -0,0 +1,71 @@
+// 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 <cstdio>
+#include <iostream>
+#include "CtxInstrContextNode.h"
+
+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) + " ")
+)
More information about the llvm-commits
mailing list