[clang] a1da106 - [clang][bytecode] Fix non-initializing `__builtin_shufflevector` (#180691)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 10 00:15:37 PST 2026
Author: Timm Baeder
Date: 2026-02-10T09:15:32+01:00
New Revision: a1da10689c8bb90288bd5f10ce3af70a92f5da74
URL: https://github.com/llvm/llvm-project/commit/a1da10689c8bb90288bd5f10ce3af70a92f5da74
DIFF: https://github.com/llvm/llvm-project/commit/a1da10689c8bb90288bd5f10ce3af70a92f5da74.diff
LOG: [clang][bytecode] Fix non-initializing `__builtin_shufflevector` (#180691)
Create a local temporary we can use as destination.
Added:
Modified:
clang/lib/AST/ByteCode/Compiler.cpp
clang/test/AST/ByteCode/builtin-functions.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index b4af1c80b1f36..52776d2a94dba 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4096,7 +4096,6 @@ bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
if (E->getNumSubExprs() == 2)
return this->emitInvalid(E);
- assert(Initializing);
assert(E->getNumSubExprs() > 2);
const Expr *Vecs[] = {E->getExpr(0), E->getExpr(1)};
@@ -4106,6 +4105,14 @@ bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
unsigned NumOutputElems = E->getNumSubExprs() - 2;
assert(NumOutputElems > 0);
+ if (!Initializing) {
+ UnsignedOrNone LocalIndex = allocateLocal(E);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
+ return false;
+ }
+
// Save both input vectors to a local variable.
unsigned VectorOffsets[2];
for (unsigned I = 0; I != 2; ++I) {
@@ -4134,6 +4141,9 @@ bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
return false;
}
+ if (DiscardResult)
+ return this->emitPopPtr(E);
+
return true;
}
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 10f7ff294ae00..65b10cf474ce2 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -1153,6 +1153,37 @@ namespace shufflevector {
// both-error {{index for __builtin_shufflevector not within the bounds of the input vectors; index of -1 found at position 0 is not permitted in a constexpr context}}
vector4charConst1,
vector4charConst2, -1, -1, -1, -1);
+
+ constexpr int discarded1() {
+ int i = 0;
+ vector4char a = {0};
+ __builtin_shufflevector((++i, a), a, 0); // both-warning {{expression result unused}}
+ return i;
+ }
+ static_assert(discarded1() == 1);
+
+ constexpr int discarded2() { // both-error {{never produces a constant expression}}
+ int i = 0;
+ vector4char a = {0};
+ __builtin_shufflevector((++i, a), a, -1); // both-error 2{{index for __builtin_shufflevector not within the bounds of the input vectors; index of -1 found at position 0 is not permitted in a constexpr context}} \
+ // both-warning {{expression result unused}}
+ return i;
+ }
+ static_assert(discarded2() == 1); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
+
+#if __cplusplus >= 202002L
+ constexpr int discarded3() {
+ int i = 0;
+ vector4char a;
+ __builtin_shufflevector((++i, a), a, 0); // both-note {{read of uninitialized object}} \
+ // both-warning {{expression result unused}}
+ return i;
+ }
+ static_assert(discarded3() == 1); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
+#endif
+
}
#endif
More information about the cfe-commits
mailing list