@@ -105,33 +105,125 @@ if(APPLE)
105105 target_compile_definitions (_netgraph_core PRIVATE _LIBCPP_DISABLE_AVAILABILITY=1 _LIBCPP_ABI_VERSION=1 )
106106endif ()
107107
108- # Optional coverage instrumentation for GCC/Clang
108+ # =============================================================================
109+ # Performance optimizations (disabled when coverage/sanitizers are enabled)
110+ # =============================================================================
111+
109112option (NETGRAPH_CORE_COVERAGE "Enable C++ coverage instrumentation" OFF )
113+ option (NETGRAPH_CORE_SANITIZE "Enable Address/Undefined sanitizers" OFF )
114+ option (NETGRAPH_CORE_NATIVE "Optimize for current CPU (not portable)" OFF )
115+ option (NETGRAPH_CORE_FAST_MATH "Enable fast math (may affect precision)" OFF )
116+ option (NETGRAPH_CORE_PGO_GENERATE "Instrument for PGO profile collection" OFF )
117+ option (NETGRAPH_CORE_PGO_USE "Use PGO profile data (set NETGRAPH_CORE_PGO_DIR)" OFF )
118+ set (NETGRAPH_CORE_PGO_DIR "${CMAKE_BINARY_DIR} /pgo" CACHE PATH "Directory for PGO profile data" )
119+
120+ # Only apply optimizations when not in debug/instrumentation mode
121+ if (NOT NETGRAPH_CORE_COVERAGE AND NOT NETGRAPH_CORE_SANITIZE)
122+ # LTO (Link Time Optimization) - enables cross-module inlining
123+ include (CheckIPOSupported )
124+ check_ipo_supported (RESULT ipo_supported OUTPUT ipo_error )
125+ if (ipo_supported)
126+ message (STATUS "Enabling LTO (Link Time Optimization)" )
127+ set_target_properties (netgraph_core PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE )
128+ set_target_properties (_netgraph_core PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE )
129+ else ()
130+ message (STATUS "LTO not supported: ${ipo_error} " )
131+ endif ()
132+
133+ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" )
134+ # GCC-only: allow aggressive inlining by promising symbols won't be interposed
135+ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
136+ target_compile_options (netgraph_core PRIVATE -fno-semantic-interposition )
137+ target_compile_options (_netgraph_core PRIVATE -fno-semantic-interposition )
138+ endif ()
139+
140+ # Safe floating-point optimizations (we don't use errno, FP exceptions, or change rounding mode)
141+ target_compile_options (netgraph_core PRIVATE -fno-math-errno -fno-trapping-math )
142+ target_compile_options (_netgraph_core PRIVATE -fno-math-errno -fno-trapping-math )
143+
144+ # Aggressive loop optimizations
145+ target_compile_options (netgraph_core PRIVATE -funroll-loops )
146+ target_compile_options (_netgraph_core PRIVATE -funroll-loops )
147+
148+ # Native CPU instructions (AVX2, etc.) - only for local builds
149+ if (NETGRAPH_CORE_NATIVE)
150+ message (STATUS "Enabling native CPU optimizations (-march=native)" )
151+ target_compile_options (netgraph_core PRIVATE -march=native )
152+ target_compile_options (_netgraph_core PRIVATE -march=native )
153+ endif ()
154+
155+ # Fast math - trades IEEE compliance for speed
156+ if (NETGRAPH_CORE_FAST_MATH)
157+ message (STATUS "Enabling fast math optimizations" )
158+ target_compile_options (netgraph_core PRIVATE -ffast-math )
159+ target_compile_options (_netgraph_core PRIVATE -ffast-math )
160+ endif ()
161+
162+ # PGO (Profile-Guided Optimization) - uses runtime profile to guide inlining decisions
163+ # GCC: -fprofile-generate/-fprofile-use, Clang: -fprofile-instr-*
164+ if (NETGRAPH_CORE_PGO_GENERATE)
165+ message (STATUS "PGO: Instrumenting for profile generation -> ${NETGRAPH_CORE_PGO_DIR} " )
166+ file (MAKE_DIRECTORY ${NETGRAPH_CORE_PGO_DIR} )
167+ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
168+ target_compile_options (netgraph_core PRIVATE -fprofile-generate=${NETGRAPH_CORE_PGO_DIR} )
169+ target_compile_options (_netgraph_core PRIVATE -fprofile-generate=${NETGRAPH_CORE_PGO_DIR} )
170+ target_link_options (netgraph_core PRIVATE -fprofile-generate=${NETGRAPH_CORE_PGO_DIR} )
171+ target_link_options (_netgraph_core PRIVATE -fprofile-generate=${NETGRAPH_CORE_PGO_DIR} )
172+ else ()
173+ # Clang/AppleClang: use LLVM instrumentation PGO
174+ target_compile_options (netgraph_core PRIVATE -fprofile-instr-generate=${NETGRAPH_CORE_PGO_DIR}/default.profraw )
175+ target_compile_options (_netgraph_core PRIVATE -fprofile-instr-generate=${NETGRAPH_CORE_PGO_DIR}/default.profraw )
176+ target_link_options (netgraph_core PRIVATE -fprofile-instr-generate=${NETGRAPH_CORE_PGO_DIR}/default.profraw )
177+ target_link_options (_netgraph_core PRIVATE -fprofile-instr-generate=${NETGRAPH_CORE_PGO_DIR}/default.profraw )
178+ endif ()
179+ elseif (NETGRAPH_CORE_PGO_USE)
180+ message (STATUS "PGO: Using profile data from ${NETGRAPH_CORE_PGO_DIR} " )
181+ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
182+ target_compile_options (netgraph_core PRIVATE -fprofile-use=${NETGRAPH_CORE_PGO_DIR} -fprofile-correction )
183+ target_compile_options (_netgraph_core PRIVATE -fprofile-use=${NETGRAPH_CORE_PGO_DIR} -fprofile-correction )
184+ else ()
185+ # Clang/AppleClang: use merged .profdata file
186+ target_compile_options (netgraph_core PRIVATE -fprofile-instr-use=${NETGRAPH_CORE_PGO_DIR}/default.profdata )
187+ target_compile_options (_netgraph_core PRIVATE -fprofile-instr-use=${NETGRAPH_CORE_PGO_DIR}/default.profdata )
188+ endif ()
189+ endif ()
190+ elseif (MSVC )
191+ # /O2: full optimization, /GL: whole program optimization (MSVC's LTO)
192+ target_compile_options (netgraph_core PRIVATE /O2 /GL )
193+ target_compile_options (_netgraph_core PRIVATE /O2 /GL )
194+ if (NETGRAPH_CORE_FAST_MATH)
195+ target_compile_options (netgraph_core PRIVATE /fp:fast )
196+ target_compile_options (_netgraph_core PRIVATE /fp:fast )
197+ endif ()
198+ endif ()
199+ endif ()
200+
201+ # =============================================================================
202+ # Debug/instrumentation options (mutually exclusive with optimizations above)
203+ # =============================================================================
204+
110205if (NETGRAPH_CORE_COVERAGE)
111- message (STATUS "Enabling coverage instrumentation for C++ targets " )
206+ message (STATUS "Enabling coverage instrumentation (disables optimizations) " )
112207 if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" )
113208 foreach (tgt netgraph_core _netgraph_core)
114- # Use no optimizations and include debug info for accurate line mapping
115209 target_compile_options (${tgt} PRIVATE -O0 -g --coverage )
116- # Link coverage runtime
117210 target_link_options (${tgt} PRIVATE --coverage )
118211 endforeach ()
119212 else ()
120- message (WARNING "NETGRAPH_CORE_COVERAGE is set but compiler ' ${CMAKE_CXX_COMPILER_ID} ' is not supported " )
213+ message (WARNING "NETGRAPH_CORE_COVERAGE requires GCC or Clang " )
121214 endif ()
122215endif ()
123216
124- # Optional sanitizers for debug testing
125- option (NETGRAPH_CORE_SANITIZE "Enable Address/Undefined sanitizers" OFF )
126217if (NETGRAPH_CORE_SANITIZE)
218+ message (STATUS "Enabling sanitizers (disables optimizations)" )
127219 if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" )
128220 set (SAN_FLAGS "-fsanitize=address,undefined -fno-omit-frame-pointer" )
129221 foreach (tgt netgraph_core _netgraph_core)
130222 target_compile_options (${tgt} PRIVATE ${SAN_FLAGS} )
131223 target_link_options (${tgt} PRIVATE ${SAN_FLAGS} )
132224 endforeach ()
133225 else ()
134- message (WARNING "NETGRAPH_CORE_SANITIZE is set but compiler ' ${CMAKE_CXX_COMPILER_ID} ' is not supported " )
226+ message (WARNING "NETGRAPH_CORE_SANITIZE requires GCC or Clang " )
135227 endif ()
136228endif ()
137229
0 commit comments