proposal: cmd/compile: go:readonly
comment directive for body-less functions
#67364
Labels
Milestone
go:readonly
comment directive for body-less functions
#67364
Proposal Details
Add
go:readonly
comment directive for body-less functions indicating the following arguments and any pointers exclusively accessed through a readonly argument wont be modified:The point of this is to hook up with 925d2fb this would allow
[]byte(str)
conversions to be elided when the byte string is passed as a readonly argument to an assembly optimized subroutine.github.com/cespare/xxhash
implementsSum64String(string) uint64
and(*Digest).WriteString(string)
usingunsafe
which allows to skip the allocation either by using(*strings.Reader).WriteTo
orio.WriteString
.Other hashing libraries such as
crypto
do not implement this optimization, this could allow to implement this using compiler optimizations rather than duplicating APIs for[]byte
andstring
. (*note: this assume you are using a devirtualizedhash.Hash
or a concrete hasher implementation, which is now possible and not unrealistic, particularly forh := sha256.New(); h.Write([]byte(str))
patterns)In the future this could improve register allocation in some rare edge cases so I don't expect this to be implemented.
Other stronger compilers like GCC and LLVM might already implement such logic in their register allocators, then work in gofrontend could be much easier (as I assume this would amount to setting up metadata flags on the function call nodes passed to gcc or llvm) so who knows.
The body-less function is allowed to read from memory, which is arguably a side effect when using
-race
altho I don't think very many body-less functions implement any kind of race tracking.For slices the body-less function would assume to not be allowed to write past capacity.
If some memory is accessible through an unknown and a readonly pointer the function is allowed to write to it:
Here the compiler would be allowed to assume only the last 32 bytes of the
x
array are readonly (I expect compilers to not implement this or to assumex
maybe completely overwritten).If a pointer to a pointer is readonly then both memory are readonly:
Note: this apply transitively to any amount of level.
Combining the two edge cases above, if a pointer to a pointer is accessible both through unknown and readonly paths, the body-less function may modify it:
The body-less function is assumed to maybe write to anything part of the global scope:
The text was updated successfully, but these errors were encountered: