[clang] [clang] support constexpr bitcasts to integer types (PR #231331)
Benjamin Ye via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 12 16:57:30 PDT 2026
https://github.com/Golf0ned created https://github.com/llvm/llvm-project/pull/231331
Fixes [`159905`](https://github.com/llvm/llvm-project/issues/159905) by extending both evaluators.
>From ab1c61652b927127ce97e3e1d8ef922fd465e56d Mon Sep 17 00:00:00 2001
From: Benjamin Ye <benjaminye.email at gmail.com>
Date: Fri, 9 Oct 2026 16:16:50 -0500
Subject: [PATCH] [clang] support constexpr bitcasts to integer types
---
clang/lib/AST/ByteCode/Compiler.cpp | 4 ++--
clang/lib/AST/ExprConstant.cpp | 11 ++++++++++-
clang/test/SemaCXX/constexpr-vectors.cpp | 13 +++++++++++++
3 files changed, 25 insertions(+), 3 deletions(-)
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, "");
+}
More information about the cfe-commits
mailing list