Skip to content

Commit 7c6fa12

Browse files
authored
Upgrade to C++26 (#42)
2 parents ef08723 + c5bb35a commit 7c6fa12

29 files changed

Lines changed: 580 additions & 425 deletions

‎.github/workflows/premerge.yml‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ on:
66

77
env:
88
CMAKE_PRESET: release
9+
CC: gcc-16
10+
CXX: g++-16
911

1012
jobs:
1113
build:
12-
runs-on: ubuntu-latest
14+
runs-on: ubuntu-24.04
1315

1416
steps:
1517
- uses: actions/checkout@v7
@@ -18,6 +20,16 @@ jobs:
1820

1921
- uses: pre-commit/action@v3.0.1
2022

23+
- name: Install GCC 16
24+
run: |
25+
# We require C++26, which currently is best supported in gcc-16.
26+
# By default, Ubuntu 24.04 only supports up to gcc-13,
27+
# but ubuntu-toolchain-r/test carries experimental trunk snapshots.
28+
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
29+
sudo apt-get update
30+
sudo apt-get install -y gcc-16 g++-16
31+
g++-16 --version
32+
2133
- name: Install LLVM
2234
run: |
2335
# LLVM 23 has branched, so it lives in its own apt suite now. Upstream
@@ -30,9 +42,7 @@ jobs:
3042
echo "deb [signed-by=/etc/apt/keyrings/apt.llvm.org.asc] https://apt.llvm.org/${CODENAME}/ llvm-toolchain-${CODENAME}-23 main" \
3143
| sudo tee /etc/apt/sources.list.d/llvm-23.list > /dev/null
3244
sudo apt-get update
33-
# The snapshot packaging pulled in llvm-23-dev via libmlir-23-dev; the
34-
# release packaging does not, and MLIRConfig.cmake needs LLVMConfig.cmake.
35-
sudo apt-get install -y clang-23 lld-23 llvm-23-dev libmlir-23-dev mlir-23-tools
45+
sudo apt-get install -y llvm-23-dev libmlir-23-dev mlir-23-tools
3646
3747
- name: ccache
3848
uses: hendrikmuhs/ccache-action@v1.2

‎CMakeLists.txt‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)
55

66
project(python++)
77

8-
set(CMAKE_CXX_STANDARD 23)
8+
set(CMAKE_CXX_STANDARD 26)
99

1010
include(cmake/CPM.cmake)
1111

@@ -77,10 +77,26 @@ target_compile_options(project_warnings
7777
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-Wno-sign-conversion;-Wno-shadow;-Wno-implicit-fallthrough;-Wno-old-style-cast;-Wno-deprecated-copy;-Wno-missing-field-initializers;-Wno-null-dereference;-Wno-maybe-uninitialized;-Wno-stringop-overflow>
7878
)
7979

80+
target_compile_options(project_options
81+
# -fno-omit-frame-pointer is needed, otherwise we cannot access the stack base
82+
# pointer reliably. The garbage collector scans the C++ stack conservatively
83+
# for roots (see MarkSweepGC::collect_roots), so every target whose frames can
84+
# be live across an allocation needs it.
85+
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-fno-omit-frame-pointer>
86+
)
87+
88+
# Every first-party target links against project_options and project_warnings
89+
# through this helper so they are all compiled the same way.
90+
include(PythonCppFlags)
91+
92+
# check_cxx_source_compiles links the snippet, so it needs a main(); and
93+
# std::uint64_t needs <cstdint> rather than coming along with <bit>.
8094
check_cxx_source_compiles(
8195
"#include <bit>
96+
#include <cstdint>
8297
constexpr double f64v = 19880124.0;
83-
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
98+
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
99+
int main() { return u64v == 0; }"
84100
STL_SUPPORTS_BIT_CAST)
85101

86102
find_library(MATH_LIBRARY m)

‎CMakePresets.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@
7070
}
7171
]
7272

73-
}
73+
}

‎cmake/PythonCppFlags.cmake‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Helper for giving every first-party target the same compiler flags.
2+
#
3+
# The flags come from the external `project_options` package (added with CPM in
4+
# the top-level CMakeLists.txt), which exposes them as two INTERFACE targets:
5+
# * project_options - sanitizers, hardening, linker and optimisation flags
6+
# * project_warnings - the warning set plus -Werror
7+
# The top-level CMakeLists.txt adjusts both to taste; everything else just links
8+
# against them through `python_cpp_link_project_options()` below. Third-party
9+
# code pulled in by CPM (spdlog, googletest, linenoise, ...) is deliberately
10+
# left alone.
11+
#
12+
# The helper exists because of the LLVM/MLIR target helpers
13+
# (add_mlir_library, add_mlir_conversion_library, add_mlir_translation_library,
14+
# ...): they compile their sources in a separate `obj.<name>` object library
15+
# rather than in `<name>` itself, and only forward include directories to it -
16+
# not the usage requirements of libraries linked afterwards. Linking the flags
17+
# to `<name>` alone would therefore silently compile nothing with them, so this
18+
# always covers the `obj.<name>` twin as well.
19+
20+
include_guard(GLOBAL)
21+
22+
function(python_cpp_link_project_options)
23+
foreach(target ${ARGN})
24+
foreach(name ${target} obj.${target})
25+
if(NOT TARGET ${name})
26+
continue()
27+
endif()
28+
get_target_property(type ${name} TYPE)
29+
if(type STREQUAL "INTERFACE_LIBRARY")
30+
target_link_libraries(${name} INTERFACE project_options project_warnings)
31+
else()
32+
target_link_libraries(${name} PRIVATE project_options project_warnings)
33+
endif()
34+
endforeach()
35+
endforeach()
36+
endfunction()

‎integration/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_executable(integration-tests_ program.cpp ../src/testing/main.cpp)
2-
target_link_libraries(integration-tests_ PRIVATE python-cpp gtest gtest_main cxxopts project_options project_warnings tsl::ordered_map)
2+
target_link_libraries(integration-tests_ PRIVATE python-cpp gtest gtest_main cxxopts tsl::ordered_map)
3+
python_cpp_link_project_options(integration-tests_)
34
# gtest_discover_tests(integration-tests_)
45

