Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UBSan safe leading/trailing zero counts can't simplify to zero-input correct variants #49484

Closed
RKSimon opened this issue Apr 27, 2021 · 3 comments
Labels
bugzilla Issues migrated from bugzilla

Comments

@RKSimon
Copy link
Collaborator

RKSimon commented Apr 27, 2021

Bugzilla Link 50140
Resolution FIXED
Resolved on Jun 22, 2021 09:47
Version trunk
OS Windows NT
CC @davidbolvansky,@LebedevRI,@nikic,@rotateright

Extended Description

https://simd.godbolt.org/z/KrahKsdhe

clang -g0 -O3 -march=haswell

#include
#include

auto lz(unsigned x) {
int c = __builtin_clz( x );
return x ? c : sizeof(x) * CHAR_BIT;
}

auto tz(unsigned x) {
int c = __builtin_ctz( x );
return x ? c : sizeof(x) * CHAR_BIT;
}

lz:
lzcntl %edi, %eax
retq

tz:
tzcntl %edi, %eax
retq

auto lz_ubsan(unsigned x) {
int c = __builtin_clz( x ? x : ~0U );
return x ? c : sizeof(x) * CHAR_BIT;
}

auto tz_ubsan(unsigned x) {
int c = __builtin_ctz( x ? x : ~0U );
return x ? c : sizeof(x) * CHAR_BIT;
}

lz_ubsan:
xorl %eax, %eax
cmpl $1, %edi
sbbl %eax, %eax
orl %edi, %eax
lzcntl %eax, %ecx
testl %edi, %edi
movl $32, %eax
cmovnel %ecx, %eax
retq

tz_ubsan:
xorl %eax, %eax
cmpl $1, %edi
sbbl %eax, %eax
orl %edi, %eax
tzcntl %eax, %ecx
testl %edi, %edi
movl $32, %eax
cmovnel %ecx, %eax
retq

To prevent ubsan warnings, we mustn't call the ctz/clz builtins with a zero-value. So we typically insert an all-bits value to make it shutup

Unfortunately this conflicts with the post-combine that recognise cases where the zero-value input should be correctly handled (e.g tzcnt/lzcnt x86 instructions).

Funnily enough, gcc has better code from the ubsan-safe cases.....

@rotateright
Copy link
Contributor

We should try to pull any bit-logic intrinsic ahead of a select with a constant operand.

We don't even optimize the case where both arms are constants:
https://alive2.llvm.org/ce/z/G9Mmww

@rotateright
Copy link
Contributor

@RKSimon
Copy link
Collaborator Author

RKSimon commented Jun 22, 2021

Thanks Sanjay!

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla
Projects
None yet
Development

No branches or pull requests

2 participants