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

[Build] fix file_data_loader.cpp build issues for windows #4899

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

python3kgae
Copy link
Contributor

@python3kgae python3kgae commented Aug 26, 2024

Two Windows build issues have been addressed in this pull request:

  1. Avoid closing when fd_ is -1, as this would cause a crash on Windows.
  2. Introduce file.h, enabling Windows builds to utilize io.h and implement pread.

For #4661

Copy link

pytorch-bot bot commented Aug 26, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/4899

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 26, 2024
@kirklandsign
Copy link
Contributor

Thank you @python3kgae !

I would prefer that we use a single file.h, and check for ifdef _WIN32. When we use #include please use the full path starting from executorch/... , not adding a include_dir in cmake. Or at least, using a file.h which selects the correct file_posix.h and file_windows.h based on compiler macro. We also maintain an internal build system and it's easier that way.

cc @dbort

Two build issues have been addressed in this pull request:

1. Avoid closing when fd_ is -1, as this would cause a crash on Windows.
2. Introduce separate headers for Windows and Unix, enabling Windows builds
   to utilize a distinct header and implement pread.

For pytorch#4661
@python3kgae
Copy link
Contributor Author

Thank you @python3kgae !

I would prefer that we use a single file.h, and check for ifdef _WIN32. When we use #include please use the full path starting from executorch/... , not adding a include_dir in cmake. Or at least, using a file.h which selects the correct file_posix.h and file_windows.h based on compiler macro. We also maintain an internal build system and it's easier that way.

cc @dbort

Changed into single file.h.

@kirklandsign
Copy link
Contributor

Thank you! One more request: in extension/data_loader/targets.bzl line 43, can you add a line

headers = ["file.h"],

@python3kgae
Copy link
Contributor Author

Thank you! One more request: in extension/data_loader/targets.bzl line 43, can you add a line

headers = ["file.h"],

Like this?

@@ -40,6 +40,7 @@ def define_common_targets():
     runtime.cxx_library(
         name = "file_data_loader",
         srcs = ["file_data_loader.cpp"],
+        headers = ["file.h"],
         exported_headers = ["file_data_loader.h"],
         visibility = [
             "//executorch/test/...",

@kirklandsign
Copy link
Contributor

Thank you! One more request: in extension/data_loader/targets.bzl line 43, can you add a line

headers = ["file.h"],

Like this?

@@ -40,6 +40,7 @@ def define_common_targets():
     runtime.cxx_library(
         name = "file_data_loader",
         srcs = ["file_data_loader.cpp"],
+        headers = ["file.h"],
         exported_headers = ["file_data_loader.h"],
         visibility = [
             "//executorch/test/...",

Yes

@python3kgae
Copy link
Contributor Author

Thank you! One more request: in extension/data_loader/targets.bzl line 43, can you add a line

headers = ["file.h"],

Like this?

@@ -40,6 +40,7 @@ def define_common_targets():
     runtime.cxx_library(
         name = "file_data_loader",
         srcs = ["file_data_loader.cpp"],
+        headers = ["file.h"],
         exported_headers = ["file_data_loader.h"],
         visibility = [
             "//executorch/test/...",

Yes

Done.

Copy link
Contributor

@dbort dbort left a comment

Choose a reason for hiding this comment

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

Normally we try to avoid using platform-based #ifdefs for implementations, and will create separate .cpp files that the build system brings in as necessary. But this is pretty light-weight and self-contained, so it seems fine.

@@ -0,0 +1,57 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

Please call this file pread.h to make its purpose more clear.

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.

* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comment describing the purpose of this header: i.e., it ensures that a pread-compatible function is defined in the global namespace for windows and posix environments.

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.


#include <windows.h>

inline ssize_t pread(int __fd, void* __buf, size_t __nbytes, size_t __offset) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove the leading underscores from the param names. Since this is our code and not part of the standard library, we shouldn't define any names beginning with underscore.

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.

Comment on lines 22 to 23
overlapped.Offset = __offset;
overlapped.OffsetHigh = __offset >> 32;
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this do the right thing on both 32-bit and 64-bit architectures? Seems like it should, but I don't know enough about the definition of DWORD on different architectures

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DWORD will always be 32 bits on both 32-bit and 64-bit Windows systems.

It's important to note that the nNumberOfBytesToRead parameter for ReadFile is a DWORD. Therefore, if we need to read more than 4GB of data at once, the current implementation needs to be updated to handle larger file sizes.

Comment on lines 54 to 56
// To avoid conflicts with std::numeric_limits<int32_t>::max() in
// file_data_loader.cpp.
#undef max
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move this to the top of the file, closer to the header that defined it. If you know, please mention which header defined it, since on its own it's not clear why this file should bother undefining a macro that it didn't define.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@python3kgae python3kgae marked this pull request as draft September 10, 2024 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants