Skip to content
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

[RISCV] Port Classic Flang to 64-bit RISC-V #1462

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 29 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ if (WIN32)
SET(CMAKE_Fortran_COMPILER_SUPPORTS_F90 1)
endif()

if(DEFINED CROSS_COMPILE_TARGET_TRIPLE)
add_definitions(-DCROSS_COMPILE_TARGET_TRIPLE="${CROSS_COMPILE_TARGET_TRIPLE}")
string(REGEX MATCH "^[^-]+"
TARGET_ARCHITECTURE ${CROSS_COMPILE_TARGET_TRIPLE})
else()
set(TARGET_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} CACHE STRING "Target Architecture")
endif()
set(TARGET_OS ${CMAKE_SYSTEM_NAME} CACHE STRING "Target OS")
set(TARGET_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR} CACHE STRING "Target Architecture")

if (POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
Expand Down Expand Up @@ -59,6 +65,9 @@ elseif (${TARGET_ARCHITECTURE} MATCHES "^(aarch64|arm64|ARM64)$")
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
set(ARCHNAME ppc64le)
set(ARCH POWER)
elseif( ${TARGET_ARCHITECTURE} STREQUAL "riscv64")
set(ARCHNAME riscv64)
set(ARCH RISCV)
else()
message("Unsupported architecture: ${TARGET_ARCHITECTURE}" )
return()
Expand All @@ -80,7 +89,6 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
set(CONFIG_COMMAND ${LLVM_CONFIG}
"--assertion-mode"
"--bindir"
"--libdir"
"--includedir"
"--prefix"
Expand All @@ -104,11 +112,17 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
endif()

list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS)
list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
list(GET CONFIG_OUTPUT 3 INCLUDE_DIR)
list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT)
list(GET CONFIG_OUTPUT 5 LLVM_HOST_TARGET)
list(GET CONFIG_OUTPUT 1 LIBRARY_DIR)
list(GET CONFIG_OUTPUT 2 INCLUDE_DIR)
list(GET CONFIG_OUTPUT 3 LLVM_OBJ_ROOT)
list(GET CONFIG_OUTPUT 4 LLVM_HOST_TARGET)

if(DEFINED CROSS_COMPILE_TARGET_TRIPLE)
set(INSTALLED_TARGET_TRIPLE ${CROSS_COMPILE_TARGET_TRIPLE})
else()
set(INSTALLED_TARGET_TRIPLE ${LLVM_HOST_TARGET})
endif()
message(STATUS "Current target triple: ${INSTALLED_TARGET_TRIPLE}")

if(NOT MSVC_IDE)
set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS}
Expand All @@ -117,10 +131,10 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
mark_as_advanced(LLVM_ENABLE_ASSERTIONS)
endif()

set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to llvm-lit, FileCheck, etc.")

set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/lib/cmake/llvm"
CACHE PATH "Path to LLVM cmake modules")
Expand Down Expand Up @@ -160,7 +174,6 @@ if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
endif()

include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}")
link_directories("${LLVM_LIBRARY_DIR}/${LLVM_HOST_TARGET}")

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
Expand All @@ -180,16 +193,18 @@ Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
endif()

# Check prebuilt llvm/utils.
if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
if(EXISTS ${LLVM_LIT_TOOLS_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
AND EXISTS ${LLVM_LIT_TOOLS_DIR}/count${CMAKE_EXECUTABLE_SUFFIX}
AND EXISTS ${LLVM_LIT_TOOLS_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
set(LLVM_UTILS_PROVIDED ON)
endif()

if(LLVM_EXTERNAL_LIT)
if(NOT LLVM_LIT)
set(LLVM_LIT ${LLVM_EXTERNAL_LIT})
endif()
elseif(EXISTS ${LLVM_LIT_TOOLS_DIR}/llvm-lit${CMAKE_EXECUTABLE_SUFFIX})
set(LLVM_LIT ${LLVM_LIT_TOOLS_DIR}/llvm-lit${CMAKE_EXECUTABLE_SUFFIX})
endif()

if(NOT LLVM_LIT OR NOT LLVM_UTILS_PROVIDED)
Expand Down Expand Up @@ -362,8 +377,8 @@ macro(add_flang_library name)
# link_system_libs( ${name} ) # getd of cmake warning messages

install(TARGETS ${name}
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
LIBRARY DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${INSTALLED_TARGET_TRIPLE}"
ARCHIVE DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${INSTALLED_TARGET_TRIPLE}"
RUNTIME DESTINATION bin)
set_target_properties(${name} PROPERTIES FOLDER "Flang libraries")
endmacro(add_flang_library)
Expand Down
51 changes: 41 additions & 10 deletions build-flang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ TARGET="X86"
BUILD_TYPE="Release"
BUILD_PREFIX="./build"
INSTALL_PREFIX="/usr/local"
CROSS_TARGET=""
NPROC=1
USE_LLVM_MAIN_SRC_DIR=""
USE_LLVM_CONFIG=""
LLVM_CONFIG_BIN=""
USE_CCACHE="0"
USE_SUDO="0"
EXTRA_CMAKE_OPTS=""
Expand All @@ -33,6 +34,7 @@ function print_usage {
echo " -d CMake build type. Default: Release";
echo " -b Build prefix. Default: ./build";
echo " -p Install prefix. Default: /usr/local";
echo " -X Build a cross-compiler for given target triple. Default: N/A"
echo " -n Number of parallel jobs. Default: 1";
echo " -l Path to LLVM sources. Default: not set";
echo " -o Path to llvm-config. Default: not set";
Expand All @@ -42,15 +44,16 @@ function print_usage {
echo " -v Enable verbose output";
}

while getopts "t:d:b:p:n:l:o:csx:v?" opt; do
while getopts "t:X:d:b:p:n:l:o:csx:v?" opt; do
case "$opt" in
t) TARGET=$OPTARG;;
d) BUILD_TYPE=$OPTARG;;
b) BUILD_PREFIX=$OPTARG;;
p) INSTALL_PREFIX=$OPTARG;;
X) CROSS_TARGET=$OPTARG;;
n) NPROC=$OPTARG;;
l) USE_LLVM_MAIN_SRC_DIR="-DLLVM_MAIN_SRC_DIR=$OPTARG";;
o) USE_LLVM_CONFIG="-DLLVM_CONFIG=$OPTARG";;
o) LLVM_CONFIG_BIN="$OPTARG";;
c) USE_CCACHE="1";;
s) USE_SUDO="1";;
x) EXTRA_CMAKE_OPTS="$OPTARG";;
Expand All @@ -61,18 +64,40 @@ done

CMAKE_OPTIONS="-DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_CXX_COMPILER=$INSTALL_PREFIX/bin/clang++ \
-DCMAKE_AR=$INSTALL_PREFIX/bin/llvm-ar \
-DCMAKE_C_COMPILER=$INSTALL_PREFIX/bin/clang \
-DCMAKE_CXX_COMPILER=$INSTALL_PREFIX/bin/clang++ \
-DCMAKE_RANLIB=$INSTALL_PREFIX/bin/llvm-ranlib \
-DLLVM_TARGETS_TO_BUILD=$TARGET \
$USE_LLVM_MAIN_SRC_DIR"

# Use lld for release_19x or newer version of classic-flang-llvm-project.
set -x
if [ -n "$LLVM_CONFIG_BIN" ]; then
CMAKE_OPTIONS="$CMAKE_OPTIONS -DLLVM_CONFIG=$LLVM_CONFIG_BIN"
LLVM_MAJOR_VERSION=$("$LLVM_CONFIG_BIN" --version | cut -f1 -d.)
if [ "$LLVM_MAJOR_VERSION" -gt 19 ]; then
CMAKE_OPTIONS="$CMAKE_OPTIONS \
-DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld \
-DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld"
fi
fi
set +x

if [ $USE_CCACHE == "1" ]; then
echo "Build using ccache"
CMAKE_OPTIONS="$CMAKE_OPTIONS \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache"
fi

if [ -n "$CROSS_TARGET" ]; then
CMAKE_OPTIONS="$CMAKE_OPTIONS \
-DCROSS_COMPILE_TARGET_TRIPLE=$CROSS_TARGET \
-DLLVM_DEFAULT_TARGET_TRIPLE=$CROSS_TARGET \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON"
fi

