[clang] [clang] Implement constexpr bit_cast for vectors (PR #66894)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 1 13:59:27 PDT 2023


================
@@ -7304,6 +7382,21 @@ class BufferToAPValueConverter {
     return ArrayValue;
   }
 
+  std::optional<APValue> visit(const VectorType *Ty, CharUnits Offset) {
+    SmallVector<uint8_t, 8> Bytes;
+    if (!Buffer.readObject(Offset, Info.Ctx.getTypeSizeInChars(Ty), Bytes))
+      return std::nullopt;
----------------
zygoloid wrote:

The object representation indeed does not seem to initialize the padding bits: https://godbolt.org/z/szWenbE8c

- In Clang, the padding bytes contain non-zero values for both vectors and structs.
- In Clang msan, only the branch on uninitialized padding in the struct is caught. (I think this is an msan bug; it presumably isn't taking into account that a vector type might contain padding.)
- In GCC, the padding bytes in the vector are zeroed but the padding bytes in the struct are not. (Though that could be an accident of implementation rather than intent.)

Casts between vector types don't seem to initialize padding bits either: https://godbolt.org/z/Gex93x3xx -- so the test in const-init.c seems to be wrong.

> Should I rewrite all the bit cast conversion code in `APValueToBufferConverter`/`BufferToAPValueConverter` again while taking uninitialized padding bytes into account, and redirect the existing `CK_BitCast` code to point to that?

:( Yeah, I guess so. Sorry.

https://github.com/llvm/llvm-project/pull/66894


More information about the cfe-commits mailing list