Skip to content

[Clang] Support elementwise/reduction builtins in constexpr contexts. #51787

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

Open
fhahn opened this issue Nov 9, 2021 · 14 comments
Open

[Clang] Support elementwise/reduction builtins in constexpr contexts. #51787

fhahn opened this issue Nov 9, 2021 · 14 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla c c++26 clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation extension:clang good first issue https://github.com/llvm/llvm-project/contribute

Comments

@fhahn
Copy link
Contributor

fhahn commented Nov 9, 2021

Bugzilla Link 52445
Version trunk
OS All
CC @DougGregor,@RKSimon,@zygoloid

Extended Description

Clang recently specified a set of builtins for elementwise operations and reductions on vectors:

https://clang.llvm.org/docs/LanguageExtensions.html#vector-builtins

They should also be supported in constexpr contexts.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@RKSimon RKSimon added the good first issue https://github.com/llvm/llvm-project/contribute label Aug 9, 2023
@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2023

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Assign the issue to you.
  2. Fix the issue locally.
  3. Run the test suite locally.
    3.1) Remember that the subdirectories under test/ create fine-grained testing targets, so you can
    e.g. use make check-clang-ast to only run Clang's AST tests.
  4. Create a git commit
  5. Run git clang-format HEAD~1 to format your changes.
  6. Submit the patch to Phabricator.
    6.1) Detailed instructions can be found here

For more instructions on how to submit a patch to LLVM, see our documentation.

If you have any further questions about this issue, don't hesitate to ask via a comment on this Github issue.

@llvm/issue-subscribers-good-first-issue

@dlumma
Copy link

dlumma commented Oct 10, 2023

This issue was submitted some time ago, but it looks like no one has claimed it, so I will make an attempt to contribute here.

@RKSimon
Copy link
Collaborator

RKSimon commented Mar 22, 2024

@dlumma Any progress?

@Endilll Endilll added extension:clang clang:to-be-triaged Should not be used for new issues labels Jul 18, 2024
@RKSimon
Copy link
Collaborator

RKSimon commented Oct 8, 2024

@dlumma reverse-ping

@RKSimon
Copy link
Collaborator

RKSimon commented Oct 24, 2024

@dlumma It doesn't look like you've made any progress on this in the past year so I've removed your assignment in case anybody else wanted to try - I know @c8ef has shown an interest in fixing this.

@c8ef
Copy link
Contributor

c8ef commented Oct 24, 2024

When investigating this issue, I discovered that certain scalar versions of these functions, like __builtin_abs and __builtin_fma, cannot be used in a constexpr context. As a result, I have chosen to prioritize addressing these limitations, which will also aid in the implementation of P0533R9 for libc++.

By the way, could you please help reviewing #113020 when you have time? Thank you! @RKSimon

@c8ef c8ef self-assigned this Nov 2, 2024
@RKSimon
Copy link
Collaborator

RKSimon commented Nov 13, 2024

@c8ef Are you interested in working on this for at least the integer cases?

@c8ef
Copy link
Contributor

c8ef commented Nov 13, 2024

Yes, I have a draft patch for this. I will start with __builtin_reduce_add.

BTW, what do you think of the latest revision in #114637? I only modified the parameter documentation and kept the nan semantics. I noticed the problem while working on this issue.

@fhahn
Copy link
Contributor Author

fhahn commented Nov 13, 2024

Great, would be great to close the gap between integer and FP support in the builtins!

c8ef added a commit that referenced this issue Nov 18, 2024
Part of #51787.

This patch adds constexpr support for the built-in reduce add function.
If this is the right way to go, I will add support for other reduce
functions in later patches.

---------

Co-authored-by: Mariya Podchishchaeva <[email protected]>
c8ef added a commit that referenced this issue Nov 19, 2024
Part of #51787.
Follow up of #116243.

This patch adds constexpr support for the built-in reduce mul function.
c8ef added a commit that referenced this issue Nov 20, 2024
Part of #51787.
Follow up of #116626.

This patch adds constexpr support for the built-in reduce and function.
c8ef added a commit that referenced this issue Nov 21, 2024
Part of #51787.
Follow up of #116822.

This patch adds constexpr support for the built-in reduce `or` and `xor`
functions.
c8ef added a commit that referenced this issue Nov 26, 2024
Part of #51787.

This patch adds constexpr support for the built-in elementwise popcount
function.
c8ef added a commit that referenced this issue Dec 3, 2024
Part of #51787.

This patch adds constexpr support for the built-in elementwise
bitreverse function.
c8ef added a commit that referenced this issue Dec 9, 2024
…19082)

Part of #51787.

This patch adds constexpr support for the built-in elementwise add_sat
and sub_sat functions.
@RKSimon
Copy link
Collaborator

RKSimon commented Dec 18, 2024

@c8ef For integer reductions we're just missing min/max coverage - are you still able to work on this?

@c8ef
Copy link
Contributor

c8ef commented Dec 18, 2024

Yes, will do.

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation and removed clang:to-be-triaged Should not be used for new issues labels Dec 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2024

@llvm/issue-subscribers-clang-frontend

Author: Florian Hahn (fhahn)

| | | | --- | --- | | Bugzilla Link | [52445](https://llvm.org/bz52445) | | Version | trunk | | OS | All | | CC | @DougGregor,@RKSimon,@zygoloid |

Extended Description

Clang recently specified a set of builtins for elementwise operations and reductions on vectors:

https://clang.llvm.org/docs/LanguageExtensions.html#vector-builtins

They should also be supported in constexpr contexts.

c8ef added a commit that referenced this issue Dec 23, 2024
Part of #51787.

This patch adds constexpr support for the built-in reduce min/max
function.
@frederick-vs-ja
Copy link
Contributor

It's also helpful for C++26 std::simd to allow constant evaluation here. Otherwise, we'd need many if consteval branches.

@c8ef
Copy link
Contributor

c8ef commented Dec 31, 2024

I'm wondering if __builtin_fmax and __builtin_elementwise_max have the same semantic for floating-point arguments. If they do, I can port the constexpr implementation to the floating-point vector version of max/min. It seems like they generate the same LLVM IR: https://godbolt.org/z/Ec3zcKaxa

@RKSimon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c c++26 clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation extension:clang good first issue https://github.com/llvm/llvm-project/contribute
Projects
None yet
Development

No branches or pull requests

8 participants