[libc-commits] [libc] [libc] Read known objects bytewise in the clang_vector string loads (PR #236612)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Thu Oct 29 19:14:08 PDT 2026


================
@@ -28,6 +28,17 @@ LIBC_INLINE constexpr cpp::simd_mask<byte_ty> shift_mask(cpp::simd_mask<char> m,
 LIBC_NO_SANITIZE_OOB_ACCESS LIBC_INLINE size_t string_length(const char *src) {
   constexpr cpp::simd<char> null_byte = cpp::splat('\0');
 
+  // The vector loads below read outside the string on purpose. That is only
+  // sound when the compiler cannot see the object, since it treats a read
+  // outside a known object as undefined. Once a call is inlined on a known
+  // array, as under LTO, the size is known, so read it bytewise.
+  if (__builtin_object_size(src, 0) != static_cast<size_t>(-1)) {
----------------
jhuber6 wrote:

Do we not need to check both bounds like I do in the printf example? And I'm a bit confused here. Isn't the point that we could get the size of the object from the builtin directly?

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


More information about the libc-commits mailing list