[compiler-rt] [ctx_profile] Integration test (PR #92456)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Thu May 16 14:33:42 PDT 2024


================
@@ -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
----------------
minglotus-6 wrote:

A dump drive-by question, I wonder if contextual profile contain block counters? If yes maybe check them as well?

https://github.com/llvm/llvm-project/pull/92456


More information about the llvm-commits mailing list