Skip to content

Commit

Permalink
Reorganize and refine the Windows.h import in C++ files (pytorch#48009)
Browse files Browse the repository at this point in the history
Summary:
This PR aims to reduce the import overhead and symbol noises from the `windows.h` headers.

Pull Request resolved: pytorch#48009

Reviewed By: gchanan

Differential Revision: D25045840

Pulled By: ezyang

fbshipit-source-id: 01fda70f433ba2dd0cd2d7cd676ab6ffe9d98b90
  • Loading branch information
skyline75489 authored and facebook-github-bot committed Nov 20, 2020
1 parent 6d5d336 commit 8177f63
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 13 deletions.
2 changes: 1 addition & 1 deletion aten/src/ATen/DynamicLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <dlfcn.h>
#include <libgen.h>
#else
#include <Windows.h>
#include <c10/util/win32-headers.h>
#endif

namespace at {
Expand Down
4 changes: 2 additions & 2 deletions aten/src/TH/THAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/* stuff for mapped files */
#ifdef _WIN32
#include <windows.h>
#include <c10/util/win32-headers.h>
#endif

#if defined(HAVE_MMAP)
Expand Down Expand Up @@ -333,7 +333,7 @@ typedef struct{
HANDLE handle;
HANDLE wait;
} ReleaseContext;
static VOID CALLBACK WaitForReleaseHandle(PVOID lpParam, BOOLEAN TimerOrWaitFired)
static void CALLBACK WaitForReleaseHandle(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
if (lpParam) {
ReleaseContext *ctx = (ReleaseContext *)lpParam;
Expand Down
3 changes: 3 additions & 0 deletions binaries/benchmark_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <string>
#include <thread>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <psapi.h>
#endif
Expand Down
6 changes: 1 addition & 5 deletions c10/util/Backtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
#include <vector>

#ifdef _MSC_VER
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <c10/util/win32-headers.h>
#include <iomanip>
#include <Windows.h>
#include <dbghelp.h>
#pragma comment(lib, "Dbghelp.lib")
#endif

Expand Down
4 changes: 4 additions & 0 deletions c10/util/C++17.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#error You need C++14 to compile PyTorch
#endif

#if defined(_WIN32) && (defined(min) || defined(max))
# error Macro clash with min and max -- define NOMINMAX when compiling your program on Windows
#endif

/*
* This header adds some polyfills with C++17 functionality
*/
Expand Down
57 changes: 57 additions & 0 deletions c10/util/win32-headers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef NOKERNEL
#define NOKERNEL
#endif
#ifndef NOUSER
#define NOUSER
#endif
#ifndef NOSERVICE
#define NOSERVICE
#endif
#ifndef NOSOUND
#define NOSOUND
#endif
#ifndef NOMCX
#define NOMCX
#endif
#ifndef NOGDI
#define NOGDI
#endif
#ifndef NOMSG
#define NOMSG
#endif
#ifndef NOMB
#define NOMB
#endif
#ifndef NOCLIPBOARD
#define NOCLIPBOARD
#endif

#include <windows.h>
#include <dbghelp.h>

#undef VOID
#undef DELETE
#undef IN
#undef THIS
#undef CONST
#undef NAN
#undef UNKNOWN
#undef NONE
#undef ANY
#undef IGNORE
#undef STRICT
#undef GetObject
#undef CreateSemaphore
#undef Yield
#undef RotateRight32
#undef RotateLeft32
#undef RotateRight64
#undef RotateLeft64
3 changes: 3 additions & 0 deletions modules/observers/perf_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ defined(TARGET_IPHONE_SIMULATOR)
#endif

#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif

Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/CudaIPCTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <random>

#ifdef _MSC_VER
#include <windows.h>
#include <c10/util/win32-headers.h>
#else
#include <sys/types.h>
#include <unistd.h>
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/Storage.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <torch/csrc/python_headers.h>
#ifdef _MSC_VER
#include <Windows.h>
#include <c10/util/win32-headers.h>
#endif
#include <structmember.h>

Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/jit/codegen/cuda/instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <torch/csrc/WindowsTorchApiMacro.h>

#ifdef _WIN32
#include <windows.h>
#include <c10/util/win32-headers.h>
#else
#include <pthread.h>
#include <unistd.h>
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/jit/codegen/fuser/cpu/temp_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#ifdef _WIN32
#include <WinError.h>
#include <Windows.h>
#include <c10/util/win32-headers.h>
#include <fcntl.h>
#include <io.h>
#include <process.h>
Expand Down
2 changes: 1 addition & 1 deletion torch/lib/c10d/FileStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <sys/stat.h>

#ifdef _WIN32
#include <windows.h>
#include <c10/util/win32-headers.h>
#include <fileapi.h>
#include <io.h>
#else
Expand Down

0 comments on commit 8177f63

Please sign in to comment.