[llvm-branch-commits] [llvm] [ctx_prof] Add support for ICP (PR #105469)

Snehasish Kumar via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Aug 26 22:57:35 PDT 2024


================
@@ -456,3 +463,170 @@ declare void @_ZN5Base35func3Ev(ptr)
   // 1 call instruction from the entry block.
   EXPECT_EQ(F->front().size(), OrigEntryBBSize + 4);
 }
+
+using namespace llvm::ctx_profile;
+
+class ContextManager final {
+  std::vector<std::unique_ptr<char[]>> Nodes;
+  std::map<GUID, const ContextNode *> Roots;
+
+public:
+  ContextNode *createNode(GUID Guid, uint32_t NrCounters, uint32_t NrCallsites,
+                          ContextNode *Next = nullptr) {
+    auto AllocSize = ContextNode::getAllocSize(NrCounters, NrCallsites);
+    auto *Mem = Nodes.emplace_back(std::make_unique<char[]>(AllocSize)).get();
+    std::memset(Mem, 0, AllocSize);
+    auto *Ret = new (Mem) ContextNode(Guid, NrCounters, NrCallsites, Next);
+    return Ret;
+  }
+};
+
+TEST(CallPromotionUtilsTest, PromoteWithIcmpAndCtxProf) {
+  LLVMContext C;
+  std::unique_ptr<Module> M = parseIR(C,
+                                      R"IR(
+define i32 @testfunc1(ptr %d) !guid !0 {
+  call void @llvm.instrprof.increment(ptr @testfunc1, i64 0, i32 1, i32 0)
+  call void @llvm.instrprof.callsite(ptr @testfunc1, i64 0, i32 1, i32 0, ptr %d)
+  %call = call i32 %d()
+  ret i32 %call
+}
+
+define i32 @f1() !guid !1 {
+  call void @llvm.instrprof.increment(ptr @f1, i64 0, i32 1, i32 0)
+  ret i32 2
+}
+
+define i32 @f2() !guid !2 {
+  call void @llvm.instrprof.increment(ptr @f2, i64 0, i32 1, i32 0)
+  call void @llvm.instrprof.callsite(ptr @f2, i64 0, i32 1, i32 0, ptr @f4)
+  %r = call i32 @f4()
+  ret i32 %r
+}
+
+define i32 @testfunc2(ptr %p) !guid !4 {
+  call void @llvm.instrprof.increment(ptr @testfunc2, i64 0, i32 1, i32 0)
+  call void @llvm.instrprof.callsite(ptr @testfunc2, i64 0, i32 1, i32 0, ptr @testfunc1)
+  %r = call i32 @testfunc1(ptr %p)
+  ret i32 %r
+}
+
+declare i32 @f3()
+
+define i32 @f4() !guid !3 {
+  ret i32 3
+}
+
+!0 = !{i64 1000}
+!1 = !{i64 1001}
+!2 = !{i64 1002}
+!3 = !{i64 1004}
+!4 = !{i64 1005}
+)IR");
+
+  const char *Profile = R"(
+    [
+    {
+      "Guid": 1000,
+      "Counters": [1],
+      "Callsites": [
+        [{ "Guid": 1001,
+            "Counters": [10]}, 
+          { "Guid": 1002,
+            "Counters": [11],
+            "Callsites": [[{"Guid": 1004, "Counters":[13]}]]
+          },
+          { "Guid": 1003,
+            "Counters": [12]
+          }]]
+    },
+    {
+      "Guid": 1005,
+      "Counters": [2],
+      "Callsites": [
+        [{ "Guid": 1000,
+            "Counters": [1],
+            "Callsites": [
+              [{ "Guid": 1001,
+                  "Counters": [101]}, 
+                { "Guid": 1002,
+                  "Counters": [102],
+                  "Callsites": [[{"Guid": 1004, "Counters":[104]}]]
+                },
+                { "Guid": 1003,
+                  "Counters": [103]
+                }]]}]]}]
+    )";
+
+  llvm::unittest::TempFile ProfileFile("ctx_profile", "", "", /*Unique*/ true);
----------------
snehasish wrote:

`/*Unique=*/` for clang-tidy to pick it up.

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


More information about the llvm-branch-commits mailing list