dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.2k stars 1.57k forks source link

make functions frameless if they don't need a frame #43208

Open mraleph opened 4 years ago

mraleph commented 4 years ago

If a function is a leaf and does not contain any spills it does not need a frame.

POC https://github.com/mraleph/sdk/commit/a70a694193917f8f43b47ebd47102346daad5736 reveals that a lot of functions in Flutter Gallery fit this definition - and dropping frame setup leads to a significant code size improvement (and potentially performance improvement as well).

Store barrier introduces a slight complication into this on ARM/ARM64 because of its outlined slow-path. This slow-path can only call leaf-runtime entry, but nevertheless calling the slow-path clobbers LR. We already support generating store barriers in intrinsics - which are also frameless and thus don't have LR saved - however this introduces explicit pushing and popping of LR which needs to be taken into account when deciding if it is worth to setup a frame (e.g. POC allows at most 1 instruction with a store barrier per frameless function). One dodgy part here is the lack of static guarantee: location summaries capture which instructions perform calls and which don't, with the exception of store barrier call. I suggest that turning POC into a real implementation requires at least adding an assertion that checks that branch-link instruction is only allows to be emitted when IL instruction is marked as clobbering LR.

part of go/dart-llvm-assessment

/cc @mkustermann for triage and assignment

mraleph commented 4 years ago

While discussing this with @mkustermann I have done a bit of digging into why this had such a drastic effect on Flutter Gallery size. Turns out that it disproportionally hits localisation related getters which are generated in large numbers on Flutter Gallery because it supports a lot of different locales. We have 83906 frameless functions and 21303 framefull functions - however out of 83906 frameless functions 80938 originate from localization related generated code.

This means on an average Flutter application this is not likely to have similar impact (unless of course it includes similar amount of localizations done in a similar way).

/cc @alexmarkov

mkustermann commented 4 years ago

We already had a bug for this in https://github.com/dart-lang/sdk/issues/40195 . Due to more context here I'll close the other issue.

@sstrickl is going to work on this soon.

mkustermann commented 3 years ago

@mraleph Since you made the PoC, maybe you can upstream them as well?