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

Add liveness analysis to optimise local var init #327

Open
drcjt opened this issue Aug 6, 2023 · 3 comments
Open

Add liveness analysis to optimise local var init #327

drcjt opened this issue Aug 6, 2023 · 3 comments
Labels
tenet-performance Performance related issue

Comments

@drcjt
Copy link
Owner

drcjt commented Aug 6, 2023

Code generator emits code to initialise locals as part of method prolog, see CodeGenerator.GenerateProlog(..). Similarly the LocalHeapCodeGenerator.GenerateCode(..) does initialisation of localloc memory.

Currently the entire of the locals/localloc is initialised - this is suboptimal if assignments are made to the locals before use. To detect this liveness analysis needs to be implemented and used to set the LocalVariableDescriptor.MustInit flag. The init code can then just init the vars that have been marked as needing it.

@drcjt drcjt added the tenet-performance Performance related issue label Aug 6, 2023
@drcjt
Copy link
Owner Author

drcjt commented Aug 7, 2023

Simple example to use for testing, note this is using unsafe code:

int i;      // must be init
int j = *&i;    // no need to init j
Console.WriteLine(j);

@drcjt
Copy link
Owner Author

drcjt commented Aug 11, 2023

For structs generally IL will contain a ldloca before a stfld or a ldloc before a ldfld. For the store the ldloca will be interpreted in the liveness analysis as a use of the struct thus resulting in the struct being marked as must-init. So currently the prolog code will be zeroing the struct.

If a constructor is used for the struct then potentially there is double initialization happening. However the current implementation is conservative and does not try to deal with this.

@drcjt
Copy link
Owner Author

drcjt commented Aug 11, 2023

In terms of testing zero initialization of locals - it looks like these are the tests in dotnet runtime: https://github.com/dotnet/runtime/tree/0916f7ba98716b18ccfa60081befa164bec3c203/src/tests/JIT/Directed/zeroinit

This was referenced Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

1 participant