[clang] 89d12a4 - [clang] support constexpr bitcasts to integer types (#231331)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 27 23:12:56 PDT 2026


Author: Benjamin Ye
Date: 2026-10-28T07:12:50+01:00
New Revision: 89d12a4dfa0ebf6362988741fc4a8d05198d9f2c

URL: https://github.com/llvm/llvm-project/commit/89d12a4dfa0ebf6362988741fc4a8d05198d9f2c
DIFF: https://github.com/llvm/llvm-project/commit/89d12a4dfa0ebf6362988741fc4a8d05198d9f2c.diff

LOG: [clang] support constexpr bitcasts to integer types (#231331)

Fixes [`159905`](https://github.com/llvm/llvm-project/issues/159905) by
extending both evaluators.

(code generation assisted by claude)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/lib/AST/ExprConstant.cpp
    clang/test/SemaCXX/constexpr-vectors.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 5624b61ee5f35..8f42e0943458d 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -781,8 +781,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 between vectors and integers of the same same size.
+    if (ETy->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 384aa084440c3..8eaa2454000e6 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -20396,7 +20396,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:
@@ -20414,6 +20413,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