llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.66k stars 11.38k forks source link

Standalone Flang build Not building omp_lib.mod on M1 Mac #90543

Open CaracalLynx opened 4 months ago

CaracalLynx commented 4 months ago

Hi, I’ve been trying to install the standalone Flang compiler, as I have Clang and Clang++ installed from Homebrew. I was following the instructions and thought I was doing fine, but noticed that I couldn’t get omp_lib.mod to build ONLY with the standalone build.

I’m using these flags to build llvm to build flang off of:

mkdir root
cd root
rm -rf build
mkdir build
rm -rf install
mkdir install
ROOTDIR=pwd
INSTALLDIR=$ROOTDIR/install

cd build

cmake … -DCMAKE_C_COMPILER=blang -DCMAKE_CXX_COMPILER=blang++
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=$INSTALLDIR
-DCMAKE_CXX_STANDARD=17
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DCMAKE_CXX_LINK_FLAGS=“-Wl,-rpath,$LD_LIBRARY_PATH”
-DFLANG_ENABLE_WERROR=ON
-DLLVM_ENABLE_ASSERTIONS=ON
-DLLVM_TARGETS_TO_BUILD=host
-DLLVM_LIT_ARGS=-v
-DLLVM_ENABLE_PROJECTS=“clang;mlir;flang;openmp”
-DDEFAULT_SYSROOT=“$(xcrun --show-sdk-path)”
…/llvm-project/llvm

make

(I had to name LLVM compilers “blang” and “blang++” to prevent conflict from Apple’s own Clang compilers)

When I do that, everything is fine; omp_lib.mod gets built in the build folder. I then follow the standalone instructions and use these flags for my Flang build:

cd llvm-project/flang
rm -rf build
mkdir build
cd build

cmake … -DCMAKE_C_COMPILER=blang -DCMAKE_CXX_COMPILER=blang++
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_STANDARD=17
-DCMAKE_CXX_LINK_FLAGS=“-Wl,-rpath,$LD_LIBRARY_PATH”
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DFLANG_ENABLE_WERROR=ON
-DLLVM_TARGETS_TO_BUILD=host
-DLLVM_ENABLE_ASSERTIONS=ON
-DLLVM_BUILD_MAIN_SRC_DIR=$ROOTDIR/build/lib/cmake/llvm
-DLLVM_EXTERNAL_LIT=$ROOTDIR/build/bin/llvm-lit
-DLLVM_LIT_ARGS=-v
-DLLVM_DIR=$ROOTDIR/build/lib/cmake/llvm
-DCLANG_DIR=$ROOTDIR/build/lib/cmake/clang
-DMLIR_DIR=$ROOTDIR/build/lib/cmake/mlir
-DCMAKE_INSTALL_PREFIX=/opt/flang
…

make

Whenever This gets made however, I get a notifications stating the following:

-- Not building omp_lib.mod, no OpenMP runtime in LLVM_ENABLED_PROJECTS
-- Not copying omp_lib.h, no OpenMP runtime in LLVM_ENABLED_PROJECTS

I’ve tried several things to get this to work, including putting the flag -DLLVM_ENABLE_PROJECTS=“openmp” in the standalone flags, but they get ignored. Can someone let me know what I am doing incorrectly? Thanks.

CaracalLynx commented 4 months ago

@mjklemm

mjklemm commented 4 months ago

This has worked for me for the main branch of today using CMake 3.29.2:

cmake $HOME/projects/llvm/llvm-project/llvm \
 -DCMAKE_C_COMPILER=gcc \
 -DCMAKE_CXX_COMPILER=g++ \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_CXX_STANDARD=17 \
 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
 -DLLVM_TARGETS_TO_BUILD=host \
 -DLLVM_ENABLE_ASSERTIONS=ON \
 -DCMAKE_INSTALL_PREFIX=/work1/micklemm/installs/clang \
 -DLLVM_ENABLE_PROJECTS="clang;lld"
ninja
ninja install

cmake $HOME/projects/llvm/llvm-project/flang \
 -DCMAKE_C_COMPILER=gcc \
 -DCMAKE_CXX_COMPILER=g++ \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_CXX_STANDARD=17 \
 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
 -DLLVM_TARGETS_TO_BUILD=host \
 -DLLVM_ENABLE_ASSERTIONS=ON \
 -DLLVM_BUILD_MAIN_SRC_DIR=/home/micklemm/git/llvm/llvm-project/build-dev/lib/cmake/llvm \
 -DLLVM_EXTERNAL_LIT=/home/micklemm/git/llvm/llvm-project/build-dev/bin/llvm-lit \
 -DLLVM_LIT_ARGS=-v \
 -DLLVM_DIR=/home/micklemm/git/llvm/llvm-project/build-dev/lib/cmake/llvm \
 -DCLANG_DIR=/home/micklemm/git/llvm/llvm-project/build-dev/lib/cmake/clang \
 -DMLIR_DIR=/home/micklemm/git/llvm/llvm-project/build-dev/lib/cmake/mlir \
 -DCMAKE_INSTALL_PREFIX=/work1/micklemm/installs/flang
ninja
ninja install

