Skip to content

Commit 11f6bb7

Browse files
authored
Enable -Werror in CMake (CI only) (#9037)
* Enable -Werror in CMake (CI only) * Fix unused variable warning for has_scalable_vector in Target.cpp * Suppress unsafe CRT function warnings in halide_image_io.h * Fix loss of precision in double-to-float conversion * Suppress unsafe CRT function warnings in test_sharding.h * Set _CRT_SECURE_NO_WARNINGS for Halide::Test sources * Fix float literal suffix. * MSVC: cast M_PI to float (C4305) * Normalize line endings in templates and string literals On Windows, raw string literals and binary2cpp-generated arrays can contain \r\n line endings (from git checkout with core.autocrlf). Normalize to \n so that generated source files have consistent, platform-independent line endings.
1 parent 09176ad commit 11f6bb7

10 files changed

Lines changed: 98 additions & 43 deletions

File tree

CMakePresets.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"toolchainFile": "$env{VCPKG_INSTALLATION_ROOT}/scripts/buildsystems/vcpkg.cmake",
2222
"cacheVariables": {
2323
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
24+
"CMAKE_COMPILE_WARNING_AS_ERROR": "ON",
2425
"FETCHCONTENT_FULLY_DISCONNECTED": "ON",
2526
"VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "${sourceDir}/cmake/toolchain.${presetName}.cmake",
2627
"VCPKG_MANIFEST_FEATURES": "developer",

cmake/HalideTestHelpers.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ if (NOT TARGET Halide::Test)
2121
# Make internal_assert, debug, etc. available to tests
2222
target_compile_definitions(Halide_test INTERFACE HALIDE_KEEP_MACROS)
2323

24+
# Disable warnings about standard C functions that have more secure replacements
25+
# in the Windows API.
26+
target_compile_definitions(Halide_test INTERFACE
27+
$<$<CXX_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>)
28+
2429
# Everyone gets to see the common headers
2530
target_include_directories(Halide_test
2631
INTERFACE

src/CodeGen_C.cpp

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,29 @@ extern "C" unsigned char halide_c_template_CodeGen_C_vectors[];
3939

4040
namespace {
4141

42+
// On Windows, raw string literals and binary2cpp-generated arrays can
43+
// contain \r\n line endings (from git checkout with core.autocrlf).
44+
// Normalize to \n so that generated source files have consistent,
45+
// platform-independent line endings.
46+
string normalize_line_endings(const char *s) {
47+
string result;
48+
result.reserve(strlen(s));
49+
for (; *s; ++s) {
50+
if (*s != '\r') {
51+
result += *s;
52+
}
53+
}
54+
return result;
55+
}
56+
57+
string normalize_line_endings(const unsigned char *s) {
58+
return normalize_line_endings(reinterpret_cast<const char *>(s));
59+
}
60+
4261
// HALIDE_MUST_USE_RESULT defined here is intended to exactly
4362
// duplicate the definition in HalideRuntime.h (so that either or
4463
// both can be present, in any order).
45-
const char *const kDefineMustUseResult = R"INLINE_CODE(#ifndef HALIDE_MUST_USE_RESULT
64+
const string kDefineMustUseResult = normalize_line_endings(R"INLINE_CODE(#ifndef HALIDE_MUST_USE_RESULT
4665
#ifdef __has_attribute
4766
#if __has_attribute(nodiscard)
4867
#define HALIDE_MUST_USE_RESULT [[nodiscard]]
@@ -55,9 +74,9 @@ const char *const kDefineMustUseResult = R"INLINE_CODE(#ifndef HALIDE_MUST_USE_R
5574
#define HALIDE_MUST_USE_RESULT
5675
#endif
5776
#endif
58-
)INLINE_CODE";
77+
)INLINE_CODE");
5978

60-
const char *const constexpr_argument_info_docs = R"INLINE_CODE(
79+
const string constexpr_argument_info_docs = normalize_line_endings(R"INLINE_CODE(
6180
/**
6281
* This function returns a constexpr array of information about a Halide-generated
6382
* function's argument signature (e.g., number of arguments, type of each, etc).
@@ -88,7 +107,7 @@ const char *const constexpr_argument_info_docs = R"INLINE_CODE(
88107
* impact aside from the numerical value of the constant.
89108
*/
90109
91-
)INLINE_CODE";
110+
)INLINE_CODE");
92111

93112
class TypeInfoGatherer : public IRGraphVisitor {
94113
private:
@@ -194,7 +213,7 @@ CodeGen_C::CodeGen_C(ostream &s, const Target &t, OutputKind output_kind, const
194213
// If it's a header, emit an include guard.
195214
stream << "#ifndef HALIDE_FUNCTION_INFO_" << c_print_name(guard) << "\n"
196215
<< "#define HALIDE_FUNCTION_INFO_" << c_print_name(guard) << "\n";
197-
stream << R"INLINE_CODE(
216+
stream << normalize_line_endings(R"INLINE_CODE(
198217
/* MACHINE GENERATED By Halide. */
199218
200219
#if !(__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
@@ -203,7 +222,7 @@ CodeGen_C::CodeGen_C(ostream &s, const Target &t, OutputKind output_kind, const
203222
204223
#include "HalideRuntime.h"
205224
206-
)INLINE_CODE";
225+
)INLINE_CODE");
207226

208227
return;
209228
}
@@ -247,9 +266,9 @@ CodeGen_C::CodeGen_C(ostream &s, const Target &t, OutputKind output_kind, const
247266
} else {
248267
// Include declarations of everything generated C source might want
249268
stream
250-
<< halide_c_template_CodeGen_C_prologue << "\n"
251-
<< halide_internal_runtime_header_HalideRuntime_h << "\n"
252-
<< halide_internal_initmod_inlined_c << "\n";
269+
<< normalize_line_endings(halide_c_template_CodeGen_C_prologue) << "\n"
270+
<< normalize_line_endings(halide_internal_runtime_header_HalideRuntime_h) << "\n"
271+
<< normalize_line_endings(halide_internal_initmod_inlined_c) << "\n";
253272
stream << "\n";
254273
}
255274

@@ -294,24 +313,24 @@ CodeGen_C::~CodeGen_C() {
294313
<< "// use the -r flag with any Halide generator binary, e.g.:\n"
295314
<< "// $ ./my_generator -r halide_runtime -o . target=host\n"
296315
<< "\n"
297-
<< halide_internal_runtime_header_HalideRuntime_h << "\n";
316+
<< normalize_line_endings(halide_internal_runtime_header_HalideRuntime_h) << "\n";
298317
if (target.has_feature(Target::CUDA)) {
299-
stream << halide_internal_runtime_header_HalideRuntimeCuda_h << "\n";
318+
stream << normalize_line_endings(halide_internal_runtime_header_HalideRuntimeCuda_h) << "\n";
300319
}
301320
if (target.has_feature(Target::HVX)) {
302-
stream << halide_internal_runtime_header_HalideRuntimeHexagonHost_h << "\n";
321+
stream << normalize_line_endings(halide_internal_runtime_header_HalideRuntimeHexagonHost_h) << "\n";
303322
}
304323
if (target.has_feature(Target::Metal)) {
305-
stream << halide_internal_runtime_header_HalideRuntimeMetal_h << "\n";
324+
stream << normalize_line_endings(halide_internal_runtime_header_HalideRuntimeMetal_h) << "\n";
306325
}
307326
if (target.has_feature(Target::OpenCL)) {
308-
stream << halide_internal_runtime_header_HalideRuntimeOpenCL_h << "\n";
327+
stream << normalize_line_endings(halide_internal_runtime_header_HalideRuntimeOpenCL_h) << "\n";
309328
}
310329
if (target.has_feature(Target::D3D12Compute)) {
311-
stream << halide_internal_runtime_header_HalideRuntimeD3D12Compute_h << "\n";
330+
stream << normalize_line_endings(halide_internal_runtime_header_HalideRuntimeD3D12Compute_h) << "\n";
312331
}
313332
if (target.has_feature(Target::WebGPU)) {
314-
stream << halide_internal_runtime_header_HalideRuntimeWebGPU_h << "\n";
333+
stream << normalize_line_endings(halide_internal_runtime_header_HalideRuntimeWebGPU_h) << "\n";
315334
}
316335
}
317336
stream << "#endif\n";
@@ -324,13 +343,7 @@ void CodeGen_C::add_platform_prologue() {
324343

325344
void CodeGen_C::add_vector_typedefs(const std::set<Type> &vector_types) {
326345
if (!vector_types.empty()) {
327-
// Voodoo fix: on at least one config (our arm32 buildbot running gcc 5.4),
328-
// emitting this long text string was regularly garbled in a predictable pattern;
329-
// flushing the stream before or after heals it. Since C++ codegen is rarely
330-
// on a compilation critical path, we'll just band-aid it in this way.
331-
stream << std::flush;
332-
stream << halide_c_template_CodeGen_C_vectors;
333-
stream << std::flush;
346+
stream << normalize_line_endings(halide_c_template_CodeGen_C_vectors);
334347

335348
for (const auto &t : vector_types) {
336349
string name = print_type(t, DoNotAppendSpace);
@@ -354,20 +367,20 @@ void CodeGen_C::add_vector_typedefs(const std::set<Type> &vector_types) {
354367

355368
void CodeGen_C::set_name_mangling_mode(NameMangling mode) {
356369
if (extern_c_open && mode != NameMangling::C) {
357-
stream << R"INLINE_CODE(
370+
stream << normalize_line_endings(R"INLINE_CODE(
358371
#ifdef __cplusplus
359372
} // extern "C"
360373
#endif
361374
362-
)INLINE_CODE";
375+
)INLINE_CODE");
363376
extern_c_open = false;
364377
} else if (!extern_c_open && mode == NameMangling::C) {
365-
stream << R"INLINE_CODE(
378+
stream << normalize_line_endings(R"INLINE_CODE(
366379
#ifdef __cplusplus
367380
extern "C" {
368381
#endif
369382
370-
)INLINE_CODE";
383+
)INLINE_CODE");
371384
extern_c_open = true;
372385
}
373386
}
@@ -2567,10 +2580,10 @@ void CodeGen_C::test() {
25672580
}
25682581

25692582
string correct_source =
2570-
string((const char *)halide_c_template_CodeGen_C_prologue) + '\n' +
2571-
string((const char *)halide_internal_runtime_header_HalideRuntime_h) + '\n' +
2572-
string((const char *)halide_internal_initmod_inlined_c) + '\n' +
2573-
'\n' + kDefineMustUseResult + R"GOLDEN_CODE(
2583+
normalize_line_endings(halide_c_template_CodeGen_C_prologue) + '\n' +
2584+
normalize_line_endings(halide_internal_runtime_header_HalideRuntime_h) + '\n' +
2585+
normalize_line_endings(halide_internal_initmod_inlined_c) + '\n' +
2586+
'\n' + kDefineMustUseResult + normalize_line_endings(R"GOLDEN_CODE(
25742587
#ifndef HALIDE_FUNCTION_ATTRS
25752588
#define HALIDE_FUNCTION_ATTRS
25762589
#endif
@@ -2638,7 +2651,7 @@ int test1(struct halide_buffer_t *_buf_buffer, float _alpha, int32_t _beta, void
26382651
} // extern "C"
26392652
#endif
26402653
2641-
)GOLDEN_CODE";
2654+
)GOLDEN_CODE");
26422655

26432656
const auto compare_srcs = [](const string &actual, const string &expected) {
26442657
if (actual != expected) {
@@ -2673,7 +2686,7 @@ int test1(struct halide_buffer_t *_buf_buffer, float _alpha, int32_t _beta, void
26732686
cg.compile(m);
26742687
}
26752688

2676-
string correct_function_info = R"GOLDEN_CODE(#ifndef HALIDE_FUNCTION_INFO__Function___Info___Test
2689+
string correct_function_info = normalize_line_endings(R"GOLDEN_CODE(#ifndef HALIDE_FUNCTION_INFO__Function___Info___Test
26772690
#define HALIDE_FUNCTION_INFO__Function___Info___Test
26782691
26792692
/* MACHINE GENERATED By Halide. */
@@ -2724,7 +2737,7 @@ inline constexpr std::array<::HalideFunctionInfo::ArgumentInfo, 4> test1_argumen
27242737
}};
27252738
}
27262739
#endif
2727-
)GOLDEN_CODE";
2740+
)GOLDEN_CODE");
27282741

27292742
compare_srcs(function_info.str(), correct_function_info);
27302743

src/PythonExtensionGen.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ using std::string;
1515

1616
namespace {
1717

18+
// See normalize_line_endings in CodeGen_C.cpp for rationale.
19+
string normalize_line_endings(const char *s) {
20+
string result;
21+
result.reserve(strlen(s));
22+
for (; *s; ++s) {
23+
if (*s != '\r') {
24+
result += *s;
25+
}
26+
}
27+
return result;
28+
}
29+
1830
string sanitize_name(const string &name) {
1931
ostringstream oss;
2032
for (char c : name) {
@@ -95,7 +107,7 @@ std::pair<string, string> print_type(const LoweredArgument *arg) {
95107
}
96108
}
97109

98-
const char kModuleRegistrationCode[] = R"INLINE_CODE(
110+
const string kModuleRegistrationCode = normalize_line_endings(R"INLINE_CODE(
99111
static_assert(PY_MAJOR_VERSION >= 3, "Python bindings for Halide require Python 3+");
100112
101113
namespace Halide::PythonExtensions {
@@ -271,7 +283,7 @@ HALIDE_EXPORT_SYMBOL PyObject *_HALIDE_EXPAND_AND_CONCAT(PyInit_, HALIDE_PYTHON_
271283
}
272284
273285
} // extern "C"
274-
)INLINE_CODE";
286+
)INLINE_CODE");
275287

276288
} // namespace
277289

@@ -300,7 +312,7 @@ void PythonExtensionGen::compile(const Module &module) {
300312
extern_decl_gen.compile(module);
301313
}
302314

303-
dest << R"INLINE_CODE(
315+
dest << normalize_line_endings(R"INLINE_CODE(
304316
namespace Halide::PythonRuntime {
305317
extern bool unpack_buffer(PyObject *py_obj,
306318
int py_getbuffer_flags,
@@ -389,7 +401,7 @@ struct PyHalideBuffer {
389401
390402
} // namespace
391403
392-
)INLINE_CODE";
404+
)INLINE_CODE");
393405

394406
for (const auto &f : module.functions()) {
395407
if (f.linkage == LinkageType::ExternalPlusMetadata) {

src/Target.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ Target calculate_host_target() {
257257
#else
258258
#if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
259259
Target::Arch arch = Target::ARM;
260+
#if !defined(__arm__)
260261
bool has_scalable_vector = false;
262+
#endif
261263

262264
#ifdef __APPLE__
263265
if (is_armv7s()) {
@@ -293,7 +295,9 @@ Target calculate_host_target() {
293295

294296
if (hwcaps2 & HWCAP2_SVE2) {
295297
initial_features.push_back(Target::SVE2);
298+
#if !defined(__arm__)
296299
has_scalable_vector = true;
300+
#endif
297301
}
298302
#endif
299303

@@ -322,12 +326,14 @@ Target calculate_host_target() {
322326

323327
if (IsProcessorFeaturePresent(PF_ARM_SVE2_INSTRUCTIONS_AVAILABLE)) {
324328
initial_features.push_back(Target::SVE2);
329+
#if !defined(__arm__)
325330
has_scalable_vector = true;
331+
#endif
326332
}
327333

328334
#endif
329335

330-
#if defined(__aarch64__)
336+
#if !defined(__arm__)
331337
if (has_scalable_vector) {
332338
vector_bits = get_sve_vector_length();
333339
}

test/autoschedulers/anderson2021/test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void expect_eq(int line, const A &expected, const B &actual) {
2121
}
2222

2323
template<typename A, typename B>
24-
void approx_eq(int line, const A &expected, const B &actual, float epsilon) {
24+
void approx_eq(int line, const A &expected, const B &actual, double epsilon) {
2525
user_assert(std::abs(expected - actual) < epsilon)
2626
<< "Assert failed on line " << line << "."
2727
<< "\nExpected value = " << expected

test/common/test_sharding.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#ifndef TEST_SHARDING_H
22
#define TEST_SHARDING_H
33

4+
#ifdef _MSC_VER
5+
#pragma warning(push)
6+
#pragma warning(disable : 4996)
7+
#endif
8+
49
// This file may be used by AOT tests, so it deliberately does not
510
// include Halide.h
611

@@ -86,4 +91,8 @@ class Sharder {
8691
} // namespace Internal
8792
} // namespace Halide
8893

94+
#ifdef _MSC_VER
95+
#pragma warning(pop)
96+
#endif
97+
8998
#endif // TEST_SHARDING_H

test/correctness/fuzz_schedule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ int main(int argc, char **argv) {
230230
// https://github.com/halide/Halide/issues/8054
231231
{
232232
ImageParam input(Float(32), 2, "input");
233-
const float r_sigma = 0.1;
233+
const float r_sigma = 0.1f;
234234
const int s_sigma = 8;
235235
Func bilateral_grid{"bilateral_grid"};
236236

test/correctness/rfactor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ enum class InlineReductionVariant {
839839
template<InlineReductionVariant variant>
840840
int inline_reductions_test() {
841841
using namespace ConciseCasts;
842-
constexpr float pi = M_PI;
842+
constexpr float pi = static_cast<float>(M_PI);
843843

844844
Func f{"f"};
845845
Var x("x");
@@ -897,7 +897,7 @@ enum class ArgMaxTupleOrder {
897897
template<ArgMaxVariant variant, ArgMaxTupleOrder order>
898898
int argmax_rfactor_test() {
899899
using namespace ConciseCasts;
900-
constexpr float pi = M_PI;
900+
constexpr float pi = static_cast<float>(M_PI);
901901

902902
Func f{"f"};
903903
Var x("x");

tools/halide_image_io.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#ifndef HALIDE_IMAGE_IO_H
55
#define HALIDE_IMAGE_IO_H
66

7+
#ifdef _MSC_VER
8+
#pragma warning(push)
9+
#pragma warning(disable : 4996) // disable unsafe CRT function warnings
10+
#endif
11+
712
#include <algorithm>
813
#include <cctype>
914
#include <cmath>
@@ -2791,4 +2796,8 @@ void convert_and_save_image(ImageType &im, const std::string &filename) {
27912796
} // namespace Tools
27922797
} // namespace Halide
27932798

2799+
#ifdef _MSC_VER
2800+
#pragma warning(pop)
2801+
#endif
2802+
27942803
#endif // HALIDE_IMAGE_IO_H

0 commit comments

Comments
 (0)