ldc-developers / ldc

The LLVM-based D Compiler.
http://wiki.dlang.org/LDC
Other
1.21k stars 261 forks source link

PowerPC64: musttail problem with va_arg #2686

Open JohanEngelen opened 6 years ago

JohanEngelen commented 6 years ago

Failing testcase with -mtriple=powerpc64-unknown-linux-gnu (see Lit codegen/variadic_thunk_gh2613.d):

// RUN: %ldc -mtriple=powerpc64-unknown-linux-gnu -c %s

interface Stream
{
    void write(...);
}

class OutputStream : Stream
{
    void write(...)
    {
        import core.vararg;
        auto arg = va_arg!string(_argptr);
        assert(arg == "Hello world");
    }
}

void main()
{
    Stream stream = new OutputStream;
    stream.write("Hello world");
}

Error:

LLVM ERROR: failed to perform tail call elimination on a call site marked musttail
dnadlinger commented 6 years ago

Clang just seems to duplicate the function body for variadic functions.

dnadlinger commented 6 years ago

(instead of forwarding to the derived-class implementation)

kinke commented 6 years ago

It's working (at least compiling fine) for ARM and AArch64, so I suspect an issue with the PPC LLVM target.

dnadlinger commented 6 years ago

It might compile fine, but is musttail actually guaranteed to behave in that fashion?

kinke commented 6 years ago

AFAICT forwarding variadics is exactly one of its purposes; I found it when looking for how to properly forward in a variadic thunk. I also see no reason why the tail call shouldn't work; the signatures are identical, only difference is that the actual implementation has a EH personality. After the call, there's immediately a ret (should be returning void or the call result). I also quickly looked at the tail-call optimization docs, but they are hopelessly outdated (supposedly only supporting x86 and PowerPC, only for 3 calling conventions excl. the default C one - i.e., stuff which has already been proven wrong by CI and manual ARM tests).

kinke commented 6 years ago

Ah I think that's what I found back then: Perfectly forwarding thunks can now be expressed in LLVM IR with musttail and varargs

dnadlinger commented 6 years ago

Ah, good to know – thanks!