-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang][bytecode] Implement __builtin_reduce_mul #118287
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Your org has enabled the Graphite merge queue for merging into mainAdd the label “FP Bundles” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link. |
Member
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesFull diff: https://github.com/llvm/llvm-project/pull/118287.diff 3 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Integral.h b/clang/lib/AST/ByteCode/Integral.h
index ca3674263aef4f..cb81b5a6989240 100644
--- a/clang/lib/AST/ByteCode/Integral.h
+++ b/clang/lib/AST/ByteCode/Integral.h
@@ -123,7 +123,9 @@ template <unsigned Bits, bool Signed> class Integral final {
APSInt toAPSInt() const {
return APSInt(APInt(Bits, static_cast<uint64_t>(V), Signed), !Signed);
}
- APSInt toAPSInt(unsigned BitWidth) const { return APSInt(toAPInt(BitWidth)); }
+ APSInt toAPSInt(unsigned BitWidth) const {
+ return APSInt(toAPInt(BitWidth), !Signed);
+ }
APInt toAPInt(unsigned BitWidth) const {
if constexpr (Signed)
return APInt(Bits, static_cast<uint64_t>(V), Signed)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 8ede6717db0463..b35b90e55e5155 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1695,32 +1695,42 @@ static bool interp__builtin_vector_reduce(InterpState &S, CodePtr OpPC,
assert(Arg.getFieldDesc()->isPrimitiveArray());
unsigned ID = Func->getBuiltinID();
- if (ID == Builtin::BI__builtin_reduce_add) {
- QualType ElemType = Arg.getFieldDesc()->getElemQualType();
- assert(Call->getType() == ElemType);
- PrimType ElemT = *S.getContext().classify(ElemType);
- unsigned NumElems = Arg.getNumElems();
-
- INT_TYPE_SWITCH(ElemT, {
- T Sum = Arg.atIndex(0).deref<T>();
- unsigned BitWidth = Sum.bitWidth();
- for (unsigned I = 1; I != NumElems; ++I) {
- T Elem = Arg.atIndex(I).deref<T>();
- if (T::add(Sum, Elem, BitWidth, &Sum)) {
+ QualType ElemType = Arg.getFieldDesc()->getElemQualType();
+ assert(Call->getType() == ElemType);
+ PrimType ElemT = *S.getContext().classify(ElemType);
+ unsigned NumElems = Arg.getNumElems();
+
+ INT_TYPE_SWITCH(ElemT, {
+ T Result = Arg.atIndex(0).deref<T>();
+ unsigned BitWidth = Result.bitWidth();
+ for (unsigned I = 1; I != NumElems; ++I) {
+ T Elem = Arg.atIndex(I).deref<T>();
+ T PrevResult = Result;
+
+ if (ID == Builtin::BI__builtin_reduce_add) {
+ if (T::add(Result, Elem, BitWidth, &Result)) {
unsigned OverflowBits = BitWidth + 1;
- (void)handleOverflow(
- S, OpPC,
- (Sum.toAPSInt(OverflowBits) + Elem.toAPSInt(OverflowBits)));
+ (void)handleOverflow(S, OpPC,
+ (PrevResult.toAPSInt(OverflowBits) +
+ Elem.toAPSInt(OverflowBits)));
return false;
}
+ } else if (ID == Builtin::BI__builtin_reduce_mul) {
+ if (T::mul(Result, Elem, BitWidth, &Result)) {
+ unsigned OverflowBits = BitWidth * 2;
+ (void)handleOverflow(S, OpPC,
+ (PrevResult.toAPSInt(OverflowBits) *
+ Elem.toAPSInt(OverflowBits)));
+ return false;
+ }
+ } else {
+ llvm_unreachable("Unhandled vector reduce builtin");
}
- pushInteger(S, Sum, Call->getType());
- });
-
- return true;
- }
+ }
+ pushInteger(S, Result, Call->getType());
+ });
- llvm_unreachable("Unsupported vector reduce builtin");
+ return true;
}
static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
@@ -2195,6 +2205,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
break;
case Builtin::BI__builtin_reduce_add:
+ case Builtin::BI__builtin_reduce_mul:
if (!interp__builtin_vector_reduce(S, OpPC, Frame, F, Call))
return false;
break;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 78b692b819a0ab..e59b7616ac5ab9 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1035,6 +1035,35 @@ namespace RecuceAdd {
#endif
}
+namespace ReduceMul {
+ static_assert(__builtin_reduce_mul((vector4char){}) == 0);
+ static_assert(__builtin_reduce_mul((vector4char){1, 2, 3, 4}) == 24);
+ static_assert(__builtin_reduce_mul((vector4short){1, 2, 30, 40}) == 2400);
+#ifndef __AVR__
+ static_assert(__builtin_reduce_mul((vector4int){10, 20, 300, 400}) == 24'000'000);
+#endif
+ static_assert(__builtin_reduce_mul((vector4long){1000L, 2000L, 3000L, 4000L}) == 24'000'000'000'000L);
+ constexpr int reduceMulInt1 = __builtin_reduce_mul((vector4int){~(1 << (sizeof(int) * 8 - 1)), 1, 1, 2});
+ // both-error@-1 {{must be initialized by a constant expression}} \
+ // both-note@-1 {{outside the range of representable values of type 'int'}}
+ constexpr long long reduceMulLong1 = __builtin_reduce_mul((vector4long){~(1LL << (sizeof(long long) * 8 - 1)), 1, 1, 2});
+ // both-error@-1 {{must be initialized by a constant expression}} \
+ // both-note@-1 {{outside the range of representable values of type 'long long'}}
+ constexpr int reduceMulInt2 = __builtin_reduce_mul((vector4int){(1 << (sizeof(int) * 8 - 1)), 1, 1, 2});
+ // both-error@-1 {{must be initialized by a constant expression}} \
+ // both-note@-1 {{outside the range of representable values of type 'int'}}
+ constexpr long long reduceMulLong2 = __builtin_reduce_mul((vector4long){(1LL << (sizeof(long long) * 8 - 1)), 1, 1, 2});
+ // both-error@-1 {{must be initialized by a constant expression}} \
+ // both-note@-1 {{outside the range of representable values of type 'long long'}}
+ static_assert(__builtin_reduce_mul((vector4uint){~0U, 1, 1, 2}) ==
+#ifdef __AVR__
+ 0);
+#else
+ (~0U - 1));
+#endif
+ static_assert(__builtin_reduce_mul((vector4ulong){~0ULL, 1, 1, 2}) == ~0ULL - 1);
+}
+
namespace BuiltinMemcpy {
constexpr int simple() {
int a = 12;
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.