Skip to content

[NewPM] Add pass options for InternalizePass to preserve GVs. #91334

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

Merged
merged 2 commits into from
May 15, 2024

Conversation

maleadt
Copy link
Contributor

@maleadt maleadt commented May 7, 2024

This PR adds a string interface to InternalizePass' MustPreserveGV option, which is a callback function to indicate if a GV is not to be internalized. This is for use in LLVM.jl, the Julia wrapper for LLVM, which uses the C API and is thus required to use the PassBuilder string API for building NewPM pipelines.

@llvmbot
Copy link
Member

llvmbot commented May 7, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Tim Besard (maleadt)

Changes

This PR adds a string interface to InternalizePass' MustPreserveGV option, which is a callback function to indicate if a GV is not to be internalized. This is for use in LLVM.jl, the Julia wrapper for LLVM, which uses the C API and is thus required to use the PassBuilder string API for building NewPM pipelines.


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

3 Files Affected:

  • (modified) llvm/lib/Passes/PassBuilder.cpp (+18)
  • (modified) llvm/lib/Passes/PassRegistry.def (+14-1)
  • (modified) llvm/test/Transforms/Internalize/lists.ll (+5)
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 51ddb73943b10d..1f9c4000ae3d86 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -1141,6 +1141,24 @@ Expected<GlobalMergeOptions> parseGlobalMergeOptions(StringRef Params) {
   return Result;
 }
 
+Expected<SmallVector<std::string, 0>> parseInternalizeGVs(StringRef Params) {
+  SmallVector<std::string, 1> PreservedGVs;
+  while (!Params.empty()) {
+    StringRef ParamName;
+    std::tie(ParamName, Params) = Params.split(';');
+
+    if (ParamName.consume_front("preserve-gv=")) {
+      PreservedGVs.push_back(ParamName.str());
+    } else {
+      return make_error<StringError>(
+          formatv("invalid Internalize pass parameter '{0}' ", ParamName).str(),
+          inconvertibleErrorCode());
+    }
+  }
+
+  return PreservedGVs;
+}
+
 } // namespace
 
 /// Tests whether a pass name starts with a valid prefix for a default pipeline
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 6864f307e56cf4..ea6b065a322fff 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -77,7 +77,6 @@ MODULE_PASS("inliner-wrapper-no-mandatory-first",
 MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass())
 MODULE_PASS("instrorderfile", InstrOrderFilePass())
 MODULE_PASS("instrprof", InstrProfilingLoweringPass())
-MODULE_PASS("internalize", InternalizePass())
 MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())
 MODULE_PASS("iroutliner", IROutlinerPass())
 MODULE_PASS("jmc-instrumenter", JMCInstrumenterPass())
@@ -199,6 +198,20 @@ MODULE_PASS_WITH_PARAMS(
       return StructuralHashPrinterPass(dbgs(), EnableDetailedStructuralHash);
     },
     parseStructuralHashPrinterPassOptions, "detailed")
+MODULE_PASS_WITH_PARAMS(
+    "internalize", "InternalizePass",
+    [](SmallVector<std::string, 0> PreservedGVs) {
+      if (PreservedGVs.empty())
+        return InternalizePass();
+      auto MustPreserveGV = [=](const GlobalValue &GV) {
+        for (auto &PreservedGV : PreservedGVs)
+          if (GV.getName() == PreservedGV)
+            return true;
+        return false;
+      };
+      return InternalizePass(MustPreserveGV);
+    },
+    parseInternalizeGVs, "preserve-gv=GV")
 #undef MODULE_PASS_WITH_PARAMS
 
 #ifndef CGSCC_ANALYSIS
diff --git a/llvm/test/Transforms/Internalize/lists.ll b/llvm/test/Transforms/Internalize/lists.ll
index df408f906b7806..83dad03d75eae1 100644
--- a/llvm/test/Transforms/Internalize/lists.ll
+++ b/llvm/test/Transforms/Internalize/lists.ll
@@ -13,6 +13,11 @@
 ; -file and -list options should be merged, the apifile contains foo and j
 ; RUN: opt < %s -passes=internalize -internalize-public-api-list bar -internalize-public-api-file %S/apifile -S | FileCheck --check-prefix=FOO_J_AND_BAR %s
 
