capy icon indicating copy to clipboard operation
capy copied to clipboard

Store some variables in registers instead of always using the stack

Open NotAFlyingGoose opened this issue 1 year ago • 2 comments

Problem

Currently, all local variables are stored in stack slots. This can create unnecessary stack stores and loads even when a variable's address is never obtained

Proposed Solution

codegen should check to see if a variable is ever aliased with ^. This could probably be done in either the hir or hir_ty crates during lowering or type checking. Maybe when an Expr::Ref is created/parsed it would check to see if it's inner expression was a Variable before marking that variable as "aliased".

codegen would then check this flag (and Ty::is_aggregate) to know if it should allocate stack space for the variable or if it should instead use cranelift_frontend::Variable

Notes

Future optimizations could probably be made if it's known that a variable is never aliased. Structs could be split up into separate cranelift_frontend::Variables for their different fields. Although this would also need to check if a variable was passed as an argument.

NotAFlyingGoose avatar Jun 23 '24 22:06 NotAFlyingGoose

In the same stroke it would probably be good to perform stack_addr instructions only when absolutely necessary. probably would lead to cranelift having to use less fuel on analyzing them. This would probably be possible by having functions like compile_expr_with_args return an enum containing either a stack slot or a value.

lenawanel avatar Jul 02 '24 20:07 lenawanel

I'm gonna start working on this

NotAFlyingGoose avatar Jul 03 '24 15:07 NotAFlyingGoose