-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
structs: add HostLayout "directive" type #66408
Comments
How about |
Will layout changes be guarded behind the module's Go version? I would find that important for the |
This proposal is just part 1, adding the Part 2, actually changing the layout of unadorned structs, is not part of this proposal. Of course, this proposal has less motivation if part 2 never happens. |
Being part of the type system is a bit weird. Can i throw it in in a interface{}? What happens when i take a pointer to it and it's set to nil. Can i make it generic with type parameters? |
@nemith All of those will work fine. This proposal just changes the layout of the fields within a struct. Field layout is already fully described by the |
@nemith Part of the reason for putting it into the type system is so that all the other Go tools understand it, from the point-of-view of type comparison, identity, etc. |
If/when additional signal types get added, what are the semantics of including more than one in the same struct? Will it be required that all signal types have orthogonal semantics, will it be a compile time error if they are incompatible? What if they are only incompatible on one platform/OS pair, would there be a vet check to alert someone that doesn't explicitly try that combination to the potential issue? |
I see no reason to require orthogonal semantics ("PlatformLayout" overlaps with "DeclaredLayout", which is not yet proposed but I can imagine it) but I do think that incompatible combinations should be diagnosed at compile time. And looking at a plausible interaction with alignment specification (the other/next layout tag I expect to someday see) I can construct plausible examples that would use both. I think for this particular tag it would be reasonable to require that it precede any other field that has non-zero size. My goal is to have as few of these tags as possible, motivated by real problems, so hopefully there will not be many combinations that apply, I think it is fine for the compiler to reject any combinations that are problematic. Even one of these tags should be a niche case; two should be niche-squared. HOWEVER: This might get messy for special C-hardware-specific types, for example, those used to talk about xmm and ymm registers, that currently have no Go equivalent. There are several approaches to that problem and I am not sure which is best; the existence/names of the very-wide data types is platform-dependent for C compilers, but Go could decide to just generally support 128 and 256-bit integers. Or, we could add per-field type tags for alignment that would precede 128-bit or 256-bit fields. So, something like:
or
(I added the boolean field just to make it clear that the hypothetical I prefer the choice where the programmer doesn't need to go read documentation to figure out what the C compiler is doing. On the other hand, after quickly checking what the internet to see what the YMM alignment rules are (and discovering a mess with annoying special cases), I can understand needing to be able to specify a platform order yet also be very picky about the alignment. Because of that, I think that specifying both platform layout and specific (increased) alignment for certain fields should be allowed. Specifying reduced alignment is probably a compile-time error, certainly taking the address of a field with reduced alignment is a compile-time error. (Why is taking the address of a reduced-alignment field a likely error? The compiler would prefer to use the fast, assume-that-integers-are-aligned, instructions for dereferencing a I am not sure if this is right for a vet check or not; vet would need to know a lot about different architecture and OS combinations and their C compilers. A different and slightly interesting question is what should happen if a struct tagged with "platform" layout contains a type that is inherently Go-oriented, like slice or map, or one for which Go has its own alignment assumptions. My inclination is to say that right now vet isn't checking any of this; platform interchange types are already a niche, and weird combinations of type tags (that don't exist yet) and/or Go types is a hypothetical niche of that niche. |
@mvdan proposed improved naming, included at the top for anyone coming upon this later and not wanting to slog through comments: After reflecting on the discussion below, I would modify this to:
the rationale for the name change is that |
Platform is a bit odd since Go is a platform too, and "the GOOS-GOARCH target platform" sounds like Go too. |
Would this also affect the alignment of the struct on the stack? |
@ydnar - it depends. On some architectures the stack is not very aligned, and so extra-aligned data is heap-allocated instead. Otherwise, yes, probably. And I am fine with |
This proposal has been added to the active column of the proposals project |
Have all remaining concerns about this proposal been addressed? The proposal is to add
that can be added as a field named |
Would like to additionally require that this directive-typed field must appear before any field with greater-than-zero size, if that's okay? (This should not be requirement for some imagined other directives, e.g., explicit next-field alignment.) |
What advantage do we get from imposing such a requirement? |
Simplifies the implementation ever-so-slightly (avoids a pre-pass over structure fields), also makes its use more uniform, and I don't see much harm in the restriction (which could be relaxed later if I turn out to be wrong in "not much harm"). |
My take on it is that the advantages we get from the restriction aren't worth the cost of complicating the spec by adding the restriction. |
Would the field be zero sized if it's the last _ field in a struct? |
Change https://go.dev/cl/578355 mentions this issue: |
I’m referring specifically to 6f07ac2:
|
Will field types also get HostLayouted automatically? Report error when a HostLayouted struct has a field of a non-HostLayouted type? type A struct {
x byte
y int64
z byte
}
type B struct {
_ structs.HostLayout
a A
}
var a A
var b B |
IMHO, the structs.AlignN should be supported in the initial release of the |
I'm not sure I fully understand what you're asking, but in terms of your example, the layout of
We're aiming to define We'd love to do alignment markers, but that's not going to happen in the next week and a half. |
|
I interpret these differently, and narrowly. HostLayout is not and should not be viral "up" or "down", nor should its signal properties survive various type transformations. For the up case specifically, I could have some larger Go type that includes some data that is passed to/from some host-linked call. E.g.
The only type that has host layout is In the "down" case, perhaps it should be an error if nested structs lack a host layout marker since that indicates a likely mistake, but they should not automatically inherit it. That would be confusing to tools and the type system; the reason we use a marker type is so that the types with different layout are also, obviously, different types. Consider this example:
What are the layouts of the structs referenced by pointers My plan for the signal types is that they are intended only to be signal types, and that there is no grand theory of inheriting properties any more than is required by their intended semantics. If a field has a type that is If a programmer wants host layout, this defines a mechanism, use that mechanism, as defined. Don't infer the existence of mechanisms that were not defined, those won't work. (Why would you do that, anyway?) My larger goal, in general, is to take weird host-specific stuff that complicates life for plain Go programmers, and wall it off so that plain Go programming is better/easier/faster. Defining this narrowly is the best way to advance that goal (I think). |
Okay, I can buy that argument. One thing that worries me though is arrays. E.g., suppose a 32-bit host has a rule that int64 must be 8 byte aligned. How do I create an array of int64s with host layout? It would be nice if you could write
But I agree that HostLayout shouldn't (and can't) "reach down". So I think what you have to write is
That's cumbersome, but at least Maybe that's good enough. Maybe we don't need complicated composition rules for HostLayout itself because any important implications become properties of the type that can then flow up (also, I can't think of any properties besides alignment this would apply to). |
Arrays are an issue, but besides what you propose, I think we could get that alignment with
this relies on (1) even with HostLayout activated, the Go compiler continuing to apply its zero-sized alignment rules, and |
About naming: package |
@dolmen, that's true but NativeLayout doesn't make as much sense as HostLayout. |
In proposal review, we talked about the case of
and concluded that it's okay for HostLayout to affect the order and offsets of fields, including |
A related question, given:
Does U get host layout? Because T is embedded, U technically has a |
I don't think that |
To put useful information in one place, from the CL, there was discussion of whether the struct needs an internal field of package-private zero-sized type, and the answer is that yes it should have one, what we do with it in the future is TBD but it provides an option for the future implementation. The "test" program has types T, AHL,HL, U, V, W, X, Y, Z, G, GP where:
With an internal private tag field, it is straightforward (I've written the code for testing) to implement either "{T,U,V,W,Z,GP} have host layout" or "{T,V,Z,GP} have host layout". I think this gives us the right amount of virality; my preference (which is not necessarily everyone's , but it's the proposal-author's intent) is that this is supposed to also be a signal to readers that a type is special and the field order matters, and writing cute code that obscures this signal is anti-recommended. I'm not super thrilled about the But maybe other people understand this differently.
The two versions of
|
Is there an existing open issue for (it's great to see the Edit: I was pointed to #67265 |
If I were to type-cast a C-struct value to a Go-struct value with the same field alignment, can I rely on this behavior in the future? Or will those Go structs still need |
We’re adding support for If a struct embeds an alias for |
As per #66408 (comment):
|
Proposal Details
Abstract
This proposes a new package for zero-sized types whose presence in a structure’s list of fields would control how the compiler lays out those fields, for the purpose of allowing programmers to indicate which structures are interchanged with the host platform and to request a host-compatible layout for those structures.
Background
While the Go language specifies very little about struct layout, in practice the Go implementation is tightly constrained to follow platform layout and alignment rules because of the few cases where a struct is interchanged with a platform API (and where this is not true it creates the possibility of incompatibility, for example, ppc64le, where the platform alignment for float64 fields is different from Go's default). This forces tradeoffs or potential problems on platforms whose constraints differ from common-case on other platforms (that is, what the Go compiler has adopted as its default) and prevents field reordering optimizations that can save memory and improve garbage collection performance.
Proposal
To address this, we propose a family of zero-sized types for struct fields to signal differences in layout or alignment where that matters. The change in the compiler’s behavior should be invisible to pure Go programs that do not use unsafe or interact with the host platform. The goal of the proposal is that programmers be able to ensure that data exchanged with the host platform have a host-compatible layout, both now, and in the face of future layout optimizations.
Subject to discussion, the proposal is this package and (for now) this one type:
After reflecting on the discussion below, I would modify this to:
the rationale for the name change is that
structs
is one word, and parallelsstrings
,bytes
, andslices
, and is generic enough to include other (future) tags specifying "nocopy" or alignment. Furthermore, such type-modifying tags only work within structures; the package name strongly hints at this.Rationale
One platform, WASM, has system interfaces that align 64-bit types to more than register size and another, ppc64le, has the possibility of non-Go interfaces that align some 64-bit types to less than register size, and both of these are contrary to the rules that Go normally follows (on ppc64le, we have handled this problem using luck). Signaling these constraints explicitly will help compatibility with these two platforms, preserve/allow implementation flexibility, perhaps make it easier to write checking tools, and perhaps (once types passed to all non-Go calls are properly tagged) allow the Go compiler to reorder structures to use less memory and save GC time by shuffling pointers as early as possible in untagged structures. This optimization is desirable because it automates something humans currently spend time on and don't always get right, and sometimes forces programmers to make compromises between most-readable code and best performance.
The most important part of this proposal is that unless someone is writing code that interacts with the platform, they do not need to know about this. If they are writing cgo, these signal types will be inserted for them.
The compiler will know the meaning of these types and modify struct alignment and layouts accordingly. It’s not clear to me whether
Platform
is adequate to capture all the cases of non-Go code, but for the current use cases (platform interfaces across all the various platforms, and cgo -- as far as I know “platform” describes their needs) it appears that it is.Why signal types versus
//go:platform
?It is a better match for the Go type system if changes in types are expressed in the type system itself. Use of field signal types meets this requirement, since the Go type of a struct depends on the fields of the struct, even if they have zero width.
Why just one
platform
tag instead of finer control?In practice, the use case is platform compatibility, and
platform
is a concept that the compiler can translate to the appropriate ARCH-OS combination without demanding that the user know the details, and those details also might not be portable across platforms even when the C type declarations are the same.In the future, we could consider adding signal types for
CacheAlign
,AtomicAlign
, orPacked
but I would not include those at first because I'm not that sure we need them, we might argue about definitions, and their implementation (forPacked
, at least) would be somewhat more costly. A non-layout signal type that might work well is “NoCopy” to indicate to vet that a type should not be copied once it has a non-zero value (this is currently implemented by vet knowing that certain types are “special”).Related: “proposal: spec: define/imply memory layout of tagged struct fields #10014”. This was a very similar proposal, approaching the problem from a slightly different direction, but did not address the issue of "the platform does not match Go's defaults". The new proposal here is more concrete in “how”, includes tweaking alignments to conform to platform constraints, but does not expect someone using the platform tag to know precisely what rules a particular platform uses.
Related: “proposal: runtime: add AlignedN types that can be used to increase alignment #19057”. This was a proposal for a family of types for specifying specific alignments, perhaps of specific fields. That proposal had additional use cases -- specifying higher alignment for various fields -- but also did not address the problem of reduced platform alignment (e.g., ppc64le float64) and its application to specific platform interfaces would require that programmers know the details of that platform’s layout rules (instead of the Go compiler/runtime knowing those details once).
Related: “proposal: cmd/compile: make 64-bit fields be 64-bit aligned on 32-bit systems, add //go:packed directive on structs #36606”. This proposal took the opposite approach -- 64-bit atomics require 64-bit alignment on 32-bit processors, therefore Go should change its default layout, rather than signaling specific types that needed this alignment. It also included a secondary proposal for “packed” types that had a far more annoying implementation burden (how is the pointer addressed in a “packed”
struct {uint8; *int}
? How does the GC find this pointer?)Compatibility
Working old code will continue to work properly.
Implementation
Besides the proposed package and type,
cmd/compile/internal/types/size.go
will need adjusting to follow the signal types. It already contains special case code forsync/atomic/align64
, so this is not outlandish.Open issues
The names. For example, “structlayout”, versus “typelayout”? If we decide that this is a good place for 0-width signal types, some of them (
NoCopy
) aren’t about type layout which means whatever-layout isn’t quite right.The text was updated successfully, but these errors were encountered: