Skip to content

[ctx_prof] Automatically convert available external linkage to local for modules with contextual roots #109203

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 18, 2024

For the modules containing context roots, the way IPO happens will potentially result in imported functions that are differently specialized (even if themselves not inlined) than their originals. So we want to convert them to local rather than elide them.

Eventually we'd perform this as a ThinLTO directive.

@mtrofin mtrofin force-pushed the users/mtrofin/09-17-_ctx_prof_handle_select_ branch from 09642a4 to 7e92883 Compare September 18, 2024 21:27
@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from 886b894 to bcac236 Compare September 18, 2024 21:27
@mtrofin mtrofin marked this pull request as ready for review September 18, 2024 22:12
@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Mircea Trofin (mtrofin)

Changes

For the modules containing context roots, the way IPO happens will potentially result in imported functions that are differently specialized (even if themselves not inlined) than their originals. So we want to convert them to local rather than elide them.

Eventually we'd perform this as a ThinLTO directive.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/ElimAvailExtern.cpp (+8-5)
  • (modified) llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll (+11-2)
diff --git a/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp b/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
index 2b34d3b5a56ea4..644effab9414ba 100644
--- a/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
+++ b/llvm/lib/Transforms/IPO/ElimAvailExtern.cpp
@@ -14,6 +14,7 @@
 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Analysis/CtxProfAnalysis.h"
 #include "llvm/IR/Constant.h"
 #include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/Function.h"
@@ -88,7 +89,7 @@ static void convertToLocalCopy(Module &M, Function &F) {
   ++NumConversions;
 }
 
