llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.06k stars 11.59k forks source link

MIPS tail calls are disabled by default at all optimization levels #107730

Open InBetweenNames opened 2 weeks ago

InBetweenNames commented 2 weeks ago

Hello, I noticed that tail call optimization is disabled on MIPS at all optimization levels by default. This is causing issues with the musttail attribute in C and C++ programs, where tail calls that otherwise would be eligible are not being treated as such.

The relevant flag is defined here:

https://github.com/llvm/llvm-project/blob/98563b19c2d13bac7fb5ee2717ccae6279699e4c/llvm/lib/Target/Mips/MipsSEISelLowering.cpp#L54-L56

When building with -mllvm -mips-tail-calls=1, tail calls appear to be optimized accordingly (in the few tests I've done). I noticed that there are test cases for this flag throughout the backend (just search for mips-tail-calls=1).

Here are a few issues that are affected by this behaviour: https://github.com/llvm/llvm-project/issues/47971 https://github.com/llvm/llvm-project/issues/51709 https://github.com/llvm/llvm-project/issues/57795

Is it possible we could make the MIPS backend behave like the others in this respect? It seems to me that if a call site is marked musttail, the backend should make every effort towards performing the tail call regardless of the current optimization level. I would be willing to submit a PR for this change if there's interest.

llvmbot commented 1 week ago

@llvm/issue-subscribers-backend-mips

Author: Shane Peelar (InBetweenNames)

Hello, I noticed that tail call optimization is disabled on MIPS at all optimization levels by default. This is causing issues with the `musttail` attribute in C and C++ programs, where tail calls that otherwise would be eligible are not being treated as such. The relevant flag is defined here: https://github.com/llvm/llvm-project/blob/98563b19c2d13bac7fb5ee2717ccae6279699e4c/llvm/lib/Target/Mips/MipsSEISelLowering.cpp#L54-L56 When building with `-mllvm -mips-tail-calls=1`, tail calls appear to be optimized accordingly (in the few tests I've done). I noticed that there are test cases for this flag throughout the backend (just search for `mips-tail-calls=1`). Here are a few issues that are affected by this behaviour: https://github.com/llvm/llvm-project/issues/47971 https://github.com/llvm/llvm-project/issues/51709 https://github.com/llvm/llvm-project/issues/57795 Is it possible we could make the MIPS backend behave like the others in this respect? It seems to me that if a call site is marked `musttail`, the backend should make every effort towards performing the tail call regardless of the current optimization level. I would be willing to submit a PR for this change if there's interest.
efriedma-quic commented 1 week ago

Flipping the flag is one thing, but do you have any idea if the Mips backend generates correct code with the flag enabled?

InBetweenNames commented 1 week ago

I'm afraid not. I have done some cursory reading of the code but that's all. It appears that this will only cause tail calls to be eliminated that are internal to the current module, and it's unclear to me why this is needed. The logic was originally implemented in 2012. Test cases do exist for this flag in the codebase, but it's hard to conclude one way or another how rigorous these are.

On Mon, Sep 9, 2024 at 8:32 PM Eli Friedman @.***> wrote:

Flipping the flag is one thing, but do you have any idea if the Mips backend generates correct code with the flag enabled?

— Reply to this email directly, view it on GitHub https://github.com/llvm/llvm-project/issues/107730#issuecomment-2339385107, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB3VLDFIJMVLA7ENLO5CC33ZVY42HAVCNFSM6AAAAABN2N4C4WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMZZGM4DKMJQG4 . You are receiving this because you authored the thread.Message ID: @.***>