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

ASM Optimization for "null operations" #6906

Open
xunilrj opened this issue Feb 9, 2025 · 0 comments
Open

ASM Optimization for "null operations" #6906

xunilrj opened this issue Feb 9, 2025 · 0 comments
Assignees
Labels
compiler: optimization IR Optimization Passes performance Everything related to performance, speed wise or memory wise. team:compiler Compiler Team

Comments

@xunilrj
Copy link
Contributor

xunilrj commented Feb 9, 2025

If we compile the code below with forc build --path {root} --release --ir final.

fn small_array() -> [bool; 5] {
    [true; 5]
}

We see the following ASM:

move $$locbase $sp            ; save locals base register for function small_array_repeat_11
move $r0 $$arg0               ; save argument 0 (__ret_value)
move $r1 $$reta               ; save return address
movi $r2 i1                   ; get array element size
mul  $r2 $zero $r2            ; get offset to array element
add  $r2 $r0 $r2              ; add array element offset to array base
sb   $r2 $one i0              ; store byte
movi $r2 i1                   ; get array element size
mul  $r2 $one $r2             ; get offset to array element
add  $r2 $r0 $r2              ; add array element offset to array base
sb   $r2 $one i0              ; store byte
movi $r2 i1                   ; get array element size
movi $r3 i2                   ; initialize constant into register
mul  $r2 $r3 $r2              ; get offset to array element
add  $r2 $r0 $r2              ; add array element offset to array base
sb   $r2 $one i0              ; store byte
movi $r2 i1                   ; get array element size
movi $r3 i3                   ; initialize constant into register
mul  $r2 $r3 $r2              ; get offset to array element
add  $r2 $r0 $r2              ; add array element offset to array base
sb   $r2 $one i0              ; store byte
movi $r2 i1                   ; get array element size
movi $r3 i4                   ; initialize constant into register
mul  $r2 $r3 $r2              ; get offset to array element
add  $r2 $r0 $r2              ; add array element offset to array base
sb   $r2 $one i0              ; store byte
move $$retv $r0               ; set return value
move $$reta $r1               ; restore return address
poph i524288                  ; restore registers 40..64
popl i15                      ; restore registers 16..40
jmp $$reta                    ; return from call

We can see a lot of "null operations" such as mul by zero and others.
For example:

movi $r2 i1                   ; get array element size
mul  $r2 $zero $r2            ; get offset to array element
    ; $r2 is guaranteed to be one. We could use
    ; $one and kill the movi above
    ;
    ; but we are multiplying by zero
    ; so we can remove this mul and the movi
    ; and just use $zero
add  $r2 $r0 $r2              ; add array element offset to array base
    ; we would replace $r2 with $zero here
    ; but sum by zero is useless
    ; we know that $r2 = $r0
    ; remove this add
sb   $r2 $one i0              ; store byte
    ; replace $r2 by $r0 here

We already have these kind optimizations, but it seems there is still space to improve them.

@xunilrj xunilrj self-assigned this Feb 9, 2025
@IGI-111 IGI-111 added the team:compiler Compiler Team label Feb 10, 2025
@IGI-111 IGI-111 added compiler: optimization IR Optimization Passes performance Everything related to performance, speed wise or memory wise. labels Feb 11, 2025 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: optimization IR Optimization Passes performance Everything related to performance, speed wise or memory wise. team:compiler Compiler Team
Projects
None yet
Development

No branches or pull requests

2 participants