Skip to content

Commit 3608cb2

Browse files
authored
Add precompiled header support (microsoft#25739)
### Description This PR introduces precompiled header (PCH) support for ONNX Runtime targets that exhibited the longest build times when built with the MSVC toolset. By analyzing build performance, I identified a subset of targets with significant compilation overhead due to repeated header processing. Enabling PCH for these targets reduces redundant parsing, improving incremental and full build performance. Changes include: Added PCH configuration to selected CMake targets with the highest build cost in MSVC builds. Ensured PCH setup is compatible with the existing build configurations. Verified successful compilation and linkage with PCH enabled under MSVC. Impact: ~30% reduction in build time
1 parent b8821e9 commit 3608cb2

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

cmake/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,3 +1791,12 @@ else()
17911791
include("${CMAKE_CURRENT_SOURCE_DIR}/arm64x.cmake")
17921792
endif()
17931793
endif()
1794+
1795+
if(onnxruntime_BUILD_UNIT_TESTS)
1796+
include("${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime_test_pch.cmake")
1797+
endif()
1798+
1799+
# Include precompiled header configuration for providers
1800+
if(TARGET onnxruntime_providers)
1801+
include("${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime_providers_pch.cmake")
1802+
endif()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Precompiled header configuration for onnxruntime_providers
2+
3+
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
4+
# Visual Studio PCH
5+
target_precompile_headers(onnxruntime_providers PRIVATE
6+
"${CMAKE_CURRENT_SOURCE_DIR}/providers_pch.h"
7+
)
8+
endif()

cmake/onnxruntime_test_pch.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Precompiled header configuration for onnxruntime_test_all
2+
3+
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
4+
# Visual Studio PCH
5+
target_precompile_headers(onnxruntime_test_all PRIVATE
6+
"${CMAKE_CURRENT_SOURCE_DIR}/test_pch.h"
7+
)
8+
endif()
9+
10+
# Exclude certain files that might conflict with PCH
11+
set(PCH_EXCLUDE_FILES
12+
# Add any problematic source files here
13+
"${TEST_SRC_DIR}/framework/tensor_shape_test.cc"
14+
)
15+
16+
foreach(file ${PCH_EXCLUDE_FILES})
17+
set_source_files_properties(${file} PROPERTIES
18+
SKIP_PRECOMPILE_HEADERS ON
19+
)
20+
endforeach()

cmake/providers_pch.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
// Core framework headers (highest compilation time impact)
7+
#include "core/framework/op_kernel.h"
8+
#include "core/framework/op_kernel_info.h"
9+
#include "core/framework/execution_provider.h"
10+
#include "core/framework/op_node_proto_helper.h"
11+
#include "core/framework/data_types.h"
12+
#include "core/framework/tensor.h"
13+
14+
// Graph-related headers
15+
#include "core/graph/graph_viewer.h"
16+
#include "core/graph/graph.h"
17+
#include "core/graph/onnx_protobuf.h"
18+
19+
// ONNX schema definitions
20+
#include "onnx/defs/schema.h"
21+
22+
// Windows-specific headers (if applicable)
23+
#ifdef _WIN32
24+
#include <windows.h>
25+
#endif

cmake/test_pch.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
// Test framework headers (highest compilation time impact)
7+
#include "gtest/gtest.h"
8+
#include "gtest/gtest-assertion-result.h"
9+
#include "gtest/gtest-message.h"
10+
#include "gtest/internal/gtest-port.h"
11+
12+
// Core test utilities (most frequently used in tests)
13+
#include "test/providers/provider_test_utils.h"
14+
#include "test/providers/checkers.h"
15+
16+
// ONNX and Protocol Buffer headers
17+
#include "core/graph/onnx_protobuf.h"
18+
#include "onnx/defs/schema.h"
19+
20+
// Data types and framework headers
21+
#include "core/framework/data_types.h"
22+
23+
// Windows-specific headers (if applicable)
24+
#ifdef _WIN32
25+
#include <windows.h>
26+
#endif

0 commit comments

Comments
 (0)