-
Notifications
You must be signed in to change notification settings - Fork 10.5k
SILOptimizer: add a pass to perform target specific constant folding. #59665
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
Conversation
@swift-ci test |
@swift-ci benchmark |
SIL optimization passes can use the IRGen options to do target specific optimizations.
TargetConstantFolding performs constant folding for target-specific values: ``` MemoryLayout<S>.size MemoryLayout<S>.alignment MemoryLayout<S>.stride ``` Constant folding those expressions in the middle of the SIL pipeline enables other optimizations to e.g. allow such expressions in statically allocated global variables (done by the GlobalOpt pass). The implementation requires to create a temporary IRGenModule, which is used to get actual constant sizes/alignments from IRGen's type lowering. rdar://94831524
b0b3ea3
to
f420468
Compare
@swift-ci test |
I'm in favor of this optimization, but I want to understand why our policy on .swiftmodule target information can suddenly be reversed. I mean, it would have been great to do this 7 years ago. It seems like it should be decided on the forum whether .swiftmodules can embed target information... there are driver implications.
Is it ok to serialize target info as long as we're limited only to SizeOf/AlignOf/StrideOf? Can we embed any target info as long as it is hashed as part of the SILModule's compiler info string? What target info is currently included in that string? It would be nice to decouple client build options as much as possible from module build options. That could avoid confusion reproducing issues and/or unnecessary .swiftmodule rebuilds. But, if all the target information is already embedded in the module, then we might as well be using it in the optimizer! I always imagined that target information would be strictly limited to the "Lowered" SIL stage, and that we would not allow Lowered SIL to be serialized. So we at least preserve some notion of target-independent SIL modules. (We don't currently enter Lowered SIL soon enough, but we could plan to do that.) But, again, if any change to IRGenOptions forces a .swiftmodule rebuild then there's no point worrying about this at all. |
The file header needs comments. I would expect SIL passes to be in /SILOptimizer/Transforms regardless of their dependency on target information. IRGen is really a separate subsystem with 150 source files of its own! If we wanted to respect library dependencies and have some conceptual layering, then we would split IRGen/TypeInfo into a separate ABI abstraction, independent of LLVM IR generation. You would have something like |
It seems a little strange that you silently handle possible truncation. I'm looking for either a bailout on actual truncation, or an assertion that reextending the truncated value yields back the original number. |
This pass runs after serialization. So there is no change in swiftmodule files.
Fine with me. I think it makes sense to add a new sub-directory in SILOptimizer for such optimizations, e.g.
That's what IRGen does if those builtins are not constant folded. But yes, it makes sense to be on the safe side. I'll add a check. |
The TargetConstantFolding pass performs constant folding for target-specific values:
Constant folding those expressions in the middle of the SIL pipeline enables other optimizations to e.g. allow such expressions in statically allocated global variables (done by the GlobalOpt pass).
The implementation requires to create a temporary IRGenModule, which is used to get actual constant sizes/alignments from IRGen's type lowering.
rdar://94831524