Skip to content

Commit

Permalink
[RISCV] Port Classic Flang to 64-bit RISC-V
Browse files Browse the repository at this point in the history
  • Loading branch information
psoni2628 authored and bryanpkc committed Dec 11, 2024
1 parent 90d81d8 commit 30525e9
Show file tree
Hide file tree
Showing 19 changed files with 10,261 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,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 Down
5 changes: 5 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,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 Down
17 changes: 10 additions & 7 deletions runtime/flang/ieee_arithmetic.F95
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ module IEEE_ARITHMETIC
integer, private, parameter :: FE_DOWNWARD = X'00800000'
integer, private, parameter :: FE_UPWARD = X'00400000'
integer, private, parameter :: FE_TOWARDZERO = X'00c00000'
#else
#ifdef TARGET_LINUX_POWER
#elif defined(TARGET_LINUX_RISCV)
integer, private, parameter :: FE_TONEAREST = 0
integer, private, parameter :: FE_DOWNWARD = 2
integer, private, parameter :: FE_UPWARD = 3
integer, private, parameter :: FE_TOWARDZERO = 1
#elif defined(TARGET_LINUX_POWER)
integer, private, parameter :: FE_TONEAREST = 0
integer, private, parameter :: FE_TOWARDZERO = 1
integer, private, parameter :: FE_UPWARD = 2
Expand All @@ -48,7 +52,6 @@ module IEEE_ARITHMETIC
integer, private, parameter :: FE_DOWNWARD = 1024
integer, private, parameter :: FE_UPWARD = 2048
integer, private, parameter :: FE_TOWARDZERO = 3072
#endif
#endif

type(ieee_round_type), parameter :: ieee_nearest = ieee_round_type(0)
Expand Down Expand Up @@ -486,7 +489,7 @@ end function ieee_support_datatyper

pure logical function ieee_support_denormalnox()
!pgi$ defaultkind
#if defined TARGET_LINUX_ARM || defined TARGET_LINUX_POWER || defined PGFLANG
#if defined(TARGET_LINUX_ARM) || defined(TARGET_LINUX_RISCV) || defined(TARGET_LINUX_POWER) || defined(PGFLANG)
ieee_support_denormalnox = .false.
#else
ieee_support_denormalnox = .true.
Expand All @@ -497,7 +500,7 @@ pure logical function ieee_support_denormalr(x)
!pgi$ defaultkind
!dir$ ignore_tkr (kr) x
real :: x
#if defined TARGET_LINUX_ARM || defined TARGET_LINUX_POWER || defined PGFLANG
#if defined(TARGET_LINUX_ARM) || defined(TARGET_LINUX_RISCV) || defined(TARGET_LINUX_POWER) || defined(PGFLANG)
ieee_support_denormalr = .false.
#else
ieee_support_denormalr = .true.
Expand Down Expand Up @@ -589,7 +592,7 @@ end function ieee_support_standardr

pure logical function ieee_support_uflowctrlnox()
!pgi$ defaultkind
#if defined TARGET_LINUX_ARM || defined TARGET_LINUX_POWER || defined PGFLANG
#if defined(TARGET_LINUX_ARM) || defined(TARGET_LINUX_RISCV) || defined(TARGET_LINUX_POWER) || defined(PGFLANG)
ieee_support_uflowctrlnox = .false.
#else
ieee_support_uflowctrlnox = .true.
Expand All @@ -600,7 +603,7 @@ pure logical function ieee_support_uflowctrlr(x)
!pgi$ defaultkind
!dir$ ignore_tkr (kr) x
real :: x
#if defined TARGET_LINUX_ARM || defined TARGET_LINUX_POWER || defined PGFLANG
#if defined(TARGET_LINUX_ARM) || defined(TARGET_LINUX_RISCV) || defined(TARGET_LINUX_POWER) || defined(PGFLANG)
ieee_support_uflowctrlr = .false.
#else
ieee_support_uflowctrlr = .true.
Expand Down
7 changes: 7 additions & 0 deletions runtime/flang/ieee_exceptions.F95
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ module IEEE_EXCEPTIONS
integer, private, parameter :: FE_INEXACT = 16
integer, private, parameter :: FE_DENORM = 0
! FE_DENORM is not currently supported on arm
#elif defined(TARGET_LINUX_RISCV)
integer, private, parameter :: FE_INVALID = 16
integer, private, parameter :: FE_DIVBYZERO = 8
integer, private, parameter :: FE_OVERFLOW = 4
integer, private, parameter :: FE_UNDERFLOW = 2
integer, private, parameter :: FE_INEXACT = 1
integer, private, parameter :: FE_DENORM = 0
#elif defined(TARGET_LINUX_POWER)
! FE_DENORM is not supported on Power
integer, private, parameter :: FE_INVALID = X'20000000'
Expand Down
74 changes: 74 additions & 0 deletions runtime/flangrti/riscv64/dumpregs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
* See https://llvm.org/LICENSE.txt for license information.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

