Skip to content

[libclc] Move logb/ilogb to CLC library; optimize #128028

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

Merged
merged 2 commits into from
May 13, 2025

Conversation

frasercrmck
Copy link
Contributor

This commit moves the logb and ilogb builtins to the CLC library.

It simultaneously optimizes them both for vector types and for half types. Vector types were being scalarized in some cases. Half types were previously promoting to float, whereas this commit provides them a native implementation.

Everything passes the OpenCL-CTS.

I had to intuit some magic numbers used by these implementations in order to generate the half variants. I gave them clearer definitions derived from what I believe are their actual component numbers, but named them 'magic' to convey that they weren't derived from first principles.

@frasercrmck frasercrmck added the libclc libclc OpenCL library label Feb 20, 2025
@frasercrmck frasercrmck requested a review from arsenm February 20, 2025 16:36
#if __CLC_FPSIZE == 32

_CLC_OVERLOAD _CLC_DEF __CLC_INTN __clc_ilogb(__CLC_GENTYPE x) {
__CLC_UINTN ux = __CLC_AS_UINTN(x);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be implemented in terms of frexp and avoid relying on the exponent bits.

Something like:

int ilogb(float x) {
    int result;
    (void)frexp(x, &result);
    result = isnan(x) ? FP_ILOGBNAN : result;
    result = isinf(x) ? INT_MAX : result;
    return x == 0.0 ? FP_ILOGB0 : result;
}

Might be able to avoid the edge case handling too



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although I guess this better if you have a dedicated frexp_exp instruction. Could factor out the frexp_exp part into a utility function, which does the bithacking or the intrinsic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure using frexp is currently better given that there's no native path for it. We talked about allowing that in the frexp PR.

But yeah even in that case, if we use the builtin/intrinsic for frexp it's not necessarily the case that it's faster than this implementation for ilogb. So I think the logb/ilogb-specific utility function would be the way to go.

I don't think we've got canonical examples of target-specific code paths for utility functions in libclc. We could do it with the SOURCES system, or preprocessor defines or some other system. I'm not yet sure.

This commit moves the logb and ilogb builtins to the CLC library.

It simultaneously optimizes them both for vector types and for half
types. Half types were previously promoting to float, whereas this
commit provides them a native implementation.

Everything passes the OpenCL-CTS.

I had to intuit some magic numbers used by these implementations in
order to generate the half variants. I gave them clearer definitions
derived from what I believe are their actual component numbers, but
named them 'magic' to convey that they weren't derived from first
principles.
@frasercrmck frasercrmck force-pushed the libclc-clc-logb-ilogb branch from 5acc4a8 to ebcca07 Compare May 12, 2025 14:39
Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this should be fixed to be in terms of frexp utilities but this is just moving it anyway and that's separate

@frasercrmck frasercrmck merged commit 95c683f into llvm:main May 13, 2025
9 checks passed
@frasercrmck frasercrmck deleted the libclc-clc-logb-ilogb branch May 13, 2025 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libclc libclc OpenCL library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants