[llvm] llvm-extract now supports unnamed basic blocks. (PR #112850)

Allin Lee via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 18 01:29:29 PDT 2024


https://github.com/AllinLeeYL updated https://github.com/llvm/llvm-project/pull/112850

>From 26c5d93dc949aac9074020f29f564c27abc1564f Mon Sep 17 00:00:00 2001
From: Allin Lee <allinleeme at gmail.com>
Date: Fri, 18 Oct 2024 15:40:51 +0800
Subject: [PATCH 1/3] llvm-extract now supports unnamed basic blocks.

---
 llvm/tools/llvm-extract/llvm-extract.cpp | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/llvm/tools/llvm-extract/llvm-extract.cpp b/llvm/tools/llvm-extract/llvm-extract.cpp
index 5fc9a31ab4ad7c..73123a699dd82f 100644
--- a/llvm/tools/llvm-extract/llvm-extract.cpp
+++ b/llvm/tools/llvm-extract/llvm-extract.cpp
@@ -355,9 +355,23 @@ 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.getNameOrAsOperand() == BBName;
+        });
+#else
         auto Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
           return BB.getName() == BBName;
         });
+        if (Res == P.first->end() && std::isdigit(BBName)) {
+          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;
+          });
+        }
+#endif
         if (Res == P.first->end()) {
           errs() << argv[0] << ": function " << P.first->getName()
                  << " doesn't contain a basic block named '" << BBName

>From 06a656e207635d99e0719c87bbf928ed9f7c769a Mon Sep 17 00:00:00 2001
From: Allin Lee <allinleeme at gmail.com>
Date: Fri, 18 Oct 2024 16:04:41 +0800
Subject: [PATCH 2/3] llvm-extract supports unnamed basic blocks.

---
 llvm/tools/llvm-extract/llvm-extract.cpp | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/llvm/tools/llvm-extract/llvm-extract.cpp b/llvm/tools/llvm-extract/llvm-extract.cpp
index 73123a699dd82f..d28796af3e34d4 100644
--- a/llvm/tools/llvm-extract/llvm-extract.cpp
+++ b/llvm/tools/llvm-extract/llvm-extract.cpp
@@ -359,17 +359,19 @@ int main(int argc, char **argv) {
         auto Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
           return BB.getNameOrAsOperand() == BBName;
         });
-#else
-        auto Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
-          return BB.getName() == BBName;
-        });
-        if (Res == P.first->end() && std::isdigit(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()) {

>From 43581e9521a9920645d57860a8319cd34247ebd8 Mon Sep 17 00:00:00 2001
From: Allin Lee <allinleeme at gmail.com>
Date: Fri, 18 Oct 2024 16:26:43 +0800
Subject: [PATCH 3/3] Add some description.

---
 llvm/tools/llvm-extract/llvm-extract.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/tools/llvm-extract/llvm-extract.cpp b/llvm/tools/llvm-extract/llvm-extract.cpp
index d28796af3e34d4..71ca2eb2487564 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.



More information about the llvm-commits mailing list