New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-10273] Add an UnsafeRaw[Buffer]Pointer API for loading and storing unaligned/packed data #52673
Comments
Misaligned loads should only be allowed for POD types. For example, we could reimplement the existing `load` and `storeBytes` using an unaligned Builtin, then broaden the existing assert so that it only performs the alignment check for non-POD types. |
It would not be difficult to implement this feature but it does require basic knowledge of all the layers of the compiler. Roughly, I think we need something like: Define a new AST Builtin for unaligned loads. In SILGen, add an unaligned flag to `emitBuiltinLoadOrTake` Add an unaligned flag to PointerToAddressInst in SILInstruction.h Add all the serialization/deserialization support for that flag. Change IRGen/GenType.cpp to check for this flag here: Address TypeInfo::getAddressForPointer(llvm::Value *ptr) const {
assert(ptr->getType()->getPointerElementType() == StorageType);
return Address(ptr, getBestKnownAlignment());
} |
Once we have a prototype, we need a Swift Evolution proposal to decide whether to change the default UnsafeRawPointer behavior or simply an a new flag. |
Does the new builtin offer us any benefits over just using memcpy? |
(which would make this a Standard-Library-only feature request) |
The cleanest thing to do would probably be to either
|
Comment by Rick M (JIRA) Whatever it is would ideally inline nicely. |
When the initial API was written, we weren’t sure how we wanted to allow opt-in/opt-out of alignment. It’s possible to move toward allowing misalignment, but not he other way around, hence the current behavior., At this point, it’s obvious to me that misalignment should be the default and we need a separate alignment opt-in for code that wants to be vectorized. Meanwhile, the current workaround goes something like this: func loadFromData<T>(data: Data, defaultVal: T) -> T {
data.withUnsafeBytes { dataBytes in
var value = defaultVal;
memcpy(&value, dataBytes.baseAddress!, MemoryLayout<T>.size)
return value
}
} |
Additional Detail from JIRA
md5: d398d32b35be5db5843911e8ca5112f6
Issue Description:
Swift does not currently provide any reasonable way to load potentially misaligned data from a raw byte buffer.
This API currently requires an aligned pointer:
Users need to be able to do something like this:
We should either remove the alignment restriction from the `UnsafeRawPointer.load` API, or add a new flag to allow unalgined loads. The current default requires alignment because:
ignoring alignment may indicate a programmer error, this way you're forced to think about alignment and make it explicit
we wanted higher-level APIs to be expressible in terms of raw pointers without changing semantics or losing performance
we can loosen the requirement over time but can't strengthen it
The text was updated successfully, but these errors were encountered: