Skip to content

Commit 4bec0d5

Browse files
authored
Merge pull request #9254 from The-OpenROAD-Project-staging/secure-enable-lto
Enabled LTO (Link Time Optimization)
2 parents 745f72f + a226472 commit 4bec0d5

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

.bazelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ build --cxxopt "-std=c++20" --host_cxxopt "-std=c++20"
1616
build --cxxopt "-xc++" --host_cxxopt "-xc++"
1717
build --cxxopt "-DBAZEL_BUILD" --host_cxxopt "-DBAZEL_BUILD"
1818

19+
# Default to optimized build (-c opt), O3
20+
build -c opt
21+
build:opt --copt=-O3
22+
23+
# Enable Full-LTO (Link Time Optimization)
24+
build:opt --copt=-flto
25+
build:opt --linkopt=-flto
26+
1927
# Needed for floating point stability in FFT (fft_test will check this).
2028
# See also https://kristerw.github.io/2021/11/09/fp-contract/
2129
build --cxxopt "-ffp-contract=off" --host_cxxopt "-ffp-contract=off"

CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,51 @@ message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
108108
message(STATUS "C++ Standard Required: ${CMAKE_CXX_STANDARD_REQUIRED}")
109109
message(STATUS "C++ Extensions: ${CMAKE_CXX_EXTENSIONS}")
110110

111+
# Detect OS distribution and version from /etc/os-release
112+
# LTO is known to cause build errors on Debian 11 and Rocky Linux 8
113+
set(LTO_UNSUPPORTED_OS FALSE)
114+
if(EXISTS "/etc/os-release")
115+
file(STRINGS "/etc/os-release" OS_RELEASE_CONTENTS)
116+
set(OS_ID "")
117+
set(OS_VERSION_ID "")
118+
foreach(line ${OS_RELEASE_CONTENTS})
119+
if(line MATCHES "^ID=(.*)$")
120+
string(REGEX REPLACE "^ID=\"?([^\"]*)\"?$" "\\1" OS_ID "${line}")
121+
endif()
122+
if(line MATCHES "^VERSION_ID=(.*)$")
123+
string(REGEX REPLACE "^VERSION_ID=\"?([^\"]*)\"?$" "\\1" OS_VERSION_ID "${line}")
124+
endif()
125+
endforeach()
126+
127+
# Check for unsupported OS for LTO
128+
if((OS_ID STREQUAL "debian" AND OS_VERSION_ID STREQUAL "11") OR
129+
(OS_ID STREQUAL "rocky" AND OS_VERSION_ID MATCHES "^8"))
130+
set(LTO_UNSUPPORTED_OS TRUE)
131+
message(STATUS "Detected unsupported OS (${OS_ID} ${OS_VERSION_ID}): LTO will be disabled due to known build issues")
132+
endif()
133+
endif()
134+
135+
option(LINK_TIME_OPTIMIZATION "Flag to control link time optimization: on by default" ON)
136+
set(LTO_STATUS "disabled")
137+
if (LINK_TIME_OPTIMIZATION AND NOT LTO_UNSUPPORTED_OS)
138+
include(CheckIPOSupported)
139+
check_ipo_supported(RESULT lto_supported OUTPUT lto_error)
140+
if(lto_supported)
141+
# LTO is enabled globally for Release build
142+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
143+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO FALSE)
144+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DEBUG FALSE)
145+
146+
string(TOUPPER "${CMAKE_BUILD_TYPE}" UPPER_BUILD_TYPE)
147+
if(UPPER_BUILD_TYPE STREQUAL "RELEASE")
148+
set(LTO_STATUS "enabled")
149+
endif()
150+
else()
151+
set(LTO_STATUS "not supported: ${lto_error}")
152+
endif()
153+
endif()
154+
message(STATUS "LTO/IPO is ${LTO_STATUS}")
155+
111156
# configure a header file to pass some of the CMake settings
112157
configure_file(
113158
${OPENROAD_HOME}/include/ord/Version.hh.cmake

src/gui/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ if (Qt5_FOUND AND BUILD_GUI)
8686
OpenSTA
8787
)
8888

89+
# Disable LTO for gui. LTO causes a crash with Qt plugin loading
90+
set_target_properties(gui PROPERTIES
91+
INTERPROCEDURAL_OPTIMIZATION OFF
92+
INTERPROCEDURAL_OPTIMIZATION_RELEASE OFF
93+
)
94+
target_compile_options(gui PRIVATE "-fno-lto")
95+
target_link_options(gui PRIVATE "-fno-lto")
96+
8997
messages(
9098
TARGET gui
9199
)

0 commit comments

Comments
 (0)