labels commented out

I happened to try building LLVM without assertions today,
and it changed the form of the LLVM IR that gets generated.
Typically, I do something like

clang -O -emit-llvm -S Test.c

For source like

typedef struct node {
struct node *left;
struct node *right;
int key;
} Tree;

int sum(Tree *p) {
int s = 0;
if (p) {
s += sum(p->left);
s += sum(p->right);
s += p->key;
}
return s;
}

The resulting Test.s looks like

define i32 @sum(%struct.node* %p) #0 {

%1 = icmp eq %struct.node* %p, null

br i1 %1, label %13, label %2

; :2 ; preds = %0

%3 = getelementptr inbounds %struct.node* %p, i64 0, i32 0

%4 = load %struct.node** %3, align 8, !tbaa !0

%5 = tail call i32 @sum(%struct.node* %4)

%6 = getelementptr inbounds %struct.node* %p, i64 0, i32 1

%7 = load %struct.node** %6, align 8, !tbaa !0

%8 = tail call i32 @sum(%struct.node* %7)

%9 = add nsw i32 %8, %5

%10 = getelementptr inbounds %struct.node* %p, i64 0, i32 2

%11 = load i32* %10, align 4, !tbaa !3

%12 = add nsw i32 %9, %11

ret i32 %12

; :13 ; preds = %0

ret i32 0

}

Note that labels %2 and %13 are not actually defined, though they’re both referred to by the first branch and in comments.

What’s the deal?

Thanks,
Preston

Hi Preston,

Note that labels %2 and %13 are not actually defined, though they're both
referred to by the first branch and in comments.

What's the deal?

Basic blocks (like instructions) mostly only get textual labels when
Clang has been built with assertions enabled. Without that they
implicitly pick up the next numeric value that hasn't been defined
yet, almost like an instruction. (In your example %2 follows the
definition of %1 and %13 follows the definition of %12).

You can actually do similar tricks with normal instructions:

define i32 @foo(i32 %in) {
  add i32 %in, %in
  ret i32 %1
}

it's just the default for labels for some reason.

Other than that it's just LLVM adding some (hopefully useful to
someone) comments when printing out the function.

Cheers.

Tim.

BB labels are implicitly assigned based on their position in the function.

This has come up before <http://thread.gmane.org/gmane.comp.compilers.llvm.devel/62194/>, <http://thread.gmane.org/gmane.comp.compilers.llvm.cvs/145768/>, r182332.

IIRC the only reason the label is in comments is that syntactically it’s not possible to put a numeric label name there. Otherwise just change AssemblyWriter::printBasicBlock (in lib/IR/AsmWriter.cpp) to print out a proper numeric label name instead of putting it in a comment.

– Sean Silva