Halide icon indicating copy to clipboard operation
Halide copied to clipboard

Compilation error on aarch64-linux: '_Float16' was not declared in this scope; did you mean '_Float64'

Open twesterhout opened this issue 2 years ago • 6 comments

Halide 16.0.0 fails to compile on aarch64-linux on GCC 12.3.0:

In file included from /build/source/build/include/HalideBuffer.h:30,
                 from /build/source/src/autoschedulers/anderson2021/Weights.h:9,
                 from /build/source/src/autoschedulers/anderson2021/weightsdir_to_weightsfile.cpp:6:
/build/source/build/include/HalideRuntime.h:2040:61: error: '_Float16' was not declared in this scope; did you mean '_Float64'?
 2040 | HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of<_Float16>() {
      |                                                             ^~~~~~~~
      |                                                             _Float64
/build/source/build/include/HalideRuntime.h:2040:46: error: template-id 'halide_type_of<<expression error> >' for 'constexpr halide_type_t halide_type_of()' does not match any template declaration
 2040 | HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of<_Float16>() {
      |                                              ^~~~~~~~~~~~~~~~~~~~~~~~
/build/source/build/include/HalideRuntime.h:2029:46: note: candidate is: 'template<class T> constexpr halide_type_t halide_type_of()'
 2029 | HALIDE_ALWAYS_INLINE constexpr halide_type_t halide_type_of() {
      |                                              ^~~~~~~~~~~~~~

See https://logs.ofborg.org/?key=nixos/nixpkgs.250545&attempt_id=35266673-36bc-4371-b6c8-54603759a1c9 for the full log.

According to GCC docs, we should use __fp16 instead of _Float16 on ARM (source https://gcc.gnu.org/onlinedocs/gcc/Half-Precision.html).

Would it make sense to introduce something like halide_float16_t as an alias for _Float16 or __fp16 depending on the architecture?

twesterhout avatar Aug 21 '23 15:08 twesterhout

According to GCC docs, we should use __fp16 instead of _Float16 on ARM (source https://gcc.gnu.org/onlinedocs/gcc/Half-Precision.html).

This link also says: It is recommended that portable code use the _Float16 type defined by ISO/IEC TS 18661-3:2015. See Additional Floating Types.

HalideRuntime.h is intended as portable code.

steven-johnson avatar Aug 21 '23 17:08 steven-johnson

Ah good catch! But I think it's the case when compiling the file as C code, not as a C++ header:

ISO/IEC TS 18661-3:2015 defines C support for additional floating types _Floatn and _Floatnx, and GCC supports these type names; the set of types supported depends on the target architecture. These types are not supported when compiling C++.

twesterhout avatar Aug 21 '23 17:08 twesterhout

Ugh, what a nightmare -- we went from having no compiler support for float16 to confusing-and-too-many-options support. (Does Clang-for-Arm report similar errors?)

I'm also genuinely puzzled here -- surely you aren't the first person to compile this on gcc-on-arm...?

At any rate, I genuinely don't know the right answer here -- hopefully someone else with more direct experience using float16 in this manner will chime in here.

steven-johnson avatar Aug 21 '23 17:08 steven-johnson

aarch64-darwin builds fine: https://logs.ofborg.org/?key=nixos/nixpkgs.250545&attempt_id=84a68c07-fc82-44cf-a6ff-5c73a1e0b7e2 So I'm tempted to conclude that the issue is gcc-specific.

In the meantime, I'll see if I can disable float16 support on aarch64-linux when building Halide in Nixpkgs.

twesterhout avatar Aug 21 '23 17:08 twesterhout

I ran into this same problem just now on a Raspberry Pi 5 while trying to build either the current source or 17.0.2 to get Vulkan beta support. (It fails with or without TARGET_VULKAN=ON.) I'm using GCC version 12 and LLVM version 16.

Any more thoughts on how to get this to work? Anyone gotten this compiling on Raspberry Pi?

Here are the build instructions I made for myself:

To use Halide on a Raspberry Pi:

sudo apt install clang-tools-16 clang-16 libclang-16-dev lld-16 liblld-16-dev
sudo ln -s /usr/bin/clang-16 /usr/bin/clang
sudo ln -s /usr/bin/llvm-config-16 /usr/bin/llvm-config

Building

Building Halide on Rasperry Pi:

mkdir -p ~/src/
cd ~/src
git clone https://github.com/halide/Halide.git
cd Halide
git checkout v17.0.2
mkdir -p ~/build/halide
cd ~/build/halide
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DTARGET_VULKAN=ON -DTARGET_WEBASSEMBLY=OFF -DLLVM_DIR=/usr/lib/llvm-16/lib/cmake/llvm -S ~/src/Halide -B build
cmake --build build

russell-taylor avatar Jul 16 '24 17:07 russell-taylor

When I added a link for clang++ and then used CC=/usr/bin/clang CXX=/usr/bin/clang++ on the cmake configuration line so that it built using clang rather than GCC, it got past this error. So that's a workaround for me.

russell-taylor avatar Jul 16 '24 18:07 russell-taylor