Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions llvm/include/llvm/Transforms/Utils/MetaRenamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -947,6 +948,65 @@ parseLowerAllowCheckPassOptions(StringRef Params) {
return Result;
}

Expected<MetaRenamerOptions> parseMetaRenamerPassOptions(StringRef Params) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should unify the syntax to handle the case pass-name<arg=a list>, pass internalize has the same request but has a different syntax, pending on pass infrastructure maintainers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

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()) {
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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")
Copy link
Contributor

Choose a reason for hiding this comment

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

git clang-format --extensions=def might handle this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed

#undef MODULE_PASS_WITH_PARAMS

#ifndef CGSCC_ANALYSIS
Expand Down
45 changes: 8 additions & 37 deletions llvm/lib/Transforms/Utils/MetaRenamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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))
Expand Down Expand Up @@ -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();
}
3 changes: 2 additions & 1 deletion llvm/test/Transforms/MetaRenamer/exclude-names.ll
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

The existing test coverage for all the options is lacking. Can you add a new test to make sure all the options parse? In particular, check the failed to parse case. Bonus points for a test which shows they work

Also these are somewhat unique in that the content is also a string, so there may be escape hazards to consider. Not sure there is any precedent for this in the existing options. Can you add some tests where the string value is quoted, and contains a ;?

Copy link
Contributor Author

@egorshamshura egorshamshura Apr 2, 2025

Choose a reason for hiding this comment

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

Added invalid-pass-parameter test and semicolon-substr test

; 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

Expand Down
9 changes: 9 additions & 0 deletions llvm/test/Transforms/MetaRenamer/invalid-pass-parameter.ll
Original file line number Diff line number Diff line change
@@ -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
}

2 changes: 1 addition & 1 deletion llvm/test/Transforms/MetaRenamer/only-inst.ll
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/Transforms/MetaRenamer/semicolon-substr.ll
Original file line number Diff line number Diff line change
@@ -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
}