Skip to content

Optimizer/IRGen: allow enums in static initializers of globals #66118

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

Merged
merged 6 commits into from
May 26, 2023

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented May 25, 2023

The main change in this PR is in IRGen which needs to be able to emit constant enum values.
Use emitValueInjection to create the enum constant. Usually this method creates code in the current function. But if all arguments to the enum are constant, the builder never has to emit an instruction. Instead it can constant fold everything and just returns the final constant.

This PR also contains a few other statically-initialized-global related improvements:

  • create statically initialized let-globals as constant global (constant instead of global)
  • simplification for the init_enum_data_addr instruction
  • simplification for the value_to_bridge_object instruction

For details see the commit messages.

rdar://26521135

@eeckstein eeckstein requested a review from aschwaighofer May 25, 2023 07:27
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

eeckstein added 6 commits May 25, 2023 16:28
…ction

This comes up in the code for constructing an empty string literal.
With this optimization it's possible to statically initialize empty string global variables.
Optimize the sequence
```
  %1 = init_enum_data_addr %enum_addr, #someCaseWithPayload
  store %payload to %1
  inject_enum_addr %enum_addr, #someCaseWithPayload
```
to
```
  %1 = enum  $E, #someCaseWithPayload, %payload
  store %1 to %enum_addr
```
This sequence of three instructions must appear in consecutive order.
But usually this is the case, because it's generated this way by SILGen.
… in static initializers

Instead of passing `IRGenFunction`, pass the `IRGenModule` and the `IRBuilder`.
This makes enum creation not dependent on the presence of a function.

NFC, just refactoring.
The main change here is in IRGen which needs to be able to emit constant enum values.
Use `emitValueInjection` to create the enum constant.
Usually this method creates code in the current function.
But if all arguments to the enum are constant, the builder never has to emit an instruction.
Instead it can constant fold everything and just returns the final constant.

Also, create statically initialized let-globals as constant global (`constant` instead of `global`).
@eeckstein
Copy link
Contributor Author

@swift-ci test

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IRGen part looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants