[clang] [HLSL] Enable InitList code to handle zero sized structs (PR #160355)
Chris B via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 24 11:26:47 PDT 2025
================
@@ -985,3 +995,35 @@ void case18() {
void case19(Unnamed U) {
TwoInts TI = {U, 1};
}
+
+// InitList with Empty Struct on LHS
+// CHECK-LABEL: case20
+// CHECK: [[E:%.*]] = alloca %struct.Empty, align 1
+// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[E]], ptr align 1 [[ConstE]], i32 1, i1 false)
----------------
llvm-beanz wrote:
The C++ object model defines that all objects of a "most derived type" have a non-zero size:
>If a complete object, a data member ([[class.mem]](https://timsong-cpp.github.io/cppwp/n3337/class.mem)), or an array element is of class type, its type is considered the [most derived class](https://timsong-cpp.github.io/cppwp/n3337/intro.object#def:most_derived_class), to distinguish it from the class type of any base class subobject; an object of a most derived class type or of a non-class type is called a [most derived object](https://timsong-cpp.github.io/cppwp/n3337/intro.object#def:most_derived_object).
>
> Unless it is a bit-field ([[class.bit]](https://timsong-cpp.github.io/cppwp/n3337/class.bit)), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class subobjects may have zero size. An object of trivially copyable or standard-layout type ([[basic.types]](https://timsong-cpp.github.io/cppwp/n3337/basic.types)) shall occupy contiguous bytes of storage.
Source: https://timsong-cpp.github.io/cppwp/n3337/intro.object#5
This means that any instance of an object (even an "empty" object), must have a size > 0.
HLSL in DXC doesn't follow this behavior, and we should figure out what we need to do about it, but my gut is that we should align closer with C++. We almost certainly need to prevent storing empty objects in resources or group shared memory, otherwise we would need to make the size of an empty structure 4 rather than 1.
This would be a good topic to discuss in the language design meeting.
https://github.com/llvm/llvm-project/pull/160355
More information about the cfe-commits
mailing list