[llvm-dev] Ignored branch predictor hints

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] Ignored branch predictor hints

Sam McCall via llvm-dev
Hello,
#define likely(x) __builtin_expect((x),1)

// switch like
char * b(int e) {
if (likely(e == 0))
return "0";
else if (e == 1)
return "1";
else return "f";
}
GCC correctly prefers the first case:
b(int):
mov eax, OFFSET FLAT:.LC0
test edi, edi
jne .L7
ret

But Clang seems to ignore _builtin_expect hints in this case.
b(int): # @b(int)
cmp edi, 1
mov eax, offset .L.str.1
mov ecx, offset .L.str.2
cmove rcx, rax
test edi, edi
mov eax, offset .L.str
cmovne rax, rcx
ret
https://godbolt.org/g/tuAVT7

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Ignored branch predictor hints

Sam McCall via llvm-dev
I’d wager that the if-else chain is being converted to a "switch statement” during an optimization pass and the __builtin_expect() hint is lost. Can you file a bug? https://bugs.llvm.org


On May 9, 2018, at 1:57 PM, Dávid Bolvanský via llvm-dev <[hidden email]> wrote:

Hello,
#define likely(x) __builtin_expect((x),1)

// switch like
char * b(int e) {
if (likely(e == 0))
return "0";
else if (e == 1)
return "1";
else return "f";
}
GCC correctly prefers the first case:
b(int):
mov eax, OFFSET FLAT:.LC0
test edi, edi
jne .L7
ret

But Clang seems to ignore _builtin_expect hints in this case.
b(int): # @b(int)
cmp edi, 1
mov eax, offset .L.str.1
mov ecx, offset .L.str.2
cmove rcx, rax
test edi, edi
mov eax, offset .L.str
cmovne rax, rcx
ret
https://godbolt.org/g/tuAVT7
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Ignored branch predictor hints

Sam McCall via llvm-dev

2018-05-09 20:33 GMT+02:00 Dávid Bolvanský <[hidden email]>:

2018-05-09 20:29 GMT+02:00 David Zarzycki <[hidden email]>:
I’d wager that the if-else chain is being converted to a "switch statement” during an optimization pass and the __builtin_expect() hint is lost. Can you file a bug? https://bugs.llvm.org


On May 9, 2018, at 1:57 PM, Dávid Bolvanský via llvm-dev <[hidden email]> wrote:

Hello,
#define likely(x) __builtin_expect((x),1)

// switch like
char * b(int e) {
if (likely(e == 0))
return "0";
else if (e == 1)
return "1";
else return "f";
}
GCC correctly prefers the first case:
b(int):
mov eax, OFFSET FLAT:.LC0
test edi, edi
jne .L7
ret

But Clang seems to ignore _builtin_expect hints in this case.
b(int): # @b(int)
cmp edi, 1
mov eax, offset .L.str.1
mov ecx, offset .L.str.2
cmove rcx, rax
test edi, edi
mov eax, offset .L.str
cmovne rax, rcx
ret
https://godbolt.org/g/tuAVT7
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev




_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Ignored branch predictor hints

Sam McCall via llvm-dev
Hi Dávid,

Looks like you can defeat the switch conversion by adding a dummy asm(“”):

#define likely(x)       __builtin_expect((x),1)

// switch like
char * b(int e) {
    if (likely(e == 0))
        return "0";
    asm("");
    if (e == 1)
        return "1";
    else return "f";
}

Dave

On May 9, 2018, at 2:33 PM, Dávid Bolvanský via llvm-dev <[hidden email]> wrote:


2018-05-09 20:33 GMT+02:00 Dávid Bolvanský <[hidden email]>:

2018-05-09 20:29 GMT+02:00 David Zarzycki <[hidden email]>:
I’d wager that the if-else chain is being converted to a "switch statement” during an optimization pass and the __builtin_expect() hint is lost. Can you file a bug? https://bugs.llvm.org


On May 9, 2018, at 1:57 PM, Dávid Bolvanský via llvm-dev <[hidden email]> wrote:

Hello,
#define likely(x) __builtin_expect((x),1)

// switch like
char * b(int e) {
if (likely(e == 0))
return "0";
else if (e == 1)
return "1";
else return "f";
}
GCC correctly prefers the first case:
b(int):
mov eax, OFFSET FLAT:.LC0
test edi, edi
jne .L7
ret

But Clang seems to ignore _builtin_expect hints in this case.
b(int): # @b(int)
cmp edi, 1
mov eax, offset .L.str.1
mov ecx, offset .L.str.2
cmove rcx, rax
test edi, edi
mov eax, offset .L.str
cmovne rax, rcx
ret
https://godbolt.org/g/tuAVT7
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Ignored branch predictor hints

Sam McCall via llvm-dev
Thanks, interesting.

But a fix needs to be made since branch predictor hints are broken in a valid C++20 code:

Dňa st 9. 5. 2018, 20:40 David Zarzycki <[hidden email]> napísal(a):
Hi Dávid,

Looks like you can defeat the switch conversion by adding a dummy asm(“”):

#define likely(x)       __builtin_expect((x),1)

// switch like
char * b(int e) {
    if (likely(e == 0))
        return "0";
    asm("");
    if (e == 1)
        return "1";
    else return "f";
}

Dave

On May 9, 2018, at 2:33 PM, Dávid Bolvanský via llvm-dev <[hidden email]> wrote:


2018-05-09 20:33 GMT+02:00 Dávid Bolvanský <[hidden email]>:

2018-05-09 20:29 GMT+02:00 David Zarzycki <[hidden email]>:
I’d wager that the if-else chain is being converted to a "switch statement” during an optimization pass and the __builtin_expect() hint is lost. Can you file a bug? https://bugs.llvm.org


On May 9, 2018, at 1:57 PM, Dávid Bolvanský via llvm-dev <[hidden email]> wrote:

Hello,
#define likely(x) __builtin_expect((x),1)

// switch like
char * b(int e) {
if (likely(e == 0))
return "0";
else if (e == 1)
return "1";
else return "f";
}
GCC correctly prefers the first case:
b(int):
mov eax, OFFSET FLAT:.LC0
test edi, edi
jne .L7
ret

But Clang seems to ignore _builtin_expect hints in this case.
b(int): # @b(int)
cmp edi, 1
mov eax, offset .L.str.1
mov ecx, offset .L.str.2
cmove rcx, rax
test edi, edi
mov eax, offset .L.str
cmovne rax, rcx
ret
https://godbolt.org/g/tuAVT7
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Ignored branch predictor hints

Sam McCall via llvm-dev
On 9 May 2018 at 19:48, Dávid Bolvanský via llvm-dev
<[hidden email]> wrote:
> But a fix needs to be made since branch predictor hints are broken in a
> valid C++20 code:

They don't affect performance in the expected way, but they also don't
actually break code. I'm not saying it's not a bug, but it's certainly
not on the same level as a miscompilation.

Tim.
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] Ignored branch predictor hints

Sam McCall via llvm-dev
Yes, the compiler may ignore attributes in [[ ]] but I believe Clang/LLVM wants to support it.

2018-05-09 21:02 GMT+02:00 Tim Northover <[hidden email]>:
On 9 May 2018 at 19:48, Dávid Bolvanský via llvm-dev
<[hidden email]> wrote:
> But a fix needs to be made since branch predictor hints are broken in a
> valid C++20 code:

They don't affect performance in the expected way, but they also don't
actually break code. I'm not saying it's not a bug, but it's certainly
not on the same level as a miscompilation.

Tim.


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev