[llvm-dev] (Question regarding the) incomplete "builtins library" of "Compiler-RT"

Stefan Kanthak via llvm-dev llvm-dev at lists.llvm.org
Fri Nov 30 13:05:03 PST 2018


"Friedman, Eli" <efriedma at codeaurora.org> wrote:

> On 11/30/2018 8:31 AM, Stefan Kanthak via llvm-dev wrote:
>> Hi @ll,
>>
>> compiler-rt implements (for example) the MSVC (really Windows)
>> specific routines compiler-rt/lib/builtins/i386/chkstk.S and
>> compiler-rt/lib/builtins/x86_64/chkstk.S as __chkstk_ms()
>> See <http://msdn.microsoft.com/en-us/library/ms648426.aspx>
>>
>> Is there any special reason why compiler-rt doesn't implement
>> other MSVC specific functions (alias builtins or "compiler
>> intrinsics") for which clang/LLVM but generates calls?
> 
> There are two "Windows" targets supported by LLVM: one using the MinGW 
> runtime, and the other using the MSVC runtime. If you're targeting the 
> MSVC runtime, it provides _aulldiv and friends; if you're targeting 
> MinGW, LLVM won't refer to those routines. So I can't see a scenario 
> where we would need those routines in compiler-rt.

What's the purpose of __chkstk_ms() then, implemented in compiler-rt?

1. LLVM does NOT create calls to __chkstk_ms();

2. both the MSVC and the MinGW runtime provide their own __chkstk()
   as well as their own __alloca() routines.

--- llvm.c ---
int main(int argc) {
    int stack[1111];
    return _alloca(argc);
}
--- EOF ---

Compiled with "-target i386-mingw" this generates the following code
(see <https://godbolt.org/z/yPk4Jo>):

_main: # @main
    push  ebp
    mov   ebp, esp
    mov   eax, 4456
    call  __alloca
    mov   eax, dword ptr [ebp + 8]
    mov   dword ptr [ebp - 4452], eax # 4-byte Spill
    call  ___main
    mov   dword ptr [ebp - 4], 0
    mov   eax, dword ptr [ebp + 8]
    mov   dword ptr [esp], eax
    call  __alloca
    add   esp, 4456
    pop   ebp
    ret


Compiled with "-target i386-win32" this generates the following code
(see <https://godbolt.org/z/Hc8jaL>):

_main: # @main
    push  ebp
    mov   ebp, esp
    push  esi
    and   esp, -16
    mov   eax, 4480
    call  __chkstk
    mov   esi, esp
    mov   eax, dword ptr [ebp + 8]
    mov   dword ptr [esi + 4464], 0
    mov   ecx, dword ptr [ebp + 8]
    add   ecx, 3
    and   ecx, -4
    mov   dword ptr [esi + 12], eax # 4-byte Spill
    mov   eax, ecx
    call  __chkstk
    mov   ecx, esp
    and   ecx, -16
    mov   esp, ecx
    mov   eax, ecx
    lea   esp, [ebp - 4]
    pop   esi
    pop   ebp
    ret


More information about the llvm-dev mailing list