(Question regarding the) incomplete "builtins library" of "Compiler-RT"

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?
See <https://godbolt.org/z/ZsHmaf> for example

* unsigned 64-bit integer operations: _aulldiv(), _aulldvrm(),
  _aullrem(), _aullshr()?
  See <https://msdn.microsoft.com/en-us/library/mt703294.aspx>

* signed 64-bit integer operations: _alldiv(), _alldvrm(),
  _allrem(), _allshl(), _allshr(), _allmul()?
  See <https://msdn.microsoft.com/en-us/library/ms648425.aspx>

For the following I'm not sure whether LLVM/clang calls them:

* trigonometric and transcendental floating-point operations:
  _CIacos(), _CIasin(), _CIatan(), _CIatan2(), _CIcos(),
  _CIcosh(), _CIexp(), _CIfmod(), _CIlog(), _CIlog10(),
  _CIpow(), _CIsin(), _CIsinh(), _CIsqrt(), _CItan(), _CItanh()?
  See <https://msdn.microsoft.com/en-us/library/ff770582.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770581.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770589.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770600.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770591.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770580.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770578.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770583.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770597.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770588.aspx>,
  <https://msdn.microsoft.com/en-us/library/ff770595.aspx>

regards
Stefan Kanthak

PS: Howard Hinnant, (one of) the original author of the builtins
    library, is still listed in compiler-rt/CODE_OWNERS.TXT,
    despite having left this project about TEN years ago!
    To quote himself:

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.

-Eli

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

__chkstk_ms is used in x86_64-mingw https://godbolt.org/z/dajIju

__chkstk_ms is used in x86_64-mingw https://godbolt.org/z/dajIju

OK.
Which leaves the following (apparently) superfluous files:

compiler-rt/lib/builtins/i386/chkstk.S
compiler-rt/lib/builtins/i386/chkstk2.S
compiler-rt/lib/builtins/x86_64/chkstk2.S
compiler-rt/lib/builtins/arm/chkstk.S
compiler-rt/lib/builtins/aarch64/chkstk.S

{Sh,C}ouldn't these be removed then?

Stefan

__chkstk_ms is used in x86_64-mingw https://godbolt.org/z/dajIju

OK.
Which leaves the following (apparently) superfluous files:

compiler-rt/lib/builtins/i386/chkstk.S

Yes, this one is potentially superfluous (although I haven't tested building without it to see if it's needed by some weirdness somewhere)

compiler-rt/lib/builtins/i386/chkstk2.S

Not superfluous, this contains the __alloca function that llvm produces calls to. The mingw runtime doesn't provide __alloca; libgcc or compiler-rt does.

compiler-rt/lib/builtins/x86_64/chkstk2.S

Yes, potentially superfluous

compiler-rt/lib/builtins/arm/chkstk.S
compiler-rt/lib/builtins/aarch64/chkstk.S

No, these contains the __chkstk function that llvm produces calls to when targeting armv7-mingw or aarch64-mingw. Contrary to x86, it's the same function name for both mingw and msvc targets, and either the msvc runtime or compiler-rt provides it.

// Martin

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.

According to <https://compiler-rt.llvm.org/index.html>

builtins - a simple library that provides an implementation of
the low-level target-specific hooks required by code generation
and other runtime components. ...

these routines SHOULD but be part of the builtins library.

EITHER document the implementation,
OR implement according to the documentation,
PLEASE!

tertium datur: document the exceptions from the general rule cited
above.

regards
Stefan Kanthak

PS: if you are interested, I can provide an implementation of _aulldiv
    and friends, which I derived from __divdi3 etc.
    They are about 3-4 times faster than the routines from the MSVC
    runtime.