ldc-developers / ldc

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

No error when using params in @naked functions #4689

Open MrSmith33 opened 3 months ago

MrSmith33 commented 3 months ago

On x86_64: https://godbolt.org/z/dEaaqPh7v

import ldc.attributes;
import ldc.llvmasm;

@naked long spawnLinuxThread1(void* data) {
    return __asm!long("syscall",
        "={eax}, {rax}, {rdi}, {rsi}",
        56, 0x50f00, data);
}

produces:

mov     rsi, qword ptr [rdi]
mov     eax, 56
mov     edi, 331520
syscall
ret

instead of

mov     rsi, rdi
mov     eax, 56
mov     edi, 331520
syscall
ret

Changing void* to ulong causes LLVM ERROR:

Invalid bitcast
  %2 = bitcast i64 %0 to ptr, !dbg !7
LLVM ERROR: Broken module found, compilation aborted!

Same happens on arm64: https://godbolt.org/z/7Eb3Pqabd

JohanEngelen commented 3 months ago

This bug is due to using @naked and the resulting IR generation not dealing with that properly. With @naked we load from passed parameter address instead of the usual loading from the temp storage address (where the passed parameter is stored).

Compare the output of this code with/without @naked:

//@naked
size_t foo(void* data) {
    return cast(size_t) data;
}
MrSmith33 commented 3 months ago

I see. Should I expect this to be fixed?

JohanEngelen commented 3 months ago

Not sure how much we want/should support with @naked.

JohanEngelen commented 3 months ago

I do consider it a bug. If we don't support this usage / it's buggy, then it's better to emit a compile error for it than to leave it unfixed.