[llvm] [llvm-extract] support unnamed bb. (PR #134974)

Allin Lee via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 9 00:08:40 PDT 2025


https://github.com/AllinLeeYL created https://github.com/llvm/llvm-project/pull/134974

Dear maintainer:

I have recently working with LLVM IR and I want to isolate basic blocks using the command "llvm-extract". However, I found that the command option "llvm-extract --bb func_name:bb_name" will only function when dumping source code into IRs with options "-fno-discard-value-names". That is to say, the "llvm-extract" command cannot support unnamed basic blocks, which is a default output of the compiler. So, I made these changes and hope they will make LLVM better.

Best regards,

>From 55721219b29b3a621f5ec90a15cd2d552bd3d7db Mon Sep 17 00:00:00 2001
From: Yilin Li <allinleeme at gmail.com>
Date: Wed, 9 Apr 2025 15:07:54 +0800
Subject: [PATCH] [llvm-extract] support unnamed bb.

---
 llvm/tools/llvm-extract/llvm-extract.cpp | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llvm-extract/llvm-extract.cpp b/llvm/tools/llvm-extract/llvm-extract.cpp
index 169cd0c2e4cbf..0bbde0b218c4a 100644
--- a/llvm/tools/llvm-extract/llvm-extract.cpp
+++ b/llvm/tools/llvm-extract/llvm-extract.cpp
@@ -90,10 +90,13 @@ static cl::list<std::string> ExtractBlocks(
         "Each pair will create a function.\n"
         "If multiple basic blocks are specified in one pair,\n"
         "the first block in the sequence should dominate the rest.\n"
+        "If an unnamed basic block is to be extracted,\n"
+        "'%' should be added before the basic block variable names.\n"
         "eg:\n"
         "  --bb=f:bb1;bb2 will extract one function with both bb1 and bb2;\n"
         "  --bb=f:bb1 --bb=f:bb2 will extract two functions, one with bb1, one "
-        "with bb2."),
+        "with bb2.\n"
+        "  --bb=f:%1 will extract one function with basic block 1;"),
     cl::value_desc("function:bb1[;bb2...]"), cl::cat(ExtractCat));
 
 // ExtractAlias - The alias to extract from the module.
@@ -355,9 +358,25 @@ int main(int argc, char **argv) {
       for (StringRef BBName : P.second) {
         // The function has been materialized, so add its matching basic blocks
         // to the block extractor list, or fail if a name is not found.
+#ifndef NDEBUG
         auto Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
-          return BB.getName() == BBName;
+          return BB.getNameOrAsOperand() == BBName;
         });
+#else   
+        llvm::Function::iterator Res;
+        if (BBName.substr(0, 1) == "%") {
+          Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
+            std::string tmpName;
+            raw_string_ostream OS(tmpName);
+            BB.printAsOperand(OS, false);
+            return OS.str() == BBName;
+          });
+        } else {
+          Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
+            return BB.getName() == BBName;
+          });
+        }
+#endif
         if (Res == P.first->end()) {
           errs() << argv[0] << ": function " << P.first->getName()
                  << " doesn't contain a basic block named '" << BBName



More information about the llvm-commits mailing list