-static bool eliminateAvailableExternally(Module &M) {
+static bool eliminateAvailableExternally(Module &M, bool Convert) {
   bool Changed = false;
 
   // Drop initializers of available externally global variables.
@@ -112,7 +113,7 @@ static bool eliminateAvailableExternally(Module &M) {
     if (F.isDeclaration() || !F.hasAvailableExternallyLinkage())
       continue;
 
-    if (ConvertToLocal)
+    if (Convert || ConvertToLocal)
       convertToLocalCopy(M, F);
     else
       deleteFunction(F);
@@ -125,8 +126,10 @@ static bool eliminateAvailableExternally(Module &M) {
 }
 
 PreservedAnalyses
-EliminateAvailableExternallyPass::run(Module &M, ModuleAnalysisManager &) {
-  if (!eliminateAvailableExternally(M))
-    return PreservedAnalyses::all();
+EliminateAvailableExternallyPass::run(Module &M, ModuleAnalysisManager &MAM) {
+  auto *CtxProf = MAM.getCachedResult<CtxProfAnalysis>(M);
+  if (!eliminateAvailableExternally(M, (CtxProf && !!(*CtxProf))))
+    ;
+  return PreservedAnalyses::all();
   return PreservedAnalyses::none();
 }
diff --git a/llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll b/llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll
index 786cc260d331c6..d0b96daf3bf3b1 100644
--- a/llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll
+++ b/llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll
@@ -1,6 +1,11 @@
 ; REQUIRES: asserts
 ; RUN: opt -passes=elim-avail-extern -avail-extern-to-local -stats -S 2>&1 < %s | FileCheck %s
+; RUN: echo '[{"Guid":1234, "Counters": [1]}]' | llvm-ctxprof-util fromJSON --input=- --output=%t_profile.ctxprofdata
+; RUN: opt -passes='assign-guid,require<ctx-prof-analysis>,elim-avail-extern' -use-ctx-profile=%t_profile.ctxprofdata -stats -S 2>&1 < %s | FileCheck %s
 
+; If the profile doesn't apply to this module, nothing gets converted.
+; RUN: echo '[{"Guid":5678, "Counters": [1]}]' | llvm-ctxprof-util fromJSON --input=- --output=%t_profile_bad.ctxprofdata
+; RUN: opt -passes='assign-guid,require<ctx-prof-analysis>,elim-avail-extern' -use-ctx-profile=%t_profile_bad.ctxprofdata -stats -S 2>&1 < %s | FileCheck %s --check-prefix=NOOP
 
 declare void @call_out(ptr %fct)
 
@@ -12,13 +17,15 @@ define available_externally hidden void @g() {
   ret void
 }
 
-define void @hello(ptr %g) {
+define void @hello(ptr %g) !guid !0 {
   call void @f()
   %f = load ptr, ptr @f
   call void @call_out(ptr %f)
   ret void
 }
 
+!0 = !{i64 1234}
+
 ; CHECK: define internal void @f.__uniq.{{[0-9|a-f]*}}()
 ; CHECK: declare hidden void @g()
 ; CHECK: call void @f.__uniq.{{[0-9|a-f]*}}()
@@ -26,4 +33,6 @@ define void @hello(ptr %g) {
 ; CHECK-NEXT: call void @call_out(ptr %f)
 ; CHECK: Statistics Collected
 ; CHECK: 1 elim-avail-extern - Number of functions converted
-; CHECK: 1 elim-avail-extern - Number of functions removed
\ No newline at end of file
+; CHECK: 1 elim-avail-extern - Number of functions removed
+
+; NOOP: 2 elim-avail-extern - Number of functions removed
\ No newline at end of file

@mtrofin mtrofin force-pushed the users/mtrofin/09-17-_ctx_prof_handle_select_ branch from 7e92883 to b68a12f Compare September 19, 2024 02:48
Copy link
Member Author

mtrofin commented Sep 19, 2024

@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from bcac236 to 24c3769 Compare September 19, 2024 02:48
@mtrofin mtrofin force-pushed the users/mtrofin/09-17-_ctx_prof_handle_select_ branch from b68a12f to dfde003 Compare September 19, 2024 04:10
@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from 24c3769 to 49a3d00 Compare September 19, 2024 04:11
@mtrofin mtrofin force-pushed the users/mtrofin/09-17-_ctx_prof_handle_select_ branch from dfde003 to 785bb3c Compare September 19, 2024 04:15
@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from 49a3d00 to ccc89f9 Compare September 19, 2024 04:16
@mtrofin mtrofin force-pushed the users/mtrofin/09-17-_ctx_prof_handle_select_ branch from 785bb3c to 17a2ba8 Compare September 19, 2024 04:32
@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from ccc89f9 to 3cafcfb Compare September 19, 2024 04:32
@mtrofin mtrofin force-pushed the users/mtrofin/09-17-_ctx_prof_handle_select_ branch from 17a2ba8 to 54fd9b5 Compare September 23, 2024 20:30
@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from 3cafcfb to 2f34ca7 Compare September 23, 2024 20:30
@mtrofin mtrofin force-pushed the users/mtrofin/09-17-_ctx_prof_handle_select_ branch from 54fd9b5 to 8ed7812 Compare September 23, 2024 20:39
@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from 2f34ca7 to 4b6269d Compare September 23, 2024 20:40
Base automatically changed from users/mtrofin/09-17-_ctx_prof_handle_select_ to main September 23, 2024 22:21
@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from 4b6269d to 44a8d38 Compare September 23, 2024 22: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 a couple of comment suggestions

@mtrofin mtrofin force-pushed the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch from 44a8d38 to 1ba77f5 Compare September 24, 2024 20:16
Copy link
Member Author

mtrofin commented Sep 24, 2024

Merge activity

  • Sep 24, 4:17 PM EDT: @mtrofin started a stack merge that includes this pull request via Graphite.
  • Sep 24, 4:18 PM EDT: @mtrofin merged this pull request with Graphite.

@mtrofin mtrofin merged commit 4911235 into main Sep 24, 2024
5 of 7 checks passed
@mtrofin mtrofin deleted the users/mtrofin/09-18-_ctx_prof_automatically_convert_available_external_linkage_to_local_for_modules_with_contextual_roots branch September 24, 2024 20:18
@mikaelholmen
Copy link
Collaborator

mikaelholmen commented Sep 25, 2024

If I compile with EXPENSIVE_CHECKS on a large number of lit tests fail with this patch.
E.g.
build-all-expensive/bin/llvm-lit -av ../lld/test/wasm/lto/thin-archivecollision.ll
fails with

LLVM ERROR: Function @blah changed by EliminateAvailableExternallyPass without invalidating analyses

I suppose this is due to the error @RKSimon pointed out in
#109203 (review)

mtrofin added a commit that referenced this pull request Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants