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

XMGLOBALCONST poorly initialized when used within a cpp modules project #211

Open
MartinDew opened this issue Oct 10, 2024 · 6 comments
Open
Assignees
Labels
complier Issue related to compiler codegen

Comments

@MartinDew
Copy link

MartinDew commented Oct 10, 2024

I've been using directx on a personal project that uses modules and had crashes because some of the XMGLOBALCONST declared constants simply don't have the proper values at runtime. In our case it was specifically g_XMInfinity and g_XMOneHalf (The values were 000). The crashes were caused by asserts failing on XMIsInfinity, among others, due to those.

In our case, we use DXTK and simplemath and import it through a Module partition with the following syntax : export import <SimpleMath.h>.

It looks like it has something to do with how __declspec(selectany) reacts when used within modules.

Are we right or is there something we are missing?

Thanks!

@MartinDew MartinDew changed the title XMGLOBALCONST poorly initialized when used with a cpp modules project XMGLOBALCONST poorly initialized when used within a cpp modules project Oct 10, 2024
@walbourn walbourn self-assigned this Oct 14, 2024
@walbourn walbourn added the conformance Related to supporting non-MSVC compilers label Oct 14, 2024
@walbourn
Copy link
Member

Can you link to how you are building the SimpleMath module?

@walbourn
Copy link
Member

It might also be a good idea to open your project in VS, and use https://aka.ms/vsfeedback or the "Report a problem..." link. This seems like a bug in the compiler or linker.

@walbourn walbourn added complier Issue related to compiler codegen and removed conformance Related to supporting non-MSVC compilers labels Oct 15, 2024
@MartinDew
Copy link
Author

Can you link to how you are building the SimpleMath module?

Not sure if it's what you meant but I am using MSbuild and a VS project. We're using https://github.com/ubisoft/Sharpmake to create our sln and proj files.

@MartinDew
Copy link
Author

Update : I ended up importing DXMath as source into my project and switching the XMGLOBALCONST to be static constexpr fixes the problem.

@walbourn
Copy link
Member

Can you check the code-generation when using static constexpr. Does it look like it's coming from a shared read-only datasegment, or are you getting a lot of copies of the same data in each module?

@walbourn
Copy link
Member

walbourn commented Oct 24, 2024

Also it does not appear there is any standard define to know if its being used in a C++20 module or not.

Note you DO NOT have the modify my source. You can use #define XMGLOBALCONST static constexpr before including the headers.

If I were to change the source, I'd probably have to go with (assuming C++20 code gen is not worse with it)

#ifndef XMGLOBALCONST
#if __cplusplus >= 202002L
#define XMGLOBALCONST static constexpr
#elif defined(__GNUC__) && !defined(__MINGW32__)
#define XMGLOBALCONST extern const __attribute__((weak))
#else
#define XMGLOBALCONST extern const __declspec(selectany)
#endif
#endif

My main concern is that math-heavy code would end up with many copies of the constants instead of just one.

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

No branches or pull requests

2 participants