+; specifying through pass builder option
+; RUN: opt < %s -passes='internalize<preserve-gv=foo;preserve-gv=j>' -S | FileCheck --check-prefix=FOO_AND_J %s
+; RUN: opt < %s -passes='internalize<preserve-gv=foo;preserve-gv=bar>' -S | FileCheck --check-prefix=FOO_AND_BAR %s
+; RUN: opt < %s -passes='internalize<preserve-gv=foo;preserve-gv=j;preserve-gv=bar>' -S | FileCheck --check-prefix=FOO_J_AND_BAR %s
+
 ; ALL: @i = internal global
 ; FOO_AND_J: @i = internal global
 ; FOO_AND_BAR: @i = internal global

@maleadt
Copy link
Contributor Author

maleadt commented May 14, 2024

cc @paperchalice (based on recent activity on the pass builder)

@maleadt maleadt force-pushed the newpm_internalize_options branch from 5c086c6 to 2ac9941 Compare May 14, 2024 10:30
; RUN: opt < %s -passes='internalize<preserve-gv=foo;preserve-gv=j>' -S | FileCheck --check-prefix=FOO_AND_J %s
; RUN: opt < %s -passes='internalize<preserve-gv=foo;preserve-gv=bar>' -S | FileCheck --check-prefix=FOO_AND_BAR %s
; RUN: opt < %s -passes='internalize<preserve-gv=foo;preserve-gv=j;preserve-gv=bar>' -S | FileCheck --check-prefix=FOO_J_AND_BAR %s

Copy link
Contributor

Choose a reason for hiding this comment

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

This is unfortunate, comma has been used to separate pass names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I had started this change using , before realizing the separator was already taken. Using other separators seemed more questionable than just repeating the flag.

@paperchalice paperchalice requested a review from aeubanks May 14, 2024 11:14
@aeubanks aeubanks requested a review from teresajohnson May 14, 2024 17:10
@aeubanks
Copy link
Contributor

seems ok to me, unsure if @teresajohnson has any other opinions

@maleadt
Copy link
Contributor Author

maleadt commented May 15, 2024

Great, thanks all! I don't have commit bit, so please anybody land this for me.

@aeubanks aeubanks merged commit ee765b0 into llvm:main May 15, 2024
3 of 4 checks passed
joker-eph added a commit that referenced this pull request May 15, 2024
joker-eph added a commit that referenced this pull request May 15, 2024
…s." (#92321)

Reverts #91334

This broke the gcc7 build.
I suspect the issue is a mismatch on user-defined move constructor on
the return: `return PreservedGVs;` does not match the return type of the
function.
@joker-eph
Copy link
Collaborator

I had to revert in #92321 because it broke the gcc7 build: https://lab.llvm.org/buildbot/#/builders/264/builds/10592

maleadt added a commit to maleadt/llvm-project that referenced this pull request May 16, 2024
…#91334)

This PR adds a string interface to `InternalizePass`' `MustPreserveGV`
option, which is a callback function to indicate if a GV is not to be
internalized. This is for use in LLVM.jl, the Julia wrapper for LLVM,
which uses the C API and is thus required to use the PassBuilder string
API for building NewPM pipelines.
aeubanks pushed a commit that referenced this pull request May 16, 2024
…#92383)

Reland of #91334, which broke
the gcc7 buildbot and was reverted in
#92321.
Work around the failure by being explicit about returning an `Expected`.

cc @joker-eph
vchuravy pushed a commit to JuliaLang/julia that referenced this pull request May 20, 2024
Specifically, llvm/llvm-project#91334, which
LLVM.jl needs to use the internalize pass on NewPM.
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Jun 5, 2024
…a0e80e87c

Local branch amd-gfx cf4a0e8 Merged main:df5804aec48f99704ef26c740e19deaa4072fe27 into amd-gfx:7c50b9e88fc0
Remote branch main ee765b0 [NewPM] Add pass options for `InternalizePass` to preserve GVs. (llvm#91334)
lazarusA pushed a commit to lazarusA/julia that referenced this pull request Jul 12, 2024
Specifically, llvm/llvm-project#91334, which
LLVM.jl needs to use the internalize pass on NewPM.
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.

6 participants