#include <sys/ucontext.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <ctype.h>
#include "stdioInterf.h"

typedef struct {
int rn; // Register index in to "regs" pointer
char *s; // Symbolic name of register
} gprs_t;


/*
* The way the structure below is organized, the registers are all
* sequential with no gaps - the structure is probably overkill - but
* allows for some flexibility.
*/

gprs_t gprs[] = {
{ 0, "x0" }, { 1, "x1" }, { 2, "x2"}, { 3, "x3" }, { 4, "x4" },
{ 5, "x5" }, { 6, "x6" }, { 7, "x7" }, { 8, "x8" }, { 9, "x9" },
{10, "x10"}, {11, "x11"}, {12, "x12"}, {13, "x13"}, {14, "x14"},
{15, "x15"}, {16, "x16"}, {17, "x17"}, {18, "x18"}, {19, "x19"},
{20, "x20"}, {21, "x21"}, {22, "x22"}, {23, "x23"}, {24, "xr24"},
{25, "x25"}, {26, "x26"}, {27, "x27"}, {28, "x28"}, {29, "x29"},
{30, "x30"}, {31, "x31"},
};

void
dumpregs(uint64_t *regs)
{
int i;
int j;
char *pc = NULL;

if (regs == NULL)
return; // Not sure if this is possible

/*
* Output has the following format:
* <REG> <HEXADECIMAL> <DECIMAL> <ASCII>
* Example:
* r0 0x00003fffaf4a309c 70367390085276 .0J..?..
* sp 0x00003ffff437d1a0 70368546509216 ..7..?..
* toc 0x0000000010019300 268538624 ........
* r3 0x0000000010000e64 268439140 d.......
* ...
*/

for (i = 0; i < sizeof gprs / sizeof *gprs; ++i) {
fprintf(__io_stderr(), " %-8s 0x%016" PRIx64 " %20" PRId64 "\t",
gprs[i].s, regs[gprs[i].rn], regs[gprs[i].rn]);
pc = (char *)&(regs[gprs[i].rn]);
for (j = 0; j < 8; ++j) {
fputc(isprint(pc[j]) ? pc[j] : '.', __io_stderr());
}
fputs("\n", __io_stderr());
}
}

uint64_t *
getRegs(ucontext_t *u)
{
mcontext_t *mc = &u->uc_mcontext;
return (uint64_t *)&(mc->__gregs);
}
3 changes: 3 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ set(UTILS_COMMON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/shared/utils/common)
if( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
add_definitions(-DLLVM_ENABLE_FFI=false)
endif()
if( ${TARGET_ARCHITECTURE} STREQUAL "riscv64" )
add_definitions(-DLLVM_ENABLE_FFI=false)
endif()

link_directories("${LLVM_LIBRARY_DIR}/${LLVM_HOST_TARGET}")

Expand Down
6 changes: 3 additions & 3 deletions tools/flang2/flang2exe/iliutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static int _pwr2(INT, int);
static int _kpwr2(INT, INT, int);
static int _ipowi(int, int);
static int _xpowi(int, int, ILI_OP);
#if defined(TARGET_X8664) || defined(TARGET_POWER) || !defined(TARGET_LLVM_ARM)
#if defined(TARGET_X8664) || defined(TARGET_POWER)
static int _frsqrt(int);
#endif
static int _mkfunc(const char *);
Expand Down Expand Up @@ -2696,7 +2696,7 @@ addarth(ILI *ilip)
#endif
#ifdef IL_FRSQRT
case IL_FRSQRT:
#if !defined(TARGET_LLVM_ARM)
#if defined(TARGET_X8664) || defined(TARGET_POWER)
if (XBIT(183, 0x10000)) {
if (ncons == 1) {
xfsqrt(con1v2, &res.numi[1]);
Expand Down Expand Up @@ -13214,7 +13214,7 @@ _xpowi(int opn, int pwr, ILI_OP opc)
return opn;
}

#if defined(TARGET_X8664) || defined(TARGET_POWER) || !defined(TARGET_LLVM_ARM)
#if defined(TARGET_X8664) || defined(TARGET_POWER)
static int
_frsqrt(int x)
{
Expand Down
1 change: 1 addition & 0 deletions tools/flang2/flang2exe/ll_structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ static const struct triple_info known_triples[] = {
{"x86_64-", "e-p:64:64-i64:64-f80:128-n8:16:32:64-S128"},
{"armv7-", "e-p:32:32-i64:64-v128:64:128-n32-S64"},
{"aarch64-", "e-m:e-i64:64-i128:128-n32:64-S128"},
{"riscv64-", "e-m:e-p:64:64-i64:64-i128:128-n64-S128"},
{"powerpc64le", "e-p:64:64-i64:64-n32:64"},
{"", ""}};

Expand Down
2 changes: 1 addition & 1 deletion tools/flang2/flang2exe/llutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3782,7 +3782,7 @@ process_ll_abi_func_ftn_mod(LL_Module *mod, SPTR func_sptr, bool update)
"Unknown function prototype",
func_sptr, ERR_Fatal);
abi->missing_prototype = true;
#if defined(TARGET_ARM)
#if defined(TARGET_ARM) || defined(TARGET_RISCV)
abi->call_as_varargs = false;
#else
abi->call_as_varargs = true;
Expand Down
2 changes: 1 addition & 1 deletion tools/flang2/flang2exe/machreg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ RGSETB rgsetb;

const int scratch_regs[3] = {IR_RAX, IR_RCX, IR_RDX};

#if defined(TARGET_LLVM_ARM) || defined(TARGET_LLVM_POWER)
#if defined(TARGET_LLVM_ARM) || defined(TARGET_LLVM_POWER) || defined(TARGET_LLVM_RISCV)

/* arguments passed in registers */
int mr_arg_ir[MR_MAX_IREG_ARGS + 1];
Expand Down
2 changes: 1 addition & 1 deletion tools/flang2/flang2exe/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static int dodebug = 0;
#define TR(str)
#endif

#if DEBUG && !defined(EXTRACTOR) && (defined(X86_64) || defined(TARGET_POWER) || defined(TARGET_ARM))
#if DEBUG && !defined(EXTRACTOR) && (defined(X86_64) || defined(TARGET_POWER) || defined(TARGET_ARM) || defined(TARGET_RISCV))
#define DEBUGQQ 1
#else
#define DEBUGQQ 0
Expand Down
72 changes: 72 additions & 0 deletions tools/flang2/flang2exe/riscv64/flgdf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
* See https://llvm.org/LICENSE.txt for license information.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/

/* flgdf.h - data definitions for FTN compiler flags */

FLG flg = {
false, /* asm = -noasm */
false, /* list = -nolist */
true, /* object = -object */
false, /* xref = -show noxref */
false, /* code = -show nocode */
false, /* include = -show noinclude */
0, /* debug = -nodebug */
1, /* opt = -opt 1 */
true, /* depchk = -depchk on */
false, /* depwarn = -depchk warn */
false, /* dclchk = -nodclchk */
false, /* locchk = -nolocchk */
false, /* onetrip = -noonetrip */
false, /* save = -nosave */
1, /* inform = -inform informational */
0xFFFFFFFF, /* xoff */
0x00000000, /* xon */
false, /* ucase = -noucase */
NULL, /* idir == empty list */
NULL, /* linker_directives == empty list */
NULL, /* llvm_target_triple == empty ptr */
NULL, /* target_features == empty ptr */
0, /* vscale_range_min = -vscale_range_min 0 */
0, /* vscale_range_max = -vscale_range_max 0 */
false, /* dlines = -nodlines */
72, /* extend_source = -noextend_source */
true, /* i4 = -i4 */
false, /* line = -noline */
false, /* symbol = -nosymbol */
0, /* profile = no profiling */
false, /* standard = don't flag non-F77 uses */
{0}, /* dbg[] */
true, /* align doubles on doubleword boundary */
0, /* astype - assembler syntax - 0-elf, 1-coff */
false, /* recursive = -norecursive */
0, /* ieee: 0 == none: num == bit value for
item (fdiv==1,ddiv==2) */
0, /* inline: 0 == none: num == max # ilms */
0, /* autoinline */
0, /* vect: 0 = none: num == vect item */
0, /* little endian */
false, /* not terse for summary, etc. */
'_', /* default is to change '$' to '_' */
{0}, /* x flags */
false, /* don't quad align "unconstrained objects";
use natural alignment */
false, /* anno - don't annotate asm file */
false, /* qa = -noqa */
false, /* es = -noes */
false, /* p = preprocessor does not emit # lines in its output */
0, /* def ptr */
NULL, /* search the standard include */
false, /* don't allow smp directives */
false, /* omptarget - don't allow OpenMP Offload directives */
25, /* errorlimit */
false, /* trans_inv */
0, /* tpcount */
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* tpvalue */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
"", /* cmdline */
false, /* qp */
};
Loading

0 comments on commit 30525e9

Please sign in to comment.