Skip to content
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

compiler: remove unused struct fields #21010

Open
andrewrk opened this issue Aug 9, 2024 · 7 comments
Open

compiler: remove unused struct fields #21010

andrewrk opened this issue Aug 9, 2024 · 7 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. optimization use case Describes a real use case that is difficult or impossible, but does not propose a solution.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Aug 9, 2024

Unfortunately we cannot have a compile error for unused struct fields (#12854) since whether a field is used depends on semantic analysis (#3028). However, for structs that have automatic layout, it is legal for the compiler to remove fields that are not used. The compiler should do that automatically.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. optimization labels Aug 9, 2024
@andrewrk andrewrk added this to the 1.1.0 milestone Aug 9, 2024
@silversquirl
Copy link
Contributor

silversquirl commented Aug 9, 2024

Wouldn't this mean no function that uses a struct with possibly-unused fields can be sent to code generation until all functions have been analyzed? Since a function analyzed later could use the field and thus affect the codegen of previous functions.
Seems like the compile time performance implications would be pretty significant

@andrewrk
Copy link
Member Author

andrewrk commented Aug 9, 2024

That's a great point, and since it might have compiler design implications, it means it needs to be tackled earlier.

@andrewrk andrewrk modified the milestones: 1.1.0, 0.15.0 Aug 9, 2024
@mlugg
Copy link
Member

mlugg commented Aug 9, 2024

Yeah, this isn't feasible as-is for precisely the reason silversquirl gave above. Moreover, any semantic analysis which requires resolving the type layout -- for instance, @sizeOf -- would require first resolving all possible usages of the type. So, we have an absurd situation where @sizeOf logically depends on the entire program -- including the code following itself -- being analyzed.

@mlugg
Copy link
Member

mlugg commented Aug 9, 2024

If we go ahead with SpexGuy's idea to make @field and friends take an @Type(.EnumLiteral) rather than a string, then we could define a sufficient condition for omitting a field: if that identifier never occurs in any other type field, enum literal, or field access, in the entire ZCU. However, this condition is so narrow as to be entirely useless.

@andrewrk andrewrk added the use case Describes a real use case that is difficult or impossible, but does not propose a solution. label Aug 9, 2024
@andrewrk
Copy link
Member Author

andrewrk commented Aug 9, 2024

It would be possible to combine these two things:

  • compile error for unused field (based on semantic analysis results)
  • ability to declare any field unused (including in transitive dependencies)

in order to trade programmer effort for optimality. It would probably be a huge pain in the ass though.

@mlugg
Copy link
Member

mlugg commented Aug 9, 2024

Hmm -- so for every field, you can mark it (fictional syntax) unused(is_field_unused) (default is unused(false)), and you get a compile error if it was used and marked unused or vice versa? Yeah, that could work, but I anticipate it being a huge PITA. And it makes the benefits of this proposal dubious -- at that point, just make the field type conditional, marking it void if it'll be unused.

@andrewrk
Copy link
Member Author

andrewrk commented Aug 9, 2024

My point is that the unused declaration would exist potentially even outside the module that declares the struct. The struct would be declared like normal (consider the motivating use case of #20977).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. optimization use case Describes a real use case that is difficult or impossible, but does not propose a solution.
Projects
None yet
Development

No branches or pull requests

3 participants