if [ -n "$EXTRA_CMAKE_OPTS" ]; then
CMAKE_OPTIONS="$CMAKE_OPTIONS $EXTRA_CMAKE_OPTS"
fi
Expand All @@ -88,15 +113,22 @@ mkdir -p $BUILD_PREFIX/libpgmath && cd $BUILD_PREFIX/libpgmath
if [ -n "$VERBOSE" ]; then
set -x
fi
cmake $CMAKE_OPTIONS $TOPDIR/runtime/libpgmath
if [ -n "$CROSS_TARGET" ]; then
cmake $CMAKE_OPTIONS \
-DCMAKE_C_COMPILER_TARGET=$CROSS_TARGET \
-DCMAKE_CXX_COMPILER_TARGET=$CROSS_TARGET \
$TOPDIR/runtime/libpgmath
else
cmake $CMAKE_OPTIONS $TOPDIR/runtime/libpgmath
fi
set +x
make -j$NPROC VERBOSE=$VERBOSE
if [ $USE_SUDO == "1" ]; then
echo "Install with sudo"
sudo make install
sudo make install VERBOSE=$VERBOSE
else
echo "Install without sudo"
make install
make install VERBOSE=$VERBOSE
fi

# Build and install flang.
Expand All @@ -110,14 +142,13 @@ cmake $CMAKE_OPTIONS \
-DFLANG_INCLUDE_DOCS=ON \
-DFLANG_LLVM_EXTENSIONS=ON \
-DWITH_WERROR=ON \
$USE_LLVM_CONFIG \
$TOPDIR
set +x
make -j$NPROC VERBOSE=$VERBOSE
if [ $USE_SUDO == "1" ]; then
echo "Install with sudo"
sudo make install
sudo make install VERBOSE=$VERBOSE
else
echo "Install without sudo"
make install
make install VERBOSE=$VERBOSE
fi
16 changes: 16 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

if(DEFINED CROSS_COMPILE_TARGET_TRIPLE)
set(CMAKE_C_FLAGS "-target ${CROSS_COMPILE_TARGET_TRIPLE}")
set(CMAKE_CXX_FLAGS "-target ${CROSS_COMPILE_TARGET_TRIPLE}")
set(CMAKE_Fortran_FLAGS "-target ${CROSS_COMPILE_TARGET_TRIPLE}")
endif()

set (RUNTIME_SHARED_DIR ${CMAKE_CURRENT_SOURCE_DIR}/shared)

