Skip to content

[ctx_prof] Handle case when no root is in this Module. #107463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

mtrofin
Copy link
Member

@mtrofin mtrofin commented Sep 5, 2024

If none of the functions in this Module are roots in the contextual profile, we can't use it and should just return the {} case.

Copy link
Member Author

mtrofin commented Sep 5, 2024

@mtrofin mtrofin marked this pull request as ready for review September 5, 2024 20:40
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Sep 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Mircea Trofin (mtrofin)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/107463.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/CtxProfAnalysis.cpp (+17-4)
  • (added) llvm/test/Analysis/CtxProfAnalysis/load-unapplicable.ll (+60)
diff --git a/llvm/lib/Analysis/CtxProfAnalysis.cpp b/llvm/lib/Analysis/CtxProfAnalysis.cpp
index 457a4dcc796847..560d9c742d5e7d 100644
--- a/llvm/lib/Analysis/CtxProfAnalysis.cpp
+++ b/llvm/lib/Analysis/CtxProfAnalysis.cpp
@@ -132,6 +132,23 @@ PGOContextualProfile CtxProfAnalysis::run(Module &M,
     return {};
   }
 
+  DenseSet<GlobalValue::GUID> ProfileRootsInModule;
+  for (const auto &F : M)
+    if (!F.isDeclaration())
+      if (auto GUID = AssignGUIDPass::getGUID(F);
+          MaybeCtx->find(GUID) != MaybeCtx->end())
+        ProfileRootsInModule.insert(GUID);
+
+  // Trim first the roots that aren't in this module.
+  for (auto &[RootGuid, _] : llvm::make_early_inc_range(*MaybeCtx))
+    if (!ProfileRootsInModule.contains(RootGuid))
+      MaybeCtx->erase(RootGuid);
+  // If none of the roots are in the module, we have no profile (for this
+  // module)
+  if (MaybeCtx->empty())
+    return {};
+
+  // OK, so we have a valid profile and it's applicable to roots in this module.
   PGOContextualProfile Result;
 
   for (const auto &F : M) {
@@ -166,10 +183,6 @@ PGOContextualProfile CtxProfAnalysis::run(Module &M,
   }
   // If we made it this far, the Result is valid - which we mark by setting
   // .Profiles.
-  // Trim first the roots that aren't in this module.
-  for (auto &[RootGuid, _] : llvm::make_early_inc_range(*MaybeCtx))
-    if (!Result.FuncInfo.contains(RootGuid))
-      MaybeCtx->erase(RootGuid);
   Result.Profiles = std::move(*MaybeCtx);
   return Result;
 }
diff --git a/llvm/test/Analysis/CtxProfAnalysis/load-unapplicable.ll b/llvm/test/Analysis/CtxProfAnalysis/load-unapplicable.ll
new file mode 100644
index 00000000000000..ad7a9eaeb07a57
--- /dev/null
+++ b/llvm/test/Analysis/CtxProfAnalysis/load-unapplicable.ll
@@ -0,0 +1,60 @@
+; REQUIRES: x86_64-linux
+;
+; RUN: rm -rf %t
+; RUN: split-file %s %t
+; RUN: llvm-ctxprof-util fromJSON --input=%t/profile.json --output=%t/profile.ctxprofdata
+; RUN: opt -passes='require<ctx-prof-analysis>,print<ctx-prof-analysis>' -ctx-profile-printer-level=everything \
+; RUN:   %t/example.ll -S 2>&1 | FileCheck %s
+
+; CHECK: No contextual profile was provided
+;
+; This is the reference profile, laid out in the format the json formatter will
+; output it from opt.
+;--- profile.json
+[
+  {
+    "Counters": [
+      9
+    ],
+    "Guid": 12341
+  },
+  {
+    "Counters": [
+      5
+    ],
+    "Guid": 12074870348631550642
+  },
+  {
+    "Callsites": [
+      [
+        {
+          "Counters": [
+            6,
+            7
+          ],
+          "Guid": 728453322856651412
+        }
+      ]
+    ],
+    "Counters": [
+      1
+    ],
+    "Guid": 11872291593386833696
+  }
+]
+;--- example.ll
+declare void @bar()
+
+define void @an_entrypoint(i32 %a) {
+  %t = icmp eq i32 %a, 0
+  br i1 %t, label %yes, label %no
+
+yes:
+  call void @bar()
+  ret void
+no:
+  ret void
+}
+
+attributes #0 = { noinline }
+!0 = !{ i64 1000 }
\ No newline at end of file

}

attributes #0 = { noinline }
!0 = !{ i64 1000 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't attached to anything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is now - the goal is to make sure it's clearly different from the given root.

; RUN: opt -passes='require<ctx-prof-analysis>,print<ctx-prof-analysis>' -ctx-profile-printer-level=everything \
; RUN: %t/example.ll -S 2>&1 | FileCheck %s

; CHECK: No contextual profile was provided
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the error be a bit more explicit - e.g. "No contextual profile containing functions from this module was provided" or something like that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not an error, this is the printout from the ctx-profile-printer.

@mtrofin mtrofin force-pushed the users/mtrofin/09-05-_ctx_prof_handle_case_when_no_root_is_in_this_module branch from 53b11dd to 1d3df1b Compare September 6, 2024 17:23
Copy link
Contributor

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with one comment suggestion

Copy link
Member Author

mtrofin commented Sep 6, 2024

Merge activity

  • Sep 6, 4:40 PM EDT: @mtrofin started a stack merge that includes this pull request via Graphite.
  • Sep 6, 4:42 PM EDT: Graphite rebased this pull request as part of a merge.
  • Sep 6, 4:44 PM EDT: @mtrofin merged this pull request with Graphite.

@mtrofin mtrofin force-pushed the users/mtrofin/09-05-_ctx_prof_handle_case_when_no_root_is_in_this_module branch from 1d3df1b to cdd6c12 Compare September 6, 2024 20:42
@mtrofin mtrofin merged commit 6cb2d40 into main Sep 6, 2024
5 of 7 checks passed
@mtrofin mtrofin deleted the users/mtrofin/09-05-_ctx_prof_handle_case_when_no_root_is_in_this_module branch September 6, 2024 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants