-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
make @memcpy
and @memset
use slices with any element type instead of pointers with bytes
#14040
Comments
Now they use slices or array pointers with any element type instead of requiring byte pointers. This is a breaking enhancement to the language. The safety check for overlapping pointers will be implemented in a future commit. closes #14040
Now they use slices or array pointers with any element type instead of requiring byte pointers. This is a breaking enhancement to the language. The safety check for overlapping pointers will be implemented in a future commit. closes #14040
FWIW, in Tigerbeetle we have separate |
Thanks for the insight, @matklad. I put some more thought into it and decided to make it the "exact" behavior. So, |
Now they use slices or array pointers with any element type instead of requiring byte pointers. This is a breaking enhancement to the language. The safety check for overlapping pointers will be implemented in a future commit. closes #14040
Now they use slices or array pointers with any element type instead of requiring byte pointers. This is a breaking enhancement to the language. The safety check for overlapping pointers will be implemented in a future commit. closes #14040
Now they use slices or array pointers with any element type instead of requiring byte pointers. This is a breaking enhancement to the language. The safety check for overlapping pointers will be implemented in a future commit. closes #14040
Now they use slices or array pointers with any element type instead of requiring byte pointers. This is a breaking enhancement to the language. The safety check for overlapping pointers will be implemented in a future commit. closes #14040
Now they use slices or array pointers with any element type instead of requiring byte pointers. This is a breaking enhancement to the language. The safety check for overlapping pointers will be implemented in a future commit. closes #14040
New prototypes:
dest
must be a slice of any element type.src
must be a pointer or a slice. It gets sliced from 0 to the same length asdest
, triggering the same set of safety checks and possible compile errors assrc[0..dest.len]
would.It is illegal for
dest
andsrc[0..dest.len]
to overlap. If safety checks are enabled, there will be a runtime check for such overlapping.dest
must be a slice of any element type.value
is coerced to the dest element type.Motivation
Such operations are common, and currently
std.mem.copy
andstd.mem.set
are used for this purpose. However,std.mem.copy
does not have the non-overlapping requirement. This restriction allows for better code generation and additionally allows lowerings to call directly into aligned memset/memcpy implementations without runtime branching.For example,
@memcpy
used to copy objects of typestruct { x: u64, y: u64 }
, when targeting a CPU with avx512 instructions, could lower directly to a memcpy implementation which uses these instructions and would not need any extra logic to handle the beginning or end of the memcpy operation.In summary, this would both increase convenience for the programmer as well as the compiler's ability to optimize.
Implementation Notes
Be sure to set the alignment of the memcpy to the minimum alignment of the two slices, taking into account over-aligned pointers. LLVM's memcpy intrinsic does not have the ability to annotate that the number of bytes will be a multiple of the element size, so this is potentially an advantage Zig codegen backends could have over LLVM.
The text was updated successfully, but these errors were encountered: