[llvm] 6f9e743 - [Metarenamer] Introduce option to only change inst names

Anna Thomas via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 30 08:13:32 PDT 2023


Author: Anna Thomas
Date: 2023-06-30T11:13:15-04:00
New Revision: 6f9e743b91ad6ac1f333ce6e3efae44e137b54bb

URL: https://github.com/llvm/llvm-project/commit/6f9e743b91ad6ac1f333ce6e3efae44e137b54bb
DIFF: https://github.com/llvm/llvm-project/commit/6f9e743b91ad6ac1f333ce6e3efae44e137b54bb.diff

LOG: [Metarenamer] Introduce option to only change inst names

This is useful when needing to modify IR and test some optimizations on
them, while keeping BB names and function names intact. If
the IR uses ordered number naming (%1, %2, %3 etc), then we cannot just
remove or reorder specific instructions since the verifier expects the
numbers to be in order.

Added: 
    llvm/test/Transforms/MetaRenamer/only-inst.ll

Modified: 
    llvm/lib/Transforms/Utils/MetaRenamer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/MetaRenamer.cpp b/llvm/lib/Transforms/Utils/MetaRenamer.cpp
index 06ccbf888aa300..4053e78c604e64 100644
--- a/llvm/lib/Transforms/Utils/MetaRenamer.cpp
+++ b/llvm/lib/Transforms/Utils/MetaRenamer.cpp
@@ -26,6 +26,7 @@
 #include "llvm/IR/GlobalAlias.h"
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/IR/Instruction.h"
+#include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/Type.h"
@@ -59,6 +60,11 @@ static cl::opt<std::string> RenameExcludeStructPrefixes(
              "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",
@@ -102,6 +108,12 @@ parseExcludedPrefixes(StringRef PrefixesStr,
   }
 }
 
+void MetaRenameOnlyInstructions(Function &F) {
+  for (auto &I : instructions(F))
+    if (!I.getType()->isVoidTy())
+      I.setName(I.getOpcodeName());
+}
+
 void MetaRename(Function &F) {
   for (Argument &Arg : F.args())
     if (!Arg.getType()->isVoidTy())
@@ -142,6 +154,26 @@ void MetaRename(Module &M,
                   [&Name](auto &Prefix) { return Name.startswith(Prefix); });
   };
 
+  // Leave library functions alone because their presence or absence could
+  // affect the behavior of other passes.
+  auto ExcludeLibFuncs = [&](Function &F) {
+    LibFunc Tmp;
+    StringRef Name = F.getName();
+    return Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
+           GetTLI(F).getLibFunc(F, Tmp) ||
+           IsNameExcluded(Name, ExcludedFuncPrefixes);
+  };
+
+  if (RenameOnlyInst) {
+    // Rename all functions
+    for (auto &F : M) {
+      if (ExcludeLibFuncs(F))
+        continue;
+      MetaRenameOnlyInstructions(F);
+    }
+    return;
+  }
+
   // Rename all aliases
   for (GlobalAlias &GA : M.aliases()) {
     StringRef Name = GA.getName();
@@ -178,18 +210,12 @@ void MetaRename(Module &M,
 
   // Rename all functions
   for (auto &F : M) {
-    StringRef Name = F.getName();
-    LibFunc Tmp;
-    // Leave library functions alone because their presence or absence could
-    // affect the behavior of other passes.
-    if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
-        GetTLI(F).getLibFunc(F, Tmp) ||
-        IsNameExcluded(Name, ExcludedFuncPrefixes))
+    if (ExcludeLibFuncs(F))
       continue;
 
     // Leave @main alone. The output of -metarenamer might be passed to
     // lli for execution and the latter needs a main entry point.
-    if (Name != "main")
+    if (F.getName() != "main")
       F.setName(renamer.newName());
 
     MetaRename(F);

diff  --git a/llvm/test/Transforms/MetaRenamer/only-inst.ll b/llvm/test/Transforms/MetaRenamer/only-inst.ll
new file mode 100644
index 00000000000000..8df7e1a0bc5db4
--- /dev/null
+++ b/llvm/test/Transforms/MetaRenamer/only-inst.ll
@@ -0,0 +1,30 @@
+; RUN: opt -passes=metarenamer -rename-only-inst=1 -S < %s | FileCheck %s
+target triple = "x86_64-pc-linux-gnu"
+
+; CHECK: %struct.foo_xxx = type { i32, float, %struct.bar_xxx }
+; CHECK: %struct.bar_xxx = type { i32, double }
+%struct.bar_xxx = type { i32, double }
+%struct.foo_xxx = type { i32, float, %struct.bar_xxx }
+
+; CHECK: @global_3_xxx = common global
+ at global_3_xxx = common global i32 0, align 4
+
+; CHECK-LABEL: func_4_xxx
+; CHECK-NOT: %1
+; CHECK-NOT: %2
+; CHECK-NOT: %3
+; CHECK-NOT: %4
+define void @func_4_xxx(ptr sret(%struct.foo_xxx) %agg.result) nounwind uwtable ssp {
+  %1 = alloca %struct.foo_xxx, align 8
+  store i32 1, ptr %1, align 4
+  %2 = getelementptr inbounds %struct.foo_xxx, ptr %1, i32 0, i32 1
+  store float 2.000000e+00, ptr %2, align 4
+  %3 = getelementptr inbounds %struct.foo_xxx, ptr %1, i32 0, i32 2
+  store i32 3, ptr %3, align 4
+  %4 = getelementptr inbounds %struct.bar_xxx, ptr %3, i32 0, i32 1
+  store double 4.000000e+00, ptr %4, align 8
+  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %agg.result, ptr align 8 %1, i64 24, i1 false)
+  ret void
+}
+
+declare void @llvm.memcpy.p0.p0.i64(ptr nocapture, ptr nocapture, i64, i1) nounwind


        


More information about the llvm-commits mailing list