Skip to content

Commit 8b37eff

Browse files
jingxu10pytorchmergebot
authored andcommitted
remove abi uncertainty and potential abi conflict (pytorch#94306)
Currently there is a potential conflict for `GLIBCXX_USE_CXX11_ABI` configuration if users don't explicitly set this variable. In `caffe2/CMakeLists.txt`, if the variable is not set, an `abi checker` will be used to retrieve the ABI configuration from compiler. https://github.com/pytorch/pytorch/blob/master/caffe2/CMakeLists.txt#L1165-L1183 However, in 'torch/csrc/Module.cpp`, if the variable is not set, it will be set to `0`. The conflict happens when the default ABI of the compiler is `1`. https://github.com/pytorch/pytorch/blob/master/torch/csrc/Module.cpp#L1612 This PR eliminate this uncertainty and potential conflict. The ABI will be checked and set in `CMakeLists.txt`, and pass the value to `caffe2/CMakeLists.txt`. Meanwhile, in case the `caffe2/CMakeLists.txt` is directly invoked from a `cmake` command, The original GLIBC check logic is kept in this file. If users doesn't explicitly assign a value to `GLIBCXX_USE_CXX11_ABI`, the `abi checker` will be executed and set the value accordingly. If the `abi checker` failed to compile or execute, the value will be set to `0`. If users explicitly assigned a value, then the provided value will be used. Moreover, if `GLIBCXX_USE_CXX11_ABI` is set to `0`, the '-DGLIBCXX_USE_CXX11_ABI=0' flag won't be appended to `CMAKE_CXX_FLAGS`. Thus, whether to use ABI=0 or ABI=1 fully depends on compiler's default configuration. It could cause an issue that even users explicitly set `GLIBCXX_USE_CXX11_ABI` to `0`, the compiler still builds the binaries with ABI=1. https://github.com/pytorch/pytorch/blob/master/CMakeLists.txt#L44-L51 Pull Request resolved: pytorch#94306 Approved by: https://github.com/malfet
1 parent 02ca225 commit 8b37eff

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

CMakeLists.txt

+8-6
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ endif()
4040
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard whose features are requested to build this target.")
4141
set(CMAKE_C_STANDARD 11 CACHE STRING "The C standard whose features are requested to build this target.")
4242

43-
if(DEFINED GLIBCXX_USE_CXX11_ABI)
43+
# ---[ Utils
44+
include(cmake/public/utils.cmake)
45+
46+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
47+
include(cmake/CheckAbi.cmake)
48+
string(APPEND CMAKE_CXX_FLAGS " -D_GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI}")
4449
if(${GLIBCXX_USE_CXX11_ABI} EQUAL 1)
4550
set(CXX_STANDARD_REQUIRED ON)
46-
string(APPEND CMAKE_CXX_FLAGS " -D_GLIBCXX_USE_CXX11_ABI=1")
4751
else()
4852
# Please note this is required in order to ensure compatibility between gcc 9 and gcc 7
4953
# This could be removed when all Linux PyTorch binary builds are compiled by the same toolchain again
50-
string(APPEND CMAKE_CXX_FLAGS " -fabi-version=11")
54+
include(CheckCXXCompilerFlag)
55+
append_cxx_flag_if_supported("-fabi-version=11" CMAKE_CXX_FLAGS)
5156
endif()
5257
endif()
5358

@@ -631,9 +636,6 @@ if(INTERN_BUILD_MOBILE)
631636
set(INTERN_DISABLE_MOBILE_INTERP ON)
632637
endif()
633638

634-
# ---[ Utils
635-
include(cmake/public/utils.cmake)
636-
637639
# ---[ Version numbers for generated libraries
638640
file(READ version.txt TORCH_DEFAULT_VERSION)
639641
# Strip trailing newline

caffe2/CMakeLists.txt

+1-24
Original file line numberDiff line numberDiff line change
@@ -1157,31 +1157,8 @@ if(BUILD_TEST)
11571157
endif()
11581158
endif()
11591159

1160-
# XXX This ABI check cannot be run with arm-linux-androideabi-g++
11611160
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
1162-
if(DEFINED GLIBCXX_USE_CXX11_ABI)
1163-
message(STATUS "_GLIBCXX_USE_CXX11_ABI is already defined as a cmake variable")
1164-
else()
1165-
message(STATUS "${CMAKE_CXX_COMPILER} ${TORCH_SRC_DIR}/abi-check.cpp -o ${CMAKE_BINARY_DIR}/abi-check")
1166-
execute_process(
1167-
COMMAND
1168-
"${CMAKE_CXX_COMPILER}"
1169-
"${TORCH_SRC_DIR}/abi-check.cpp"
1170-
"-o"
1171-
"${CMAKE_BINARY_DIR}/abi-check"
1172-
RESULT_VARIABLE ABI_CHECK_COMPILE_RESULT)
1173-
if(ABI_CHECK_COMPILE_RESULT)
1174-
message(FATAL_ERROR "Could not compile ABI Check: ${ABI_CHECK_COMPILE_RESULT}")
1175-
endif()
1176-
execute_process(
1177-
COMMAND "${CMAKE_BINARY_DIR}/abi-check"
1178-
RESULT_VARIABLE ABI_CHECK_RESULT
1179-
OUTPUT_VARIABLE GLIBCXX_USE_CXX11_ABI)
1180-
if(ABI_CHECK_RESULT)
1181-
message(WARNING "Could not run ABI Check: ${ABI_CHECK_RESULT}")
1182-
endif()
1183-
endif()
1184-
message(STATUS "Determined _GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI}")
1161+
include(../cmake/CheckAbi.cmake)
11851162
endif()
11861163

11871164
# CMake config for external projects.

cmake/CheckAbi.cmake

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
if(DEFINED GLIBCXX_USE_CXX11_ABI)
2+
message(STATUS "_GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI} is already defined as a cmake variable")
3+
return()
4+
endif()
5+
6+
# XXX This ABI check cannot be run with arm-linux-androideabi-g++
7+
message(STATUS "${CMAKE_CXX_COMPILER} ${PROJECT_SOURCE_DIR}/torch/abi-check.cpp -o ${CMAKE_BINARY_DIR}/abi-check")
8+
execute_process(
9+
COMMAND
10+
"${CMAKE_CXX_COMPILER}"
11+
"${PROJECT_SOURCE_DIR}/torch/abi-check.cpp"
12+
"-o"
13+
"${CMAKE_BINARY_DIR}/abi-check"
14+
RESULT_VARIABLE ABI_CHECK_COMPILE_RESULT)
15+
if(ABI_CHECK_COMPILE_RESULT)
16+
message(FATAL_ERROR "Could not compile ABI Check: ${ABI_CHECK_COMPILE_RESULT}")
17+
set(GLIBCXX_USE_CXX11_ABI 0)
18+
endif()
19+
execute_process(
20+
COMMAND "${CMAKE_BINARY_DIR}/abi-check"
21+
RESULT_VARIABLE ABI_CHECK_RESULT
22+
OUTPUT_VARIABLE GLIBCXX_USE_CXX11_ABI)
23+
if(ABI_CHECK_RESULT)
24+
message(WARNING "Could not run ABI Check: ${ABI_CHECK_RESULT}")
25+
set(GLIBCXX_USE_CXX11_ABI 0)
26+
endif()
27+
message(STATUS "Determined _GLIBCXX_USE_CXX11_ABI=${GLIBCXX_USE_CXX11_ABI}")

0 commit comments

Comments
 (0)