Skip to content

Commit 6f16a8b

Browse files
authored
[clang][bytecode] Use bitcasts to cast from integer to vector (#117547)
In C, a cast from an integer to a vector is a CK_BitCast. Implement this using the same code we use for __builtin_bit_cast.
1 parent 9b76e7f commit 6f16a8b

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
448448

449449
QualType SubExprTy = SubExpr->getType();
450450
std::optional<PrimType> FromT = classify(SubExprTy);
451+
// Casts from integer to vectors in C.
452+
if (FromT && CE->getType()->isVectorType())
453+
return this->emitBuiltinBitCast(CE);
454+
451455
std::optional<PrimType> ToT = classify(CE->getType());
452456
if (!FromT || !ToT)
453457
return false;
@@ -6494,8 +6498,23 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
64946498
}
64956499

64966500
// Get a pointer to the value-to-cast on the stack.
6497-
if (!this->visit(SubExpr))
6498-
return false;
6501+
// For CK_LValueToRValueBitCast, this is always an lvalue and
6502+
// we later assume it to be one (i.e. a PT_Ptr). However,
6503+
// we call this function for other utility methods where
6504+
// a bitcast might be useful, so convert it to a PT_Ptr in that case.
6505+
if (SubExpr->isGLValue()) {
6506+
if (!this->visit(SubExpr))
6507+
return false;
6508+
} else if (std::optional<PrimType> FromT = classify(SubExpr)) {
6509+
unsigned TempOffset = allocateLocalPrimitive(
6510+
SubExpr, *FromT, /*IsConst=*/true, /*IsExtended=*/false);
6511+
if (!this->visit(SubExpr))
6512+
return false;
6513+
if (!this->emitSetLocal(*FromT, TempOffset, E))
6514+
return false;
6515+
if (!this->emitGetPtrLocal(TempOffset, E))
6516+
return false;
6517+
}
64996518

65006519
if (!ToT || ToT == PT_Ptr) {
65016520
if (!this->emitBitCastPtr(E))

clang/test/AST/ByteCode/c23.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: %clang_cc1 -std=c23 -fexperimental-new-constant-interpreter -verify=expected,both %s
22
// RUN: %clang_cc1 -std=c23 -verify=ref,both %s
3+
// RUN: %clang_cc1 -std=c23 -triple=aarch64_be-linux-gnu -fexperimental-new-constant-interpreter -verify=expected,both %s
4+
// RUN: %clang_cc1 -std=c23 -triple=aarch64_be-linux-gnu -verify=ref,both %s
5+
36

47
typedef typeof(nullptr) nullptr_t;
58

@@ -23,5 +26,26 @@ char bar() {
2326
return ((struct S *)buffer)->c;
2427
}
2528

26-
2729
static_assert((nullptr_t){} == 0);
30+
31+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
32+
# define LITTLE_END 1
33+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
34+
# define LITTLE_END 0
35+
#else
36+
# error "huh?"
37+
#endif
38+
39+
typedef unsigned char u8x4_t __attribute__((vector_size(4)));
40+
constexpr u8x4_t arg1 = (u8x4_t)0xCAFEBABE; // okay
41+
#if LITTLE_END
42+
static_assert(arg1[0] == 190);
43+
static_assert(arg1[1] == 186);
44+
static_assert(arg1[2] == 254);
45+
static_assert(arg1[3] == 202);
46+
#else
47+
static_assert(arg1[0] == 202);
48+
static_assert(arg1[1] == 254);
49+
static_assert(arg1[2] == 186);
50+
static_assert(arg1[3] == 190);
51+
#endif

0 commit comments

Comments
 (0)