Skip to content

Commit 19b3ff5

Browse files
committed
build: Enable compiler warnings for all targets
1 parent c5a5c9f commit 19b3ff5

24 files changed

Lines changed: 543 additions & 419 deletions

‎CMakeLists.txt‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/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)

‎src/executable/mlir/Conversion/PythonToPythonBytecode/CollectionPatterns.cpp‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@ namespace {
3636
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
3737
if (to_expand) {
3838
if (!result.has_value()) {
39-
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
40-
op.getLoc(), op.getOutput().getType(), keys, values);
39+
result = mlir::emitpybytecode::BuildDict::create(
40+
rewriter, op.getLoc(), op.getOutput().getType(), keys, values);
4141
keys.clear();
4242
values.clear();
4343
}
44-
rewriter.create<mlir::emitpybytecode::DictUpdate>(
45-
op.getLoc(), *result, value);
44+
mlir::emitpybytecode::DictUpdate::create(
45+
rewriter, op.getLoc(), *result, value);
4646
} else {
4747
if (!result.has_value()) {
4848
keys.push_back(key);
4949
values.push_back(value);
5050
} else {
5151
ASSERT(keys.empty());
5252
ASSERT(values.empty());
53-
rewriter.create<mlir::emitpybytecode::DictAdd>(
54-
op.getLoc(), *result, key, value);
53+
mlir::emitpybytecode::DictAdd::create(
54+
rewriter, op.getLoc(), *result, key, value);
5555
}
5656
}
5757
}
@@ -89,12 +89,12 @@ namespace {
8989
llvm::ArrayRef<bool> requires_expansion)
9090
{
9191
auto list =
92-
rewriter.create<mlir::emitpybytecode::BuildList>(loc, list_type, mlir::ValueRange{});
92+
mlir::emitpybytecode::BuildList::create(rewriter, loc, list_type, mlir::ValueRange{});
9393
for (auto [el, expand] : llvm::zip(elements, requires_expansion)) {
9494
if (expand) {
95-
rewriter.create<mlir::emitpybytecode::ListExtend>(loc, list, el);
95+
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
9696
} else {
97-
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
97+
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
9898
}
9999
}
100100
return list;
@@ -181,23 +181,23 @@ namespace {
181181
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
182182
if (expand) {
183183
if (!set.has_value()) {
184-
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
185-
op->getLoc(), op.getOutput().getType(), elements);
184+
set = mlir::emitpybytecode::BuildSet::create(
185+
rewriter, op->getLoc(), op.getOutput().getType(), elements);
186186
} else {
187187
for (auto el : elements) {
188-
rewriter.create<mlir::emitpybytecode::SetAdd>(
189-
op.getLoc(), *set, el);
188+
mlir::emitpybytecode::SetAdd::create(
189+
rewriter, op.getLoc(), *set, el);
190190
}
191191
}
192192
elements.clear();
193-
rewriter.create<mlir::emitpybytecode::SetUpdate>(op.getLoc(), *set, el);
193+
mlir::emitpybytecode::SetUpdate::create(rewriter, op.getLoc(), *set, el);
194194
} else {
195195
elements.push_back(el);
196196
}
197197
}
198198
ASSERT(set.has_value());
199199
for (auto el : elements) {
200-
rewriter.create<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
200+
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
201201
}
202202
rewriter.replaceOp(op, *set);
203203
} else {

‎src/executable/mlir/Conversion/PythonToPythonBytecode/ControlFlowPatterns.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ namespace {
9797
auto insertion_point = rewriter.getInsertionPoint();
9898
auto *return_block = rewriter.createBlock(&op.getRegion());
9999
auto value =
100-
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
101-
rewriter.create<mlir::func::ReturnOp>(op.getLoc(), mlir::ValueRange{ value });
100+
mlir::py::ConstantOp::create(rewriter, op.getLoc(), rewriter.getNoneType());
101+
mlir::func::ReturnOp::create(rewriter, op.getLoc(), mlir::ValueRange{ value });
102102
rewriter.setInsertionPoint(insertion_point->getBlock(), insertion_point);
103103
return return_block;
104104
})
@@ -136,9 +136,10 @@ namespace {
136136
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
137137
mlir::PatternRewriter &rewriter) const final
138138
{
139-
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
140-
op.getLoc(), op.getIterable().getType(), op.getIterable());
141-
auto value = rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
139+
auto iterator = mlir::emitpybytecode::YieldFromIter::create(
140+
rewriter, op.getLoc(), op.getIterable().getType(), op.getIterable());
141+
auto value =
142+
mlir::py::ConstantOp::create(rewriter, op.getLoc(), rewriter.getNoneType());
142143

143144
rewriter.replaceOpWithNewOp<mlir::emitpybytecode::YieldFrom>(
144145
op, iterator.getType(), iterator, value);

0 commit comments

Comments
 (0)