<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/4.6.6">
</HEAD>
<BODY>
On Wed, 2014-05-21 at 18:05 -0400, Ilia Mirkin wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
On Wed, May 21, 2014 at 6:02 PM, Timothy Arceri <<A HREF="mailto:t_arceri@yahoo.com.au">t_arceri@yahoo.com.au</A>> wrote:
<FONT COLOR="#737373">> Unless we run out of memory the old if statement would always fail so reflect the more likely outcome. Should be be faster most of the time and slightly cleaner looking code.</FONT>
<FONT COLOR="#737373">></FONT>
<FONT COLOR="#737373">> Signed-off-by: Timothy Arceri <<A HREF="mailto:t_arceri@yahoo.com.au">t_arceri@yahoo.com.au</A>></FONT>
<FONT COLOR="#737373">> ---</FONT>
<FONT COLOR="#737373">> src/gallium/auxiliary/util/u_upload_mgr.c | 7 +++----</FONT>
<FONT COLOR="#737373">> 1 file changed, 3 insertions(+), 4 deletions(-)</FONT>
<FONT COLOR="#737373">></FONT>
<FONT COLOR="#737373">> diff --git a/src/gallium/auxiliary/util/u_upload_mgr.c b/src/gallium/auxiliary/util/u_upload_mgr.c</FONT>
<FONT COLOR="#737373">> index 744ea2e..99f9a08 100644</FONT>
<FONT COLOR="#737373">> --- a/src/gallium/auxiliary/util/u_upload_mgr.c</FONT>
<FONT COLOR="#737373">> +++ b/src/gallium/auxiliary/util/u_upload_mgr.c</FONT>
<FONT COLOR="#737373">> @@ -247,11 +247,10 @@ enum pipe_error u_upload_data( struct u_upload_mgr *upload,</FONT>
<FONT COLOR="#737373">> enum pipe_error ret = u_upload_alloc(upload, min_out_offset, size,</FONT>
<FONT COLOR="#737373">> out_offset, outbuf,</FONT>
<FONT COLOR="#737373">> (void**)&ptr);</FONT>
<FONT COLOR="#737373">> - if (ret != PIPE_OK)</FONT>
<FONT COLOR="#737373">> - return ret;</FONT>
<FONT COLOR="#737373">> + if (ret == PIPE_OK)</FONT>
<FONT COLOR="#737373">> + memcpy(ptr, data, size);</FONT>
<FONT COLOR="#737373">></FONT>
<FONT COLOR="#737373">> - memcpy(ptr, data, size);</FONT>
<FONT COLOR="#737373">> - return PIPE_OK;</FONT>
<FONT COLOR="#737373">> + return ret;</FONT>
<FONT COLOR="#737373">> }</FONT>
Have you actually compared the generated code? I find it can be fairly
instructive to do so... btw, there's a likely() (and unlikely() )
primitive (in p_compiler.h) which will indicate to gcc whether a
particular condition is likely or unlikely, so that it can adjust its
code generation accordingly.
-ilia
</PRE>
</BLOCKQUOTE>
<BR>
Hi Ilia,<BR>
<BR>
I've taken a look at the results (see below) and read up on likely/unlikely (I was aware of these but wasn't confident in when they should be used).<BR>
<BR>
The result is my patch avoids a jump which I assume should help with branch prediction in the cpu? Something like this is what I was expecting with the change.<BR>
The interesting thing is that in this case using likely seems to have added an extra 'test' instruction for no added value (please correct me if I'm wrong). However there is probably other places in u_upload_alloc(), etc where likely might be more helpful.<BR>
<BR>
Tim<BR>
<BR>
Common code:<BR>
<BR>
444fce: 55 push %rbp<BR>
444fcf: 48 89 e5 mov %rsp,%rbp<BR>
444fd2: 48 83 ec 40 sub $0x40,%rsp<BR>
444fd6: 48 89 7d e8 mov %rdi,-0x18(%rbp)<BR>
444fda: 89 75 e4 mov %esi,-0x1c(%rbp)<BR>
444fdd: 89 55 e0 mov %edx,-0x20(%rbp)<BR>
444fe0: 48 89 4d d8 mov %rcx,-0x28(%rbp)<BR>
444fe4: 4c 89 45 d0 mov %r8,-0x30(%rbp)<BR>
444fe8: 4c 89 4d c8 mov %r9,-0x38(%rbp)<BR>
444fec: 4c 8d 45 f0 lea -0x10(%rbp),%r8<BR>
444ff0: 48 8b 7d c8 mov -0x38(%rbp),%rdi<BR>
444ff4: 48 8b 4d d0 mov -0x30(%rbp),%rcx<BR>
444ff8: 8b 55 e0 mov -0x20(%rbp),%edx<BR>
444ffb: 8b 75 e4 mov -0x1c(%rbp),%esi<BR>
444ffe: 48 8b 45 e8 mov -0x18(%rbp),%rax<BR>
445002: 4d 89 c1 mov %r8,%r9<BR>
445005: 49 89 f8 mov %rdi,%r8<BR>
445008: 48 89 c7 mov %rax,%rdi<BR>
44500b: e8 78 fd ff ff callq 444d88 <u_upload_alloc><BR>
445010: 89 45 fc mov %eax,-0x4(%rbp)<BR>
445013: 83 7d fc 00 cmpl $0x0,-0x4(%rbp)<BR>
<BR>
Pre change:<BR>
<BR>
0000000000444fce <u_upload_data>:<BR>
445017: 74 05 je 44501e <u_upload_data+0x50><BR>
445019: 8b 45 fc mov -0x4(%rbp),%eax<BR>
44501c: eb 1b jmp 445039 <u_upload_data+0x6b><BR>
44501e: 8b 55 e0 mov -0x20(%rbp),%edx<BR>
445021: 48 8b 45 f0 mov -0x10(%rbp),%rax<BR>
445025: 48 8b 4d d8 mov -0x28(%rbp),%rcx<BR>
445029: 48 89 ce mov %rcx,%rsi<BR>
44502c: 48 89 c7 mov %rax,%rdi<BR>
44502f: e8 2c 40 c0 ff callq 49060 <memcpy@plt><BR>
445034: b8 00 00 00 00 mov $0x0,%eax<BR>
445039: c9 leaveq <BR>
44503a: c3 retq <BR>
<BR>
<BR>
Post change:<BR>
<BR>
0000000000444fce <u_upload_data>:<BR>
445017: 75 16 jne 44502f <u_upload_data+0x61><BR>
445019: 8b 55 e0 mov -0x20(%rbp),%edx<BR>
44501c: 48 8b 45 f0 mov -0x10(%rbp),%rax<BR>
445020: 48 8b 4d d8 mov -0x28(%rbp),%rcx<BR>
445024: 48 89 ce mov %rcx,%rsi<BR>
445027: 48 89 c7 mov %rax,%rdi<BR>
44502a: e8 31 40 c0 ff callq 49060 <memcpy@plt><BR>
44502f: 8b 45 fc mov -0x4(%rbp),%eax<BR>
445032: c9 leaveq <BR>
445033: c3 retq <BR>
<BR>
<BR>
With likely:<BR>
<BR>
0000000000444fce <u_upload_data>:<BR>
44501d: 48 85 c0 test %rax,%rax<BR>
445020: 74 16 je 445038 <u_upload_data+0x6a><BR>
445022: 8b 55 e0 mov -0x20(%rbp),%edx<BR>
445025: 48 8b 45 f0 mov -0x10(%rbp),%rax<BR>
445029: 48 8b 4d d8 mov -0x28(%rbp),%rcx<BR>
44502d: 48 89 ce mov %rcx,%rsi<BR>
445030: 48 89 c7 mov %rax,%rdi<BR>
445033: e8 28 40 c0 ff callq 49060 <memcpy@plt><BR>
445038: 8b 45 fc mov -0x4(%rbp),%eax<BR>
44503b: c9 leaveq <BR>
44503c: c3 retq<BR>
<BR>
</BODY>
</HTML>