add_definitions(
Expand Down Expand Up @@ -31,6 +37,11 @@ elseif( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
-DTARGET_${OS}_ARM
-DTARGET_${OS}_ARM64
)
elseif( ${TARGET_ARCHITECTURE} STREQUAL "riscv64" )
add_definitions(
-DTARGET_LLVM_RISCV64
-DTARGET_LINUX_RISCV
)
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
add_definitions(
-DTARGET_${OS}_POWER
Expand All @@ -39,6 +50,11 @@ elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
if(DEFINED CROSS_COMPILE_TARGET_TRIPLE)
link_directories("${LLVM_LIBRARY_DIR}/${CROSS_COMPILE_TARGET_TRIPLE}")
else()
link_directories("${LLVM_LIBRARY_DIR}/${LLVM_HOST_TARGET}")
endif()

add_subdirectory(ompstub)

Expand Down
28 changes: 14 additions & 14 deletions runtime/flang/gather.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/* local gather functions */

static void
local_gather_INT1(int n, void *dstp, void *srcp, int *gv)
local_gather_INT1(int n, void *dstp, void *srcp, int *gv, int unused)
{
__INT1_T *dst = (__INT1_T *)dstp;
__INT1_T *src = (__INT1_T *)srcp;
Expand All @@ -22,7 +22,7 @@ local_gather_INT1(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_INT2(int n, void *dstp, void *srcp, int *gv)
local_gather_INT2(int n, void *dstp, void *srcp, int *gv, int unused)
{
__INT2_T *dst = (__INT2_T *)dstp;
__INT2_T *src = (__INT2_T *)srcp;
Expand All @@ -32,7 +32,7 @@ local_gather_INT2(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_INT4(int n, void *dstp, void *srcp, int *gv)
local_gather_INT4(int n, void *dstp, void *srcp, int *gv, int unused)
{
__INT4_T *dst = (__INT4_T *)dstp;
__INT4_T *src = (__INT4_T *)srcp;
Expand All @@ -42,7 +42,7 @@ local_gather_INT4(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_INT8(int n, void *dstp, void *srcp, int *gv)
local_gather_INT8(int n, void *dstp, void *srcp, int *gv, int unused)
{
__INT8_T *dst = (__INT8_T *)dstp;
__INT8_T *src = (__INT8_T *)srcp;
Expand All @@ -52,7 +52,7 @@ local_gather_INT8(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_LOG1(int n, void *dstp, void *srcp, int *gv)
local_gather_LOG1(int n, void *dstp, void *srcp, int *gv, int unused)
{
__LOG1_T *dst = (__LOG1_T *)dstp;
__LOG1_T *src = (__LOG1_T *)srcp;
Expand All @@ -62,7 +62,7 @@ local_gather_LOG1(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_LOG2(int n, void *dstp, void *srcp, int *gv)
local_gather_LOG2(int n, void *dstp, void *srcp, int *gv, int unused)
{
__LOG2_T *dst = (__LOG2_T *)dstp;
__LOG2_T *src = (__LOG2_T *)srcp;
Expand All @@ -72,7 +72,7 @@ local_gather_LOG2(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_LOG4(int n, void *dstp, void *srcp, int *gv)
local_gather_LOG4(int n, void *dstp, void *srcp, int *gv, int unused)
{
__LOG4_T *dst = (__LOG4_T *)dstp;
__LOG4_T *src = (__LOG4_T *)srcp;
Expand All @@ -82,7 +82,7 @@ local_gather_LOG4(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_LOG8(int n, void *dstp, void *srcp, int *gv)
local_gather_LOG8(int n, void *dstp, void *srcp, int *gv, int unused)
{
__LOG8_T *dst = (__LOG8_T *)dstp;
__LOG8_T *src = (__LOG8_T *)srcp;
Expand All @@ -92,7 +92,7 @@ local_gather_LOG8(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_REAL4(int n, void *dstp, void *srcp, int *gv)
local_gather_REAL4(int n, void *dstp, void *srcp, int *gv, int unused)
{
__REAL4_T *dst = (__REAL4_T *)dstp;
__REAL4_T *src = (__REAL4_T *)srcp;
Expand All @@ -102,7 +102,7 @@ local_gather_REAL4(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_REAL8(int n, void *dstp, void *srcp, int *gv)
local_gather_REAL8(int n, void *dstp, void *srcp, int *gv, int unused)
{
__REAL8_T *dst = (__REAL8_T *)dstp;
__REAL8_T *src = (__REAL8_T *)srcp;
Expand All @@ -112,7 +112,7 @@ local_gather_REAL8(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_REAL16(int n, void *dstp, void *srcp, int *gv)
local_gather_REAL16(int n, void *dstp, void *srcp, int *gv, int unused)
{
__REAL16_T *dst = (__REAL16_T *)dstp;
__REAL16_T *src = (__REAL16_T *)srcp;
Expand All @@ -122,7 +122,7 @@ local_gather_REAL16(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_CPLX8(int n, void *dstp, void *srcp, int *gv)
local_gather_CPLX8(int n, void *dstp, void *srcp, int *gv, int unused)
{
__CPLX8_T *dst = (__CPLX8_T *)dstp;
__CPLX8_T *src = (__CPLX8_T *)srcp;
Expand All @@ -132,7 +132,7 @@ local_gather_CPLX8(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_CPLX16(int n, void *dstp, void *srcp, int *gv)
local_gather_CPLX16(int n, void *dstp, void *srcp, int *gv, int unused)
{
__CPLX16_T *dst = (__CPLX16_T *)dstp;
__CPLX16_T *src = (__CPLX16_T *)srcp;
Expand All @@ -142,7 +142,7 @@ local_gather_CPLX16(int n, void *dstp, void *srcp, int *gv)
}

static void
local_gather_CPLX32(int n, void *dstp, void *srcp, int *gv)
local_gather_CPLX32(int n, void *dstp, void *srcp, int *gv, int unused)
{
__CPLX32_T *dst = (__CPLX32_T *)dstp;
__CPLX32_T *src = (__CPLX32_T *)srcp;
Expand Down
Loading
Loading