haskell / c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
http://hackage.haskell.org/package/c2hs
Other
199 stars 50 forks source link

The symbol `v' does not fit here #215

Open newhoggy opened 5 years ago

newhoggy commented 5 years ago
c2hs: C header contains errors:

cbits/../cbits/simd.h:6: (column 29) [ERROR]  >>> Syntax error !
  The symbol `v' does not fit here.

Only reproducible in Linux CI for me. It builds locally on MacOS.

https://circleci.com/gh/haskell-works/hw-json-simd/152

https://github.com/haskell-works/hw-json-simd/pull/4

newhoggy commented 5 years ago

Can be reproduced like this:

$ docker run -ti quay.io/haskell_works/stack-build-cabal:18.04_2018-09-07
root@350d7d409e47:/# cd
root@350d7d409e47:~# git clone https://github.com/haskell-works/hw-json-simd.git
Cloning into 'hw-json-simd'...
remote: Enumerating objects: 181, done.
remote: Counting objects: 100% (181/181), done.
remote: Compressing objects: 100% (100/100), done.
remote: Total 307 (delta 49), reused 151 (delta 24), pack-reused 126
Receiving objects: 100% (307/307), 50.45 KiB | 263.00 KiB/s, done.
Resolving deltas: 100% (82/82), done.
root@350d7d409e47:~# cd hw-json-simd/
root@350d7d409e47:~/hw-json-simd# git checkout sliced-preparsing-2
Branch 'sliced-preparsing-2' set up to track remote branch 'sliced-preparsing-2' from 'origin'.
Switched to a new branch 'sliced-preparsing-2'
root@350d7d409e47:~/hw-json-simd# ./project.sh build
bits-extra-0.0.1.3: configure
bits-extra-0.0.1.3: build
bits-extra-0.0.1.3: copy/register
Building all executables for `hw-json-simd' once. After a successful build of all of them, only specified executables will be rebuilt.
hw-json-simd-0.1.0.0: configure (lib + exe + test)
Configuring hw-json-simd-0.1.0.0...
hw-json-simd-0.1.0.0: build (lib + exe + test)
Preprocessing library for hw-json-simd-0.1.0.0..
c2hs: C header contains errors:

cbits/../cbits/simd.h:6: (column 29) [ERROR]  >>> Syntax error !
  The symbol `v' does not fit here.

Progress 2/3

--  While building custom Setup.hs for package hw-json-simd-0.1.0.0 using:
      /root/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_2.2.0.1_ghc-8.4.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-2.2.0.1 build lib:hw-json-simd exe:hw-json-simd-exe test:hw-json-simd-test --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1
newhoggy commented 5 years ago

For reference the failing build is on the commit 1d21c17d27c9031b32e006ed53aa59d688230901

newhoggy commented 5 years ago

After applying the following patch, the code builds:

