[clang] [clang] Implement constexpr bit_cast for vectors (PR #66894)
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 10 23:55:37 PDT 2023
================
@@ -7307,6 +7324,74 @@ class BufferToAPValueConverter {
return ArrayValue;
}
+ std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) {
+ QualType EltTy = VTy->getElementType();
+ unsigned NElts = VTy->getNumElements();
+ unsigned EltSize =
+ VTy->isExtVectorBoolType() ? 1 : Info.Ctx.getTypeSize(EltTy);
+
+ if ((NElts * EltSize) % Info.Ctx.getCharWidth() != 0) {
+ // The vector's size in bits is not a multiple of the target's byte size,
+ // so its layout is unspecified. For now, we'll simply treat these cases
+ // as unsupported (this should only be possible with OpenCL bool vectors
+ // whose element count isn't a multiple of the byte size).
+ Info.FFDiag(BCE->getBeginLoc(),
+ diag::note_constexpr_bit_cast_invalid_vector)
+ << QualType(VTy, 0) << EltSize << NElts << Info.Ctx.getCharWidth();
+ return std::nullopt;
+ }
+
+ SmallVector<APValue, 4> Elts;
+ Elts.reserve(NElts);
+ if (VTy->isExtVectorBoolType()) {
+ // Special handling for OpenCL bool vectors:
+ // Since these vectors are stored as packed bits, but we can't read
+ // individual bits from the BitCastBuffer, we'll buffer all of the
+ // elements together into an appropriately sized APInt and write them all
+ // out at once. Because we don't accept vectors where NElts * EltSize
+ // isn't a multiple of the char size, there will be no padding space, so
+ // we don't have to worry about reading any padding data which didn't
+ // actually need to be accessed.
+ bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
+
+ SmallVector<uint8_t, 8> Bytes;
+ Bytes.reserve(NElts / 8);
+ if (!Buffer.readObject(Offset, CharUnits::fromQuantity(NElts / 8), Bytes))
+ return std::nullopt;
+
+ APSInt SValInt(NElts, true);
+ llvm::LoadIntFromMemory(SValInt, &*Bytes.begin(), Bytes.size());
+
+ for (unsigned I = 0; I < NElts; ++I) {
+ llvm::APInt Elt =
+ SValInt.extractBits(1, (BigEndian ? NElts - I - 1 : I) * EltSize);
+ Elts.emplace_back(
+ APSInt(std::move(Elt), !EltTy->isSignedIntegerType()));
+ }
+ } else {
+ // Iterate over each of the elements and read them from the buffer at
+ // the appropriate offset.
+ CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(EltTy);
+
+ // Special handling for vectors of x86_fp80: use a size of exactly 80 bits
+ // because LLVM stores vector elements without padding
+ if (EltTy->isRealFloatingType() &&
+ &Info.Ctx.getFloatTypeSemantics(EltTy) ==
+ &APFloat::x87DoubleExtended())
+ EltSizeChars = Info.Ctx.toCharUnitsFromBits(80);
----------------
zygoloid wrote:
(Same comment as above.)
https://github.com/llvm/llvm-project/pull/66894
More information about the cfe-commits
mailing list