Building LLVM to restore OpenMP functionality

Imagine running Clear Linux for a while and suddenly clang++ -fopenmp stops working. No announcement was made at the time, OpenMP removal in CL 39970.

LLVM-17 Installation

Building LLVM requires the c-basic and git bundles.

sudo swupd bundle-add c-basic git

Ensure adequate disk space 11.1+ GB. After git clone, the llvm-17 folder consumes 2.0 GB on disk. Which grows to 7.2 GB after compilation, and 3.9 GB for the installation folder /opt/llvm-17. I have the NVIDIA RTX 3070 GPU and default to sm_86. Note that 75 is included for compute capabilities.

The LLVM base version must match the system clang --version if you want to restore OpenMP functionality, described later.


sudo mkdir -p /opt/llvm-17
sudo mkdir -p /usr/local/share/gdb
sudo chown $USER:$USER /opt/llvm-17
sudo chown $USER:$USER /usr/local/share/gdb

unset CFLAGS

export CC=clang
export CXX=clang++

git clone --depth 1 -b "release/17.x" llvm-17

rm -fr llvm-17/build
cmake -S llvm-17/llvm -B llvm-17/build -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_HOST_TRIPLE="x86_64-generic-linux" \
    -DLLVM_BINUTILS_INCDIR=/usr/include \
    -DLLVM_ENABLE_PROJECTS="lld;lldb;clang;clang-tools-extra;compiler-rt;openmp;polly" \

[[ $? -ne 0 ]] && exit $?

cmake --build llvm-17/build && \
cmake --install llvm-17/build --prefix=/opt/llvm-17

OpenMP Support

Using clang++ from the /opt/llvm-17 folder is simply setting two environment variables.

export PATH=/opt/llvm-17/bin:$PATH
export LD_LIBRARY_PATH=/opt/llvm-17/lib64:$LD_LIBRARY_PATH

clang++ -fopenmp ...

System LLVM Workaround

Another option is restoring OpenMP functionality for /usr/bin/clang++ and friends. I do this with a script. Run with sudo privilege. The OpenMP GPU targets may be copied as well, but not needed for my use case.

#!/bin/bash -x

if [[ -d /opt/llvm-17/lib64/clang/17 && -d /usr/lib64/clang/17 ]]; then
  cd /usr/lib64/cmake
  cp -a /opt/llvm-17/lib64/cmake/openmp .

  cd /usr/lib64/clang/17/include
  cp -a /opt/llvm-17/lib64/clang/17/include/omp*.h .

  cd /usr/lib64
  cp -a /opt/llvm-17/lib64/ .
# cp -a /opt/llvm-17/lib64/libomptarget.* .
# cp -a /opt/llvm-17/lib64/libomptarget-* .
  cp -a /opt/llvm-17/lib64/ .
  cp -a /opt/llvm-17/lib64/ .
  ln -sf

Running clang++ -fopenmp on Clear Linux is again, fun, consuming many CPU cores. :slight_smile:

1 Like

we’ll try to take a look – working on llvm takes a long time for us (the package takes a very long time to build) so we tend to touch it only once in a while


Many thanks for bringing OpenMP functionality back into LLVM. CL 41570+.