Skip to contentSkip to conversation

branches are expensive

Qemu-devel Steffen Liebergeld 5 participants 14 messages Mar 17, 2009 – Mar 19, 2009

lore
Share & tools
lore
#0 lore raw

Hi,

while measuring the execution of an ARM guest, I encountered that branches are extremely expensive in terms of executed host instructions. A single ARM branch takes 148 to 152 host instructions. In my setup host and guest use the ARM instruction set architecture.

My question is what makes branches so expensive? What code is run when executing a branch? Other instructions are translated to 14 to 40 instructions.

Any help is appreciated.

Greetings, Steffen

#1 lore raw
Quoted 11 lines · Steffen Liebergeld

Hi,

while measuring the execution of an ARM guest, I encountered that branches are extremely expensive in terms of executed host instructions. A single ARM branch takes 148 to 152 host instructions. In my setup host and guest use the ARM instruction set architecture.

My question is what makes branches so expensive? What code is run when executing a branch? Other instructions are translated to 14 to 40 instructions.

I'm no tcg guru, but if branches are not chained, you'd need an expensive lookup to find the next translation block. If branches are chained they'll probably be much faster.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Steffen Liebergeld

Hi,

while measuring the execution of an ARM guest, I encountered that branches are
extremely expensive in terms of executed host instructions. A single ARM branch takes 148 to 152 host instructions. In my setup host and guest use the
ARM instruction set architecture.

Quoted 8 lines
Quoted 4 lines

My question is what makes branches so expensive? What code is run when executing a branch? Other instructions are translated to 14 to 40 instructions.

I'm no tcg guru, but if branches are not chained, you'd need an expensive lookup to find the next translation block. If branches are chained they'll probably be much faster.

That is probably the reason.

You can check to generated host code and compare it to the guest code via -d in_asm,out_asm (or via the monitor: log in_asm,out_asm), logs will be written /tmp/qemu.log by default. The ratio of direct (chained) jumps is given via "info jit".

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2
Corporate Competence Center Embedded Linux
#3 lore raw

Hi,

Quoted 20 lines · Jan Kiszka
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Steffen Liebergeld

Hi,

while measuring the execution of an ARM guest, I encountered that branches are
extremely expensive in terms of executed host instructions. A single ARM branch takes 148 to 152 host instructions. In my setup host and guest use the
ARM instruction set architecture.

Quoted 8 lines
Quoted 4 lines

My question is what makes branches so expensive? What code is run when executing a branch? Other instructions are translated to 14 to 40 instructions.

I'm no tcg guru, but if branches are not chained, you'd need an expensive lookup to find the next translation block. If branches are chained they'll probably be much faster.

Quoted 6 lines

That is probably the reason.

You can check to generated host code and compare it to the guest code via -d in_asm,out_asm (or via the monitor: log in_asm,out_asm), logs will be written /tmp/qemu.log by default. The ratio of direct (chained) jumps is given via "info jit".

The radio is quite bad. Do you have any documentation on when Qemu does the chaining and more important, when it does not. For example are unconditional jumps always chained, or only in one direction (forward or backward).

Many thanks.
Steffen

#4 lore raw
Quoted 4 lines

The ratio is quite bad. Do you have any documentation on when Qemu does the chaining and more important, when it does not. For example are unconditional jumps always chained, or only in one direction (forward or backward).

Direct jumps[1] within the same page are chained (including ). Indirect jumps[2] and direct jumps to a different page are not chained. Chaining jumps between pages would require breaking TB chains every time a TLB flush occurs.

Unchained jumps just a two stage lookup to cache frequently used entries.

Paul

[1] b, b<cc> and bl
[2] bx, mov pc, ldr pc, pop {pc}

#5 lore raw
Quoted 8 lines · Paul Brook
Quoted 5 lines

The ratio is quite bad. Do you have any documentation on when Qemu does the chaining and more important, when it does not. For example are unconditional jumps always chained, or only in one direction (forward or backward).

Direct jumps[1] within the same page are chained (including ). Indirect jumps[2] and direct jumps to a different page are not chained. Chaining jumps between pages would require breaking TB chains every time a TLB flush occurs.

You could optimize interpage direct jumps as follows:

   if (tb->tlb_generation != global_tlb_generation)
       revalidate_interpage_branch();
   asm ("B target_address")

A tlb flush (or switching execution to a different cpu) increments global_tlb_generation; revalidate_interpage_branch() sets target_address to the slow path which does the tb lookup, and sets tlb_generation = global_tlb_generation. Should compile an unconditional branch to 5 instructions.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
In reply to Paul Brook

Hi Paul,

Quoted 8 lines · Paul Brook
Quoted 4 lines

The ratio is quite bad. Do you have any documentation on when Qemu does the chaining and more important, when it does not. For example are unconditional jumps always chained, or only in one direction (forward or backward).

Direct jumps[1] within the same page are chained (including ). Indirect jumps[2] and direct jumps to a different page are not chained. Chaining jumps between pages would require breaking TB chains every time a TLB flush occurs.

I've tested Qemu 0.10.0 and with i386-softmmu on a i386 host I get the following numbers:
direct jump count 70%, 2 jumps 54%

For qemu-system-arm on an ARM host, the numbers look like this:
direct jump count 47%, 2 jumps 40%

For completeness I tested qemu-system-arm on a i386 host as well:
direct jump count 44%, 2 jumps 37%

So it looks like the chaining on ARM targets is not as effective as on i386 targets (regardless of the guest, I used the same guest setup, compiled for different architectures, on all tests). Do you have any ideas why this is the case?

Quoted 1 line

Unchained jumps just a two stage lookup to cache frequently used entries.

Many thanks for your explanations.

Quoted 4 lines

Paul

[1] b, b<cc> and bl
[2] bx, mov pc, ldr pc, pop {pc}

Greetings, Steffen

#7 lore raw
Quoted 8 lines · Steffen Liebergeld

I've tested Qemu 0.10.0 and with i386-softmmu on a i386 host I get the following numbers:
direct jump count 70%, 2 jumps 54%

For qemu-system-arm on an ARM host, the numbers look like this:
direct jump count 47%, 2 jumps 40%

Quoted 7 lines

For completeness I tested qemu-system-arm on a i386 host as well:
direct jump count 44%, 2 jumps 37%

So it looks like the chaining on ARM targets is not as effective as on i386 targets (regardless of the guest, I used the same guest setup, compiled for different architectures, on all tests). Do you have any ideas why this is the case?

Different instruction sets, different compilers. You'd better compare guest code before drawing any conclusion.

Laurent

#8 lore raw
Quoted 12 lines · Laurent Desnogues
Quoted 11 lines · Steffen Liebergeld

I've tested Qemu 0.10.0 and with i386-softmmu on a i386 host I get the following numbers:
direct jump count 70%, 2 jumps 54%

For qemu-system-arm on an ARM host, the numbers look like this:
direct jump count 47%, 2 jumps 40%

For completeness I tested qemu-system-arm on a i386 host as well:
direct jump count 44%, 2 jumps 37%

Quoted 7 lines
Quoted 4 lines

So it looks like the chaining on ARM targets is not as effective as on i386 targets (regardless of the guest, I used the same guest setup, compiled for different architectures, on all tests). Do you have any ideas why this is the case?

Different instruction sets, different compilers. You'd better compare guest code before drawing any conclusion.

The qemu-system-arm on ARM and i386 were compiled by the same compiler.

Greetings, Steffen

#9 lore raw
Quoted 18 lines · Steffen Liebergeld
Quoted 17 lines · Laurent Desnogues
Quoted 15 lines · Steffen Liebergeld

I've tested Qemu 0.10.0 and with i386-softmmu on a i386 host I get the following numbers:
direct jump count 70%, 2 jumps 54%

