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

Match SPDLOG_CONSTEXPR_FUNC to FMT_CONSTEXPR #2901

Merged
merged 5 commits into from
Oct 13, 2023
Merged

Conversation

kkraus14
Copy link
Contributor

@kkraus14 kkraus14 commented Oct 11, 2023

Fixes #2856.

PR based on #2858 and #2859.

Fixes the issue where a constexpr function in spdlog may call a non-constexpr function in fmt because FMT_CONSTEXPR is not defined or is defined differently for a given compiler.

Ideally, a test could be added of building a .cu file using nvcc and add that to CI to prevent nvcc breakages moving forward.

ShujianQian and others added 2 commits October 11, 2023 16:15
fix the issue where constexpr function in spdlog may call non-constexpr function in the bundled fmt because FMT_USE_CONSTEXPR is not defined.
@kkraus14
Copy link
Contributor Author

An alternative approach that might work here is to reuse FMT_CONSTEXPR with something like:

#ifdef FMT_CONSTEXPR
    #define SPDLOG_CONSTEXPR_FUNC FMT_CONSTEXPR
#else
    #if __cplusplus >= 201402L
        #define SPDLOG_CONSTEXPR_FUNC constexpr
    #else
        #define SPDLOG_CONSTEXPR_FUNC inline
    #endif
#endif

This may be friendlier to when we're using std::format as opposed to fmt as well.

@gabime
Copy link
Owner

gabime commented Oct 11, 2023

An alternative approach that might work here is to reuse FMT_CONSTEXPR with something like

Agreed. Seems cleaner and to the point (use same constexpr as fmt).

Comment on lines +81 to +84
#if FMT_USE_CONSTEXPR
#define SPDLOG_CONSTEXPR_FUNC FMT_CONSTEXPR
#else
#define SPDLOG_CONSTEXPR_FUNC inline
#define SPDLOG_CONSTEXPR_FUNC inline
Copy link
Contributor Author

@kkraus14 kkraus14 Oct 11, 2023

Choose a reason for hiding this comment

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

I needed to change this slightly to maintain the current behavior. If fmt determines it can't use constexpr then it defines FMT_CONSTEXPR as empty

// Check if relaxed C++14 constexpr is supported.
// GCC doesn't allow throw in constexpr until version 6 (bug 67371).
#ifndef FMT_USE_CONSTEXPR
# if (FMT_HAS_FEATURE(cxx_relaxed_constexpr) || FMT_MSC_VERSION >= 1912 || \
(FMT_GCC_VERSION >= 600 && FMT_CPLUSPLUS >= 201402L)) && \
!FMT_ICC_VERSION && !defined(__NVCC__)
# define FMT_USE_CONSTEXPR 1
# else
# define FMT_USE_CONSTEXPR 0
# endif
#endif
#if FMT_USE_CONSTEXPR
# define FMT_CONSTEXPR constexpr
#else
# define FMT_CONSTEXPR
#endif
while spdlog's current behavior is to define it as inline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could probably use some preprocessor magic to detect when FMT_CONSTEXPR is empty and use inline instead in those situations, but that adds some non-trivial complexity.

Copy link
Owner

Choose a reason for hiding this comment

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

Why would it be empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It gets defined as empty for some reason:

# define FMT_CONSTEXPR

Copy link
Owner

Choose a reason for hiding this comment

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

I think it won't matter for spdlog. It can be empty for spdlog as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated based on that

@kkraus14
Copy link
Contributor Author

Looks like without the inline in C++11 (in C++14 and newer it would be constexpr) we hit some ODR violations from the header being included in multiple translation units that then get linked into a single artifact.

@gabime
Copy link
Owner

gabime commented Oct 12, 2023

So I guess we need to use your first suggestion after all.

@kkraus14
Copy link
Contributor Author

Reverted the change where I think this should be good to go. Thanks for your review and feedback @gabime!

@gabime gabime merged commit 0c4fb03 into gabime:v1.x Oct 13, 2023
8 checks passed
@gabime
Copy link
Owner

gabime commented Oct 13, 2023

Thanks @kkraus14

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