llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AVR] Optimize 'call' to 'rcall' for short programs

Open beenshi opened this issue 2 years ago • 1 comments

As a possible optimization, we could look into using rcall instead of call instructions when the target is close enough. For example, here: https://godbolt.org/z/rEz9j71dq (apparently avr-gcc doesn't do this optimization).

int foo(int a, int b) {
    return a + b;
}

int bar(int a, int b) {
    return foo(a, b) + 3;
}

If -ffunction-sections is not used, rcall is both shorter in code size and faster in execution speed.

beenshi avatar Mar 23 '22 02:03 beenshi

We need to investigate this can be done by linker relaxation.

benshi001 avatar Jun 16 '22 04:06 benshi001

What's more, we can check if other relax optimization can be done in lld for AVR.

benshi001 avatar Aug 17 '22 12:08 benshi001

apparently avr-gcc doesn't do this optimization

Link with -mrelax, which performs other optimizations, too. Notice there are cases / sections that must not be optimized like .vectors or .jumptables.

Also if you are relaxing, the assembler must not relax by itself, and all relaxations must be postponed until link.

sprintersb avatar Nov 07 '22 19:11 sprintersb

apparently avr-gcc doesn't do this optimization

Link with -mrelax, which performs other optimizations, too. Notice there are cases / sections that must not be optimized like .vectors or .jumptables.

Also if you are relaxing, the assembler must not relax by itself, and all relaxations must be postponed until link.

Sure. Thanks.

benshi001 avatar Nov 11 '22 09:11 benshi001

It seems impossible to do this call -> rcall transform with clang + gnu-avr-ld.

benshi001 avatar Feb 22 '23 11:02 benshi001

What's the problem? All that has to be done is to pass -mrelax to ld.

sprintersb avatar Feb 22 '23 16:02 sprintersb

What's the problem? All that has to be done is to pass -mrelax to ld.

Sure. It is my mistake. clang's AVR driver does not handle -mrelax/-mno-relax properly.

benshi001 avatar Feb 23 '23 03:02 benshi001

fixed by: https://reviews.llvm.org/D144617 https://reviews.llvm.org/D144620

benshi001 avatar Feb 23 '23 08:02 benshi001

@llvm/issue-subscribers-clang-driver

llvmbot avatar Feb 23 '23 23:02 llvmbot