-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flang] MSVC runtime library selection #68017
Comments
#67675 is related in that it asks for MSVC-like predefined macros (identifying the ABI and target architecture). |
For reference, this came up in discussion of CMake Issue 24840, requesting support for targeting the MSVC ABI with |
@llvm/issue-subscribers-flang-driver
For targets such as `x86_64-pc-windows-msvc`, LLVM/Flang generates
object files for the MSVC ABI and uses MSVC-compatible tooling.
Compilers that target the MSVC ABI need to select one of the MSVC
MSVC toolsets provide the following runtime library variants:
LLVM/Flang should be updated as follows:
For reference, LLVM/Clang does all of these when targeting the MSVC ABI. Additionally, LLVM/Flang's own runtime libraries ( Cc: @DavidTruby |
There’s an ongoing refactor of the flang runtime library happening at the moment but once that has landed I will take a look at this |
The flang runtime library build refactor is talking a while so I'm going to pick this up and try and fix it separately to that |
AFAIK none of the other runtime-libraries (compiler-rt, libcxx, libomp) currently have this. Should this be done as a coordinated effort? |
I was just checking and agree with @Meinersbur; at the moment every runtime uses just the static version. I think that doesn't preclude the end user linking to the dynamic one for their own program though, since no C++ classes cross the ABI boundary in the runtime? So a first step to get flang on the same level as everything else should be just to:
Probably in the long run we want to be able to have both options for all of the runtimes. That will need to be done as a co-ordinated effort with all of the other runtimes since we depend on those in flang too. This part might have to wait until the flang runtime refactor though, after which the flang runtime will be built in the same way as the rest of the runtimes as I understand it. |
On third thought; we don't have any dependency on the CRT outside the Fortran runtime, so I think we should just always link Fortran programs against the static runtime (libcmt) for now as I think it makes no difference other than introducing a dll dependency that isn't actually used to the generated binaries. What do you two think? I will admit to not being that familiar with how all this works on Windows |
If the Fortran runtime library has dependencies on the CRT, then the specific If the Fortran compiler itself never generates references to the CRT, then the only reason to have flags controlling which CRT to use would be if corresponding variants of the Fortran runtime library were available. Such flags would then control which Fortran runtime variant to name in However, even without multiple CRT variants of the Fortran runtime library,
|
Ok yes then my understanding was correct (at least on the third try!) and I'll implement it that way with the changes you suggest. I do have one question/comment about it though, which is; I think the reason we pass the runtimes to the linker directly rather than relying on defaultlib is so that we can look up where they are e.g. in the LLVM build directory or install directory relative to the linker. I suppose we can pass |
IIRC in my local experiments with |
Ok sure, I'm happy to do it that way. I've hit another snag though, which is that |
Okay, thanks. Please post back here once we know whether that is on purpose. Note that if the compiler emits references to symbols provided by a "builtins" library, a |
Does it work transitively? I.e. if I have FortranRuntime.lib which has |
Let me loop back round for a second as I think I'm still not 100% understood where the issue is coming from. Let's say that for now (because the other libraries we depend on do the same) we just use whatever CRT the user built with, so by default
|
The response wrt |
It works transitively, but not necessarily at the granularity of whole libraries. Inside static libraries, the In general, whenever the compiler emits a reference to a symbol it expects to be provided by the Fortran runtime, clang-rt/builtins, or CRT, the object file the compiler writes should have a corresponding |
At least MSVC CRT preprocessor definitions and emitting
In the That is actually one case where we do need to directly reference the CRT from a Fortran object file. It should select the same CRT as |
Ok I see! I'm surprised it doesn't get the msvcrt.lib from Fortran_main.lib here which is where mainCRTStartup is referenced I believe. I suppose nothing from the Fortran side technically references into Fortran_main though so I guess it gets skipped because of this? We can probably just add a reference to the crt to any object file containing the |
For that part of the issue, yes. Or, make sure that the |
Hmm. Actually I see |
For targets such as
x86_64-pc-windows-msvc
, LLVM/Flang generatesobject files for the MSVC ABI and uses MSVC-compatible tooling.
Compilers that target the MSVC ABI need to select one of the MSVC
runtime libraries. They should define preprocessor macros identifying
the selected runtime library, and add
#pragma comment
-style annotationsto object files to pass
/defaultlib:...
flags to the linker withoutbuild system intervention. With LLVM/Flang the latter is needed even
for a trivial empty Fortran program:
MSVC toolsets provide the following runtime library variants:
libcmt
: Multi-threaded, optimized, linked statically.Defines
-D_MT
, links/defaultlib:libcmt
.MSVC flag:
cl -MT
.msvcrt
: Multi-threaded, optimized, linked shared.Defines
-D_MT -D_DLL
, links/defaultlib:msvcrt
.MSVC flag:
cl -MD
.libcmtd
: Multi-threaded, debug, linked statically.Defines
-D_MT -D_DEBUG
, links/defaultlib:libcmtd
.MSVC flag:
cl -MTd
.msvcrtd
: Multi-threaded, debug, linked shared.Defines
-D_MT -D_DEBUG -D_DLL
, links/defaultlib:msvcrtd
.MSVC flag:
cl -MDd
.LLVM/Flang should be updated as follows:
for the preprocessor.
/defaultlib:...
flags in object files referencing theselected MSVC runtime library for the linker.
For reference, LLVM/Clang does all of these when targeting the MSVC ABI.
Additionally, LLVM/Flang's own runtime libraries (
Fortran*.lib
)could be provided as variants corresponding to each MSVC runtime
library variant. Currently the
Fortran*.lib
libraries included inthe LLVM/Flang distribution only use the
msvcrt
runtime library.LLVM/Flang should then write
/defaultlib:...
references in objectfiles, to link its own
Fortran*.lib
runtime libraries, matching theselected MSVC runtime library variant.
Cc: @DavidTruby
The text was updated successfully, but these errors were encountered: