Skip to content

Commit c443899

Browse files
psoni2628bryanpkc
authored andcommitted
[RISCV] Port Classic Flang to 64-bit RISC-V
1 parent 649ff89 commit c443899

File tree

19 files changed

+10261
-15
lines changed

19 files changed

+10261
-15
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ elseif (${TARGET_ARCHITECTURE} MATCHES "^(aarch64|arm64|ARM64)$")
6565
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
6666
set(ARCHNAME ppc64le)
6767
set(ARCH POWER)
68+
elseif( ${TARGET_ARCHITECTURE} STREQUAL "riscv64")
69+
set(ARCHNAME riscv64)
70+
set(ARCH RISCV)
6871
else()
6972
message("Unsupported architecture: ${TARGET_ARCHITECTURE}" )
7073
return()

runtime/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ elseif( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
3737
-DTARGET_${OS}_ARM
3838
-DTARGET_${OS}_ARM64
3939
)
40+
elseif( ${TARGET_ARCHITECTURE} STREQUAL "riscv64" )
41+
add_definitions(
42+
-DTARGET_LLVM_RISCV64
43+
-DTARGET_LINUX_RISCV
44+
)
4045
elseif( ${TARGET_ARCHITECTURE} STREQUAL "ppc64le" )
4146
add_definitions(
4247
-DTARGET_${OS}_POWER

runtime/flang/ieee_arithmetic.F95

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ module IEEE_ARITHMETIC
3636
integer, private, parameter :: FE_DOWNWARD = X'00800000'
3737
integer, private, parameter :: FE_UPWARD = X'00400000'
3838
integer, private, parameter :: FE_TOWARDZERO = X'00c00000'
39-
#else
40-
#ifdef TARGET_LINUX_POWER
39+
#elif defined(TARGET_LINUX_RISCV)
40+
integer, private, parameter :: FE_TONEAREST = 0
41+
integer, private, parameter :: FE_DOWNWARD = 2
42+
integer, private, parameter :: FE_UPWARD = 3
43+
integer, private, parameter :: FE_TOWARDZERO = 1
44+
#elif defined(TARGET_LINUX_POWER)
4145
integer, private, parameter :: FE_TONEAREST = 0
4246
integer, private, parameter :: FE_TOWARDZERO = 1
4347
integer, private, parameter :: FE_UPWARD = 2
@@ -48,7 +52,6 @@ module IEEE_ARITHMETIC
4852
integer, private, parameter :: FE_DOWNWARD = 1024
4953
integer, private, parameter :: FE_UPWARD = 2048
5054
integer, private, parameter :: FE_TOWARDZERO = 3072
51-
#endif
5255
#endif
5356

5457
type(ieee_round_type), parameter :: ieee_nearest = ieee_round_type(0)
@@ -486,7 +489,7 @@ end function ieee_support_datatyper
486489

487490
pure logical function ieee_support_denormalnox()
488491
!pgi$ defaultkind
489-
#if defined TARGET_LINUX_ARM || defined TARGET_LINUX_POWER || defined PGFLANG
492+
#if defined(TARGET_LINUX_ARM) || defined(TARGET_LINUX_RISCV) || defined(TARGET_LINUX_POWER) || defined(PGFLANG)
490493
ieee_support_denormalnox = .false.
491494
#else
492495
ieee_support_denormalnox = .true.
@@ -497,7 +500,7 @@ pure logical function ieee_support_denormalr(x)
497500
!pgi$ defaultkind
498501
!dir$ ignore_tkr (kr) x
499502
real :: x
500-
#if defined TARGET_LINUX_ARM || defined TARGET_LINUX_POWER || defined PGFLANG
503+
#if defined(TARGET_LINUX_ARM) || defined(TARGET_LINUX_RISCV) || defined(TARGET_LINUX_POWER) || defined(PGFLANG)
501504
ieee_support_denormalr = .false.
502505
#else
503506
ieee_support_denormalr = .true.
@@ -589,7 +592,7 @@ end function ieee_support_standardr
589592

590593
pure logical function ieee_support_uflowctrlnox()
591594
!pgi$ defaultkind
592-
#if defined TARGET_LINUX_ARM || defined TARGET_LINUX_POWER || defined PGFLANG
595+
#if defined(TARGET_LINUX_ARM) || defined(TARGET_LINUX_RISCV) || defined(TARGET_LINUX_POWER) || defined(PGFLANG)
593596
ieee_support_uflowctrlnox = .false.
594597
#else
595598
ieee_support_uflowctrlnox = .true.
@@ -600,7 +603,7 @@ pure logical function ieee_support_uflowctrlr(x)
600603
!pgi$ defaultkind
601604
!dir$ ignore_tkr (kr) x
602605
real :: x
603-
#if defined TARGET_LINUX_ARM || defined TARGET_LINUX_POWER || defined PGFLANG
606+
#if defined(TARGET_LINUX_ARM) || defined(TARGET_LINUX_RISCV) || defined(TARGET_LINUX_POWER) || defined(PGFLANG)
604607
ieee_support_uflowctrlr = .false.
605608
#else
606609
ieee_support_uflowctrlr = .true.

runtime/flang/ieee_exceptions.F95

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ module IEEE_EXCEPTIONS
3737
integer, private, parameter :: FE_INEXACT = 16
3838
integer, private, parameter :: FE_DENORM = 0
3939
! FE_DENORM is not currently supported on arm
40+
#elif defined(TARGET_LINUX_RISCV)
41+
integer, private, parameter :: FE_INVALID = 16
42+
integer, private, parameter :: FE_DIVBYZERO = 8
43+
integer, private, parameter :: FE_OVERFLOW = 4
44+
integer, private, parameter :: FE_UNDERFLOW = 2
45+
integer, private, parameter :: FE_INEXACT = 1
46+
integer, private, parameter :: FE_DENORM = 0
4047
#elif defined(TARGET_LINUX_POWER)
4148
! FE_DENORM is not supported on Power
4249
integer, private, parameter :: FE_INVALID = X'20000000'
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3+
* See https://llvm.org/LICENSE.txt for license information.
4+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
*
6+
*/
7+
8+
#include <sys/ucontext.h>
9+
#include <stdio.h>
10+
#include <stdint.h>
11+
#include <inttypes.h>
12+
#include <ctype.h>
13+
#include "stdioInterf.h"
14+
15+
typedef struct {
16+
int rn; // Register index in to "regs" pointer
17+
char *s; // Symbolic name of register
18+
} gprs_t;
19+
20+
21+
/*
22+
* The way the structure below is organized, the registers are all
23+
* sequential with no gaps - the structure is probably overkill - but
24+
* allows for some flexibility.
25+
*/
26+
27+
gprs_t gprs[] = {
28+
{ 0, "x0" }, { 1, "x1" }, { 2, "x2"}, { 3, "x3" }, { 4, "x4" },
29+
{ 5, "x5" }, { 6, "x6" }, { 7, "x7" }, { 8, "x8" }, { 9, "x9" },
30+
{10, "x10"}, {11, "x11"}, {12, "x12"}, {13, "x13"}, {14, "x14"},
31+
{15, "x15"}, {16, "x16"}, {17, "x17"}, {18, "x18"}, {19, "x19"},
32+
{20, "x20"}, {21, "x21"}, {22, "x22"}, {23, "x23"}, {24, "xr24"},
33+
{25, "x25"}, {26, "x26"}, {27, "x27"}, {28, "x28"}, {29, "x29"},
34+
{30, "x30"}, {31, "x31"},
35+
};
36+
37+
void
38+
dumpregs(uint64_t *regs)
39+
{
40+
int i;
41+
int j;
42+
char *pc = NULL;
43+
44+
if (regs == NULL)
45+
return; // Not sure if this is possible
46+
47+
/*
48+
* Output has the following format:
49+
* <REG> <HEXADECIMAL> <DECIMAL> <ASCII>
50+
* Example:
51+
* r0 0x00003fffaf4a309c 70367390085276 .0J..?..
52+
* sp 0x00003ffff437d1a0 70368546509216 ..7..?..
53+
* toc 0x0000000010019300 268538624 ........
54+
* r3 0x0000000010000e64 268439140 d.......
55+
* ...
56+
*/
57+
58+
for (i = 0; i < sizeof gprs / sizeof *gprs; ++i) {
59+
fprintf(__io_stderr(), " %-8s 0x%016" PRIx64 " %20" PRId64 "\t",
60+
gprs[i].s, regs[gprs[i].rn], regs[gprs[i].rn]);
61+
pc = (char *)&(regs[gprs[i].rn]);
62+
for (j = 0; j < 8; ++j) {
63+
fputc(isprint(pc[j]) ? pc[j] : '.', __io_stderr());
64+
}
65+
fputs("\n", __io_stderr());
66+
}
67+
}
68+
69+
uint64_t *
70+
getRegs(ucontext_t *u)
71+
{
72+
mcontext_t *mc = &u->uc_mcontext;
73+
return (uint64_t *)&(mc->__gregs);
74+
}

tools/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ set(UTILS_COMMON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/shared/utils/common)
1414
if( ${TARGET_ARCHITECTURE} STREQUAL "aarch64" )
1515
add_definitions(-DLLVM_ENABLE_FFI=false)
1616
endif()
17+
if( ${TARGET_ARCHITECTURE} STREQUAL "riscv64" )
18+
add_definitions(-DLLVM_ENABLE_FFI=false)
19+
endif()
1720

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

tools/flang2/flang2exe/iliutil.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static int _pwr2(INT, int);
9494
static int _kpwr2(INT, INT, int);
9595
static int _ipowi(int, int);
9696
static int _xpowi(int, int, ILI_OP);
97-
#if defined(TARGET_X8664) || defined(TARGET_POWER) || !defined(TARGET_LLVM_ARM)
97+
#if defined(TARGET_X8664) || defined(TARGET_POWER)
9898
static int _frsqrt(int);
9999
#endif
100100
static int _mkfunc(const char *);
@@ -2696,7 +2696,7 @@ addarth(ILI *ilip)
26962696
#endif
26972697
#ifdef IL_FRSQRT
26982698
case IL_FRSQRT:
2699-
#if !defined(TARGET_LLVM_ARM)
2699+
#if defined(TARGET_X8664) || defined(TARGET_POWER)
27002700
if (XBIT(183, 0x10000)) {
27012701
if (ncons == 1) {
27022702
xfsqrt(con1v2, &res.numi[1]);
@@ -13214,7 +13214,7 @@ _xpowi(int opn, int pwr, ILI_OP opc)
1321413214
return opn;
1321513215
}
1321613216

13217-
#if defined(TARGET_X8664) || defined(TARGET_POWER) || !defined(TARGET_LLVM_ARM)
13217+
#if defined(TARGET_X8664) || defined(TARGET_POWER)
1321813218
static int
1321913219
_frsqrt(int x)
1322013220
{

tools/flang2/flang2exe/ll_structure.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ static const struct triple_info known_triples[] = {
464464
{"x86_64-", "e-p:64:64-i64:64-f80:128-n8:16:32:64-S128"},
465465
{"armv7-", "e-p:32:32-i64:64-v128:64:128-n32-S64"},
466466
{"aarch64-", "e-m:e-i64:64-i128:128-n32:64-S128"},
467+
{"riscv64-", "e-m:e-p:64:64-i64:64-i128:128-n64-S128"},
467468
{"powerpc64le", "e-p:64:64-i64:64-n32:64"},
468469
{"", ""}};
469470

tools/flang2/flang2exe/llutil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3782,7 +3782,7 @@ process_ll_abi_func_ftn_mod(LL_Module *mod, SPTR func_sptr, bool update)
37823782
"Unknown function prototype",
37833783
func_sptr, ERR_Fatal);
37843784
abi->missing_prototype = true;
3785-
#if defined(TARGET_ARM)
3785+
#if defined(TARGET_ARM) || defined(TARGET_RISCV)
37863786
abi->call_as_varargs = false;
37873787
#else
37883788
abi->call_as_varargs = true;

tools/flang2/flang2exe/machreg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ RGSETB rgsetb;
5656

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

59-
#if defined(TARGET_LLVM_ARM) || defined(TARGET_LLVM_POWER)
59+
#if defined(TARGET_LLVM_ARM) || defined(TARGET_LLVM_POWER) || defined(TARGET_LLVM_RISCV)
6060

6161
/* arguments passed in registers */
6262
int mr_arg_ir[MR_MAX_IREG_ARGS + 1];

0 commit comments

Comments
 (0)