Skip to content

release/22.x: [libclc] mark __clc_flush_denormal_if_not_supported as static inline - #207264

Closed
karolherbst wants to merge 1 commit into
llvm:release/22.xfrom
karolherbst:fix/pr/158791
Closed

karolherbst wants to merge 1 commit into
llvm:release/22.xfrom
karolherbst:fix/pr/158791

Conversation

@karolherbst

Copy link
Copy Markdown
Contributor

The compiled SPIR-V libclc binaries ended up with an empty definition for __clc_flush_denormal_if_not_supported.

b8a5888 ("[libclc] Fix memory fence scope mapping for OpenCL (#170542)") also added the static keyword probably due to the same issue.

Fixes: 7f36611 ("[libclc] Remove attribute((always_inline)) (#158791)")

The compiled SPIR-V libclc binaries ended up with an empty definition for
__clc_flush_denormal_if_not_supported.

b8a5888 ("[libclc] Fix memory fence scope mapping for OpenCL (llvm#170542)")
also added the static keyword probably due to the same issue.

Fixes: 7f36611 ("[libclc] Remove __attribute__((always_inline)) (llvm#158791)")
@llvmorg-github-actions llvmorg-github-actions Bot added the libclc libclc OpenCL library label Jul 2, 2026
@karolherbst

Copy link
Copy Markdown
Contributor Author

Can we please get this fixed? Users start running into the issue of _Z37__clc_flush_denormal_if_not_supportedf to being defined.

@kiefernforst

Copy link
Copy Markdown

@wenju-he Have you seen this?

@zhangweize9-cyber zhangweize9-cyber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. The addition of static correctly addresses the potential symbol collision issue in Header-only scenarios. Good catch.

@wenju-he wenju-he left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

this function was deleted in 85e9ac5, so main branch has no issue.

@karolherbst

Copy link
Copy Markdown
Contributor Author

LGTM

this function was deleted in 85e9ac5, so main branch has no issue.

yeah, this is only an issue on the 22 release branch. Sadly I didn't catch that issue earlier (and I should probably start testing with newer releases more aggressively). But I also wasn't aware how short lived releases branches are as I just realized that normally no more releases are planned, is that right?

@wenju-he

Copy link
Copy Markdown
Contributor

But I also wasn't aware how short lived releases branches are as I just realized that normally no more releases are planned, is that right?

I'm not familiar with llvm release cadence. 22.x branch is probably not frozen yet because 23 is not released yet. So critical bug fix should be ok to land in 22.x branch.

@karolherbst

karolherbst commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor Author

But I also wasn't aware how short lived releases branches are as I just realized that normally no more releases are planned, is that right?

I'm not familiar with llvm release cadence. 22.x branch is probably not frozen yet because 23 is not released yet. So critical bug fix should be ok to land in 22.x branch.

Yeah, not sure if this is critical enough to make yet another release, but not my call to make. But I've seen other PRs against 22.x that were closed already, because of "we don't do another 22.x release".

But it does break certain builtins like cos, because there is no definition for __clc_flush_denormal_if_not_supported in the final SPIR-V.

It also feels like the bug is actually something else, because it also looks like a codegen or tooling issue, though this PR is rather trivial and does fix the issue.

Like in the SPIR-V (without this PR) I'm seeing this:

%_Z37__clc_flush_denormal_if_not_supportedf = OpFunction %float None %4639
       %4641 = OpFunctionParameter %float
               OpFunctionEnd

And then multiple calls to it across the entire libclc:

     %113249 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %113248
     %113251 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %113250
     %113253 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %113252
     %198384 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %198383
     %198386 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %198385
     %199563 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %199562
     %199565 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %199564
     %200742 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %200741
     %200744 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %200743
     %201921 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %201920
     %201923 = OpFunctionCall %float %_Z37__clc_flush_denormal_if_not_supportedf %201922

And I'd have expected that this would already break at linking time or something.

Or I guess those are just normal C rules, because no translation unit actually instantiated the definition of __clc_flush_denormal_if_not_supported. But then again, I think it would be better if at build time it would already made sure that all symbols are resolved so we won't run into this issue in the future?

@wenju-he

Copy link
Copy Markdown
Contributor

But I've seen other PRs against 22.x that were closed already, because of "we don't do another 22.x release".

Ok, then it might be difficult to land this to 22.x

It also feels like the bug is actually something else, because it also looks like a codegen or tooling issue, though this PR is rather trivial and does fix the issue.

This PR is the correct root-cause fix. Without static, _CLC_INLINE (inline) function in header is problematic in C: when function is not inlined in a TU, compiler assumes it will be provided by another file (which won't happen) and generates declaration for __clc_flush_denormal_if_not_supported in that TU.

@karolherbst

Copy link
Copy Markdown
Contributor Author

I guess I can really assume this isn't going to get merged? It's unfortunate because this now requires every distribution to patch their libclc packaging to get this working.

So I'm kinda interested what's the best course of action for this is, because it feels weird that bugs preventing applications from running (in this case compilation of kernels would fail due to missing functions implementations) are fine to have.

Is it common to require distribution to fix those kind of LLVM issues in their packaging? Is there a way to notify them? Should we just vendor libclc in mesa and deal with backporting on our own and ask distributions to simply use our vendored version?

What's LLVM's position on this from a project perspective? If you prefer distributions to not ship libclc and that we should just ship it as a vendored version inside mesa, that's fine by us. Just wondering what the expectations here are? And to be fair, this is just outfall from not catching the bug in time, but now distributions just started to ship with libclc-22 and it's kinda broken now...

@nikic

nikic commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Yes, this will not be merged, because the LLVM 22 release cycle has closed and there will be no further releases. This is not a value judgement on the importance of this issue, just a consequence of the release cycle. For issues found after the upstream release goes EOL, backporting at the distro level is indeed the right thing to do. In Fedora we usually accumulate a handful of additional backports after upstream EOL (for LLVM in general -- I can't recall an instance of us having to backport a libclc patch.)

We probably should consider slightly extending the release cycle, to better bridge the gap between two releases. I believe the last time this happened was in https://discourse.llvm.org/t/rfc-distribute-patch-releases-over-lifecycle-of-an-llvm-version/80955, which extended the maintenance window by 6 weeks.

@karolherbst

karolherbst commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Yes, this will not be merged, because the LLVM 22 release cycle has closed and there will be no further releases. This is not a value judgement on the importance of this issue, just a consequence of the release cycle. For issues found after the upstream release goes EOL, backporting at the distro level is indeed the right thing to do. In Fedora we usually accumulate a handful of additional backports after upstream EOL (for LLVM in general -- I can't recall an instance of us having to backport a libclc patch.)

We probably should consider slightly extending the release cycle, to better bridge the gap between two releases. I believe the last time this happened was in https://discourse.llvm.org/t/rfc-distribute-patch-releases-over-lifecycle-of-an-llvm-version/80955, which extended the maintenance window by 6 weeks.

Yeah.. atm I'm wondering what solution to go with. I currently created a fork for libclc based on llvm 21, 22 and 23 (so distros can pick their base) which is upstream libclc + a hand full of patches to at least make sure the CL CTS doesn't trigger issues caused by something inside libclc.

I can say "yeah use those patches", but then I don't know if it could break things for other users, so I can only say "it's safe to use those patches with mesa".

But maybe distributions would be fine with this, because I'm not sure there are any current users of the libclc-spirv packages and mesa could just use its own patched version for the time being?

There are a few issues with the 23 version, but we have still time to fix it all up, if we manage in time.

@aaronpuchert

Copy link
Copy Markdown
Member

Without static, _CLC_INLINE (inline) function in header is problematic in C: when function is not inlined in a TU, compiler assumes it will be provided by another file (which won't happen) and generates declaration for __clc_flush_denormal_if_not_supported in that TU.

I was under the impression that C just requires an "anchor" definition in a source file, as explained in https://en.cppreference.com/c/language/inline:

If a function is declared inline in some translation units, it does not need to be declared inline everywhere: at most one translation unit may also provide a regular, non-inline non-static function, or a function declared extern inline. This one translation unit is said to provide the external definition. In order to avoid undefined behavior, one external definition must exist in the program if the name of the function with external linkage is used in an expression, see one definition rule.

The example illustrates how this is done:

Header file "test.h"

#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

inline int sum(int a, int b)
{
    return a + b;
}

#endif

Source file "sum.c"

#include "test.h"

extern inline int sum(int a, int b); // provides external definition

Wouldn't it be possible to provide such an anchor definition in libclc? The problem with static in header files is that every translation unit gets their own copy of the function, and in this case it doesn't seem to be inlined. (Otherwise there would be no error.) Those duplicate copies are probably not a big issue for OpenCL, but it could still blow up the size of the library unnecessarily.

@karolherbst

karolherbst commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Without static, _CLC_INLINE (inline) function in header is problematic in C: when function is not inlined in a TU, compiler assumes it will be provided by another file (which won't happen) and generates declaration for __clc_flush_denormal_if_not_supported in that TU.

I was under the impression that C just requires an "anchor" definition in a source file, as explained in https://en.cppreference.com/c/language/inline:

If a function is declared inline in some translation units, it does not need to be declared inline everywhere: at most one translation unit may also provide a regular, non-inline non-static function, or a function declared extern inline. This one translation unit is said to provide the external definition. In order to avoid undefined behavior, one external definition must exist in the program if the name of the function with external linkage is used in an expression, see one definition rule.

The example illustrates how this is done:

Header file "test.h"

#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

inline int sum(int a, int b)
{
    return a + b;
}

#endif

Source file "sum.c"

#include "test.h"

extern inline int sum(int a, int b); // provides external definition

Wouldn't it be possible to provide such an anchor definition in libclc? The problem with static in header files is that every translation unit gets their own copy of the function, and in this case it doesn't seem to be inlined. (Otherwise there would be no error.) Those duplicate copies are probably not a big issue for OpenCL, but it could still blow up the size of the library unnecessarily.

Yeah maybe, but 1. this issue doesn't exist on 23 and 2. on GPUs you are generally more aggressive with inlining anyway and 3. the function in question is rather small. I'm more concerned about the actual implementation because on most GPUs you can implement this function in 0 instructions (or if you are unlucky 1), but the current impl requires pattern matching in the compiler to actually optimize this.

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.

6 participants