cmake $HOME/projects/llvm/llvm-project/runtimes \
 -DCMAKE_C_COMPILER=clang \
 -DCMAKE_CXX_COMPILER=clang++ \
 -DCMAKE_Fortran_COMPILER=flang-new \
 -DCMAKE_BUILD_TYPE=Release \
 -DLLVM_ENABLE_RUNTIMES=openmp \
 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
 -DLLVM_TARGETS_TO_BUILD=host \
 -DLLVM_ENABLE_ASSERTIONS=ON \
 -DLLVM_DIR=/home/micklemm/git/llvm/llvm-project/build-dev/lib/cmake/llvm \
 -DOPENMP_STANDALONE_BUILD=ON \
 -DLIBOMP_FORTRAN_MODULES=ON \
 -DCMAKE_INSTALL_PREFIX=/work1/micklemm/installs/flang
ninja
ninja install

I guess the non-obvious part is -DLIBOMP_FORTRAN_MODULES=ON.

Let me know if that worked for you.

CaracalLynx commented 3 months ago

I followed your instructions and this was the output of the install:

[  8%] Built target libomp-mod
[  8%] Built target libomp-needed-headers
[100%] Built target omp
Install the project...
-- Install configuration: "Release"
-- Installing: /opt/flang/lib/libomp.dylib
-- Installing: /opt/homebrew/Cellar/llvm/17.0.6_1/lib/clang/17/include/omp.h
-- Installing: /opt/homebrew/Cellar/llvm/17.0.6_1/lib/clang/17/include/ompx.h
-- Installing: /opt/homebrew/Cellar/llvm/17.0.6_1/lib/clang/17/include/omp-tools.h
-- Installing: /opt/homebrew/Cellar/llvm/17.0.6_1/lib/clang/17/include/ompt.h
-- Installing: /opt/homebrew/Cellar/llvm/17.0.6_1/lib/clang/17/include/omp_lib.h
-- Installing: /opt/homebrew/Cellar/llvm/17.0.6_1/lib/clang/17/include/omp_lib.mod
-- Installing: /opt/homebrew/Cellar/llvm/17.0.6_1/lib/clang/17/include/omp_lib_kinds.mod

It seems that the runtime built omp_lib.mod in the llvm folder in homebrew, but not in the Flang folder, and flang cannot access OpenMP still.

mjklemm commented 3 months ago

OK, good news is that it is being built for you. Bad news is, that there's still a bug that the module files do end up in the right place. Would it be acceptable for you to copy the files to the right place or point Flang to pick them up from where there are right now, while we are working on a fix?

CaracalLynx commented 3 months ago

As long as it works; I'll take anything lol. Just let me know what to put where.

CaracalLynx commented 3 months ago

Update:

I copied the exact files installed into my Cellar folder into the folder corresponding to the location in my flang folder (so, from /opt/homebrew/Cellar/llvm/17.0.6_1/lib/clang/17/include to /opt/flang/lib/clang/19/include) and I still get errors compiling files using openmp. I then copied those files to flang's include folder directly (/opt/flang/include) and tried to compile and still got the error.

For the record, this is the error I'm receiving while trying to compile programs utilizing openmp:

error: Semantic errors in hello_openmp.f90
./hello_openmp.f90:2:7: error: Cannot read module file for module 'omp_lib': Source file 'omp_lib.mod' was not found
    use omp_lib
        ^^^^^^^
./hello_openmp.f90:12:9: error: No explicit type declared for 'omp_get_thread_num'
    tid = omp_get_thread_num()
          ^^^^^^^^^^^^^^^^^^
./hello_openmp.f90:13:14: error: No explicit type declared for 'omp_get_num_threads'
    nthreads = omp_get_num_threads()
               ^^^^^^^^^^^^^^^^^^^
mjklemm commented 3 months ago

This has worked for me:

$ cp clang/lib/clang/19/include/omp_lib*.{mod,h} flang/include/flang
$ flang-new -fopenmp -o omp omp.f90
flang-new -fopenmp -o omp omp.f90  0.30s user 4.19s system 1578% cpu 0.284 total
$ cat omp.f90
program omp
    use omp_lib
    implicit none

    !$omp parallel
        print *, 'Hello from thread ', omp_get_thread_num()
    !$omp end parallel
end program omp
$ OMP_NUM_THREADS=4 ./omp
 Hello from thread  0
 Hello from thread  3
 Hello from thread  1
 Hello from thread  2
CaracalLynx commented 3 months ago

Placed the files where you asked and this happened:

dyld[1558]: Library not loaded: @rpath/libomp.dylib
  Referenced from: <0DFADB19-DEA5-3291-BFB7-F90249371163> /Users/caracallynx/Desktop/omp
  Reason: no LC_RPATH's found

I step forward, one step back I guess 🥲

mjklemm commented 3 months ago

I step forward, one step back I guess 🥲

Hmpf. Looks like it. :-(

mjklemm commented 3 months ago

Can you please tell me where in your system the file is? I don't have access to a Mac, so I cannot really test this.

CaracalLynx commented 3 months ago

Sure! As stated before, the runtime build you helped me with installed libomp in /opt/flang/lib (with /opt/flang being the install folder prefix). However, I also have a copy of libomp in the homebrew folder /opt/homebrew/opt/libomp/lib.

CaracalLynx commented 3 months ago

Any updates? Do you need any more info from me?