Skip to content
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

MSVC improvements and data truncation cleanup. #739

Merged
merged 1 commit into from
May 18, 2018

Conversation

mwinterb
Copy link
Contributor

MSVC is timid about evaluating constexpr functions unless it has to, so the "TYPES" variables end up in read-write memory even though the optimizer removes the initializer. Making TYPES constexpr causes MSVC to try harder to initialize these variables at compile time, which also ends up completely removing the (named) variable from the final compiled binary.
Fixed a data truncation warning being reported in ostream-test.

@mwinterb
Copy link
Contributor Author

Since part of the reason for the timidity about not evaluating internal::get_types at compile time is because they've been buggy, do you think there should be explicit tests validating the correctness of TYPES, or do you believe the existing tests should be good enough?

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! Looks good, just two small comments.

@@ -73,7 +73,8 @@
# endif
#endif

#if FMT_HAS_FEATURE(cxx_explicit_conversions)
#if FMT_HAS_FEATURE(cxx_explicit_conversions) || \
FMT_MSC_VER >= 1800
Copy link
Contributor

Choose a reason for hiding this comment

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

MSVC 18.0 is the minimum supported version in master, so the check is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since MSVC doesn't support __has_feature, surely there needs to be some check? FMT_MSC_VER > 0 seems like a silly test.

Copy link
Contributor

Choose a reason for hiding this comment

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

My bad, not sure what I was thinking about =).

#if FMT_USE_CONSTEXPR
static constexpr uint64_t TYPES =
IS_PACKED ? internal::get_types<Context, Args...>()
: -static_cast<int64_t>(NUM_ARGS);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please factor out the initializer into a separate function and reuse here and below to reduce copy-paste.

@vitaut
Copy link
Contributor

vitaut commented May 16, 2018

Since part of the reason for the timidity about not evaluating internal::get_types at compile time is because they've been buggy, do you think there should be explicit tests validating the correctness of TYPES, or do you believe the existing tests should be good enough?

What do you mean by get_types being buggy? They are not (always) constexpr only for compatibility with gcc 4.4, but normally all the computation is optimized away. In any case, I think existing tests should provide enough coverage.

@mwinterb
Copy link
Contributor Author

Sorry, I don't mean get_types is buggy. I meant something along the lines of "are the existing tests enough of a sanity check to be 'certain' that get_types doesn't come across any bugs in MSVC's fragile constexpr implementation?" I didn't want to make things worse for MSVC users for something that may not have any tangible benefits. As you noted, the computation is optimized away, but it leaves behind a mutable global. New mutable globals trigger some internal reviews at my company. It's easy enough to filter them out in this instance, but not needing to filter them out is even easier.

@vitaut
Copy link
Contributor

vitaut commented May 17, 2018

Sorry, I don't mean get_types is buggy. I meant something along the lines of "are the existing tests enough of a sanity check to be 'certain' that get_types doesn't come across any bugs in MSVC's fragile constexpr implementation?"

Got it. I think existing tests should be enough.

As you noted, the computation is optimized away, but it leaves behind a mutable global. New mutable globals trigger some internal reviews at my company. It's easy enough to filter them out in this instance, but not needing to filter them out is even easier.

Sure. I'm OK with the change, but please address the remaining inline comment above.

MSVC is timid about evaluating constexpr functions unless it has to, so the "TYPES" variables end up in read-write memory even though the optimizer removes the initializer. Making TYPES constexpr causes MSVC to try harder to initialize these variables at compile time, which also ends up completely removing the (named) variable from the final compiled binary.
Fixed a data truncation warning being reported in ostream-test.
@mwinterb
Copy link
Contributor Author

Addressed. I wanted to wait for your guidance on the explicit conversion thing since I figured I was missing something.

@vitaut vitaut merged commit 550ef1d into fmtlib:master May 18, 2018
@vitaut
Copy link
Contributor

vitaut commented May 18, 2018

Merged, thanks!

@mwinterb mwinterb deleted the msvcbetterments branch January 2, 2019 17:42
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.

2 participants