56
add_test(

‎src/CMakeLists.txt‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ add_executable(unittests_ ${UNITTEST_SOURCES})
269269
target_link_libraries(python-cpp
270270
PUBLIC spdlog m
271271
PRIVATE
272-
project_options
273-
project_warnings
274272
ICU::uc
275273
ICU::data
276274
${GMPXX_LIBRARIES}
@@ -283,11 +281,7 @@ target_include_directories(python-cpp
283281
PRIVATE ${GMP_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}
284282
)
285283

286-
target_compile_options(
287-
python-cpp
288-
PRIVATE
289-
# -fno-omit-frame-pointer is needed, otherwise we cannot access the stack base pointer reliably
290-
-fno-omit-frame-pointer)
284+
python_cpp_link_project_options(python-cpp)
291285

292286
if(STL_SUPPORTS_BIT_CAST)
293287
target_compile_definitions(python-cpp PUBLIC "STL_SUPPORTS_BIT_CAST")
@@ -312,14 +306,6 @@ elseif(ENABLE_LLVM_BACKEND AND LLVM_FOUND)
312306
message(STATUS "Configuring LLVM backend")
313307
add_library(python-cpp-llvm ${LLVM_BACKEND_FILES})
314308

315-
target_compile_options(
316-
python-cpp-llvm
317-
PRIVATE -Wall
318-
-Wextra
319-
-Werror
320-
-Wno-unused-parameter
321-
-fno-omit-frame-pointer)
322-
323309
add_library(llvm-interface INTERFACE)
324310
target_include_directories(llvm-interface INTERFACE . )
325311
# include llvm include directories as system paths to silence compiler warnings
@@ -337,7 +323,8 @@ elseif(ENABLE_LLVM_BACKEND AND LLVM_FOUND)
337323
orcjit
338324
x86asmparser
339325
x86codegen)
340-
target_link_libraries(llvm-interface INTERFACE ${llvm_libs} project_options project_warnings)
326+
target_link_libraries(llvm-interface INTERFACE ${llvm_libs})
327+
python_cpp_link_project_options(llvm-interface)
341328
# TODO: not all versions of llvm are ready for C++20, figure out when to use this
342329
set_property(TARGET python-cpp-llvm PROPERTY CXX_STANDARD 17)
343330

@@ -355,12 +342,14 @@ elseif(ENABLE_LLVM_BACKEND AND LLVM_FOUND)
355342
target_link_libraries(unittests_ PRIVATE python-cpp-llvm)
356343
endif()
357344

358-
target_link_libraries(unittests_ PRIVATE python-cpp gtest gtest_main cxxopts project_options project_warnings tsl::ordered_map)
345+
target_link_libraries(unittests_ PRIVATE python-cpp gtest gtest_main cxxopts tsl::ordered_map)
359346
gtest_discover_tests(unittests_)
360347

361348
add_executable(python repl/repl.cpp)
362-
target_link_libraries(python PRIVATE linenoise cxxopts python-cpp project_options project_warnings stdc++)
349+
target_link_libraries(python PRIVATE linenoise cxxopts python-cpp stdc++)
363350

364351
add_executable(freeze utilities/freeze.cpp)
365-
target_link_libraries(freeze PRIVATE python-cpp cxxopts project_options project_warnings)
352+
target_link_libraries(freeze PRIVATE python-cpp cxxopts)
366353
target_include_directories(freeze SYSTEM PRIVATE ${MLIR_INCLUDE_DIRS})
354+
355+
python_cpp_link_project_options(unittests_ python freeze)

‎src/executable/bytecode/instructions/Instructions.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,20 @@
7979

8080
using namespace py;
8181

82+
// GCC 16 inlines the std::variant copy-assignment behind py::Value and then
83+
// attributes the std::vector destructor's operator delete to the stack slot
84+
// holding the variant. Nothing is freed here; the diagnostic is a false positive.
85+
#if defined(__GNUC__) && !defined(__clang__)
86+
#pragma GCC diagnostic push
87+
#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
88+
#endif
8289
Instruction::RAIIStoreNonCallInstructionData::RAIIStoreNonCallInstructionData()
8390
{
8491
reg0 = VirtualMachine::the().reg(0);
8592
}
93+
#if defined(__GNUC__) && !defined(__clang__)
94+
#pragma GCC diagnostic pop
95+
#endif
8696

8797
Instruction::RAIIStoreNonCallInstructionData::~RAIIStoreNonCallInstructionData()
8898
{

‎src/executable/mlir/CMakeLists.txt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ include(AddLLVM)
2727
set(PYTHON_MLIR_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src/executable/mlir)
2828
set(PYTHON_MLIR_BINARY_DIR ${PROJECT_BINARY_DIR}/src/executable/mlir)
2929

30+
# The LLVM/MLIR helpers used below (add_mlir_library and friends) compile their
31+
# sources in a separate `obj.<name>` object library and forward the target's
32+
# INCLUDE_DIRECTORIES to it as plain include paths, which drops the SYSTEM
33+
# marking. Marking the third-party headers as system directories for the whole
34+
# subtree survives that, and keeps our warning set (-Werror included) from
35+
# firing inside LLVM, MLIR and spdlog headers.
36+
#
37+
# ${PYTHON_MLIR_BINARY_DIR} holds nothing but TableGen output (Ops.h.inc,
38+
# Passes.h.inc, ...), which is machine-generated and not ours to clean up, so it
39+
# is treated the same way.
40+
include_directories(SYSTEM
41+
${LLVM_INCLUDE_DIRS}
42+
${MLIR_INCLUDE_DIRS}
43+
${spdlog_SOURCE_DIR}/include
44+
${PYTHON_MLIR_BINARY_DIR}
45+
${PYTHON_MLIR_BINARY_DIR}/Dialect)
46+
3047
add_subdirectory(Conversion)
3148
add_subdirectory(Dialect)
3249
add_subdirectory(Target)
@@ -35,3 +52,4 @@ add_subdirectory(test)
3552

3653
add_library(python-mlir compile.cpp)
3754
target_link_libraries(python-mlir PRIVATE PythonMLIRDialect TargetPythonBytecode PythonConversionPasses)
55+
python_cpp_link_project_options(python-mlir)

‎src/executable/mlir/Conversion/CMakeLists.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses
1515

1616
LINK_LIBS PUBLIC
1717
${PYTHON_CONVERSION_LIBS}
18-
)
18+
)
19+
20+
python_cpp_link_project_options(PythonConversionPasses)

‎src/executable/mlir/Conversion/PythonToPythonBytecode/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
2727
${PYTHON_MLIR_BINARY_DIR}
2828
)
2929

30-
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
30+
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
31+
python_cpp_link_project_options(PythonToPythonBytecode)

0 commit comments

Comments
 (0)