Skip to content

Avoiding __builtin_strlen #4429

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

Merged
merged 5 commits into from
Apr 27, 2025
Merged

Avoiding __builtin_strlen #4429

merged 5 commits into from
Apr 27, 2025

Conversation

brevzin
Copy link
Contributor

@brevzin brevzin commented Apr 26, 2025

The added test fails on gcc 14 because of the call to __builtin_strlen, so just avoiding it during constant evaluation. Fixed #4423.

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! The change LGTM but please move the test to base-test and check the actual API being changed, namely basic_string_view's ctor.

@brevzin
Copy link
Contributor Author

brevzin commented Apr 26, 2025

Thanks for the PR! The change LGTM but please move the test to base-test and check the actual API being changed, namely basic_string_view's ctor.

Makes sense, done.

@@ -539,7 +539,7 @@ template <typename Char> class basic_string_view {
#endif
FMT_CONSTEXPR20 basic_string_view(const Char* s) : data_(s) {
#if FMT_HAS_BUILTIN(__builtin_strlen) || FMT_GCC_VERSION || FMT_CLANG_VERSION
if (std::is_same<Char, char>::value) {
if (std::is_same<Char, char>::value && !detail::is_constant_evaluated()) {
size_ = __builtin_strlen(detail::narrow(s));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the advantage of still using __builtin_strlen? Will gcc or clang still end up producing a constant in some scenarios that they wouldn't if just strlen was used?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... libstdc++ __builtin_strlen in char_traits, so I guess there's that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was some benefit to the debug codegen at some point but probably no longer matters on newer compilers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More importantly, strlen is not constexpr.

@vitaut vitaut merged commit 8978ab0 into fmtlib:master Apr 27, 2025
45 checks passed
@vitaut
Copy link
Contributor

vitaut commented Apr 27, 2025

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't use __builtin_strlen
3 participants