Hmm. Well, I won't promise that this is going to be easy.
I think the best approach here is to allow generic parameters to be constrained to only apply to "trivial" types (meaning "plain old data" types that can be copied with
memcpy), then add a constrained builtin that performs an unaligned load, then implement
UnsafePointer.loadUnaligned using that builtin. The "trivial-only" type constraint will require an evolution proposal, as will
loadUnaligned; technically we could allow
loadUnaligned to be constrained to trivial types "magically" without proposing an official way of spelling that, but I think that would be a mistake.
So, the first step is probably to start a new thread in the Compiler Development category.
Most of the infrastructure for trivial type constraints is already implemented using the spelling
_Trivial. The main thing that's missing is type-checking: you can declare a type parameter to be constrained to be trivial, but we don't actually check that. So the first step would be to actually add that type-checking; this is probably the hardest part. I think this is a matter of grepping lib/Sema for
LayoutConstraint and trying to fix those places to handle
Trivial, not just class constraints.
For the builtin, you would basically copy how
LoadRaw is handled, at least up until SILGen; we'll need to talk with the larger team about how to represent it in SIL. I suppose adding another flag to
LoadInst would be consistent. In IRGenSIL, you would then check for unaligned loads and then emit them differently; we can talk about that when we get to that point.