Skip to content

Commit d223c9a

Browse files
committed
Make HALIDE_CPP_COMPILER_HAS_FLOAT16 overrideable and numeric
Define HALIDE_CPP_COMPILER_HAS_FLOAT16 as 0 or 1 so callers can override detection and use it in #if guards consistently. Exclude __wasm__ from the Clang _Float16 detection path to avoid enabling _Float16 on wasm targets that advertise fp16 features but do not support the source-language type. Fixes #8986
1 parent e70074c commit d223c9a

7 files changed

Lines changed: 31 additions & 25 deletions

File tree

src/Expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ struct Expr : public Internal::IRHandle {
298298
Expr(bfloat16_t x)
299299
: IRHandle(Internal::FloatImm::make(BFloat(16), (double)x)) {
300300
}
301-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
301+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
302302
explicit Expr(_Float16 x)
303303
: IRHandle(Internal::FloatImm::make(Float(16), (double)x)) {
304304
}

src/Float16.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct float16_t {
4040
* positive zero.*/
4141
float16_t() = default;
4242

43-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
43+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
4444
/** Construct a float16_t from compiler's built-in _Float16 type. */
4545
explicit float16_t(_Float16 value) {
4646
memcpy(&data, &value, sizeof(_Float16));
@@ -57,7 +57,7 @@ struct float16_t {
5757
/** Cast to int */
5858
explicit operator int() const;
5959

60-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
60+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
6161
/** Cast to compiler's built-in _Float16 type. */
6262
explicit operator _Float16() const {
6363
_Float16 result;

src/Type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ HALIDE_DECLARE_EXTERN_SIMPLE_TYPE(Halide::float16_t);
166166
HALIDE_DECLARE_EXTERN_SIMPLE_TYPE(Halide::bfloat16_t);
167167
HALIDE_DECLARE_EXTERN_SIMPLE_TYPE(halide_task_t);
168168
HALIDE_DECLARE_EXTERN_SIMPLE_TYPE(halide_loop_task_t);
169-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
169+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
170170
HALIDE_DECLARE_EXTERN_SIMPLE_TYPE(_Float16);
171171
#endif
172172
HALIDE_DECLARE_EXTERN_SIMPLE_TYPE(float);

src/runtime/HalideRuntime.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,21 @@ extern "C" {
9898
#define HALIDE_RUNTIME_ASAN_DETECTED
9999
#endif
100100

101+
#if !defined(HALIDE_CPP_COMPILER_HAS_FLOAT16)
102+
#define HALIDE_CPP_COMPILER_HAS_FLOAT16 0
103+
101104
#if !defined(HALIDE_RUNTIME_ASAN_DETECTED)
102105

103106
// clang had _Float16 added as a reserved name in clang 8, but
104107
// doesn't actually support it on most platforms until clang 15.
105108
// Ideally there would be a better way to detect if the type
106109
// is supported, even in a compiler independent fashion, but
107110
// coming up with one has proven elusive.
108-
#if defined(__clang__) && (__clang_major__ >= 15) && !defined(__EMSCRIPTEN__) && !defined(__i386__)
111+
#if defined(__clang__) && (__clang_major__ >= 15) && !defined(__EMSCRIPTEN__) && !defined(__i386__) && !defined(__wasm__)
109112
#if defined(__is_identifier)
110113
#if !__is_identifier(_Float16)
111-
#define HALIDE_CPP_COMPILER_HAS_FLOAT16
114+
#undef HALIDE_CPP_COMPILER_HAS_FLOAT16
115+
#define HALIDE_CPP_COMPILER_HAS_FLOAT16 1
112116
#endif
113117
#endif
114118
#endif
@@ -118,11 +122,13 @@ extern "C" {
118122
// we assume support. This may need revision.
119123
#if defined(__GNUC__) && (__GNUC__ >= 12)
120124
#if defined(__x86_64__) || (defined(__i386__) && (__GNUC__ >= 14) && defined(__SSE2__)) || ((defined(__arm__) || defined(__aarch64__)) && (__GNUC__ >= 13))
121-
#define HALIDE_CPP_COMPILER_HAS_FLOAT16
125+
#undef HALIDE_CPP_COMPILER_HAS_FLOAT16
126+
#define HALIDE_CPP_COMPILER_HAS_FLOAT16 1
122127
#endif
123128
#endif
124129

125130
#endif // !HALIDE_RUNTIME_ASAN_DETECTED
131+
#endif // !defined(HALIDE_CPP_COMPILER_HAS_FLOAT16)
126132

127133
#endif // !COMPILING_HALIDE_RUNTIME
128134

@@ -2149,7 +2155,7 @@ HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of() {
21492155
return halide_type_t(halide_type_handle, 64);
21502156
}
21512157

2152-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
2158+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
21532159
template<>
21542160
HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of<_Float16>() {
21552161
return halide_type_t(halide_type_float, 16);

test/correctness/float16_t.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ int run_test() {
322322
std::abs(halfway_plus_eps - (double)to_even));
323323

324324
assert(float(halfway_plus_eps) == halfway);
325-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
325+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
326326
assert(_Float16(halfway_plus_eps) == _Float16(float(to_odd)));
327327
#endif
328328
assert(float16_t(halfway_plus_eps) == to_odd);
@@ -475,7 +475,7 @@ int main(int argc, char **argv) {
475475
}
476476

477477
printf("Testing _Float16...\n");
478-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
478+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
479479
if (run_test<_Float16>() != 0) {
480480
fprintf(stderr, "_Float16 test failed!\n");
481481
return 1;

test/correctness/image_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ int main(int argc, char **argv) {
278278
do_test<uint32_t>();
279279
do_test<uint64_t>();
280280
do_test<float>();
281-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
281+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
282282
do_test<_Float16>();
283283
#endif
284284
do_test<double>();

tools/halide_image_io.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ template<>
116116
inline bool convert(const int64_t &in) {
117117
return in != 0;
118118
}
119-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
119+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
120120
template<>
121121
inline bool convert(const _Float16 &in) {
122122
return (float)in != 0;
@@ -171,7 +171,7 @@ template<>
171171
inline uint8_t convert(const int64_t &in) {
172172
return convert<uint8_t, uint64_t>(in);
173173
}
174-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
174+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
175175
template<>
176176
inline uint8_t convert(const _Float16 &in) {
177177
return (uint8_t)std::lround((float)in * 255.0f);
@@ -223,7 +223,7 @@ template<>
223223
inline uint16_t convert(const int64_t &in) {
224224
return convert<uint16_t, uint64_t>(in);
225225
}
226-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
226+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
227227
template<>
228228
inline uint16_t convert(const _Float16 &in) {
229229
return (uint16_t)std::lround((float)in * 65535.0f);
@@ -275,7 +275,7 @@ template<>
275275
inline uint32_t convert(const int64_t &in) {
276276
return convert<uint32_t, uint64_t>(in);
277277
}
278-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
278+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
279279
template<>
280280
inline uint32_t convert(const _Float16 &in) {
281281
return (uint32_t)std::llround((float)in * 4294967295.0);
@@ -327,7 +327,7 @@ template<>
327327
inline uint64_t convert(const int64_t &in) {
328328
return convert<uint64_t, uint64_t>(in);
329329
}
330-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
330+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
331331
template<>
332332
inline uint64_t convert(const _Float16 &in) {
333333
return convert<uint64_t, uint32_t>((uint32_t)std::llround((float)in * 4294967295.0));
@@ -379,7 +379,7 @@ template<>
379379
inline int8_t convert(const int64_t &in) {
380380
return convert<uint8_t, int64_t>(in);
381381
}
382-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
382+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
383383
template<>
384384
inline int8_t convert(const _Float16 &in) {
385385
return convert<uint8_t, float>((float)in);
@@ -431,7 +431,7 @@ template<>
431431
inline int16_t convert(const int64_t &in) {
432432
return convert<uint16_t, int64_t>(in);
433433
}
434-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
434+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
435435
template<>
436436
inline int16_t convert(const _Float16 &in) {
437437
return convert<uint16_t, float>((float)in);
@@ -483,7 +483,7 @@ template<>
483483
inline int32_t convert(const int64_t &in) {
484484
return convert<uint32_t, int64_t>(in);
485485
}
486-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
486+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
487487
template<>
488488
inline int32_t convert(const _Float16 &in) {
489489
return convert<uint32_t, float>((float)in);
@@ -535,7 +535,7 @@ template<>
535535
inline int64_t convert(const int64_t &in) {
536536
return convert<uint64_t, int64_t>(in);
537537
}
538-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
538+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
539539
template<>
540540
inline int64_t convert(const _Float16 &in) {
541541
return convert<uint64_t, float>((float)in);
@@ -550,7 +550,7 @@ inline int64_t convert(const double &in) {
550550
return convert<uint64_t, double>(in);
551551
}
552552

553-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
553+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
554554
// Convert to f16
555555
template<>
556556
inline _Float16 convert(const bool &in) {
@@ -639,7 +639,7 @@ template<>
639639
inline float convert(const int64_t &in) {
640640
return convert<float, uint64_t>(in);
641641
}
642-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
642+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
643643
template<>
644644
inline float convert(const _Float16 &in) {
645645
return (float)in;
@@ -691,7 +691,7 @@ template<>
691691
inline double convert(const int64_t &in) {
692692
return convert<double, uint64_t>(in);
693693
}
694-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
694+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
695695
template<>
696696
inline double convert(const _Float16 &in) {
697697
return (double)in;
@@ -2496,7 +2496,7 @@ struct ImageTypeConversion {
24962496

24972497
const halide_type_t src_type = src.type();
24982498
switch (src_type.element_of().as_u32()) {
2499-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
2499+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
25002500
case halide_type_t(halide_type_float, 16).as_u32():
25012501
return convert_image<DstElemType>(src.template as<_Float16, AnyDims>());
25022502
#endif
@@ -2545,7 +2545,7 @@ struct ImageTypeConversion {
25452545
// Call the appropriate static-to-static conversion routine
25462546
// based on the desired dst type.
25472547
switch (dst_type.element_of().as_u32()) {
2548-
#ifdef HALIDE_CPP_COMPILER_HAS_FLOAT16
2548+
#if HALIDE_CPP_COMPILER_HAS_FLOAT16
25492549
case halide_type_t(halide_type_float, 16).as_u32():
25502550
return convert_image<_Float16>(src);
25512551
#endif

0 commit comments

Comments
 (0)