__builtin_wmemchr does not work with clang-cl if wchar.h is not included
| Bugzilla Link | 41226 |
| Resolution | FIXED |
| Resolved on | Sep 24, 2021 00:01 |
| Version | 8.0 |
| OS | other |
| CC | @DHowett,@emmenlau,@mwinterb,@nico,@pcc,@zygoloid,@rnk,@StephanTLavavej,@tru,@yurybura |
Extended Description
int main() {
static_assert(__has_feature(cxx_constexpr_string_builtins)); // is true static_assert(__has_builtin(__builtin_wmemchr)); // is true
static_assert(!__builtin_wmemchr(nullptr, 0, 0)); // is true __builtin_wmemchr(nullptr, 0, 0); // no error reported
}
This program does not compile.
link.exe reports this error: LNK2019 unresolved external symbol _wmemchr referenced in function _main
lld-link.exe reports this error: undefined symbol: _wmemchr
This issue is related to Bug 37911 (#37911 #c1) reported by Stephan Lavavej from Microsoft.
It seems that if __builtin_wmemchr is not forced to be evaluated at compile time it is not "called" and clang emits the symbol _wmemchr instead. But Microsoft's libraries do not have that symbol.
I think this is a bug. clang should be able to "call" its builtin function at both compile time and run-time.
Maybe this is a leftover from previous clang versions, that is, before wmemchr was made constexpr and builtin?
I tested this in VS 2017 and 2019 RC with the LLVM Compiler Toolchain extension.
Awesome that its fixed, thanks!
Amy fixed this in https://reviews.llvm.org/D98472 / https://github.com/llvm/llvm-project/commit/f5352dd9dab1ee8a2ae19a3fca3111c8b5de8ce2.
We basically gave up and just reimplemented the builtin in the compiler, even though I really didn't want to do that. It's the same as what we did for wmemcmp.
Dear All, I've found this issue after failing to build Qt 6.0 with clang-cl 10.0.0 (the one shipped with Visual Studio, currently v16.8.3). I've reported the issue upstream with Qt support, see https://bugreports.qt.io/browse/QTBUG-89642. the error messages is
FAILED: qtbase/bin/Qt6Cored.dll qtbase/lib/Qt6Cored.lib qtbase/src/corelib/Core.version cmd.exe /C "cmd.exe /C "cd /D C:\data\tmp\qt\qtbase\src\corelib && C:\bda-ci\ActivePerl\bin\perl.exe C:/data/tmp/qt/qtbase/mkspecs/features/data/unix/findclasslist.pl < C:/data/tmp/qt/x64/qtbase/src/corelib/Core.version.in > C:/data/tmp/qt/x64/qtbase/src/corelib/Core.version && cd C:\data\tmp\qt\x64" && C:\data\user-artifacts-ClangCl-Haswell-7-x64-cl19.28.29335_clang10.0.0\Tools\bin\cmake.exe -E vs_link_dll --intdir=qtbase\src\corelib\CMakeFiles\Core.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\Llvm\x64\bin\lld-link.exe @CMakeFiles\Core.rsp /out:qtbase\bin\Qt6Cored.dll /implib:qtbase\lib\Qt6Cored.lib /pdb:qtbase\bin\Qt6Cored.pdb /dll /version:6.0 /machine:x64 /MACHINE:X64 /DEBUG:FULL /debug -Wl,--version-script,C:/data/tmp/qt/x64/qtbase/src/corelib/Core.version -Wl,--no-undefined -DYNAMICBASE -NXCOMPAT && cd ." LINK: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\Llvm\x64\bin\lld-link.exe @CMakeFiles\Core.rsp /out:qtbase\bin\Qt6Cored.dll /implib:qtbase\lib\Qt6Cored.lib /pdb:qtbase\bin\Qt6Cored.pdb /dll /version:6.0 /machine:x64 /MACHINE:X64 /DEBUG:FULL /debug -Wl,--version-script,C:/data/tmp/qt/x64/qtbase/src/corelib/Core.version -Wl,--no-undefined -DYNAMICBASE -NXCOMPAT /MANIFEST /MANIFESTFILE:qtbase\bin\Qt6Cored.dll.manifest" failed (exit code 1) with the following output: lld-link: error: undefined symbol: wmemchr
referenced by C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xstring:258 qtbase\src\corelib\CMakeFiles\Core.dir\global\qglobal.cpp.obj:(public: static wchar_t const * __cdecl std::_WChar_traits<wchar_t>::find(wchar_t const *, unsigned __int64, wchar_t const &))
Officially clang-cl is not supported by Qt so its unlikely somebody upstream will fix it. I personally could understand the problem, but I'm not knowledgeable enough to understand how to fix it. Does it require a fix on clangs side or can user-code do something about it? And is something currently in the works for this issue?
Hello,
I ran into this with Clang 11. Except for setting the standard to C++14 (which works) is there a good workaround for this issue?
Please see my reply to Yury's post.
We are using latest 2019 as well. So there is more to this story.
Could you give me a sample project (including all project properties) that does not work for you? In theory, it should not work for me, too.
And could you try Yury's sample code? It should work for you, too.
Hello,
I ran into this with Clang 11. Except for setting the standard to C++14 (which works) is there a good workaround for this issue?
Please see my reply to Yury's post.
We are using latest 2019 as well. So there is more to this story.
Hello,
I ran into this with Clang 11. Except for setting the standard to C++14 (which works) is there a good workaround for this issue?
Please see my reply to Yury's post.
Created attachment 22971 [details] Example to reproduce
I still have the following link error for attached example for Clang 9:
2>------ Build started: Project: test_exe, Configuration: Debug x64 ------ 2>Building Custom Rule D:/libs/test/test_pch_with_clang_9/CMakeLists.txt 2>lld-link : error : undefined symbol: wmemchr 2>>>> referenced by C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\iosfwd:332 2>>>> test_exe.dir\Debug\test_exe.obj:(public: static wchar_t const * __cdecl std::_WChar_traits<wchar_t>::find(wchar_t const *, unsigned __int64, wchar_t const &)) 2>Done building project "test_exe.vcxproj" -- FAILED.
I just tested your example using Visual Studio 2019 16.7.6 and Windows SDK 10.0.19041.0 and clang/LLVM 11 and /std:c++latest.
The result: no linker error. It compiles and links successfully for both 32-bit and 64-bit, with or without precompiled header.
I know this sounds silly, but using VS2019 instead of VS2017 seems like a workaround.
Perhaps we should explore this idea further:
Maybe in MSVC mode we can require the translation unit to declare the CRT function before allowing a call to the _builtin variant?
I think wchar.h is included already, but we need to teach clang to mark ::wmemchr as ODR-used so that an inline definition will be emitted.
That seems... reasonable and general.
Thanks for your reply Reid. Could you give an example of how to do this. I haven't worked with this part of clang yet. I saw in your email to llvm-dev you mentioned calling MarkFunctionReferenced for the builtins in the MSVC. If I understand it correctly calling this function tells the compiler that we are using this symbol (__builtin_wemchar) in this case and that makes sure it's not removed(?) later on.
In that case I would need to know where this needs to happen (SemaDeclCXX?) and how to get the list of builtins that we need to call this for. Would it be a static list of things or parsed from the source in some way?
Sorry if these seems like basic questions - but I am new to the internals of clang - but happy to learn and help.
I just want to provide some information or add to the confusion.
I checked the code of my original post using Visual Studio 2019 16.7.6 and Windows SDK 10.0.19041.0 (where wchar.h is in the subfolder ucrt) and clang/LLVM 11.
There is NO linker error if either wchar.h or cwchar is included. There is still a linker error if none of them are included.
This applies to both 32-bit and 64-bit compilation.
Maybe this helps somehow.
Perhaps we should explore this idea further:
Maybe in MSVC mode we can require the translation unit to declare the CRT function before allowing a call to the _builtin variant?
I think wchar.h is included already, but we need to teach clang to mark ::wmemchr as ODR-used so that an inline definition will be emitted.
That seems... reasonable and general.
Hello,
I ran into this with Clang 11. Except for setting the standard to C++14 (which works) is there a good workaround for this issue?
Example to reproduce I still have the following link error for attached example for Clang 9:
2>------ Build started: Project: test_exe, Configuration: Debug x64 ------ 2>Building Custom Rule D:/libs/test/test_pch_with_clang_9/CMakeLists.txt 2>lld-link : error : undefined symbol: wmemchr 2>>>> referenced by C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\iosfwd:332 2>>>> test_exe.dir\Debug\test_exe.obj:(public: static wchar_t const * __cdecl std::_WChar_traits<wchar_t>::find(wchar_t const *, unsigned __int64, wchar_t const &)) 2>Done building project "test_exe.vcxproj" -- FAILED.
Seems like a coincidence. Maybe constexpr evaluation got a little bit smarter. If you call __builtin_wmemchr or any other string builtin, clang will emit a call to that symbol and expect the C runtime to provide it:
int main() { return __builtin_wmemchr(L"foo/bar", L'/', 0) == 0; } https://gcc.godbolt.org/z/CD4kZJ
The duplicate Bug 37911 seems to be fixed, too (non-constexpr std::wstring::find works now). Maybe Microsoft fixed the problem indirectly (by mistake) with the latest MSVC library version?
Seems like a coincidence. Maybe constexpr evaluation got a little bit smarter. If you call __builtin_wmemchr or any other string builtin, clang will emit a call to that symbol and expect the C runtime to provide it:
int main() { return __builtin_wmemchr(L"foo/bar", L'/', 0) == 0; } https://gcc.godbolt.org/z/CD4kZJ
It seems that the problem has disappeared in the latest version 9.0.0. No more linker errors. Is this just coincidence or has it been fixed?
*** Bug llvm/llvm-project#37259 has been marked as a duplicate of this bug. ***
For what it's worth, I posted a comment to Karsten's original bug reported to Microsoft to ask why they're using the builtins given that char_traits<wchar_t> (which uses the builtins) and char_traits<char16_t> (which doesn't) produce roughly equivalent code for the find and compare functions. I got a response from Stephan saying that build time was a separate benefit, and obviously they have more wstring users than other STL implementations, so that's probably a fair choice from them.
I put more details in the issue reported to Microsoft, but it was a bit incoherent because I was trying to quickly go through a codebase (clang) that I'm not familiar with.
Microsoft's STL impl does include wchar.h before calling __builtin_wmemchr, so mandating that wouldn't fully address the issue as originally reported. In the original issue, the problem only occurs with precompiled headers are enabled. My hasty and ignorant analysis is:
- Clang parses PCH files lazily.
- There's no earlier reference to the wmemchr function that would cause the definition of wmemchr to be read from the PCH.
- When getBuiltinLibFunction is called for __builtin_wmemchr, it generates a reference to an extern function because it knows that will work.
- If nothing else forces wmemchr to exist as a real function, then the linker error occurs.
Since this is specific to PCH's and builtins, if the builtin corresponds to a C function, is it feasible to perform the lookup in the PCH file associated with the translation unit? I guess this is ultimately what Peter's suggesting, but it seems like it would need a change/fix on Clang's side as well.
Presumably the MSVC standard library does something similar with wmemchr. Yes, it does so in the class template _WChar_traits in file xstring:
_NODISCARD static _CONSTEX#17 const _Elem* find(
_In_reads_(_Count) const _Elem* _First, const size_t _Count, const _Elem& _Ch) noexcept { // strengthened
// look for _Ch in [_First, _First + _Count)
#if _HAS_CXX17 if constexpr (is_same_v<_Elem, wchar_t>) { return __builtin_wmemchr(_First, _Ch, _Count); } else { return _Char_traits<_Elem, unsigned short>::find(_First, _Count, _Ch); } #else // _HAS_CXX17 return reinterpret_cast<const _Elem*>(_CSTD wmemchr(reinterpret_cast<const wchar_t*>(_First), _Ch, _Count)); #endif // _HAS_CXX17 }
I searched all of Microsoft's libraries for __builtin_wmemchr, and this is the only place where it is used.
Oops, I linked to the wrong line. I mean this one: https://github.com/llvm/llvm-project/blob/3813fe0bda83f40355c23298c3ca887c51ce3b22/libcxx/include/__string#L339