Skip to content

[clang] constexpr built-in reduce add function. #116243

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 4 commits into from
Nov 18, 2024
Merged

[clang] constexpr built-in reduce add function. #116243

merged 4 commits into from
Nov 18, 2024

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Nov 14, 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.

@c8ef c8ef changed the title Draft [clang] constexpr built-in reduce add function. Nov 14, 2024
@c8ef c8ef marked this pull request as ready for review November 14, 2024 15:43
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 14, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2024

@llvm/pr-subscribers-clang

Author: None (c8ef)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/116243.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/include/clang/Basic/Builtins.td (+1-1)
  • (modified) clang/lib/AST/ExprConstant.cpp (+14)
  • (modified) clang/test/Sema/constant_builtins_vector.cpp (+6)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 78ba70c624d18c..2bc2bda663168e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -353,6 +353,8 @@ Non-comprehensive list of changes in this release
   The flexible array member (FAM) can now be accessed immediately without causing
   issues with the sanitizer because the counter is automatically set.
 
+- ``__builtin_reduce_add`` function can now be used in constant expressions.
+
 New Compiler Flags
 ------------------
 
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index e866605ac05c09..a17e1353b2e1b4 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -1504,7 +1504,7 @@ def ReduceAnd : Builtin {
 
 def ReduceAdd : Builtin {
   let Spellings = ["__builtin_reduce_add"];
-  let Attributes = [NoThrow, Const, CustomTypeChecking];
+  let Attributes = [NoThrow, Const, CustomTypeChecking, Constexpr];
   let Prototype = "void(...)";
 }
 
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d664c503655ba6..e9fb3b4b00d164 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13528,6 +13528,20 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
     return Success(DidOverflow, E);
   }
 
+  case Builtin::BI__builtin_reduce_add: {
+    APValue Source;
+    if (!EvaluateAsRValue(Info, E->getArg(0), Source))
+      return false;
+
+    auto SourceLen = Source.getVectorLength();
+    APSInt Reduced = Source.getVectorElt(0).getInt();
+    for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
+      Reduced += Source.getVectorElt(EltNum).getInt();
+    }
+
+    return Success(Reduced, E);
+  }
+
   case clang::X86::BI__builtin_ia32_addcarryx_u32:
   case clang::X86::BI__builtin_ia32_addcarryx_u64:
   case clang::X86::BI__builtin_ia32_subborrow_u32:
diff --git a/clang/test/Sema/constant_builtins_vector.cpp b/clang/test/Sema/constant_builtins_vector.cpp
index c6b1b37cef28b4..6d2db6e5111eff 100644
--- a/clang/test/Sema/constant_builtins_vector.cpp
+++ b/clang/test/Sema/constant_builtins_vector.cpp
@@ -723,3 +723,9 @@ not within the bounds of the input vectors; index of -1 found at position 0 is n
 permitted in a constexpr context}}
         vector4charConst1,
         vector4charConst2, -1, -1, -1, -1);
+
+static_assert(__builtin_reduce_add((vector4char){}) == 0);
+static_assert(__builtin_reduce_add((vector4char){1, 2, 3, 4}) == 10);
+static_assert(__builtin_reduce_add((vector4short){10, 20, 30, 40}) == 100);
+static_assert(__builtin_reduce_add((vector4int){100, 200, 300, 400}) == 1000);
+static_assert(__builtin_reduce_add((vector4long){1000, 2000, 3000, 4000}) == 10000);

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

This seems reasonable to me - I'd recommend you take a look at the min/max reductions to see if signed/unsigned can be correctly handled with this approach.

if (!EvaluateAsRValue(Info, E->getArg(0), Source))
return false;

auto SourceLen = Source.getVectorLength();
Copy link
Collaborator

Choose a reason for hiding this comment

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

(style) Avoid auto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@RKSimon RKSimon requested a review from sethp November 15, 2024 10:21
@@ -723,3 +723,9 @@ not within the bounds of the input vectors; index of -1 found at position 0 is n
permitted in a constexpr context}}
vector4charConst1,
vector4charConst2, -1, -1, -1, -1);

static_assert(__builtin_reduce_add((vector4char){}) == 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

How is this handled? I'd have expected Source.getVectorElt(0).getInt() to have failed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this is similar to zero initializing a vector with 4 char elements.

@c8ef
Copy link
Contributor Author

c8ef commented Nov 15, 2024

This seems reasonable to me - I'd recommend you take a look at the min/max reductions to see if signed/unsigned can be correctly handled with this approach.

Thank you for your suggestions! Signed integer overflow in C++ is undefined behavior and cannot occur in a constexpr context. I have addressed the issue in the latest revision.

@c8ef c8ef requested a review from RKSimon November 15, 2024 12:17
@RKSimon RKSimon requested a review from Fznamznon November 15, 2024 17:13
Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Co-authored-by: Mariya Podchishchaeva <[email protected]>
@c8ef c8ef merged commit 1e4646d into llvm:main Nov 18, 2024
9 checks passed
@RKSimon
Copy link
Collaborator

RKSimon commented Nov 18, 2024

Thanks @c8ef - are you OK to add the remaining integer reduction cases next? Also, please can you also update the LanguageExtensions docs page to mention that they are constexpr legal?

@c8ef
Copy link
Contributor Author

c8ef commented Nov 18, 2024

Thanks @c8ef - are you OK to add the remaining integer reduction cases next? Also, please can you also update the LanguageExtensions docs page to mention that they are constexpr legal?

Will do.

c8ef added a commit that referenced this pull request Nov 19, 2024
Part of #51787.
Follow up of #116243.

This patch adds constexpr support for the built-in reduce mul function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants