Question about jumptable and indirect function call.

Hi all,

I’m a beginner in LLVM. I read a paper recently, and I’m trying to use LLVM jumptable in 3.5. When I compile the .bc file into .s file, I tried to use the different jumptable type: all, single, …

e.g.
clang -c -emit-llvm test.c
llc test.bc -jump-table-type=full -o test-full.s
llc test.bc -jump-table-type=single -o test-single.s

The tested C source code is like:

void foo() {
printf(“foo…\n”);
}

void bar() {
printf(“bar…\n”);
}

void foo1(int arg) {
printf(“foo1… %d…\n”, arg);
}

void bar1(int arg) {
printf(“bar1… %d…\n”, arg);
}

// function pointer
void (*fp)() = 0;
void (*fp1)(int) = 0;

int main(int argc, char* argv[])
{
int input = 0;

printf(“Hello\n”);
fp = foo;
fp();
fp = bar;
fp();
fp1 = foo1;
fp1(1);
fp1 = bar1;
fp1(2);

}

However, they produced the same .s assembly (test-full.s, test-single.s). I think the llc will produce difference .s files, since I choose the jumptable options. And according to my understanding, the jumptable contains the destination address of each indirect function call.
So what’s going on with my test. Is there something I did wrong with jumptable options? Or is the jumptable tried to address other problem, not indirect call?

Thanks!

Sincerely,

Xiaoguang Wang

Hi,

So what's going on with my test. Is there something I did wrong with
jumptable options? Or is the jumptable tried to address other problem, not
indirect call?

I think LLVM's jump table pass requires explicit function annotations
in the IR for functions that should go via the table, which Clang
doesn't create. Something like this in its simplest form:

define void @foo() unnamed_addr jumptable {
  ret void
}

define void @main() {
  %var = alloca void()*
  store void()* @foo, void()** %var
  %func = load void()** %var
  call void %func()
  ret void
}

Cheers.

Tim.

Thanks Tim,

Yes, that's the right patch to use. You might also want to look at the
test cases in, e.g., test/CodeGen/X86/jump_table*.ll to see the code that
is generated by the annotations. I'd be happy to answer other questions you
might have.