Skip to content

Building Flang

Steve Scalpone edited this page Apr 28, 2018 · 24 revisions

Building Flang

We build Flang on Intel x86-64 and OpenPOWER hardware running either Ubuntu or Red Hat.

Prerequisites

Building LLVM requires a fairly modern compiler toolchain and CMake, check Getting started with LLVM and Building LLVM with CMake for the full list of tools required to build flang.

Dependencies

Flang depends on forks of clang and llvm.

The clang fork, flang-compiler/flang-driver, has been modified to support compilation of Fortran files, Fortran-specific command-line options, and the flang toolchain.

The llvm fork, flang-compiler/llvm, has been extended to support enhanced debug metadata specific to Fortran. It also has bug fixes that have been exposed by flang but have not yet been fixed in the llvm repository.

The latest supported LLVM version is 6.0. Flang also supports LLVM version 5.0.

To use 5.0, substitute 50 for 60 in the build instructions.

Building Flang

Flang is built outside of the llvm source tree.

The Linux command-line examples below will install everything into the default system location unless you set a custom install location.

When building flang, your PATH must include the install bin directory.

Using a Custom Install Location

To specify a custom install location, in each step below add -DCMAKE_INSTALL_PREFIX=<INSTALL_PREFIX> to every CMake command.

If you use CMAKE_INSTALL_PREFIX in any step, you must use the same CMAKE_INSTALL_PREFIX in every step.

When using a custom install location, you must make sure that the bin directory is on your PATH when building and running flang.

If you use CMAKE_INSTALL_PREFIX and <INSTALL_PREFIX>/bin is not in your path, you need to add several additional command-line arguments for CMake:

-DLLVM_CONFIG=<INSTALL_PREFIX>/bin/llvm-config
-DCMAKE_CXX_COMPILER=<INSTALL_PREFIX>/bin/clang++
-DCMAKE_C_COMPILER=<INSTALL_PREFIX>/bin/clang
-DCMAKE_Fortran_COMPILER=<INSTALL_PREFIX>/bin/flang)

Step-by-step instructions

  1. Get Flang LLVM, build and install it according to instructions

    cd where/you/want/to/build
    git clone https://github.com/flang-compiler/llvm.git
    cd llvm
    git checkout release_60
    mkdir build && cd build
    cmake <your custom options> ..
    make
    sudo make install
    
  2. Get the flang driver, build and install it

    cd where/you/want/to/build
    git clone https://github.com/flang-compiler/flang-driver.git
    cd flang-driver
    git checkout release_60
    mkdir build && cd build
    cmake <your custom options> ..
    make
    sudo make install
    

    If you use CMAKE_INSTALL_PREFIX in Step 1 and <INSTALL_PREFIX>/bin is not in your path, you need to add -DLLVM_CONFIG=<INSTALL_PREFIX>/bin/llvm-config when invoking CMake, otherwise you will encounter an error related to LLVMConfig.cmake not being found.

  3. Get the OpenMP runtime library, build and install it

    cd where/you/want/to/build
    git clone https://github.com/llvm-mirror/openmp.git
    cd openmp/runtime
    git checkout release_60
    mkdir build && cd build
    cmake <your custom options> ../..
    make
    sudo make install
    
  4. Get the flang source code

    cd where/you/want/to/build
    git clone https://github.com/flang-compiler/flang.git
    
  5. Build libpgmath and install it

    Note that libpgmath on x86 requires a toolchain that understands AVX-512 instructions, such as gcc 7.2.

    For example, you might add custom options similar to these:

    -DCMAKE_CXX_COMPILER=<GCC_PREFIX>/bin/g++
    -DCMAKE_C_COMPILER=<GCC_PREFIX>/bin/gcc
    

    The source for libpgmath is in flang/runtime/libpgmath.

    cd where/you/want/to/build
    cd flang/runtime/libpgmath
    mkdir build && cd build
    cmake <your custom options> ..
    make
    sudo make install
    
  6. Built flang and install it

    cd where/you/want/to/build
    git clone https://github.com/flang-compiler/flang.git
    cd flang
    mkdir build && cd build
    cmake <your custom options> ..
    make
    sudo make install
    

(Alternative:) Build flang using Spack

Spack is a flexible package manager for HPC system and can be used to build flang and its dependencies.

  1. Get Spack

    git clone https://github.com/llnl/spack.git
    

    On bash:

    source spack/share/spack/setup-env.sh
    

    or tcsh:

    source spack/share/spack/setup-env.csh
    
  2. Build Flang and its dependencies:

    spack install flang
    

    watch out for the installation path, the flang wrapper script in there is ready to use.

  3. (optional) setup Flang as a compiler inside spack if you want to build spack package with flang:

    spack compiler add path/to/flang/bin
    

    Now you might want to edit your ~/.spack/*/compilers.yaml to combine flang with any c compiler of your choice.

Using Flang

To test your installation, create a simple "hello world" program, like the following:

program hello
  print *, 'hello world'
end

Next, compile the program in the following manner. We will assume the program is called hello.f90

% flang hello.f90

If the build succeeds, then you can execute the program in the following manner:

% ./a.out