<div dir="ltr">Isn't this what the instnamer pass was for?<div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><br></div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jun 30, 2023 at 8:13 AM Anna Thomas via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Author: Anna Thomas<br>
Date: 2023-06-30T11:13:15-04:00<br>
New Revision: 6f9e743b91ad6ac1f333ce6e3efae44e137b54bb<br>
<br>
URL: <a href="https://github.com/llvm/llvm-project/commit/6f9e743b91ad6ac1f333ce6e3efae44e137b54bb" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/6f9e743b91ad6ac1f333ce6e3efae44e137b54bb</a><br>
DIFF: <a href="https://github.com/llvm/llvm-project/commit/6f9e743b91ad6ac1f333ce6e3efae44e137b54bb.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/6f9e743b91ad6ac1f333ce6e3efae44e137b54bb.diff</a><br>
<br>
LOG: [Metarenamer] Introduce option to only change inst names<br>
<br>
This is useful when needing to modify IR and test some optimizations on<br>
them, while keeping BB names and function names intact. If<br>
the IR uses ordered number naming (%1, %2, %3 etc), then we cannot just<br>
remove or reorder specific instructions since the verifier expects the<br>
numbers to be in order.<br>
<br>
Added: <br>
llvm/test/Transforms/MetaRenamer/only-inst.ll<br>
<br>
Modified: <br>
llvm/lib/Transforms/Utils/MetaRenamer.cpp<br>
<br>
Removed: <br>
<br>
<br>
<br>
################################################################################<br>
diff --git a/llvm/lib/Transforms/Utils/MetaRenamer.cpp b/llvm/lib/Transforms/Utils/MetaRenamer.cpp<br>
index 06ccbf888aa300..4053e78c604e64 100644<br>
--- a/llvm/lib/Transforms/Utils/MetaRenamer.cpp<br>
+++ b/llvm/lib/Transforms/Utils/MetaRenamer.cpp<br>
@@ -26,6 +26,7 @@<br>
#include "llvm/IR/GlobalAlias.h"<br>
#include "llvm/IR/GlobalVariable.h"<br>
#include "llvm/IR/Instruction.h"<br>
+#include "llvm/IR/InstIterator.h"<br>
#include "llvm/IR/Module.h"<br>
#include "llvm/IR/PassManager.h"<br>
#include "llvm/IR/Type.h"<br>
@@ -59,6 +60,11 @@ static cl::opt<std::string> RenameExcludeStructPrefixes(<br>
"by a comma"),<br>
cl::Hidden);<br>
<br>
+static cl::opt<bool><br>
+ RenameOnlyInst("rename-only-inst", cl::init(false),<br>
+ cl::desc("only rename the instructions in the function"),<br>
+ cl::Hidden);<br>
+<br>
static const char *const metaNames[] = {<br>
// See <a href="http://en.wikipedia.org/wiki/Metasyntactic_variable" rel="noreferrer" target="_blank">http://en.wikipedia.org/wiki/Metasyntactic_variable</a><br>
"foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",<br>
@@ -102,6 +108,12 @@ parseExcludedPrefixes(StringRef PrefixesStr,<br>
}<br>
}<br>
<br>
+void MetaRenameOnlyInstructions(Function &F) {<br>
+ for (auto &I : instructions(F))<br>
+ if (!I.getType()->isVoidTy())<br>
+ I.setName(I.getOpcodeName());<br>
+}<br>
+<br>
void MetaRename(Function &F) {<br>
for (Argument &Arg : F.args())<br>
if (!Arg.getType()->isVoidTy())<br>
@@ -142,6 +154,26 @@ void MetaRename(Module &M,<br>
[&Name](auto &Prefix) { return Name.startswith(Prefix); });<br>
};<br>
<br>
+ // Leave library functions alone because their presence or absence could<br>
+ // affect the behavior of other passes.<br>
+ auto ExcludeLibFuncs = [&](Function &F) {<br>
+ LibFunc Tmp;<br>
+ StringRef Name = F.getName();<br>
+ return Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||<br>
+ GetTLI(F).getLibFunc(F, Tmp) ||<br>
+ IsNameExcluded(Name, ExcludedFuncPrefixes);<br>
+ };<br>
+<br>
+ if (RenameOnlyInst) {<br>
+ // Rename all functions<br>
+ for (auto &F : M) {<br>
+ if (ExcludeLibFuncs(F))<br>
+ continue;<br>
+ MetaRenameOnlyInstructions(F);<br>
+ }<br>
+ return;<br>
+ }<br>
+<br>
// Rename all aliases<br>
for (GlobalAlias &GA : M.aliases()) {<br>
StringRef Name = GA.getName();<br>
@@ -178,18 +210,12 @@ void MetaRename(Module &M,<br>
<br>
// Rename all functions<br>
for (auto &F : M) {<br>
- StringRef Name = F.getName();<br>
- LibFunc Tmp;<br>
- // Leave library functions alone because their presence or absence could<br>
- // affect the behavior of other passes.<br>
- if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||<br>
- GetTLI(F).getLibFunc(F, Tmp) ||<br>
- IsNameExcluded(Name, ExcludedFuncPrefixes))<br>
+ if (ExcludeLibFuncs(F))<br>
continue;<br>
<br>
// Leave @main alone. The output of -metarenamer might be passed to<br>
// lli for execution and the latter needs a main entry point.<br>
- if (Name != "main")<br>
+ if (F.getName() != "main")<br>
F.setName(renamer.newName());<br>
<br>
MetaRename(F);<br>
<br>
diff --git a/llvm/test/Transforms/MetaRenamer/only-inst.ll b/llvm/test/Transforms/MetaRenamer/only-inst.ll<br>
new file mode 100644<br>
index 00000000000000..8df7e1a0bc5db4<br>
--- /dev/null<br>
+++ b/llvm/test/Transforms/MetaRenamer/only-inst.ll<br>
@@ -0,0 +1,30 @@<br>
+; RUN: opt -passes=metarenamer -rename-only-inst=1 -S < %s | FileCheck %s<br>
+target triple = "x86_64-pc-linux-gnu"<br>
+<br>
+; CHECK: %struct.foo_xxx = type { i32, float, %struct.bar_xxx }<br>
+; CHECK: %struct.bar_xxx = type { i32, double }<br>
+%struct.bar_xxx = type { i32, double }<br>
+%struct.foo_xxx = type { i32, float, %struct.bar_xxx }<br>
+<br>
+; CHECK: @global_3_xxx = common global<br>
+@global_3_xxx = common global i32 0, align 4<br>
+<br>
+; CHECK-LABEL: func_4_xxx<br>
+; CHECK-NOT: %1<br>
+; CHECK-NOT: %2<br>
+; CHECK-NOT: %3<br>
+; CHECK-NOT: %4<br>
+define void @func_4_xxx(ptr sret(%struct.foo_xxx) %agg.result) nounwind uwtable ssp {<br>
+ %1 = alloca %struct.foo_xxx, align 8<br>
+ store i32 1, ptr %1, align 4<br>
+ %2 = getelementptr inbounds %struct.foo_xxx, ptr %1, i32 0, i32 1<br>
+ store float 2.000000e+00, ptr %2, align 4<br>
+ %3 = getelementptr inbounds %struct.foo_xxx, ptr %1, i32 0, i32 2<br>
+ store i32 3, ptr %3, align 4<br>
+ %4 = getelementptr inbounds %struct.bar_xxx, ptr %3, i32 0, i32 1<br>
+ store double 4.000000e+00, ptr %4, align 8<br>
+ call void @llvm.memcpy.p0.p0.i64(ptr align 8 %agg.result, ptr align 8 %1, i64 24, i1 false)<br>
+ ret void<br>
+}<br>
+<br>
+declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture, i64, i1) nounwind<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>