Compiler-rt builtins on MSVC

I’ve been trying to compile Rust on MSVC/Windows without having MinGW at all.

Turns out the only real problem with this (AFAIK the rest of the source tree does not depend on gcc at all) is that LLVM’s compiler-rt builtins don’t build on MSVC.

There is a comment in rt.mk from the person who did MSVC support at first (I presume, didn’t git blame) about how he couldn’t get CMake to build compiler-rt.

# Note that on MSVC-targeting builds we hardwire CC/AR to gcc/ar even though
# we're targeting MSVC. This is because although compiler-rt has a CMake build
# config I can't actually figure out how to use it, so I'm not sure how to use
# cl.exe to build the objects. Additionally, the compiler-rt library when built
# with gcc has the same ABI as cl.exe, so they're largely compatible

If the person didn’t (smartly) just force GCC to build it he would’ve realised that builtins won’t build on MSVC.

The CMakeLists.txt for builtins leaves builtins with no source files to build on MSVC:

add_custom_target(builtins)

if (APPLE)
<snip>
elseif (NOT WIN32 OR MINGW)
<snip>
      add_compiler_rt_runtime(clang_rt.builtins
                              STATIC
                              ARCHS ${arch}
                              SOURCES ${${arch}_SOURCES}
                              CFLAGS "-std=c99"
                              PARENT_TARGET builtins)
<snip>
endif ()

add_dependencies(compiler-rt builtins)

Builtins uses C syntax that MSVC doesn’t support (seems like mainly __attributes__). You could probably get it to compile but then we’d diverge from LLVM …

I’ve been trying to find out what clang does when compiled with MSVC but I can’t find it at all.

Would be nice to build rust without MinGW (and eventually, without MSYS2) for MSVC but someone has to figure out this first.

(The alternative is to disable builtins/intrinsics for MSVC, which is bad)

Hello!

I totally agree! We've got quite a distance to go here, but this is certainly one of the crucial pieces to get us over the finish line. The current elephant-in-the-room is the jungle of makefiles we have, but I have an in-progress branch where our build system moves over to Cargo, removing this dependency. Once that's done, and once compiler-rt is building without gcc, I believe we'll only need MSYS2 for the test suite (e.g. src/test/run-make) and even that can be phased out over time in favor of Rust scripts.

This may be worth investigating actually, I'm not actually sure to what degree LLVM depends on symbols being in libcompiler-rt, so if it turns out on MSVC it doesn't actually lower to anything then we could elide the library entirely. If there are modes to tell LLVM "don't lower to calls in this library" as well that may also be quite useful, although that's where performance implications may come into play and be worth investigating.

2 Likes

I don't think it'll be a good idea to do that, it doesn't seem like LLVM is bothered about builtins on MSVC. Unless LLVM is willing to take in any changes we make it'll mean maintaining that as well :frowning:

I just tried replacing compiler-rt.lib in a nightly build of Rust with an empty file. Linker barfed because std wants __powidf2 which of course is from builtins. ...

I'm asking the LLVM mailing list about how these intrinsics are supposed to be handled on MSVC, .. I'll see what they say.

@retep998 says that in MSVC the (some?) intrinsics are implemented as inline functions, so there's nothing from MSVC we can actually link to.

Edit: Someone said Clang "expects to link against the VC++ runtime", so this means MSVCRT might have the intrinsics needed.

Although that would mean Rust code built using an MSVC rustc has to link to MSVCRT which is bad, copyright issues and all.

With Clang, for something like __powidf2, since the msvc version of Clang uses the MSVC provided headers, that means it would do the same thing that msvc does, aka pow(5., 2) would invoke _Pow_int which is an inline template function in the headers. Really the question is which builtins does Rust actually need from compiler-rt. Maybe it would be possible to replace them with pure Rust versions and call it a day?

There’s no point trying to get compiler-rt/builtins to work on MSVC, it’s too much effort for nothing…

Gah. I just tried to but turns out MS refuses to implement C99 properly and compiler-rt/builtins uses complex which of course MS had to implement in their own way that I can’t simply fix with an #ifdef here or there.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.