What are trivial types exactly?

I am reading about the various unsafe, raw or untyped, possibly mutable pointers (this is in the context of MTLBuffer used to pass data between the GPU and CPU). The term “trivial types” comes up a lot, I’ll quote the Note below:

A trivial type can be copied with just a bit-for-bit copy without any indirection or reference-counting operations. Generally, native Swift types that do not contain strong or weak references or other forms of indirection are trivial, as are imported C structs and enums.

To me that feels written a bit too vague and non-specific, as if to leave the door open for changes. Is there a precise definition somewhere with the trivial types listed specifically?

Also, when copying their values, does one have to worry about endianness, alignment, and padding? It seems not and in my specific—and so far very simple—use cases (writing in CPU land, reading in GPU land) things seem to work but what guarantees they will continue to on future Apple architectures?

This is older terminology from before there was any language level support for specifying or checking this behavior, It's by analogy to C++'s TrviallyCopyable. The current way to test for this is to make sure the type in question conforms to BitwiseCopyable.

3 Likes

Metal Shading Language is explicitly designed to support importing the same struct declaration in both your CPU and shader code. From this it follows that the default endianness, alignment, and padding must match between the Clang and Metal compilers that ship with Xcode. (To get this guarantee in Swift, you have to declare the struct in C and import it.) YMMV with other compilers/toolchains.

1 Like

Yes. Hopefully, @Cwill be extended to support structs, but until then, any non-enum datatype does have to be defined in a C header. Although, I think primitive types CInt, Float, Double, etc. (and InlineArrays thereof) are also guaranteed to be appropriately layout compatible.

Thanks for the tip, that makes it easier to figure out what’s possible.

By this, do you mean the bridging header as in described here?

Yes, your shared data types should be declared in a C header that is #imported by your project’s Objective-C and Metal source files as well as your project’s Swift bridging header.