Skip to content

Commit ce91cf3

Browse files
committed
[OpenMP][RISCV] Add riscv32 support
CMake flags and some core changes are added to support riscv32 platform basically. Fix #99427
1 parent 2f925d7 commit ce91cf3

13 files changed

+80
-51
lines changed

openmp/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Options for all Libraries
141141
Options for ``libomp``
142142
----------------------
143143

144-
**LIBOMP_ARCH** = ``aarch64|aarch64_32|arm|i386|loongarch64|mic|mips|mips64|ppc64|ppc64le|x86_64|riscv64|s390x``
144+
**LIBOMP_ARCH** = ``aarch64|aarch64_32|arm|i386|loongarch64|mic|mips|mips64|ppc64|ppc64le|x86_64|riscv32|riscv64|s390x``
145145
The default value for this option is chosen based on probing the compiler for
146146
architecture macros (e.g., is ``__x86_64__`` predefined by compiler?).
147147

@@ -198,7 +198,7 @@ Optional Features
198198
**LIBOMP_OMPT_SUPPORT** = ``ON|OFF``
199199
Include support for the OpenMP Tools Interface (OMPT).
200200
This option is supported and ``ON`` by default for x86, x86_64, AArch64,
201-
PPC64, RISCV64, LoongArch64, and s390x on Linux* and macOS*.
201+
PPC64, RISCV32, RISCV64, LoongArch64, and s390x on Linux* and macOS*.
202202
This option is ``OFF`` if this feature is not supported for the platform.
203203

204204
**LIBOMP_OMPT_OPTIONAL** = ``ON|OFF``

openmp/runtime/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if(${OPENMP_STANDALONE_BUILD})
3030
# If adding a new architecture, take a look at cmake/LibompGetArchitecture.cmake
3131
libomp_get_architecture(LIBOMP_DETECTED_ARCH)
3232
set(LIBOMP_ARCH ${LIBOMP_DETECTED_ARCH} CACHE STRING
33-
"The architecture to build for (x86_64/i386/arm/ppc/ppc64/ppc64le/aarch64/aarch64_32/mic/mips/mips64/riscv64/loongarch64/ve/s390x/wasm32).")
33+
"The architecture to build for (x86_64/i386/arm/ppc/ppc64/ppc64le/aarch64/aarch64_32/mic/mips/mips64/riscv32/riscv64/loongarch64/ve/s390x/wasm32).")
3434
# Should assertions be enabled? They are on by default.
3535
set(LIBOMP_ENABLE_ASSERTIONS TRUE CACHE BOOL
3636
"enable assertions?")
@@ -63,6 +63,8 @@ else() # Part of LLVM build
6363
set(LIBOMP_ARCH aarch64)
6464
elseif(LIBOMP_NATIVE_ARCH MATCHES "arm")
6565
set(LIBOMP_ARCH arm)
66+
elseif(LIBOMP_NATIVE_ARCH MATCHES "riscv32")
67+
set(LIBOMP_ARCH riscv32)
6668
elseif(LIBOMP_NATIVE_ARCH MATCHES "riscv64")
6769
set(LIBOMP_ARCH riscv64)
6870
elseif(LIBOMP_NATIVE_ARCH MATCHES "loongarch64")
@@ -93,7 +95,7 @@ if(LIBOMP_ARCH STREQUAL "aarch64")
9395
endif()
9496
endif()
9597

96-
libomp_check_variable(LIBOMP_ARCH 32e x86_64 32 i386 arm ppc ppc64 ppc64le aarch64 aarch64_32 aarch64_a64fx mic mips mips64 riscv64 loongarch64 ve s390x wasm32)
98+
libomp_check_variable(LIBOMP_ARCH 32e x86_64 32 i386 arm ppc ppc64 ppc64le aarch64 aarch64_32 aarch64_a64fx mic mips mips64 riscv32 riscv64 loongarch64 ve s390x wasm32)
9799

98100
set(LIBOMP_LIB_TYPE normal CACHE STRING
99101
"Performance,Profiling,Stubs library (normal/profile/stubs)")
@@ -180,6 +182,7 @@ set(PPC64 FALSE)
180182
set(MIC FALSE)
181183
set(MIPS64 FALSE)
182184
set(MIPS FALSE)
185+
set(RISCV32 FALSE)
183186
set(RISCV64 FALSE)
184187
set(LOONGARCH64 FALSE)
185188
set(VE FALSE)
@@ -212,6 +215,8 @@ elseif("${LIBOMP_ARCH}" STREQUAL "mips") # MIPS architecture
212215
set(MIPS TRUE)
213216
elseif("${LIBOMP_ARCH}" STREQUAL "mips64") # MIPS64 architecture
214217
set(MIPS64 TRUE)
218+
elseif("${LIBOMP_ARCH}" STREQUAL "riscv32") # RISCV32 architecture
219+
set(RISCV32 TRUE)
215220
elseif("${LIBOMP_ARCH}" STREQUAL "riscv64") # RISCV64 architecture
216221
set(RISCV64 TRUE)
217222
elseif("${LIBOMP_ARCH}" STREQUAL "loongarch64") # LoongArch64 architecture

openmp/runtime/README.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ Architectures Supported
5353
* IBM(R) Power architecture (big endian)
5454
* IBM(R) Power architecture (little endian)
5555
* MIPS and MIPS64 architecture
56-
* RISCV64 architecture
56+
* RISC-V 32 bits architecture
57+
* RISC-V 64 bits architecture
5758
* LoongArch64 architecture
5859

5960
Supported RTL Build Configurations

openmp/runtime/cmake/LibompGetArchitecture.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ function(libomp_get_architecture return_arch)
4949
#error ARCHITECTURE=mips64
5050
#elif defined(__mips__) && !defined(__mips64)
5151
#error ARCHITECTURE=mips
52+
#elif defined(__riscv) && __riscv_xlen == 32
53+
#error ARCHITECTURE=riscv32
5254
#elif defined(__riscv) && __riscv_xlen == 64
5355
#error ARCHITECTURE=riscv64
5456
#elif defined(__loongarch__) && __loongarch_grlen == 64

openmp/runtime/cmake/LibompMicroTests.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ else()
208208
elseif(${MIPS} OR ${MIPS64})
209209
libomp_append(libomp_expected_library_deps libc.so.6)
210210
libomp_append(libomp_expected_library_deps ld.so.1)
211-
elseif(${RISCV64})
211+
elseif(${RISCV32} OR ${RISCV64})
212212
libomp_append(libomp_expected_library_deps libc.so.6)
213213
libomp_append(libomp_expected_library_deps ld.so.1)
214214
elseif(${LOONGARCH64})

openmp/runtime/cmake/LibompUtils.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ function(libomp_get_legal_arch return_arch_string)
109109
set(${return_arch_string} "MIPS" PARENT_SCOPE)
110110
elseif(${MIPS64})
111111
set(${return_arch_string} "MIPS64" PARENT_SCOPE)
112+
elseif(${RISCV32})
113+
set(${return_arch_string} "RISCV32" PARENT_SCOPE)
112114
elseif(${RISCV64})
113115
set(${return_arch_string} "RISCV64" PARENT_SCOPE)
114116
elseif(${LOONGARCH64})

openmp/runtime/cmake/config-ix.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ else()
314314
(LIBOMP_ARCH STREQUAL aarch64_a64fx) OR
315315
(LIBOMP_ARCH STREQUAL ppc64le) OR
316316
(LIBOMP_ARCH STREQUAL ppc64) OR
317+
(LIBOMP_ARCH STREQUAL riscv32) OR
317318
(LIBOMP_ARCH STREQUAL riscv64) OR
318319
(LIBOMP_ARCH STREQUAL loongarch64) OR
319320
(LIBOMP_ARCH STREQUAL s390x))

openmp/runtime/src/kmp_affinity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class KMPHwlocAffinity : public KMPAffinity {
278278
#elif __NR_sched_getaffinity != 123
279279
#error Wrong code for getaffinity system call.
280280
#endif /* __NR_sched_getaffinity */
281-
#elif KMP_ARCH_RISCV64
281+
#elif KMP_ARCH_RISCV64 || KMP_ARCH_RISCV32
282282
#ifndef __NR_sched_setaffinity
283283
#define __NR_sched_setaffinity 122
284284
#elif __NR_sched_setaffinity != 122

openmp/runtime/src/kmp_os.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ typedef unsigned long long kmp_uint64;
179179
#endif /* KMP_OS_UNIX */
180180

181181
#if KMP_ARCH_X86 || KMP_ARCH_ARM || KMP_ARCH_MIPS || KMP_ARCH_WASM || \
182-
KMP_ARCH_PPC || KMP_ARCH_AARCH64_32
182+
KMP_ARCH_PPC || KMP_ARCH_AARCH64_32 || KMP_ARCH_RISCV32
183183
#define KMP_SIZE_T_SPEC KMP_UINT32_SPEC
184184
#elif KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 || \
185185
KMP_ARCH_MIPS64 || KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 || \
@@ -1050,8 +1050,9 @@ extern kmp_real64 __kmp_xchg_real64(volatile kmp_real64 *p, kmp_real64 v);
10501050
#endif /* KMP_OS_WINDOWS */
10511051

10521052
#if KMP_ARCH_PPC64 || KMP_ARCH_ARM || KMP_ARCH_AARCH64 || KMP_ARCH_MIPS || \
1053-
KMP_ARCH_MIPS64 || KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 || \
1054-
KMP_ARCH_VE || KMP_ARCH_S390X || KMP_ARCH_PPC || KMP_ARCH_AARCH64_32
1053+
KMP_ARCH_MIPS64 || KMP_ARCH_RISCV32 || KMP_ARCH_RISCV64 || \
1054+
KMP_ARCH_LOONGARCH64 || KMP_ARCH_VE || KMP_ARCH_S390X || KMP_ARCH_PPC || \
1055+
KMP_ARCH_AARCH64_32
10551056
#if KMP_OS_WINDOWS
10561057
#undef KMP_MB
10571058
#define KMP_MB() std::atomic_thread_fence(std::memory_order_seq_cst)

openmp/runtime/src/kmp_platform.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
#define KMP_ARCH_PPC_XCOFF 0
119119
#define KMP_ARCH_MIPS 0
120120
#define KMP_ARCH_MIPS64 0
121+
#define KMP_ARCH_RISCV32 0
121122
#define KMP_ARCH_RISCV64 0
122123
#define KMP_ARCH_LOONGARCH64 0
123124
#define KMP_ARCH_VE 0
@@ -178,6 +179,9 @@
178179
#undef KMP_ARCH_MIPS
179180
#define KMP_ARCH_MIPS 1
180181
#endif
182+
#elif defined __riscv && __riscv_xlen == 32
183+
#undef KMP_ARCH_RISCV32
184+
#define KMP_ARCH_RISCV32 1
181185
#elif defined __riscv && __riscv_xlen == 64
182186
#undef KMP_ARCH_RISCV64
183187
#define KMP_ARCH_RISCV64 1
@@ -263,8 +267,8 @@
263267
// TODO: Fixme - This is clever, but really fugly
264268
#if (1 != KMP_ARCH_X86 + KMP_ARCH_X86_64 + KMP_ARCH_ARM + KMP_ARCH_PPC64 + \
265269
KMP_ARCH_AARCH64 + KMP_ARCH_MIPS + KMP_ARCH_MIPS64 + \
266-
KMP_ARCH_RISCV64 + KMP_ARCH_LOONGARCH64 + KMP_ARCH_VE + \
267-
KMP_ARCH_S390X + KMP_ARCH_WASM + KMP_ARCH_PPC + \
270+
KMP_ARCH_RISCV32 + KMP_ARCH_RISCV64 + KMP_ARCH_LOONGARCH64 + \
271+
KMP_ARCH_VE + KMP_ARCH_S390X + KMP_ARCH_WASM + KMP_ARCH_PPC + \
268272
KMP_ARCH_AARCH64_32)
269273
#error Unknown or unsupported architecture
270274
#endif

openmp/runtime/src/kmp_runtime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8947,8 +8947,8 @@ __kmp_determine_reduction_method(
89478947
int atomic_available = FAST_REDUCTION_ATOMIC_METHOD_GENERATED;
89488948

89498949
#if KMP_ARCH_X86_64 || KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 || \
8950-
KMP_ARCH_MIPS64 || KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 || \
8951-
KMP_ARCH_VE || KMP_ARCH_S390X || KMP_ARCH_WASM
8950+
KMP_ARCH_MIPS64 || KMP_ARCH_RISCV32 || KMP_ARCH_RISCV64 || \
8951+
KMP_ARCH_LOONGARCH64 || KMP_ARCH_VE || KMP_ARCH_S390X || KMP_ARCH_WASM
89528952

89538953
#if KMP_OS_LINUX || KMP_OS_DRAGONFLY || KMP_OS_FREEBSD || KMP_OS_NETBSD || \
89548954
KMP_OS_OPENBSD || KMP_OS_WINDOWS || KMP_OS_DARWIN || KMP_OS_HURD || \

openmp/runtime/src/z_Linux_asm.S

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,17 @@ __kmp_invoke_microtask:
17881788

17891789
#endif /* KMP_ARCH_PPC64 */
17901790

1791-
#if KMP_ARCH_RISCV64
1791+
#if KMP_ARCH_RISCV32 || KMP_ARCH_RISCV64
1792+
1793+
#if KMP_ARCH_RISCV32
1794+
#define REG_L lw
1795+
#define REG_S sw
1796+
#define SZREG 4
1797+
#else
1798+
#define REG_L ld
1799+
#define REG_S sd
1800+
#define SZREG 8
1801+
#endif
17921802

17931803
//------------------------------------------------------------------------
17941804
//
@@ -1846,24 +1856,24 @@ __kmp_invoke_microtask:
18461856
.cfi_startproc
18471857

18481858
// First, save ra and fp
1849-
addi sp, sp, -16
1850-
sd ra, 8(sp)
1851-
sd fp, 0(sp)
1852-
addi fp, sp, 16
1859+
addi sp, sp, -2*SZREG
1860+
REG_S ra, 1*SZREG(sp)
1861+
REG_S fp, 0*SZREG(sp)
1862+
addi fp, sp, 2*SZREG
18531863
.cfi_def_cfa fp, 0
1854-
.cfi_offset ra, -8
1855-
.cfi_offset fp, -16
1864+
.cfi_offset ra, -1*SZREG
1865+
.cfi_offset fp, -2*SZREG
18561866

18571867
// Compute the dynamic stack size:
18581868
//
18591869
// - We need 8 bytes for storing 'gtid' and 'tid', so we can pass them by
18601870
// reference
1861-
// - We need 8 bytes for each argument that cannot be passed to the 'pkfn'
1862-
// function by register. Given that we have 8 of such registers (a[0-7])
1863-
// and two + 'argc' arguments (consider &gtid and &tid), we need to
1864-
// reserve max(0, argc - 6)*8 extra bytes
1871+
// - We need XLenBytes (4 for riscv32 and 8 for riscv64) bytes for each argument
1872+
// that cannot be passed to the 'pkfn' function by register. Given that we have
1873+
// 8 of such registers (a[0-7]) and two + 'argc' arguments (consider &gtid and
1874+
// &tid), we need to reserve max(0, argc - 6)*XLenBytes extra bytes
18651875
//
1866-
// The total number of bytes is then max(0, argc - 6)*8 + 8
1876+
// The total number of bytes is then max(0, argc - 6)*XLenBytes + 8
18671877

18681878
// Compute max(0, argc - 6) using the following bithack:
18691879
// max(0, x) = x - (x & (x >> 31)), where x := argc - 6
@@ -1874,8 +1884,11 @@ __kmp_invoke_microtask:
18741884
sub t0, t0, t1
18751885

18761886
addi t0, t0, 1
1877-
1887+
#if KMP_ARCH_RISCV32
1888+
slli t0, t0, 2
1889+
#else
18781890
slli t0, t0, 3
1891+
#endif
18791892
sub sp, sp, t0
18801893

18811894
// Align the stack to 16 bytes
@@ -1887,7 +1900,7 @@ __kmp_invoke_microtask:
18871900

18881901
#if OMPT_SUPPORT
18891902
// Save frame pointer into exit_frame
1890-
sd fp, 0(a5)
1903+
REG_S fp, 0(a5)
18911904
#endif
18921905

18931906
// Prepare arguments for the pkfn function (first 8 using a0-a7 registers)
@@ -1899,37 +1912,37 @@ __kmp_invoke_microtask:
18991912
addi a1, fp, __tid
19001913

19011914
beqz t3, .L_kmp_3
1902-
ld a2, 0(t4)
1915+
REG_L a2, 0*SZREG(t4)
19031916

19041917
addi t3, t3, -1
19051918
beqz t3, .L_kmp_3
1906-
ld a3, 8(t4)
1919+
REG_L a3, 1*SZREG(t4)
19071920

19081921
addi t3, t3, -1
19091922
beqz t3, .L_kmp_3
1910-
ld a4, 16(t4)
1923+
REG_L a4, 2*SZREG(t4)
19111924

19121925
addi t3, t3, -1
19131926
beqz t3, .L_kmp_3
1914-
ld a5, 24(t4)
1927+
REG_L a5, 3*SZREG(t4)
19151928

19161929
addi t3, t3, -1
19171930
beqz t3, .L_kmp_3
1918-
ld a6, 32(t4)
1931+
REG_L a6, 4*SZREG(t4)
19191932

19201933
addi t3, t3, -1
19211934
beqz t3, .L_kmp_3
1922-
ld a7, 40(t4)
1935+
REG_L a7, 5*SZREG(t4)
19231936

19241937
// Prepare any additional argument passed through the stack
1925-
addi t4, t4, 48
1938+
addi t4, t4, 6*SZREG
19261939
mv t1, sp
19271940
j .L_kmp_2
19281941
.L_kmp_1:
1929-
ld t2, 0(t4)
1930-
sd t2, 0(t1)
1931-
addi t4, t4, 8
1932-
addi t1, t1, 8
1942+
REG_L t2, 0(t4)
1943+
REG_S t2, 0(t1)
1944+
addi t4, t4, SZREG
1945+
addi t1, t1, SZREG
19331946
.L_kmp_2:
19341947
addi t3, t3, -1
19351948
bnez t3, .L_kmp_1
@@ -1942,18 +1955,18 @@ __kmp_invoke_microtask:
19421955

19431956
addi a0, zero, 1
19441957

1945-
addi sp, fp, -16
1946-
ld fp, 0(sp)
1947-
ld ra, 8(sp)
1948-
addi sp, sp, 16
1958+
addi sp, fp, -2*SZREG
1959+
REG_L fp, 0*SZREG(sp)
1960+
REG_L ra, 1*SZREG(sp)
1961+
addi sp, sp, 2*SZREG
19491962
ret
19501963
.Lfunc_end0:
19511964
.size __kmp_invoke_microtask, .Lfunc_end0-__kmp_invoke_microtask
19521965
.cfi_endproc
19531966

19541967
// -- End __kmp_invoke_microtask
19551968

1956-
#endif /* KMP_ARCH_RISCV64 */
1969+
#endif /* KMP_ARCH_RISCV32 || KMP_ARCH_RISCV64 */
19571970

19581971
#if KMP_ARCH_LOONGARCH64
19591972

@@ -2475,8 +2488,8 @@ KMP_PREFIX_UNDERSCORE(__kmp_unnamed_critical_addr):
24752488
#endif /* KMP_ARCH_ARM || KMP_ARCH_MIPS || KMP_ARCH_AARCH64_32 */
24762489

24772490
#if KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 || KMP_ARCH_MIPS64 || \
2478-
KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 || KMP_ARCH_VE || \
2479-
KMP_ARCH_S390X
2491+
KMP_ARCH_RISCV32 || KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 || \
2492+
KMP_ARCH_VE || KMP_ARCH_S390X
24802493
#ifndef KMP_PREFIX_UNDERSCORE
24812494
# define KMP_PREFIX_UNDERSCORE(x) x
24822495
#endif
@@ -2491,8 +2504,8 @@ KMP_PREFIX_UNDERSCORE(__kmp_unnamed_critical_addr):
24912504
.size KMP_PREFIX_UNDERSCORE(__kmp_unnamed_critical_addr),8
24922505
#endif
24932506
#endif /* KMP_ARCH_PPC64 || KMP_ARCH_AARCH64 || KMP_ARCH_MIPS64 ||
2494-
KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 || KMP_ARCH_VE ||
2495-
KMP_ARCH_S390X */
2507+
KMP_ARCH_RISCV32 || KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 ||
2508+
KMP_ARCH_VE || KMP_ARCH_S390X */
24962509

24972510
#if KMP_OS_LINUX
24982511
# if KMP_ARCH_ARM || KMP_ARCH_AARCH64

openmp/runtime/src/z_Linux_util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,9 +2743,9 @@ int __kmp_get_load_balance(int max) {
27432743

27442744
#if !(KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_MIC || \
27452745
((KMP_OS_LINUX || KMP_OS_DARWIN) && KMP_ARCH_AARCH64) || \
2746-
KMP_ARCH_PPC64 || KMP_ARCH_RISCV64 || KMP_ARCH_LOONGARCH64 || \
2747-
KMP_ARCH_ARM || KMP_ARCH_VE || KMP_ARCH_S390X || KMP_ARCH_PPC_XCOFF || \
2748-
KMP_ARCH_AARCH64_32)
2746+
KMP_ARCH_PPC64 || KMP_ARCH_RISCV32 || KMP_ARCH_RISCV64 || \
2747+
KMP_ARCH_LOONGARCH64 || KMP_ARCH_ARM || KMP_ARCH_VE || KMP_ARCH_S390X || \
2748+
KMP_ARCH_PPC_XCOFF || KMP_ARCH_AARCH64_32)
27492749

27502750
// Because WebAssembly will use `call_indirect` to invoke the microtask and
27512751
// WebAssembly indirect calls check that the called signature is a precise

0 commit comments

Comments
 (0)