diff --git a/cbits/simd-spliced.c b/cbits/simd-spliced.c
index efc8958..5447c16 100644
--- a/cbits/simd-spliced.c
+++ b/cbits/simd-spliced.c
@@ -170,7 +170,7 @@ size_t write_bp_chunk(
     uint64_t w64_a  = w64_result_a[i];
     uint64_t w64_z  = w64_result_z[i];

-    size_t pc_ib = _popcnt64(w64_ib);
+    size_t pc_ib = __builtin_popcountll(w64_ib);

     uint64_t ext_d = _pext_u64(~(w64_a | w64_z) , w64_ib);
     uint64_t ext_a = _pext_u64(w64_a            , w64_ib);
@@ -409,7 +409,7 @@ uint64_t process_chunk(
     w64_result_a[i]   = w64_a;
     w64_result_z[i]   = w64_z;

-    size_t pc = _popcnt64(w64_bits_of_q[i]);
+    size_t pc = __builtin_popcountll(w64_bits_of_q[i]);
     *quote_odds_carry  += pc;
     *quote_evens_carry += pc;
   }

Any chance we could have a better error message?

deech commented 5 years ago

Sorry this took me a while to get to this. Last week was Thanksgiving and today was the first time I was able to work on the this issue.

In any case I've was able to get c2hs generate bindings correctly in your Docker container, not by using your patch, but by adding stdint.h to simd.h since this is where uint64_t is defined. I do run into other compilatiton errors but they don't seem c2hs related and probably do require the patch to popcnt64.

root@832cc42e0351:~/hw-json-simd# ./project.sh build
bits-extra-0.0.1.3: configure
bits-extra-0.0.1.3: build
bits-extra-0.0.1.3: copy/register
Building all executables for `hw-json-simd' once. After a successful build of all of them, only specified executables will be rebuilt.
hw-json-simd-0.1.0.0: configure (lib + exe + test)
Configuring hw-json-simd-0.1.0.0...
hw-json-simd-0.1.0.0: build (lib + exe + test)
Preprocessing library for hw-json-simd-0.1.0.0..
Building library for hw-json-simd-0.1.0.0..
[1 of 6] Compiling HaskellWorks.Data.Json.Simd.Internal.Foreign ( .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/HaskellWorks/Data/Json/Simd/Internal/Foreign.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/HaskellWorks/Data/Json/Simd/Internal/Foreign.o )
[2 of 6] Compiling HaskellWorks.Data.Json.Simd.Internal.Fsm.Core ( src/HaskellWorks/Data/Json/Simd/Internal/Fsm/Core.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/HaskellWorks/Data/Json/Simd/Internal/Fsm/Core.o )
[3 of 6] Compiling HaskellWorks.Data.Json.Simd.Internal.Fsm ( src/HaskellWorks/Data/Json/Simd/Internal/Fsm.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/HaskellWorks/Data/Json/Simd/Internal/Fsm.o )
[4 of 6] Compiling HaskellWorks.Data.Json.Simd.Internal.Index.Simple ( src/HaskellWorks/Data/Json/Simd/Internal/Index/Simple.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/HaskellWorks/Data/Json/Simd/Internal/Index/Simple.o )
[5 of 6] Compiling HaskellWorks.Data.Json.Simd.Index.Simple ( src/HaskellWorks/Data/Json/Simd/Index/Simple.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/HaskellWorks/Data/Json/Simd/Index/Simple.o )
[6 of 6] Compiling Paths_hw_json_simd ( .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/autogen/Paths_hw_json_simd.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/Paths_hw_json_simd.o )
cbits/simd-spliced.c: In function ‘write_bp_chunk’:

/root/hw-json-simd/cbits/simd-spliced.c:173:20: error:
     warning: implicit declaration of function ‘_popcnt64’; did you mean ‘__lzcnt64’? [-Wimplicit-function-declaration]
         size_t pc_ib = _popcnt64(w64_ib);
                        ^~~~~~~~~ 
                        __lzcnt64 
    |                             
173 |     size_t pc_ib = _popcnt64(w64_ib);
    |                    ^        
cbits/simd-spliced.c: In function ‘write_bp_chunk’:

/root/hw-json-simd/cbits/simd-spliced.c:173:20: error:
     warning: implicit declaration of function ‘_popcnt64’; did you mean ‘__lzcnt64’? [-Wimplicit-function-declaration]
         size_t pc_ib = _popcnt64(w64_ib);
                        ^~~~~~~~~ 
                        __lzcnt64 
    |                             
173 |     size_t pc_ib = _popcnt64(w64_ib);
    |                    ^        
ignoring (possibly broken) abi-depends field for packages
Preprocessing test suite 'hw-json-simd-test' for hw-json-simd-0.1.0.0..
Building test suite 'hw-json-simd-test' for hw-json-simd-0.1.0.0..
[1 of 2] Compiling Main             ( test/Spec.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-test/hw-json-simd-test-tmp/Main.o )
[2 of 2] Compiling Paths_hw_json_simd ( .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-test/autogen/Paths_hw_json_simd.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-test/hw-json-simd-test-tmp/Paths_hw_json_simd.o )
Linking .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-test/hw-json-simd-test ...
Preprocessing executable 'hw-json-simd-exe' for hw-json-simd-0.1.0.0..
Building executable 'hw-json-simd-exe' for hw-json-simd-0.1.0.0..
[1 of 6] Compiling App.Commands.Types ( app/App/Commands/Types.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-exe/hw-json-simd-exe-tmp/App/Commands/Types.o )
[2 of 6] Compiling App.Lens         ( app/App/Lens.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-exe/hw-json-simd-exe-tmp/App/Lens.o )
[3 of 6] Compiling App.Commands.CreateIndex ( app/App/Commands/CreateIndex.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-exe/hw-json-simd-exe-tmp/App/Commands/CreateIndex.o )
[4 of 6] Compiling App.Commands     ( app/App/Commands.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-exe/hw-json-simd-exe-tmp/App/Commands.o )
[5 of 6] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-exe/hw-json-simd-exe-tmp/Main.o )
[6 of 6] Compiling Paths_hw_json_simd ( .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-exe/autogen/Paths_hw_json_simd.hs, .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-exe/hw-json-simd-exe-tmp/Paths_hw_json_simd.o )
Linking .stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/hw-json-simd-exe/hw-json-simd-exe ...
/root/hw-json-simd/.stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/libHShw-json-simd-0.1.0.0-Blt5pcFOlZs6J9vzS9b6U8.a(simd-spliced.o):simd-spliced.c:function write_bp_chunk: error: undefined reference to '_popcnt64'
/root/hw-json-simd/.stack-work/dist/x86_64-linux/Cabal-2.2.0.1/build/libHShw-json-simd-0.1.0.0-Blt5pcFOlZs6J9vzS9b6U8.a(simd-spliced.o):simd-spliced.c:function process_chunk: error: undefined reference to '_popcnt64'
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)
Progress 2/3                      

--  While building custom Setup.hs for package hw-json-simd-0.1.0.0 using:
      /root/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_2.2.0.1_ghc-8.4.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-2.2.0.1 build lib:hw-json-simd exe:hw-json-simd-exe test:hw-json-simd-test --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

All I did was add stdint.h to simd.h:

#include <immintrin.h>
#include <mmintrin.h>
#include <stdint.h> // <-- added
...