For qemu-system-arm on an ARM host, the numbers look like this:
direct jump count 47%, 2 jumps 40%

For completeness I tested qemu-system-arm on a i386 host as well:
direct jump count 44%, 2 jumps 37%

So it looks like the chaining on ARM targets is not as effective as on i386 targets (regardless of the guest, I used the same guest setup, compiled for different architectures, on all tests). Do you have any ideas why this is the case?

Quoted 4 lines
Quoted 2 lines

Different instruction sets, different compilers.  You'd better compare guest code before drawing any conclusion.

The qemu-system-arm on ARM and i386 were compiled by the same compiler.

I said "guest" not "host" :)

You need to compare ARM and i386 blocks *before* translation.

Laurent

#10 lore raw
In reply to Steffen Liebergeld
Quoted 7 lines · Steffen Liebergeld

I've tested Qemu 0.10.0 and with i386-softmmu on a i386 host I get the following numbers:
direct jump count 70%, 2 jumps 54%

For qemu-system-arm on an ARM host, the numbers look like this:
direct jump count 47%, 2 jumps 40%

Quoted 8 lines

For completeness I tested qemu-system-arm on a i386 host as well:
direct jump count 44%, 2 jumps 37%

So it looks like the chaining on ARM targets is not as effective as on i386 targets (regardless of the guest, I used the same guest setup, compiled for different architectures, on all tests). Do you have any ideas why this is the case?

I'd guess that predicated instructions are heavily used on ARM for if/else sequences, so intra-page branches would be less frequent.

Signature · 1 line
error compiling committee.c: too many arguments to function
#11 lore raw
In reply to Steffen Liebergeld
Quoted 11 lines

I've tested Qemu 0.10.0 and with i386-softmmu on a i386 host I get the following numbers:
direct jump count 70%, 2 jumps 54%

For completeness I tested qemu-system-arm on a i386 host as well:
direct jump count 44%, 2 jumps 37%

So it looks like the chaining on ARM targets is not as effective as on i386 targets (regardless of the guest, I used the same guest setup, compiled for different architectures, on all tests). Do you have any ideas why this is the case?

A couple of likely reasons:

- ARM uses 1k pages, i386 uses 4k pages, so there's greater probability of spanning a page boundary.
- ARM has conditional execution, so code will tend to have less conditional branches. The number of function calls (in particular function returns, which are indirect branches) is likely to be about the same, so the proportion of direct jumps is less.

Paul

#12 lore raw
In reply to Avi Kivity

Hi Avi,

Quoted 8 lines · Avi Kivity
Quoted 7 lines · Steffen Liebergeld

Hi,

while measuring the execution of an ARM guest, I encountered that branches are extremely expensive in terms of executed host instructions. A single ARM branch takes 148 to 152 host instructions. In my setup host and guest use the ARM instruction set architecture.

Quoted 8 lines
Quoted 4 lines

My question is what makes branches so expensive? What code is run when executing a branch? Other instructions are translated to 14 to 40 instructions.

I'm no tcg guru, but if branches are not chained, you'd need an expensive lookup to find the next translation block. If branches are chained they'll probably be much faster.

Sure. But how can I get to know how many TBs are chained.

Greetings, Steffen

#13 lore raw
In reply to Steffen Liebergeld
Quoted 9 lines · Steffen Liebergeld

while measuring the execution of an ARM guest, I encountered that branches are extremely expensive in terms of executed host instructions. A single ARM branch takes 148 to 152 host instructions. In my setup host and guest use the ARM instruction set architecture.

My question is what makes branches so expensive? What code is run when executing a branch? Other instructions are translated to 14 to 40 instructions.

This raises a few questions:

1. are you talking of qemu system or qemu user (I guess the former)?
2. how did you measure executed host instructions?
3. what is your host processor?

My experience on TB for qemu user is about x7 code *size* expansion on a x86_64 (though svn qemu is probably higher as I have a few specific tunings).

Laurent