constinit: C++20 tip about constexpr initialization for global variables
Hello WebKittens, Now, our baseline is GCC 10, so I would like to introduce useful C++20 feature here, constinit. https://en.cppreference.com/w/cpp/language/constinit You can annotate the global variables etc. with `constinit`, like, ``` static constinit StaticStringImpl aboutBlankString { "about:blank" }; ``` Tthe benefit is that, compiler knows the strong requirement that this variable must be initialized at compile-time (constexpr constructors). Our global constructor warning is just a warning, so compilers typically allow compiling and at the very end, it emits a warning as an error. So, at that time, we don't know what is breaking constexpr condition. For example, ``` /Volumes/WebKit/macOS/OpenSource/Source/WTF/wtf/URL.cpp:932:25: error: declaration requires a global constructor [-Werror,-Wglobal-constructors] static StaticStringImpl aboutBlankString { "about:blank" }; ``` You can see that *something* in StaticStringImpl breaks `constexpr`, but we cannot see what is breaking it, because, when emitting this warning, compiler already lost that information (and compiler just already compiled it via non constexpr, and at the very end, it emits warnings like this). But if you annotate this variable with `constinit`, then compiler knows this requirement a-priori, thus, ``` /Users/yusukesuzuki/dev/OpenSource/Source/WTF/wtf/URL.cpp:932:35: error: variable does not have a constant initializer static constinit StaticStringImpl aboutBlankString { "about:blank" }; /Users/yusukesuzuki/dev/OpenSource/Source/WTF/wtf/URL.cpp:932:8: note: required by 'constinit' specifier here static constinit StaticStringImpl aboutBlankString { "about:blank" }; In file included from /Users/yusukesuzuki/dev/OpenSource/Source/WTF/wtf/URL.cpp:28: In file included from /Users/yusukesuzuki/dev/OpenSource/Source/WTF/wtf/URL.h:28: In file included from /Users/yusukesuzuki/dev/OpenSource/WebKitBuild/Release/usr/local/include/wtf/text/WTFString.h:28: In file included from /Users/yusukesuzuki/dev/OpenSource/WebKitBuild/Release/usr/local/include/wtf/text/StringImpl.h:43: /Users/yusukesuzuki/dev/OpenSource/WebKitBuild/Release/usr/local/include/wtf/text/WYHasher.h:248:28: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression const uint8_t* p = (const uint8_t*)characters; /Users/yusukesuzuki/dev/OpenSource/WebKitBuild/Release/usr/local/include/wtf/text/WYHasher.h:64:40: note: in call to 'computeHashImpl(&"about:blank"[0], 11, 0)' return finalizeAndMaskTop8Bits(computeHashImpl<T, Converter>(data, characterCount, 0)); /Users/yusukesuzuki/dev/OpenSource/WebKitBuild/Release/usr/local/include/wtf/text/WYHasher.h:76:16: note: in call to 'computeHashAndMaskTop8Bits(&"about:blank"[0], 11)' return computeHashAndMaskTop8Bits<T, DefaultConverter>(characters, charactersCount - 1); In file included from /Users/yusukesuzuki/dev/OpenSource/Source/WTF/wtf/URL.cpp:28: In file included from /Users/yusukesuzuki/dev/OpenSource/Source/WTF/wtf/URL.h:28: In file included from /Users/yusukesuzuki/dev/OpenSource/WebKitBuild/Release/usr/local/include/wtf/text/WTFString.h:28: /Users/yusukesuzuki/dev/OpenSource/WebKitBuild/Release/usr/local/include/wtf/text/StringImpl.h:1251:89: note: in call to 'computeLiteralHashAndMaskTop8Bits("about:blank")' s_hashFlag8BitBuffer | s_hashFlagDidReportCost | stringKind | BufferInternal | (WYHasher::computeLiteralHashAndMaskTop8Bits(characters) << s_flagCount), ConstructWithConstExpr) ``` The compiler can tell directly what is breaking `constexpr` condition. `constinit` is available from GCC 10, so it is now supported in all WebKit-supporting compilers. Best regards, -Yusuke
participants (1)
-
Yusuke Suzuki