[llvm] MetaRenamer: replaced command line options with pass parameters (PR #133975)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 1 23:00:43 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Shamshura Egor (egorshamshura)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/133975.diff
8 Files Affected:
- (modified) llvm/include/llvm/Transforms/Utils/MetaRenamer.h (+26)
- (modified) llvm/lib/Passes/PassBuilder.cpp (+60)
- (modified) llvm/lib/Passes/PassRegistry.def (+6-2)
- (modified) llvm/lib/Transforms/Utils/MetaRenamer.cpp (+8-37)
- (modified) llvm/test/Transforms/MetaRenamer/exclude-names.ll (+2-1)
- (added) llvm/test/Transforms/MetaRenamer/invalid-pass-parameter.ll (+9)
- (modified) llvm/test/Transforms/MetaRenamer/only-inst.ll (+1-1)
- (added) llvm/test/Transforms/MetaRenamer/semicolon-substr.ll (+8)
``````````diff
diff --git a/llvm/include/llvm/Transforms/Utils/MetaRenamer.h b/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
index fff3dff75837e..4434fb4ac89a5 100644
--- a/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
+++ b/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
@@ -18,7 +18,33 @@
#include "llvm/IR/PassManager.h"
namespace llvm {
+
+struct MetaRenamerOptions {
+ /// Prefixes for functions that don't need to be renamed, separated by a comma
+ StringRef RenameExcludeFunctionPrefixes;
+
+ /// Prefixes for aliases that don't need to be renamed, separated by a comma
+ StringRef RenameExcludeAliasPrefixes;
+
+ /// Prefixes for global values that don't need to be renamed, separated by a
+ /// comma
+ StringRef RenameExcludeGlobalPrefixes;
+
+ /// Prefixes for structs that don't need to be renamed, separated by a comma
+ StringRef RenameExcludeStructPrefixes;
+
+ /// Only rename the instructions in the function
+ bool RenameOnlyInst = false;
+};
+
struct MetaRenamerPass : PassInfoMixin<MetaRenamerPass> {
+private:
+ const MetaRenamerOptions Options;
+
+public:
+ MetaRenamerPass(MetaRenamerOptions Options = MetaRenamerOptions())
+ : Options(Options) {}
+
PreservedAnalyses run(Module &, ModuleAnalysisManager &);
};
} // namespace llvm
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 5cda1517e127d..2951959602afc 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -176,6 +176,7 @@
#include "llvm/Passes/OptimizationLevel.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Regex.h"
@@ -947,6 +948,65 @@ parseLowerAllowCheckPassOptions(StringRef Params) {
return Result;
}
+Expected<MetaRenamerOptions> parseMetaRenamerPassOptions(StringRef Params) {
+ MetaRenamerOptions Result;
+ bool InQuotes = false;
+
+ while (!Params.empty()) {
+ StringRef ParamName;
+ size_t SplitPos = 0;
+
+ for (size_t I = 0; I < Params.size(); ++I) {
+ if (Params[I] == '"') {
+ InQuotes = !InQuotes;
+ } else if (Params[I] == ';' && !InQuotes) {
+ SplitPos = I;
+ break;
+ }
+ }
+
+ if (SplitPos == 0) {
+ ParamName = Params;
+ Params = "";
+ } else {
+ ParamName = Params.substr(0, SplitPos);
+ Params = Params.substr(SplitPos + 1);
+ }
+
+ bool Enable = !ParamName.consume_front("no-");
+
+ auto ExtractValue = [](StringRef Str) -> StringRef {
+ if (Str.starts_with("\"")) {
+ Str = Str.drop_front();
+ if (Str.ends_with("\"")) {
+ Str = Str.drop_back();
+ }
+ }
+ return Str;
+ };
+
+ if (ParamName == "rename-only-inst") {
+ Result.RenameOnlyInst = Enable;
+ } else if (ParamName.consume_front("rename-exclude-struct-prefixes=")) {
+ Result.RenameExcludeStructPrefixes = ExtractValue(ParamName);
+ } else if (ParamName.consume_front("rename-exclude-global-prefixes=")) {
+ Result.RenameExcludeGlobalPrefixes = ExtractValue(ParamName);
+ } else if (ParamName.consume_front("rename-exclude-alias-prefixes=")) {
+ Result.RenameExcludeAliasPrefixes = ExtractValue(ParamName);
+ } else if (ParamName.consume_front("rename-exclude-function-prefixes=")) {
+ Result.RenameExcludeFunctionPrefixes = ExtractValue(ParamName);
+ } else {
+ return make_error<StringError>(
+ formatv("invalid metarenamer pass parameter '{0}' ", ParamName)
+ .str(),
+ inconvertibleErrorCode());
+ }
+ }
+
+ return Result;
+}
+
+
Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) {
MemorySanitizerOptions Result;
while (!Params.empty()) {
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 510a505995304..ffe2f2ab34d53 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -103,7 +103,6 @@ MODULE_PASS("pgo-force-function-attrs", PGOForceFunctionAttrsPass(PGOOpt ? PGOOp
MODULE_PASS("memprof-context-disambiguation", MemProfContextDisambiguation())
MODULE_PASS("memprof-module", ModuleMemProfilerPass())
MODULE_PASS("mergefunc", MergeFunctionsPass())
-MODULE_PASS("metarenamer", MetaRenamerPass())
MODULE_PASS("module-inline", ModuleInlinerPass())
MODULE_PASS("name-anon-globals", NameAnonGlobalPass())
MODULE_PASS("no-op-module", NoOpModulePass())
@@ -233,7 +232,12 @@ MODULE_PASS_WITH_PARAMS(
return StructuralHashPrinterPass(errs(), Options);
},
parseStructuralHashPrinterPassOptions, "detailed;call-target-ignored")
-
+MODULE_PASS_WITH_PARAMS("metarenamer", "MetaRenamerPass",
+ [](MetaRenamerOptions Options) {
+ return MetaRenamerPass(Options);
+ },
+ parseMetaRenamerPassOptions,
+ "rename-exclude-alias-prefixes=S;rename-exclude-function-prefixes=S;rename-exclude-global-prefixes=S;rename-exclude-struct-prefixes=S;no-rename-only-inst;rename-only-inst")
#undef MODULE_PASS_WITH_PARAMS
#ifndef CGSCC_ANALYSIS
diff --git a/llvm/lib/Transforms/Utils/MetaRenamer.cpp b/llvm/lib/Transforms/Utils/MetaRenamer.cpp
index fd0112ae529cc..205236955c12c 100644
--- a/llvm/lib/Transforms/Utils/MetaRenamer.cpp
+++ b/llvm/lib/Transforms/Utils/MetaRenamer.cpp
@@ -35,36 +35,6 @@
using namespace llvm;
-static cl::opt<std::string> RenameExcludeFunctionPrefixes(
- "rename-exclude-function-prefixes",
- cl::desc("Prefixes for functions that don't need to be renamed, separated "
- "by a comma"),
- cl::Hidden);
-
-static cl::opt<std::string> RenameExcludeAliasPrefixes(
- "rename-exclude-alias-prefixes",
- cl::desc("Prefixes for aliases that don't need to be renamed, separated "
- "by a comma"),
- cl::Hidden);
-
-static cl::opt<std::string> RenameExcludeGlobalPrefixes(
- "rename-exclude-global-prefixes",
- cl::desc(
- "Prefixes for global values that don't need to be renamed, separated "
- "by a comma"),
- cl::Hidden);
-
-static cl::opt<std::string> RenameExcludeStructPrefixes(
- "rename-exclude-struct-prefixes",
- cl::desc("Prefixes for structs that don't need to be renamed, separated "
- "by a comma"),
- cl::Hidden);
-
-static cl::opt<bool>
- RenameOnlyInst("rename-only-inst", cl::init(false),
- cl::desc("only rename the instructions in the function"),
- cl::Hidden);
-
static const char *const metaNames[] = {
// See http://en.wikipedia.org/wiki/Metasyntactic_variable
"foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
@@ -129,7 +99,8 @@ void MetaRename(Function &F) {
}
void MetaRename(Module &M,
- function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
+ function_ref<TargetLibraryInfo &(Function &)> GetTLI,
+ const MetaRenamerOptions &Options) {
// Seed our PRNG with simple additive sum of ModuleID. We're looking to
// simply avoid always having the same function names, and we need to
// remain deterministic.
@@ -143,10 +114,10 @@ void MetaRename(Module &M,
SmallVector<StringRef, 8> ExcludedGlobalsPrefixes;
SmallVector<StringRef, 8> ExcludedStructsPrefixes;
SmallVector<StringRef, 8> ExcludedFuncPrefixes;
- parseExcludedPrefixes(RenameExcludeAliasPrefixes, ExcludedAliasesPrefixes);
- parseExcludedPrefixes(RenameExcludeGlobalPrefixes, ExcludedGlobalsPrefixes);
- parseExcludedPrefixes(RenameExcludeStructPrefixes, ExcludedStructsPrefixes);
- parseExcludedPrefixes(RenameExcludeFunctionPrefixes, ExcludedFuncPrefixes);
+ parseExcludedPrefixes(Options.RenameExcludeAliasPrefixes, ExcludedAliasesPrefixes);
+ parseExcludedPrefixes(Options.RenameExcludeGlobalPrefixes, ExcludedGlobalsPrefixes);
+ parseExcludedPrefixes(Options.RenameExcludeStructPrefixes, ExcludedStructsPrefixes);
+ parseExcludedPrefixes(Options.RenameExcludeFunctionPrefixes, ExcludedFuncPrefixes);
auto IsNameExcluded = [](StringRef &Name,
SmallVectorImpl<StringRef> &ExcludedPrefixes) {
@@ -164,7 +135,7 @@ void MetaRename(Module &M,
IsNameExcluded(Name, ExcludedFuncPrefixes);
};
- if (RenameOnlyInst) {
+ if (Options.RenameOnlyInst) {
// Rename all functions
for (auto &F : M) {
if (ExcludeLibFuncs(F))
@@ -230,7 +201,7 @@ PreservedAnalyses MetaRenamerPass::run(Module &M, ModuleAnalysisManager &AM) {
auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
return FAM.getResult<TargetLibraryAnalysis>(F);
};
- MetaRename(M, GetTLI);
+ MetaRename(M, GetTLI, Options);
return PreservedAnalyses::all();
}
diff --git a/llvm/test/Transforms/MetaRenamer/exclude-names.ll b/llvm/test/Transforms/MetaRenamer/exclude-names.ll
index 6e088ef7f4190..1002216f632ac 100644
--- a/llvm/test/Transforms/MetaRenamer/exclude-names.ll
+++ b/llvm/test/Transforms/MetaRenamer/exclude-names.ll
@@ -1,4 +1,5 @@
-; RUN: opt -passes=metarenamer -rename-exclude-function-prefixes=my_func -rename-exclude-global-prefixes=my_global -rename-exclude-struct-prefixes=my_struct -rename-exclude-alias-prefixes=my_alias -S %s | FileCheck %s
+; RUN: opt -passes='metarenamer<rename-exclude-function-prefixes=my_func;rename-exclude-global-prefixes=my_global;rename-exclude-struct-prefixes=my_struct;rename-exclude-alias-prefixes=my_alias>' -S %s | FileCheck %s
+; RUN: opt -passes='metarenamer<rename-exclude-function-prefixes="my_func";rename-exclude-global-prefixes="my_global";rename-exclude-struct-prefixes="my_struct";rename-exclude-alias-prefixes="my_alias">' -S %s | FileCheck %s
; Check that excluded names don't get renamed while all the other ones do
diff --git a/llvm/test/Transforms/MetaRenamer/invalid-pass-parameter.ll b/llvm/test/Transforms/MetaRenamer/invalid-pass-parameter.ll
new file mode 100644
index 0000000000000..2e672f9409b12
--- /dev/null
+++ b/llvm/test/Transforms/MetaRenamer/invalid-pass-parameter.ll
@@ -0,0 +1,9 @@
+; RUN: not opt -S -passes='metarenamer<invalid>' %s 2>&1 | FileCheck -check-prefix=ERR %s
+
+; ERR: invalid metarenamer pass parameter 'invalid'
+
+define i32 @0(i32, i32) {
+ %3 = add i32 %0, %1
+ ret i32 %3
+}
+
diff --git a/llvm/test/Transforms/MetaRenamer/only-inst.ll b/llvm/test/Transforms/MetaRenamer/only-inst.ll
index f13371519682f..c423bab08e301 100644
--- a/llvm/test/Transforms/MetaRenamer/only-inst.ll
+++ b/llvm/test/Transforms/MetaRenamer/only-inst.ll
@@ -1,4 +1,4 @@
-; RUN: opt -passes=metarenamer -rename-only-inst=1 -S < %s | FileCheck %s
+; RUN: opt -passes='metarenamer<rename-only-inst>' -S < %s | FileCheck %s
target triple = "x86_64-pc-linux-gnu"
; CHECK: %struct.foo_xxx = type { i32, float, %struct.bar_xxx }
diff --git a/llvm/test/Transforms/MetaRenamer/semicolon-substr.ll b/llvm/test/Transforms/MetaRenamer/semicolon-substr.ll
new file mode 100644
index 0000000000000..ca76316189e01
--- /dev/null
+++ b/llvm/test/Transforms/MetaRenamer/semicolon-substr.ll
@@ -0,0 +1,8 @@
+; RUN: opt -passes='metarenamer<rename-exclude-function-prefixes="my_fu;nc">' -S %s | FileCheck %s
+
+; CHECK: define i32 @"my_fu;nc"(
+define i32 @"my_fu;nc"(i32, i32) {
+ %3 = add i32 %0, %1
+ ret i32 %3
+}
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/133975
More information about the llvm-commits
mailing list