[llvm] MetaRenamer: replaced command line options with pass parameters (PR #133975)
Shamshura Egor via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 2 01:40:43 PDT 2025
https://github.com/egorshamshura updated https://github.com/llvm/llvm-project/pull/133975
>From ca8115a7dcd81732c0174849d48865a959a9ebfb Mon Sep 17 00:00:00 2001
From: Egor Shamshura <shamshuraegor at gmail.com>
Date: Tue, 1 Apr 2025 19:50:32 +0000
Subject: [PATCH 1/4] replaced command line options with pass parameters for
MetaRenamer
---
.../llvm/Transforms/Utils/MetaRenamer.h | 26 +++++++++++
llvm/lib/Passes/PassBuilder.cpp | 27 +++++++++++
llvm/lib/Passes/PassRegistry.def | 8 +++-
llvm/lib/Transforms/Utils/MetaRenamer.cpp | 45 ++++---------------
.../Transforms/MetaRenamer/exclude-names.ll | 2 +-
llvm/test/Transforms/MetaRenamer/only-inst.ll | 2 +-
6 files changed, 69 insertions(+), 41 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Utils/MetaRenamer.h b/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
index fff3dff75837e..be2c66b8cdaed 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
+ std::string RenameExcludeFunctionPrefixes;
+
+ /// Prefixes for aliases that don't need to be renamed, separated by a comma
+ std::string RenameExcludeAliasPrefixes;
+
+ /// Prefixes for global values that don't need to be renamed, separated by a
+ /// comma
+ std::string RenameExcludeGlobalPrefixes;
+
+ /// Prefixes for structs that don't need to be renamed, separated by a comma
+ std::string 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..52c403c5ff0ba 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,32 @@ parseLowerAllowCheckPassOptions(StringRef Params) {
return Result;
}
+Expected<MetaRenamerOptions> parseMetaRenamerPassOptions(StringRef Params) {
+ MetaRenamerOptions Result;
+ while (!Params.empty()) {
+ StringRef ParamName;
+ std::tie(ParamName, Params) = Params.split(';');
+ bool Enable = !ParamName.consume_front("no-");
+ if (ParamName == "rename-only-inst") {
+ Result.RenameOnlyInst = Enable;
+ } else if (ParamName.consume_front("rename-exclude-struct-prefixes=")) {
+ Result.RenameExcludeStructPrefixes = ParamName;
+ } else if (ParamName.consume_front("rename-exclude-global-prefixes=")) {
+ Result.RenameExcludeGlobalPrefixes = ParamName;
+ } else if (ParamName.consume_front("rename-exclude-alias-prefixes=")) {
+ Result.RenameExcludeAliasPrefixes = ParamName;
+ } else if (ParamName.consume_front("rename-exclude-function-prefixes=")) {
+ Result.RenameExcludeFunctionPrefixes = 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..8eead4f5cdc6e 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-function-prefixes=S;rename-exclude-alias-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..6c47ec2f47e14 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,
+ MetaRenamerOptions const& 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..d53a0f96a51fe 100644
--- a/llvm/test/Transforms/MetaRenamer/exclude-names.ll
+++ b/llvm/test/Transforms/MetaRenamer/exclude-names.ll
@@ -1,4 +1,4 @@
-; 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/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 }
>From 21e2a364bfb8783d17669dccb42d2d34e9f40abc Mon Sep 17 00:00:00 2001
From: Egor Shamshura <shamshuraegor at gmail.com>
Date: Wed, 2 Apr 2025 04:53:30 +0000
Subject: [PATCH 2/4] changed std::string to StringRef, changed error message,
sorted by alphabetical order in PassRegistry
---
llvm/include/llvm/Transforms/Utils/MetaRenamer.h | 8 ++++----
llvm/lib/Passes/PassBuilder.cpp | 2 +-
llvm/lib/Passes/PassRegistry.def | 2 +-
llvm/lib/Transforms/Utils/MetaRenamer.cpp | 2 +-
4 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Utils/MetaRenamer.h b/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
index be2c66b8cdaed..4434fb4ac89a5 100644
--- a/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
+++ b/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
@@ -21,17 +21,17 @@ namespace llvm {
struct MetaRenamerOptions {
/// Prefixes for functions that don't need to be renamed, separated by a comma
- std::string RenameExcludeFunctionPrefixes;
+ StringRef RenameExcludeFunctionPrefixes;
/// Prefixes for aliases that don't need to be renamed, separated by a comma
- std::string RenameExcludeAliasPrefixes;
+ StringRef RenameExcludeAliasPrefixes;
/// Prefixes for global values that don't need to be renamed, separated by a
/// comma
- std::string RenameExcludeGlobalPrefixes;
+ StringRef RenameExcludeGlobalPrefixes;
/// Prefixes for structs that don't need to be renamed, separated by a comma
- std::string RenameExcludeStructPrefixes;
+ StringRef RenameExcludeStructPrefixes;
/// Only rename the instructions in the function
bool RenameOnlyInst = false;
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 52c403c5ff0ba..62e038e1e53f2 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -966,7 +966,7 @@ Expected<MetaRenamerOptions> parseMetaRenamerPassOptions(StringRef Params) {
Result.RenameExcludeFunctionPrefixes = ParamName;
} else {
return make_error<StringError>(
- formatv("invalid MetaRenamer pass parameter '{0}' ", ParamName)
+ formatv("invalid metarenamer pass parameter '{0}' ", ParamName)
.str(),
inconvertibleErrorCode());
}
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 8eead4f5cdc6e..ffe2f2ab34d53 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -237,7 +237,7 @@ MODULE_PASS_WITH_PARAMS("metarenamer", "MetaRenamerPass",
return MetaRenamerPass(Options);
},
parseMetaRenamerPassOptions,
- "rename-exclude-function-prefixes=S;rename-exclude-alias-prefixes=S;rename-exclude-global-prefixes=S;rename-exclude-struct-prefixes=S;no-rename-only-inst;rename-only-inst")
+ "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 6c47ec2f47e14..205236955c12c 100644
--- a/llvm/lib/Transforms/Utils/MetaRenamer.cpp
+++ b/llvm/lib/Transforms/Utils/MetaRenamer.cpp
@@ -100,7 +100,7 @@ void MetaRename(Function &F) {
void MetaRename(Module &M,
function_ref<TargetLibraryInfo &(Function &)> GetTLI,
- MetaRenamerOptions const& Options) {
+ 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.
>From 61327d753114e6b3ad5d57a59811ab163818d6a2 Mon Sep 17 00:00:00 2001
From: Egor Shamshura <shamshuraegor at gmail.com>
Date: Wed, 2 Apr 2025 05:59:48 +0000
Subject: [PATCH 3/4] added invalid pass parameter test, added semicolon substr
test, added quote test; updated parser
---
llvm/lib/Passes/PassBuilder.cpp | 43 ++++++++++++++++---
.../Transforms/MetaRenamer/exclude-names.ll | 1 +
.../MetaRenamer/invalid-pass-parameter.ll | 9 ++++
.../MetaRenamer/semicolon-substr.ll | 8 ++++
4 files changed, 56 insertions(+), 5 deletions(-)
create mode 100644 llvm/test/Transforms/MetaRenamer/invalid-pass-parameter.ll
create mode 100644 llvm/test/Transforms/MetaRenamer/semicolon-substr.ll
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 62e038e1e53f2..2951959602afc 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -950,20 +950,51 @@ parseLowerAllowCheckPassOptions(StringRef Params) {
Expected<MetaRenamerOptions> parseMetaRenamerPassOptions(StringRef Params) {
MetaRenamerOptions Result;
+ bool InQuotes = false;
+
while (!Params.empty()) {
StringRef ParamName;
- std::tie(ParamName, Params) = Params.split(';');
+ 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 = ParamName;
+ Result.RenameExcludeStructPrefixes = ExtractValue(ParamName);
} else if (ParamName.consume_front("rename-exclude-global-prefixes=")) {
- Result.RenameExcludeGlobalPrefixes = ParamName;
+ Result.RenameExcludeGlobalPrefixes = ExtractValue(ParamName);
} else if (ParamName.consume_front("rename-exclude-alias-prefixes=")) {
- Result.RenameExcludeAliasPrefixes = ParamName;
+ Result.RenameExcludeAliasPrefixes = ExtractValue(ParamName);
} else if (ParamName.consume_front("rename-exclude-function-prefixes=")) {
- Result.RenameExcludeFunctionPrefixes = ParamName;
+ Result.RenameExcludeFunctionPrefixes = ExtractValue(ParamName);
} else {
return make_error<StringError>(
formatv("invalid metarenamer pass parameter '{0}' ", ParamName)
@@ -971,9 +1002,11 @@ Expected<MetaRenamerOptions> parseMetaRenamerPassOptions(StringRef Params) {
inconvertibleErrorCode());
}
}
+
return Result;
}
+
Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) {
MemorySanitizerOptions Result;
while (!Params.empty()) {
diff --git a/llvm/test/Transforms/MetaRenamer/exclude-names.ll b/llvm/test/Transforms/MetaRenamer/exclude-names.ll
index d53a0f96a51fe..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
; 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/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
+}
+
>From bbfaafc52dd0a3d9cfc85f2f7625b93947ac66f7 Mon Sep 17 00:00:00 2001
From: Egor Shamshura <shamshuraegor at gmail.com>
Date: Wed, 2 Apr 2025 08:40:24 +0000
Subject: [PATCH 4/4] clang-formatted PassRegistry.def, changed syntax to be
the same as internalize pass, changed tests
---
.../llvm/Transforms/Utils/MetaRenamer.h | 17 ++++----
llvm/lib/Passes/PassBuilder.cpp | 39 +++----------------
llvm/lib/Passes/PassRegistry.def | 13 ++++---
llvm/lib/Transforms/Utils/MetaRenamer.cpp | 31 +++------------
.../Transforms/MetaRenamer/exclude-names.ll | 1 -
.../MetaRenamer/semicolon-substr.ll | 12 ++++--
6 files changed, 34 insertions(+), 79 deletions(-)
diff --git a/llvm/include/llvm/Transforms/Utils/MetaRenamer.h b/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
index 4434fb4ac89a5..165940428b158 100644
--- a/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
+++ b/llvm/include/llvm/Transforms/Utils/MetaRenamer.h
@@ -20,18 +20,17 @@
namespace llvm {
struct MetaRenamerOptions {
- /// Prefixes for functions that don't need to be renamed, separated by a comma
- StringRef RenameExcludeFunctionPrefixes;
+ /// Prefixes for functions that don't need to be renamed
+ SmallVector<StringRef> ExcludedFunctionsPrefixes;
- /// Prefixes for aliases that don't need to be renamed, separated by a comma
- StringRef RenameExcludeAliasPrefixes;
+ /// Prefixes for aliases that don't need to be renamed
+ SmallVector<StringRef> ExcludedAliasesPrefixes;
- /// Prefixes for global values that don't need to be renamed, separated by a
- /// comma
- StringRef RenameExcludeGlobalPrefixes;
+ /// Prefixes for global values that don't need to be renamed
+ SmallVector<StringRef> ExcludedGlobalsPrefixes;
- /// Prefixes for structs that don't need to be renamed, separated by a comma
- StringRef RenameExcludeStructPrefixes;
+ /// Prefixes for structs that don't need to be renamed
+ SmallVector<StringRef> ExcludedStructsPrefixes;
/// Only rename the instructions in the function
bool RenameOnlyInst = false;
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 2951959602afc..00e512250282d 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -950,51 +950,22 @@ parseLowerAllowCheckPassOptions(StringRef Params) {
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);
- }
-
+ std::tie(ParamName, Params) = Params.split(';');
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);
+ Result.ExcludedStructsPrefixes.push_back(ParamName);
} else if (ParamName.consume_front("rename-exclude-global-prefixes=")) {
- Result.RenameExcludeGlobalPrefixes = ExtractValue(ParamName);
+ Result.ExcludedGlobalsPrefixes.push_back(ParamName);
} else if (ParamName.consume_front("rename-exclude-alias-prefixes=")) {
- Result.RenameExcludeAliasPrefixes = ExtractValue(ParamName);
+ Result.ExcludedAliasesPrefixes.push_back(ParamName);
} else if (ParamName.consume_front("rename-exclude-function-prefixes=")) {
- Result.RenameExcludeFunctionPrefixes = ExtractValue(ParamName);
+ Result.ExcludedFunctionsPrefixes.push_back(ParamName);
} else {
return make_error<StringError>(
formatv("invalid metarenamer pass parameter '{0}' ", ParamName)
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index ffe2f2ab34d53..7d5be4a6af2a5 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -232,12 +232,13 @@ 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")
+MODULE_PASS_WITH_PARAMS(
+ "metarenamer", "MetaRenamerPass",
+ [](MetaRenamerOptions Options) { return MetaRenamerPass(Options); },
+ parseMetaRenamerPassOptions,
+ "rename-exclude-function-prefixes=S;rename-exclude-alias-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 205236955c12c..6f694c4467031 100644
--- a/llvm/lib/Transforms/Utils/MetaRenamer.cpp
+++ b/llvm/lib/Transforms/Utils/MetaRenamer.cpp
@@ -66,18 +66,6 @@ struct Renamer {
PRNG prng;
};
-static void
-parseExcludedPrefixes(StringRef PrefixesStr,
- SmallVectorImpl<StringRef> &ExcludedPrefixes) {
- for (;;) {
- auto PrefixesSplit = PrefixesStr.split(',');
- if (PrefixesSplit.first.empty())
- break;
- ExcludedPrefixes.push_back(PrefixesSplit.first);
- PrefixesStr = PrefixesSplit.second;
- }
-}
-
void MetaRenameOnlyInstructions(Function &F) {
for (auto &I : instructions(F))
if (!I.getType()->isVoidTy() && I.getName().empty())
@@ -110,17 +98,8 @@ void MetaRename(Module &M,
Renamer renamer(randSeed);
- SmallVector<StringRef, 8> ExcludedAliasesPrefixes;
- SmallVector<StringRef, 8> ExcludedGlobalsPrefixes;
- SmallVector<StringRef, 8> ExcludedStructsPrefixes;
- SmallVector<StringRef, 8> ExcludedFuncPrefixes;
- parseExcludedPrefixes(Options.RenameExcludeAliasPrefixes, ExcludedAliasesPrefixes);
- parseExcludedPrefixes(Options.RenameExcludeGlobalPrefixes, ExcludedGlobalsPrefixes);
- parseExcludedPrefixes(Options.RenameExcludeStructPrefixes, ExcludedStructsPrefixes);
- parseExcludedPrefixes(Options.RenameExcludeFunctionPrefixes, ExcludedFuncPrefixes);
-
auto IsNameExcluded = [](StringRef &Name,
- SmallVectorImpl<StringRef> &ExcludedPrefixes) {
+ const SmallVectorImpl<StringRef> &ExcludedPrefixes) {
return any_of(ExcludedPrefixes,
[&Name](auto &Prefix) { return Name.starts_with(Prefix); });
};
@@ -132,7 +111,7 @@ void MetaRename(Module &M,
StringRef Name = F.getName();
return Name.starts_with("llvm.") || (!Name.empty() && Name[0] == 1) ||
GetTLI(F).getLibFunc(F, Tmp) ||
- IsNameExcluded(Name, ExcludedFuncPrefixes);
+ IsNameExcluded(Name, Options.ExcludedFunctionsPrefixes);
};
if (Options.RenameOnlyInst) {
@@ -149,7 +128,7 @@ void MetaRename(Module &M,
for (GlobalAlias &GA : M.aliases()) {
StringRef Name = GA.getName();
if (Name.starts_with("llvm.") || (!Name.empty() && Name[0] == 1) ||
- IsNameExcluded(Name, ExcludedAliasesPrefixes))
+ IsNameExcluded(Name, Options.ExcludedAliasesPrefixes))
continue;
GA.setName("alias");
@@ -159,7 +138,7 @@ void MetaRename(Module &M,
for (GlobalVariable &GV : M.globals()) {
StringRef Name = GV.getName();
if (Name.starts_with("llvm.") || (!Name.empty() && Name[0] == 1) ||
- IsNameExcluded(Name, ExcludedGlobalsPrefixes))
+ IsNameExcluded(Name, Options.ExcludedGlobalsPrefixes))
continue;
GV.setName("global");
@@ -171,7 +150,7 @@ void MetaRename(Module &M,
for (StructType *STy : StructTypes) {
StringRef Name = STy->getName();
if (STy->isLiteral() || Name.empty() ||
- IsNameExcluded(Name, ExcludedStructsPrefixes))
+ IsNameExcluded(Name, Options.ExcludedStructsPrefixes))
continue;
SmallString<128> NameStorage;
diff --git a/llvm/test/Transforms/MetaRenamer/exclude-names.ll b/llvm/test/Transforms/MetaRenamer/exclude-names.ll
index 1002216f632ac..d53a0f96a51fe 100644
--- a/llvm/test/Transforms/MetaRenamer/exclude-names.ll
+++ b/llvm/test/Transforms/MetaRenamer/exclude-names.ll
@@ -1,5 +1,4 @@
; 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/semicolon-substr.ll b/llvm/test/Transforms/MetaRenamer/semicolon-substr.ll
index ca76316189e01..131a03cebefca 100644
--- a/llvm/test/Transforms/MetaRenamer/semicolon-substr.ll
+++ b/llvm/test/Transforms/MetaRenamer/semicolon-substr.ll
@@ -1,7 +1,13 @@
-; RUN: opt -passes='metarenamer<rename-exclude-function-prefixes="my_fu;nc">' -S %s | FileCheck %s
+; RUN: opt -passes='metarenamer<rename-exclude-function-prefixes=my_func;rename-exclude-function-prefixes=some_func>' -S %s | FileCheck %s
-; CHECK: define i32 @"my_fu;nc"(
-define i32 @"my_fu;nc"(i32, i32) {
+; CHECK: define i32 @my_func(
+define i32 @my_func(i32, i32) {
+ %3 = add i32 %0, %1
+ ret i32 %3
+}
+
+; CHECK: define i32 @some_func(
+define i32 @some_func(i32, i32) {
%3 = add i32 %0, %1
ret i32 %3
}
More information about the llvm-commits
mailing list