Skip to content

[Driver] Enable Classic Flang cross-compilation, e.g. from X86 to AArch64 #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/llvm-project-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ jobs:
# This should be a no-op for non-mac OSes
PKG_CONFIG_PATH: /usr/local/Homebrew/Library/Homebrew/os/mac/pkgconfig//12
run: |
./build-llvm-project.sh -t X86 -e "clang" -p /usr/local -s -n $(sysctl -n hw.logicalcpu) -i -x "-DLLVM_ENABLE_ASSERTIONS=ON" -v
./build-llvm-project.sh -t X86 -e "clang" -p /usr/local -s -n $(sysctl -n hw.logicalcpu) -i -x "-DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_DISABLE_ASSEMBLY_FILES=ON" -v
cd build
make check-all
- name: Test clang ubuntu
Expand Down
81 changes: 60 additions & 21 deletions build-llvm-project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
TARGET="X86"
BUILD_TYPE="Release"
INSTALL_PREFIX="/usr/local"
CROSS_TARGETS=""
NPROC=1
USE_CCACHE="0"
DO_INSTALL="0"
USE_SUDO="0"
C_COMPILER_PATH="/usr/bin/gcc"
CXX_COMPILER_PATH="/usr/bin/g++"
LLVM_ENABLE_PROJECTS="clang;openmp"
LLVM_EXTRA_PROJECTS=""
EXTRA_CMAKE_OPTS=""
VERBOSE=""

Expand All @@ -19,56 +20,50 @@ set -e # Exit script on first error.
function print_usage {
echo "Usage: ./build-llvm-project.sh [options]";
echo "";
echo "Build and install classic-flang-llvm-project.";
echo "Build and install classic-flang-llvm-project (including clang, lld, and openmp).";
echo "Run this script in a directory with project sources.";
echo "Example:";
echo " $ git clone https://github.com/flang-compiler/classic-flang-llvm-project";
echo " $ cd classic-flang-llvm-project";
echo " $ .github/workflows/build-llvm-project.sh -t X86 -p /install/prefix/ \\";
echo " $ -a /usr/bin/gcc-10 -b /usr/bin/g++-10 -i -s";
echo " $ ./build-llvm-project.sh -t X86 -p /opt/classic-flang/ -i -s";
echo "";
echo "Options:";
echo " -t Target to build for (X86, AArch64, PowerPC). Default: X86";
echo " -d Set the CMake build type. Default: Release";
echo " -p Install prefix. Default: /usr/local";
echo " -X Cross-compile OpenMP for given list of target triples. Default: none";
echo " -n Number of parallel jobs. Default: 1";
echo " -c Use ccache. Default: 0 - do not use ccache";
echo " -i Install the build. Default 0 - just build, do not install";
echo " -s Use sudo to install. Default: 0 - do not use sudo";
echo " -a C compiler path. Default: /usr/bin/gcc";
echo " -b C++ compiler path. Default: /usr/bin/g++";
echo " -e List of the LLVM sub-projects to build. Default: clang;openmp";
echo " -e List of additional LLVM sub-projects to build. Default: none";
echo " -x Extra CMake options. Default: ''";
echo " -v Enable verbose output";
}

while getopts "t:d:p:n:cisa:b:e:x:v?" opt; do
while getopts "t:d:p:X:n:cisa:b:e:x:v?" opt; do
case "$opt" in
t) TARGET=$OPTARG;;
d) BUILD_TYPE=$OPTARG;;
p) INSTALL_PREFIX=$OPTARG;;
X) CROSS_TARGETS=$OPTARG;;
n) NPROC=$OPTARG;;
c) USE_CCACHE="1";;
i) DO_INSTALL="1";;
s) USE_SUDO="1";;
a) C_COMPILER_PATH=$OPTARG;;
b) CXX_COMPILER_PATH=$OPTARG;;
e) LLVM_ENABLE_PROJECTS=$OPTARG;;
e) LLVM_EXTRA_PROJECTS=$OPTARG;;
x) EXTRA_CMAKE_OPTS="$OPTARG";;
v) VERBOSE="1";;
?) print_usage; exit 0;;
esac
done

