[PATCH] D17452: [CMake] Add partial support for MSVC in compiler-rt builtins
roman.shirokiy created this revision. roman.shirokiy added reviewers: rnk, majnemer, beanz, pcc. roman.shirokiy added a subscriber: cfe-commits. This enables MSVC build of complex number arithmetic compiler-rt builtins. Consider Clang which is built under native Windows environment. Compiling the following code will result in "unresolved external symbol __divsc3 referenced in function main" int main(){ float _Complex a, b, c; c = a / b; return 0; } Clang front-end generates calls to complex number arithmetic functions, which are nowhere to be found in Microsoft runtime libs. The idea of this patch is to enable MSVC build of compiler-rt builtins library with certain subset of ported-to-CL sources and link this lib in case if user has explicitly demanded so through "--rtlib" option. This will allow to compile code with usage of complex number arithmetic in general and spec2006/462.libquantum in particular. http://reviews.llvm.org/D17452 Files: CMakeLists.txt lib/builtins/CMakeLists.txt Index: CMakeLists.txt === --- CMakeLists.txt +++ CMakeLists.txt @@ -241,6 +241,8 @@ # FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214. if(COMPILER_RT_HAS_MT_FLAG) foreach(flag_var + CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE + CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) string(REGEX REPLACE "/M[DT]d" "/MT" ${flag_var} "${${flag_var}}") Index: lib/builtins/CMakeLists.txt === --- lib/builtins/CMakeLists.txt +++ lib/builtins/CMakeLists.txt @@ -143,6 +143,15 @@ umodsi3.c umodti3.c) +set(MSVC_SOURCES + divsc3.c + divdc3.c + divxc3.c + mulsc3.c + muldc3.c + mulxc3.c) + + if(APPLE) set(GENERIC_SOURCES ${GENERIC_SOURCES} @@ -216,14 +225,15 @@ ${i386_SOURCES}) else () # MSVC # Use C versions of functions when building on MSVC - # MSVC's assembler takes Intel syntax, not AT&T syntax + # MSVC's assembler takes Intel syntax, not AT&T syntax. + # Also use only MSVC compilable builtin implementations. set(x86_64_SOURCES x86_64/floatdidf.c x86_64/floatdisf.c x86_64/floatdixf.c - ${GENERIC_SOURCES}) + ${MSVC_SOURCES}) set(x86_64h_SOURCES ${x86_64_SOURCES}) - set(i386_SOURCES ${GENERIC_SOURCES}) + set(i386_SOURCES ${MSVC_SOURCES}) set(i686_SOURCES ${i386_SOURCES}) endif () # if (NOT MSVC) @@ -363,7 +373,7 @@ add_subdirectory(Darwin-excludes) add_subdirectory(macho_embedded) darwin_add_builtin_libraries(${BUILTIN_SUPPORTED_OS}) -elseif (NOT WIN32 OR MINGW) +else () append_string_if(COMPILER_RT_HAS_STD_C99_FLAG -std=c99 maybe_stdc99) foreach (arch ${BUILTIN_SUPPORTED_ARCH}) Index: CMakeLists.txt === --- CMakeLists.txt +++ CMakeLists.txt @@ -241,6 +241,8 @@ # FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214. if(COMPILER_RT_HAS_MT_FLAG) foreach(flag_var + CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE + CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) string(REGEX REPLACE "/M[DT]d" "/MT" ${flag_var} "${${flag_var}}") Index: lib/builtins/CMakeLists.txt === --- lib/builtins/CMakeLists.txt +++ lib/builtins/CMakeLists.txt @@ -143,6 +143,15 @@ umodsi3.c umodti3.c) +set(MSVC_SOURCES + divsc3.c + divdc3.c + divxc3.c + mulsc3.c + muldc3.c + mulxc3.c) + + if(APPLE) set(GENERIC_SOURCES ${GENERIC_SOURCES} @@ -216,14 +225,15 @@ ${i386_SOURCES}) else () # MSVC # Use C versions of functions when building on MSVC - # MSVC's assembler takes Intel syntax, not AT&T syntax + # MSVC's assembler takes Intel syntax, not AT&T syntax. + # Also use only MSVC compilable builtin implementations. set(x86_64_SOURCES x86_64/floatdidf.c x86_64/floatdisf.c x86_64/floatdixf.c - ${GENERIC_SOURCES}) + ${MSVC_SOURCES}) set(x86_64h_SOURCES ${x86_64_SOURCES}) - set(i386_SOURCES ${GENERIC_SOURCES}) + set(i386_SOURCES ${MSVC_SOURCES}) set(i686_SOURCES ${i386_SOURCES}) endif () # if (NOT MSVC) @@ -363,7 +373,7 @@ add_subdirectory(Darwin-excludes) add_subdirectory(macho_embedded) darwin_add_builtin_libraries(${BUILTIN_SUPPORTED_OS}) -elseif (NOT WIN32 OR MINGW) +else () append_string_if(COMPILER_RT_HAS_STD_C99_FLAG -std=c99 maybe_stdc99) foreach (arch ${BUILTIN_SUPPORTED_ARCH}) ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo
[PATCH] D17453: [Driver] Enable --rtlib option for MSVC target
roman.shirokiy created this revision. roman.shirokiy added reviewers: rnk, majnemer, beanz, pcc. roman.shirokiy added a subscriber: cfe-commits. This enables "--rtlib compiler-rt" option under MSVC environment. Related compiler-rt patch: http://reviews.llvm.org/D17452 http://reviews.llvm.org/D17453 Files: lib/Driver/Tools.cpp test/Driver/msvc-compiler-rt.c Index: lib/Driver/Tools.cpp === --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -8766,10 +8766,20 @@ break; } break; - case ToolChain::RLT_Libgcc: -AddLibgcc(TC.getTriple(), D, CmdArgs, Args); -break; - } +case ToolChain::RLT_Libgcc: + // Make sure libgcc is not used under MSVC environment by default + if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { + // Issue error diagnostic if libgcc is explicitly specified + // through command line as --rtlib option argument. + if (Args.hasArg(options::OPT_rtlib_EQ)) { + TC.getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform) + << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "MSVC"; + } + } + else +AddLibgcc(TC.getTriple(), D, CmdArgs, Args); + break; +} } static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) { @@ -9573,6 +9583,12 @@ } } + // Add compiler-rt lib in case if it was explicitly + // specified as an argument for --rtlib option. + if (!Args.hasArg(options::OPT_nostdlib)) { +AddRunTimeLibs(TC, TC.getDriver(), CmdArgs, Args); + } + // Add filenames, libraries, and other linker inputs. for (const auto &Input : Inputs) { if (Input.isFilename()) { Index: test/Driver/msvc-compiler-rt.c === --- test/Driver/msvc-compiler-rt.c +++ test/Driver/msvc-compiler-rt.c @@ -0,0 +1,5 @@ +// RUN: %clang -target x86_64-pc-windows-msvc --rtlib=compiler-rt -### %s 2>&1 | FileCheck %s -check-prefix MSVC-COMPILER-RT +// RUN: not %clang %s -target x86_64-pc-windows-msvc --rtlib=libgcc 2>&1 | FileCheck %s -check-prefix CHECK-ERROR + +// MSVC-COMPILER-RT: "{{.*}}clang_rt.builtins{{.*}}" +// CHECK-ERROR: unsupported runtime library 'libgcc' for platform 'MSVC' Index: lib/Driver/Tools.cpp === --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -8766,10 +8766,20 @@ break; } break; - case ToolChain::RLT_Libgcc: -AddLibgcc(TC.getTriple(), D, CmdArgs, Args); -break; - } +case ToolChain::RLT_Libgcc: + // Make sure libgcc is not used under MSVC environment by default + if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { + // Issue error diagnostic if libgcc is explicitly specified + // through command line as --rtlib option argument. + if (Args.hasArg(options::OPT_rtlib_EQ)) { + TC.getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform) + << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "MSVC"; + } + } + else +AddLibgcc(TC.getTriple(), D, CmdArgs, Args); + break; +} } static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) { @@ -9573,6 +9583,12 @@ } } + // Add compiler-rt lib in case if it was explicitly + // specified as an argument for --rtlib option. + if (!Args.hasArg(options::OPT_nostdlib)) { +AddRunTimeLibs(TC, TC.getDriver(), CmdArgs, Args); + } + // Add filenames, libraries, and other linker inputs. for (const auto &Input : Inputs) { if (Input.isFilename()) { Index: test/Driver/msvc-compiler-rt.c === --- test/Driver/msvc-compiler-rt.c +++ test/Driver/msvc-compiler-rt.c @@ -0,0 +1,5 @@ +// RUN: %clang -target x86_64-pc-windows-msvc --rtlib=compiler-rt -### %s 2>&1 | FileCheck %s -check-prefix MSVC-COMPILER-RT +// RUN: not %clang %s -target x86_64-pc-windows-msvc --rtlib=libgcc 2>&1 | FileCheck %s -check-prefix CHECK-ERROR + +// MSVC-COMPILER-RT: "{{.*}}clang_rt.builtins{{.*}}" +// CHECK-ERROR: unsupported runtime library 'libgcc' for platform 'MSVC' ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D17453: [Driver] Enable --rtlib option for MSVC target
roman.shirokiy added a comment. For now every native Windows installation of Clang contains small compiler-rt builtins lib (http://reviews.llvm.org/rL261432: [CMake] Add partial support for MSVC in compiler-rt builtins), but there is no specific interface to make use of this lib on Windows. This patch is ought to provide command line option (existing one) for linking compiler-rt builtins lib on user demand. http://reviews.llvm.org/D17453 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D17453: [Driver] Enable --rtlib option for MSVC target
roman.shirokiy added a comment. Hello! Thanks for the feedback! MSVC got its own runtime lib, so the function "AddRunTimeLibs" is not used anywhere in "visualstudio::Linker::Constructjob" i.e. "--rtlib" option is currently unused in MSVC environment (there is always a warning: argument unused during compilation'). if (!Args.hasArg(options::OPT_nostdlib)) { AddRunTimeLibs(TC, TC.getDriver(), CmdArgs, Args); } This change is necessary to actually handle "--rtlib" on MSVC, but I totally agree that test for "-nostdlib --rtlib=compiler-rt" case is missed. http://reviews.llvm.org/D17453 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D17453: [Driver] Enable --rtlib option for MSVC target
roman.shirokiy updated this revision to Diff 50260. roman.shirokiy added a comment. Updated diff with the test for "nostdlib" + "rtlib" case. http://reviews.llvm.org/D17453 Files: lib/Driver/Tools.cpp test/Driver/msvc-compiler-rt.c test/Driver/nostdlib.c Index: lib/Driver/Tools.cpp === --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -8945,10 +8945,20 @@ break; } break; - case ToolChain::RLT_Libgcc: -AddLibgcc(TC.getTriple(), D, CmdArgs, Args); -break; - } +case ToolChain::RLT_Libgcc: + // Make sure libgcc is not used under MSVC environment by default + if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { + // Issue error diagnostic if libgcc is explicitly specified + // through command line as --rtlib option argument. + if (Args.hasArg(options::OPT_rtlib_EQ)) { + TC.getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform) + << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "MSVC"; + } + } + else +AddLibgcc(TC.getTriple(), D, CmdArgs, Args); + break; +} } static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) { @@ -9752,6 +9762,12 @@ } } + // Add compiler-rt lib in case if it was explicitly + // specified as an argument for --rtlib option. + if (!Args.hasArg(options::OPT_nostdlib)) { +AddRunTimeLibs(TC, TC.getDriver(), CmdArgs, Args); + } + // Add filenames, libraries, and other linker inputs. for (const auto &Input : Inputs) { if (Input.isFilename()) { Index: test/Driver/nostdlib.c === --- test/Driver/nostdlib.c +++ test/Driver/nostdlib.c @@ -22,6 +22,10 @@ // RUN: -resource-dir=%S/Inputs/resource_dir -lclang_rt.builtins-i686 \ // RUN: | FileCheck --check-prefix=CHECK-LINUX-NOSTDLIB %s // +// RUN: %clang -target x86_64-pc-windows-msvc -nostdlib --rtlib=compiler-rt -### %s 2>&1 | FileCheck %s -check-prefix CHECK-MSVC-NOSTDLIB +// RUN: %clang -target x86_64-pc-windows-msvc --rtlib=compiler-rt -nostdlib -### %s 2>&1 | FileCheck %s -check-prefix CHECK-MSVC-NOSTDLIB +// // CHECK-LINUX-NOSTDLIB: warning: argument unused during compilation: '--rtlib=compiler-rt' // CHECK-LINUX-NOSTDLIB: "{{(.*[^.0-9A-Z_a-z])?}}ld{{(.exe)?}}" // CHECK-LINUX-NOSTDLIB-NOT: "{{.*}}/Inputs/resource_dir{{/|}}lib{{/|}}linux{{/|}}libclang_rt.builtins-i686.a" +// CHECK-MSVC-NOSTDLIB: warning: argument unused during compilation: '--rtlib=compiler-rt' Index: test/Driver/msvc-compiler-rt.c === --- test/Driver/msvc-compiler-rt.c +++ test/Driver/msvc-compiler-rt.c @@ -0,0 +1,5 @@ +// RUN: %clang -target x86_64-pc-windows-msvc --rtlib=compiler-rt -### %s 2>&1 | FileCheck %s -check-prefix MSVC-COMPILER-RT +// RUN: not %clang %s -target x86_64-pc-windows-msvc --rtlib=libgcc 2>&1 | FileCheck %s -check-prefix CHECK-ERROR + +// MSVC-COMPILER-RT: "{{.*}}clang_rt.builtins{{.*}}" +// CHECK-ERROR: unsupported runtime library 'libgcc' for platform 'MSVC' Index: lib/Driver/Tools.cpp === --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -8945,10 +8945,20 @@ break; } break; - case ToolChain::RLT_Libgcc: -AddLibgcc(TC.getTriple(), D, CmdArgs, Args); -break; - } +case ToolChain::RLT_Libgcc: + // Make sure libgcc is not used under MSVC environment by default + if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { + // Issue error diagnostic if libgcc is explicitly specified + // through command line as --rtlib option argument. + if (Args.hasArg(options::OPT_rtlib_EQ)) { + TC.getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform) + << Args.getLastArg(options::OPT_rtlib_EQ)->getValue() << "MSVC"; + } + } + else +AddLibgcc(TC.getTriple(), D, CmdArgs, Args); + break; +} } static const char *getLDMOption(const llvm::Triple &T, const ArgList &Args) { @@ -9752,6 +9762,12 @@ } } + // Add compiler-rt lib in case if it was explicitly + // specified as an argument for --rtlib option. + if (!Args.hasArg(options::OPT_nostdlib)) { +AddRunTimeLibs(TC, TC.getDriver(), CmdArgs, Args); + } + // Add filenames, libraries, and other linker inputs. for (const auto &Input : Inputs) { if (Input.isFilename()) { Index: test/Driver/nostdlib.c === --- test/Driver/nostdlib.c +++ test/Driver/nostdlib.c @@ -22,6 +22,10 @@ // RUN: -resource-dir=%S/Inputs/resource_dir -lclang_rt.builtins-i686 \ // RUN: | FileCheck --check-prefix=CHECK-LINUX-NOSTDLIB %s // +// RUN: %clang -target x86_64-pc-windows-msvc -nostdlib --rtlib=compiler-rt -### %s 2>&1 | FileChe