[clang] [clang] support constexpr bitcasts to integer types (PR #231331)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 12 16:58:30 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Benjamin Ye (Golf0ned)
<details>
<summary>Changes</summary>
Fixes [`159905`](https://github.com/llvm/llvm-project/issues/159905) by extending both evaluators.
---
Full diff: https://github.com/llvm/llvm-project/pull/231331.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+2-2)
- (modified) clang/lib/AST/ExprConstant.cpp (+10-1)
- (modified) clang/test/SemaCXX/constexpr-vectors.cpp (+13)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index a78bf7acf054c..7cb3ba28e8aef 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -786,8 +786,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *E) {
}
QualType SubExprTy = SubExpr->getType();
OptPrimType FromT = classify(SubExprTy);
- // Casts from integer/vector to vector.
- if (E->getType()->isVectorType())
+ // Casts from integer/vector to vector, and from vector to integer.
+ if (E->getType()->isVectorType() || SubExprTy->isVectorType())
return this->emitBuiltinBitCast(E);
OptPrimType ToT = classify(E->getType());
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 8551f4c9ae279..8f4680c9367a5 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -20214,7 +20214,6 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
case CK_HLSLAggregateSplatCast:
llvm_unreachable("invalid cast kind for integral value");
- case CK_BitCast:
case CK_Dependent:
case CK_LValueBitCast:
case CK_ARCProduceObject:
@@ -20232,6 +20231,16 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
case CK_HLSLArrayRValue:
return ExprEvaluatorBaseTy::VisitCastExpr(E);
+ case CK_BitCast: {
+ APValue Src;
+ if (!Evaluate(Src, Info, SubExpr))
+ return false;
+ APValue Dst;
+ if (!handleRValueToRValueBitCast(Info, Dst, Src, E))
+ return false;
+ return Success(Dst, E);
+ }
+
case CK_MemberPointerToBoolean:
case CK_PointerToBoolean:
case CK_IntegralToBoolean:
diff --git a/clang/test/SemaCXX/constexpr-vectors.cpp b/clang/test/SemaCXX/constexpr-vectors.cpp
index 1f6fe9310066e..fdc1e159dde37 100644
--- a/clang/test/SemaCXX/constexpr-vectors.cpp
+++ b/clang/test/SemaCXX/constexpr-vectors.cpp
@@ -835,3 +835,16 @@ void BoolVecUsage() {
constexpr auto k = ~FourBoolsExtVec{true, false, true, false};
static_assert(k[0] == false && k[1] == true && k[2] == false && k[3] == true, "");
}
+
+// GH159905
+constexpr FourCharsVecSize ShiftVec(FourCharsVecSize a, int b) {
+ return (FourCharsVecSize)((int)a << b);
+}
+
+void VecToIntegralUsage() {
+ static_assert((int)FourCharsVecSize{1, 2, 3, 4} == 0x04030201, "");
+ static_assert((__int128)FourIntsVecSize{1, 2, 3, 4} ==
+ (((__int128)0x400000003 << 64) | 0x200000001), "");
+ static_assert((__int128)FourFloatsVecSize{1.0f, 0, 0, 0} == 0x3f800000, "");
+ static_assert(ShiftVec(FourCharsVecSize{1, 0, 0, 0}, 8)[1] == 1, "");
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/231331
More information about the cfe-commits
mailing list