CMAKE_OPTIONS="-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_C_COMPILER=$C_COMPILER_PATH \
-DCMAKE_CXX_COMPILER=$CXX_COMPILER_PATH \
-DLLVM_TARGETS_TO_BUILD=$TARGET \
-DLLVM_ENABLE_CLASSIC_FLANG=ON \
-DFLANG_BUILD_NEW_DRIVER=OFF"
# Warning: the -DLLVM_ENABLE_PROJECTS option is specified with cmake
# to avoid issues with nested quotation marks
-DCMAKE_BUILD_TYPE=$BUILD_TYPE"

if [ $USE_CCACHE == "1" ]; then
echo "Build using ccache"
Expand All @@ -77,16 +72,19 @@ if [ $USE_CCACHE == "1" ]; then
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache"
fi

if [ -n "$EXTRA_CMAKE_OPTS" ]; then
CMAKE_OPTIONS="$CMAKE_OPTIONS $EXTRA_CMAKE_OPTS"
fi

# Build and install
# Build and install.
mkdir -p build && cd build
if [ -n "$VERBOSE" ]; then
set -x
fi
cmake $CMAKE_OPTIONS -DLLVM_ENABLE_PROJECTS=$LLVM_ENABLE_PROJECTS ../llvm
cmake $CMAKE_OPTIONS \
-DCMAKE_C_COMPILER=$C_COMPILER_PATH \
-DCMAKE_CXX_COMPILER=$CXX_COMPILER_PATH \
-DLLVM_ENABLE_CLASSIC_FLANG=ON \
-DLLVM_ENABLE_PROJECTS="clang;lld;openmp;$LLVM_EXTRA_PROJECTS" \
-DLLVM_TARGETS_TO_BUILD="$TARGET" \
$EXTRA_CMAKE_OPTS \
../llvm
set +x
make -j$NPROC VERBOSE=$VERBOSE
if [ $DO_INSTALL == "1" ]; then
Expand All @@ -100,3 +98,44 @@ if [ $DO_INSTALL == "1" ]; then
fi
cd ..

