Skip to content

Commit 8a57df6

Browse files
AllinLeeYLYilin Li
andauthored
[llvm-extract] support unnamed bbs. (#135140)
Dear developer: 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, Co-authored-by: Yilin Li <[email protected]>
1 parent 91edbe2 commit 8a57df6

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

llvm/include/llvm/IR/Value.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ class Value {
290290
/// \note It is an error to call V->takeName(V).
291291
void takeName(Value *V);
292292

293-
#ifndef NDEBUG
294293
std::string getNameOrAsOperand() const;
295-
#endif
296294

297295
/// Change all uses of this to point to a new Value.
298296
///

llvm/lib/IR/Value.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ void Value::takeName(Value *V) {
441441
ST->reinsertValue(this);
442442
}
443443

444-
#ifndef NDEBUG
445444
std::string Value::getNameOrAsOperand() const {
446445
if (!getName().empty())
447446
return std::string(getName());
@@ -451,7 +450,6 @@ std::string Value::getNameOrAsOperand() const {
451450
printAsOperand(OS, false);
452451
return OS.str();
453452
}
454-
#endif
455453

456454
void Value::assertModuleIsMaterializedImpl() const {
457455
#ifndef NDEBUG
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: llvm-extract -S --bb "_Z6kernelv.extracted:%5" < %s | FileCheck %s
2+
3+
; CHECK: define dso_local void @_Z6kernelv.extracted.extracted(i64 %0, i64 %1) {
4+
5+
; CHECK 2:
6+
; CHECK: %3 = add nuw nsw i64 %0, 1
7+
; CHECK-NEXT: %4 = sub nuw nsw i64 %3, %1
8+
; CHECK-NEXT: br label %.exitStub
9+
10+
define dso_local void @_Z6kernelv.extracted(i64 %0, ptr %.out) #0 {
11+
newFuncRoot:
12+
br label %1
13+
14+
1:
15+
%2 = phi i64 [ 0, %newFuncRoot ], [ %3, %1 ]
16+
%3 = add nuw nsw i64 %2, 1
17+
%4 = icmp eq i64 %2, %3
18+
br i1 %4, label %5, label %1
19+
20+
5:
21+
%6 = add nuw nsw i64 %0, 1
22+
%7 = sub nuw nsw i64 %6, %3
23+
br label %8
24+
25+
8:
26+
%9 = add nuw i64 %0, 2
27+
ret void
28+
}

llvm/tools/llvm-extract/llvm-extract.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ static cl::list<std::string> ExtractBlocks(
9090
"Each pair will create a function.\n"
9191
"If multiple basic blocks are specified in one pair,\n"
9292
"the first block in the sequence should dominate the rest.\n"
93+
"If an unnamed basic block is to be extracted,\n"
94+
"'%' should be added before the basic block variable names.\n"
9395
"eg:\n"
9496
" --bb=f:bb1;bb2 will extract one function with both bb1 and bb2;\n"
9597
" --bb=f:bb1 --bb=f:bb2 will extract two functions, one with bb1, one "
96-
"with bb2."),
98+
"with bb2.\n"
99+
" --bb=f:%1 will extract one function with basic block 1;"),
97100
cl::value_desc("function:bb1[;bb2...]"), cl::cat(ExtractCat));
98101

99102
// ExtractAlias - The alias to extract from the module.
@@ -356,7 +359,7 @@ int main(int argc, char **argv) {
356359
// The function has been materialized, so add its matching basic blocks
357360
// to the block extractor list, or fail if a name is not found.
358361
auto Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
359-
return BB.getName() == BBName;
362+
return BB.getNameOrAsOperand() == BBName;
360363
});
361364
if (Res == P.first->end()) {
362365
errs() << argv[0] << ": function " << P.first->getName()

0 commit comments

Comments
 (0)