Running Accelerate on Apple Silicon

213 views
Skip to first unread message

Tymur Lysenko

unread,
Dec 8, 2021, 7:11:18 AM12/8/21
to Accelerate
Hello!

I am trying to use Accelerate on M1 (also known as Apple Silicon) Macbook with [this stack snapshot](https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/18/16.yaml). I see on [hackage current version of llvm-hs is 9.0.1](https://hackage.haskell.org/package/llvm-hs), which accelerate-llvm depends on. In turn, llvm-hs needs an actual LLVM library to be built and run correctly. It means, that I have to build LLVM 9 from sources (since it is not available in brew for M1) targeting x86 architecture, because when llvm-hs will be built for x86 (there is no support for M1 in GHC 8.10.7) it needs to be linked against LLVM 9 dynamic library and this can be done only when the shared library is built for x86 (otherwise, the linker will issue a warning). So I use the following command to build LLVM 9 from sources:

```
cmake -G 'Ninja' ../llvm \
  -DCMAKE_BUILD_TYPE='Release' \
  -DLLVM_ENABLE_ASSERTIONS=ON \
  -DLLVM_BUILD_LLVM_DYLIB=ON -DLLVM_LINK_LLVM_DYLIB=ON \
  -DCMAKE_C_FLAGS='-arch x86_64' \
  -DCMAKE_CXX_FLAGS='-arch x86_64'
```

And `sudo cmake --build . --target install` to install.

Also, following [accelerate-llvm readme](https://github.com/AccelerateHS/accelerate-llvm/tree/master/accelerate-llvm#building-from-source) I run the following commands:

```
export INSTALL_PREFIX=/usr/local
cd $INSTALL_PREFIX/lib
sudo ln -s libLLVM.dylib libLLVM-9.dylib
sudo install_name_tool -id $PWD/libLTO.dylib libLTO.dylib
sudo install_name_tool -id $PWD/libLLVM.dylib libLLVM.dylib
sudo install_name_tool -change '@rpath/libLLVM.dylib' $PWD/libLLVM.dylib libLTO.dylib
```

After running the commands I go to my project and run `stack build`. Project's `stack.yml` is:

```
resolver:

packages:
- .

extra-deps:
- accelerate-1.3.0.0
- accelerate-llvm-1.3.0.0
- accelerate-llvm-native-1.3.0.0
- llvm-hs-9.0.1

flags:
  llvm-hs:
    shared-llvm: true

ghc-options:
  "$everything": -haddock
  "$locals": -Wall -Werror
  llvm-hs: -optcxx=-std=c++11
```

It results in a successful build of llvm-hs, but then accelerate-llvm fails to build with error:

```
<command line>: dlopen(/Users/user/.stack/snapshots/x86_64-osx/aa871c0d39823e39cd95f72eca98ef70c02ddfb7560544d56e9e7b145b40f449/8.10.7/lib/x86_64-osx-ghc-8.10.7/libHSllvm-hs-9.0.1-5INivu5SR3W8mSsWX7hrNz-ghc8.10.7.dylib, 0x0005): symbol not found in flat namespace '__ZTIN4llvm13ErrorInfoBaseE'
```

I tryied to figure out how to fix this, but did not succeed on my own. Can anyone, please, tell me what is this error related to and how it can be fixed?

Thank you,
Tymur Lysenko

Trevor L. McDonell

unread,
Dec 8, 2021, 4:12:39 PM12/8/21
to Accelerate
Hi Tymur,

Sorry for the late reply, I missed the notification for your message. But, welcome to the mailing list!

I've created an issue for this here: https://github.com/AccelerateHS/accelerate-llvm/issues/80

The short story is that yes I would like to do this. The main stopping point at the moment is that I don't have access to an apple silicon Mac to work on this. In theory this shouldn't require too much effort once I have access to some hardware.

If you use llvm-hs from GitHub, then you can use newer versions of LLVM, which are probably easier to compile on apple silicon. Accelerate-llvm understands how to use these newer versions as well, so that might get you a bit further in the process. Sorry I couldn't really provide a solution for you yet \:

Cheers,
-Trevor

Tymur Lysenko

unread,
Jan 16, 2022, 3:29:10 PM1/16/22
to Accelerate
Thank you for your reply, Trevor!

Sorry for the long delay from my side. I tried to build master of accelerate-llvm and unfortunately didn't succeed. LLVM I am building against is the same 9-th version that I built using the commands from the previous email. The error is the same. Here is stack.yaml that I use:

```
resolver: lts-18.21 # GHC 8.10.7

packages:
- .

extra-deps:
- git: g...@github.com:AccelerateHS/accelerate.git
  commit: 2fbefeff8e76aa83875b9f8c3f6e6cdd915053a8
- git: g...@github.com:AccelerateHS/accelerate-llvm.git
  commit: bca280a90d94b129dc5250ae4e938a62d0e7a9b0
  subdirs:
  - accelerate-llvm
  - accelerate-llvm-native
  - accelerate-llvm-ptx


ghc-options:
  "$everything": -haddock
  "$locals": -Wall -Werror
  llvm-hs: -optcxx=-std=c++11
``` 

I also tried installing 12-th version using formulas from homebrew-llvm, but in this, case llvm-hs build fails with the message that it could not find 9-th version of LLVM. I was not able to install 9-th version of llvm from this repository; here is the error from brew:

```
==> Installing llvm-9 from llvm-hs/llvm
==> cmake -G Unix Makefiles .. -DCMAKE_INSTALL_PREFIX=/opt/homebrew/Cellar/llvm-9/9.0.1/lib/llvm-9 -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_OPTIMIZED_TABLEGEN=ON -DLLVM_INCLUDE_DOCS=OFF -DLLVM_ENAB
Last 15 lines from /Users/sitiritis/Library/Logs/Homebrew/llvm-9/01.cmake:
  command.


CMake Warning at tools/xcode-toolchain/CMakeLists.txt:60 (message):
  Failed to detect the version of an installed copy of Xcode, falling back to
  highest supported version.  Set XCODE_VERSION to override.


CMake Error at tools/xcode-toolchain/CMakeLists.txt:80 (message):
  Could not identify toolchain dir


-- Configuring incomplete, errors occurred!
See also "/tmp/llvm-9-20220116-35697-vkz6vu/llvm-9.0.1.src/build/CMakeFiles/CMakeOutput.log".
See also "/tmp/llvm-9-20220116-35697-vkz6vu/llvm-9.0.1.src/build/CMakeFiles/CMakeError.log".

If reporting this issue please do so at (not Homebrew/brew or Homebrew/core):
  https://github.com/llvm-hs/homebrew-llvm/issues

These open issues may also help:
Building llvm 9 fails on macOS 10.15 https://github.com/llvm-hs/homebrew-llvm/issues/17
```

For now, to develop with accelerate I decided to set up a linux server on a machine with an Intel CPU and Nvidia GPU to which I connect via SSH.

Unfortunately, I don't have much spare time to work on the issue now, but if there is anything I can help with, e.g. trying to build accelerate once again after some changes are made — let me know.

Cheers,
Tymur Lysenko
Reply all
Reply to author
Forward
0 new messages