Skip to content

Fix c++20 compilation problem for clang10 #1228

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

Conversation

ledergec
Copy link

This fixes the following issue:
#1193

@coveralls
Copy link

Coverage Status

Coverage remained the same at 93.801% when pulling 3279095 on ledergec:fix_compilation_problem_with_c++20_standard into c60ebf7 on open-source-parsers:master.

void deallocate(volatile pointer p, size_type n) {
std::memset(p, 0, n * sizeof(T));
void deallocate(pointer p, size_type n) {
volatile pointer do_not_optimize_memset_p = p;
Copy link
Contributor

Choose a reason for hiding this comment

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

I really don't see how this ever prevented an optimizer from omitting the memset call.

Again this is just a volatile pointer to T, NOT a pointer to volatile T.

Pointer values are not modified by a memset call, so from the optimizer's PoV, it doesn't matter if the pointer changes or not. A local variable or function parameter (equivalently) that is not leaked outside the function has a provable scope of visible effects that the optimizer can exploit whether it's marked volatile or not. My opinion about this variable is that the volatile applied only to the parameter local and didn't affect the function signature before. Same as a const parameter is ignored. It can be removed without an ABI break. Further, the do_not_optimize_memset_p has no effect and we might as well just memset p directly.

Copy link
Author

Choose a reason for hiding this comment

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

What you write sounds reasonable to me. However, the codes intention is stated clearly in the comment. Hence, the question: is this actually a bug?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this may have been a "faith-based" use of volatile. It happens. It should be replaced with something that actually works.

I think we want the C11 memset_s on platforms on which it's available.
It is guaranteed not to be optimized away, and memset has no such guarantee, regardless of any irrelevant volatile qualifications on the pointer argument.

https://en.cppreference.com/w/c/string/byte/memset

Copy link
Author

Choose a reason for hiding this comment

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

I updated the PR with memset_s. Indeed according to the c++ doc this should never get optimized away.

I hope my usage is correct.

Thanks for your comments.

@ledergec ledergec force-pushed the fix_compilation_problem_with_c++20_standard branch from 3279095 to 6e442c5 Compare October 13, 2020 15:56
@ledergec ledergec force-pushed the fix_compilation_problem_with_c++20_standard branch from 6e442c5 to 8a923cb Compare October 14, 2020 09:32
@ledergec
Copy link
Author

ledergec commented Nov 2, 2020

I feel like this is ready to be merged? Anything blocking?

@BillyDonahue
Copy link
Contributor

Looks good to me.

@BillyDonahue BillyDonahue merged commit 30170d6 into open-source-parsers:master Nov 2, 2020
@BillyDonahue
Copy link
Contributor

FYI: There are Github magic words that would make this issue auto-fix when the PR is merged.
So, rather than "This fixes the following issue: #1193", you'd say:

Fixes #1193

@BillyDonahue BillyDonahue linked an issue May 14, 2021 that may be closed by this pull request
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.

Volatile is deprecated in C++20
3 participants