# Cross-compile OpenMP libraries if requested.
IFS=';' read -ra CROSS_TARGET_LIST <<< "$CROSS_TARGETS"
for T in ${CROSS_TARGET_LIST[@]}; do
mkdir -p "build/openmp-$T"
pushd "build/openmp-$T"
CMAKE_OPTIONS="$CMAKE_OPTIONS \
-DCMAKE_AR=$INSTALL_PREFIX/bin/llvm-ar \
-DCMAKE_ASM_COMPILER=$INSTALL_PREFIX/bin/clang \
-DCMAKE_ASM_COMPILER_TARGET=$T \
-DCMAKE_C_COMPILER=$INSTALL_PREFIX/bin/clang \
-DCMAKE_C_COMPILER_TARGET=$T \
-DCMAKE_CXX_COMPILER=$INSTALL_PREFIX/bin/clang++ \
-DCMAKE_CXX_COMPILER_TARGET=$T \
-DCMAKE_RANLIB=$INSTALL_PREFIX/bin/llvm-ranlib \
-DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld"
if [ -n "$VERBOSE" ]; then
set -x
fi
cmake $CMAKE_OPTIONS \
-DLLVM_DEFAULT_TARGET_TRIPLE=$T \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON \
-DLLVM_ENABLE_RUNTIMES="openmp" \
-DLIBOMP_OMPT_SUPPORT=OFF \
-DOPENMP_ENABLE_LIBOMPTARGET=OFF \
-DOPENMP_ENABLE_OMPT_TOOLS=OFF \
-DOPENMP_LLVM_TOOLS_DIR=$PWD/../bin \
$EXTRA_CMAKE_OPTS \
../../runtimes
set +x
make -j$NPROC VERBOSE=$VERBOSE
if [ $DO_INSTALL -eq 1 ]; then
if [ $USE_SUDO -eq 1 ]; then
echo "Install with sudo"
sudo make install
else
echo "Install without sudo"
make install
fi
fi
popd
done
13 changes: 10 additions & 3 deletions clang/lib/Driver/ToolChains/ClassicFlang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,11 @@ void ClassicFlang::ConstructJob(Compilation &C, const JobAction &JA,
/***** Upper part of the Fortran frontend *****/

// TODO do we need to invoke this under GDB sometimes?
const char *UpperExec = Args.MakeArgString(getToolChain().GetProgramPath("flang1"));
std::string flang1 = "flang1";
if (getToolChain().isCrossCompiling())
flang1 = getToolChain().getEffectiveTriple().getTriple() + "-" + flang1;
const char *UpperExec =
Args.MakeArgString(getToolChain().GetProgramPath(flang1.c_str()));

UpperCmdArgs.push_back("-opt"); UpperCmdArgs.push_back(Args.MakeArgString(OptOStr));
UpperCmdArgs.push_back("-terse"); UpperCmdArgs.push_back("1");
Expand Down Expand Up @@ -997,8 +1001,11 @@ void ClassicFlang::ConstructJob(Compilation &C, const JobAction &JA,
Args.hasArg(options::OPT_E)) return;

/***** Lower part of Fortran frontend *****/

const char *LowerExec = Args.MakeArgString(getToolChain().GetProgramPath("flang2"));
std::string flang2 = "flang2";
if (getToolChain().isCrossCompiling())
flang2 = getToolChain().getEffectiveTriple().getTriple() + "-" + flang2;
const char *LowerExec =
Args.MakeArgString(getToolChain().GetProgramPath(flang2.c_str()));

// TODO FLANG arg handling
LowerCmdArgs.push_back("-fn"); LowerCmdArgs.push_back(Input.getBaseInput());
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/Driver/ToolChains/Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,11 @@ void Linux::AddFlangSystemIncludeArgs(const ArgList &DriverArgs,

{
SmallString<128> P(D.Dir);
llvm::sys::path::append(P, "../include");
if (isCrossCompiling())
llvm::sys::path::append(P, "../include",
getEffectiveTriple().getTriple());
else
llvm::sys::path::append(P, "../include");
IncludePathList.push_back(P.c_str());
}

Expand Down
15 changes: 15 additions & 0 deletions clang/test/Driver/flang/classic-flang-cross-compile.F95
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
! REQUIRES: classic_flang
! REQUIRES: host-x86_64

! Check if the cross-compiling flang1/flang2 binaries and include paths
! are used by Clang when cross-compiling.

! RUN: %clang --driver-mode=flang -target aarch64-unknown-linux-gnu %s -### 2>&1 \
! RUN: | FileCheck %s

! RUN: %clang --driver-mode=flang -target aarch64-linux-gnu %s -### 2>&1 \
! RUN: | FileCheck %s

! CHECK: aarch64-unknown-linux-gnu-flang1
! CHECK-SAME: include/aarch64-unknown-linux-gnu
! CHECK: aarch64-unknown-linux-gnu-flang2
2 changes: 2 additions & 0 deletions clang/test/lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,5 @@ def calculate_arch_features(arch_string):

if config.use_classic_flang:
config.available_features.add("classic_flang")

config.available_features.add("host-" + config.host_arch)
6 changes: 5 additions & 1 deletion cmake/Modules/GetClangResourceDir.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ function(get_clang_resource_dir out_var)
set(ret_dir bin/${CLANG_RESOURCE_DIR})
else()
if (NOT CLANG_VERSION_MAJOR)
string(REGEX MATCH "^[0-9]+" CLANG_VERSION_MAJOR ${PACKAGE_VERSION})
if (NOT PACKAGE_VERSION)
string(REGEX MATCH "^[0-9]+" CLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
else()
string(REGEX MATCH "^[0-9]+" CLANG_VERSION_MAJOR ${PACKAGE_VERSION})
endif()
endif()
set(ret_dir lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION_MAJOR})
endif()
Expand Down
2 changes: 1 addition & 1 deletion openmp/runtime/test/worksharing/for/collapse_test.inc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <omp.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#define LOOP_IV_TYPE0 LOOP_TYPES
Expand Down
Loading