Skip to content

[stdlib] Add RISCV64 support #61815

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 1 commit into from
Nov 2, 2022
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 cmake/modules/SwiftConfigureSDK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ macro(configure_sdk_unix name architectures)
set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabi")
elseif(arch MATCHES "(armv6|armv7)")
set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabihf")
elseif(arch MATCHES "(aarch64|i686|powerpc|powerpc64|powerpc64le|s390x|x86_64)")
elseif(arch MATCHES "(aarch64|i686|powerpc|powerpc64|powerpc64le|s390x|x86_64|riscv64)")
set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnu")
else()
message(FATAL_ERROR "unknown arch for ${prefix}: ${arch}")
Expand Down
3 changes: 2 additions & 1 deletion cmake/modules/SwiftSetIfArchBitness.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function(set_if_arch_bitness var_name)
"${SIA_ARCH}" STREQUAL "aarch64" OR
"${SIA_ARCH}" STREQUAL "powerpc64" OR
"${SIA_ARCH}" STREQUAL "powerpc64le" OR
"${SIA_ARCH}" STREQUAL "s390x")
"${SIA_ARCH}" STREQUAL "s390x" OR
"${SIA_ARCH}" STREQUAL "riscv64")
set("${var_name}" "${SIA_CASE_64_BIT}" PARENT_SCOPE)
else()
message(FATAL_ERROR "Unknown architecture: ${SIA_ARCH}")
Expand Down
5 changes: 5 additions & 0 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static const SupportedConditionalValue SupportedConditionalCompilationArches[] =
"powerpc64le",
"s390x",
"wasm32",
"riscv64",
};

static const SupportedConditionalValue SupportedConditionalCompilationEndianness[] = {
Expand Down Expand Up @@ -368,6 +369,9 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
case llvm::Triple::ArchType::wasm32:
addPlatformConditionValue(PlatformConditionKind::Arch, "wasm32");
break;
case llvm::Triple::ArchType::riscv64:
addPlatformConditionValue(PlatformConditionKind::Arch, "riscv64");
break;
default:
UnsupportedArch = true;
}
Expand All @@ -386,6 +390,7 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
case llvm::Triple::ArchType::wasm32:
case llvm::Triple::ArchType::x86:
case llvm::Triple::ArchType::x86_64:
case llvm::Triple::ArchType::riscv64:
addPlatformConditionValue(PlatformConditionKind::Endianness, "little");
break;
case llvm::Triple::ArchType::ppc64:
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/core/AtomicInt.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt(
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32(
target._rawValue, expected.pointee._value, desired._value)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int64(
target._rawValue, expected.pointee._value, desired._value)
#endif
Expand All @@ -85,7 +85,7 @@ func _swift_stdlib_atomicLoadInt(
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
let value = Builtin.atomicload_seqcst_Int32(target._rawValue)
return Int(value)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
let value = Builtin.atomicload_seqcst_Int64(target._rawValue)
return Int(value)
#endif
Expand All @@ -97,7 +97,7 @@ internal func _swift_stdlib_atomicStoreInt(
desired: Int) {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
Builtin.atomicstore_seqcst_Int64(target._rawValue, desired._value)
#endif
}
Expand All @@ -115,7 +115,7 @@ func _swift_stdlib_atomicFetch${operation}Int(
let value = _swift_stdlib_atomicFetch${operation}Int32(
object: rawTarget.assumingMemoryBound(to: Int32.self),
operand: Int32(operand))
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
let value = _swift_stdlib_atomicFetch${operation}Int64(
object: rawTarget.assumingMemoryBound(to: Int64.self),
operand: Int64(operand))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/runtime/HeapObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static inline bool isValidPointerForNativeRetain(const void *p) {
// Check the top of the second byte instead, since Android AArch64 reserves
// the top byte for its own pointer tagging since Android 11.
return (intptr_t)((uintptr_t)p << 8) > 0;
#elif defined(__x86_64__) || defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64) || defined(__s390x__) || (defined(__powerpc64__) && defined(__LITTLE_ENDIAN__))
#elif defined(__x86_64__) || defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64) || defined(__s390x__) || (defined(__riscv) && __riscv_xlen == 64) || (defined(__powerpc64__) && defined(__LITTLE_ENDIAN__))
// On these platforms, except s390x, the upper half of address space is reserved for the
// kernel, so we can assume that pointer values in this range are invalid.
// On s390x it is theoretically possible to have high bit set but in practice
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/stubs/MathStubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ extern "C" {
(defined(__linux__) && defined(__aarch64__)) || \
(defined(__linux__) && defined(__powerpc64__)) || \
(defined(__linux__) && defined(__s390x__)) || \
(defined(__linux__) && defined(__riscv) && __riscv_xlen == 64) || \
(defined(__ANDROID__) && defined(__aarch64__))

SWIFT_RUNTIME_STDLIB_API
Expand Down
2 changes: 1 addition & 1 deletion test/ClangImporter/ctypes_parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func testAnonEnum() {
verifyIsInt(&a)
#elseif arch(i386) || arch(arm) || arch(arm64_32)
verifyIsUInt64(&a)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
verifyIsUInt(&a)
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ protocol _CFObject: Hashable {}
public struct CGFloat {
#if arch(i386) || arch(arm) || arch(arm64_32)
public typealias UnderlyingType = Float
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
public typealias UnderlyingType = Double
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public func == (lhs: CGPoint, rhs: CGPoint) -> Bool {
public struct CGFloat {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(powerpc)
public typealias UnderlyingType = Float
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
public typealias UnderlyingType = Double
#endif

Expand Down
6 changes: 6 additions & 0 deletions test/Interpreter/builtin_bridge_object.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ let OBJC_TAGGED_POINTER_BITS: UInt = 0
let NATIVE_SPARE_BITS: UInt = 0x0000_0000_0000_0007
let OBJC_TAGGED_POINTER_BITS: UInt = 0

#elseif arch(riscv64)

// We have no ObjC tagged pointers, and three low spare bits due to alignment.
let NATIVE_SPARE_BITS: UInt = 0x0000_0000_0000_0007
let OBJC_TAGGED_POINTER_BITS: UInt = 0

#endif

func bitPattern(_ x: Builtin.BridgeObject) -> UInt {
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/Inputs/FloatingPointConversion.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ FloatingPointConversionFailures.test("${OtherFloat}To${Self}Conversion/AlwaysSuc

#if arch(i386) || arch(arm) || arch(arm64_32) || arch(powerpc)
% int_types = all_integer_types(32)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
% int_types = all_integer_types(64)
#else
_UnimplementedError()
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ var BitTwiddlingTestSuite = TestSuite("BitTwiddling")
BitTwiddlingTestSuite.test("_pointerSize") {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
expectEqual(4, MemoryLayout<Optional<AnyObject>>.size)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
expectEqual(8, MemoryLayout<Optional<AnyObject>>.size)
#else
fatalError("implement")
Expand All @@ -715,7 +715,7 @@ BitTwiddlingTestSuite.test("_isPowerOf2/Int") {
expectTrue(_isPowerOf2(asInt(1024)))
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32) || arch(powerpc)
// Not applicable to 32-bit architectures.
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
expectTrue(_isPowerOf2(asInt(0x8000_0000)))
#else
fatalError("implement")
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib/UnsafePointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ ${SelfName}TestSuite.test("customMirror") {
expectEqual(1, mirror.children.count)
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
expectEqual("18446744071562067968", String(describing: mirror.children.first!.1))
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
expectEqual("9223372036854775808", String(describing: mirror.children.first!.1))
#else
fatalError("Unimplemented")
Expand All @@ -424,7 +424,7 @@ ${SelfName}TestSuite.test("customPlaygroundQuickLook") {
if case let .text(desc) = ptr.customPlaygroundQuickLook {
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
expectEqual("${SelfName}(0xFFFFFFFF80000000)", desc)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
expectEqual("${SelfName}(0x8000000000000000)", desc)
#else
fatalError("Unimplemented")
Expand Down
4 changes: 2 additions & 2 deletions validation-test/stdlib/Bitset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ BitsetTests.test("_UnsafeBitset.wordCount(forCapacity:)") {
for i in 97...128 {
expectEqual(4, _UnsafeBitset.wordCount(forCapacity: i), "i=\(i)")
}
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
for i in 1...64 {
expectEqual(1, _UnsafeBitset.wordCount(forCapacity: i), "i=\(i)")
}
Expand Down Expand Up @@ -57,7 +57,7 @@ BitsetTests.test("_UnsafeBitset.split(_:)") {
expectEqual(3, comps.word, "i=\(i)")
expectEqual(i - 96, comps.bit, "i=\(i)")
}
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
for i in 0...63 {
let comps = _UnsafeBitset.split(i)
expectEqual(0, comps.word, "i=\(i)")
Expand Down
8 changes: 4 additions & 4 deletions validation-test/stdlib/FixedPoint.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ FixedPoint.test("Integers.Stride") {
expectEqualType(Int.self, ${Self}.Stride.self)
% end

#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)

% for self_ty in all_integer_types(64):
% Self = self_ty.stdlib_name
Expand Down Expand Up @@ -138,7 +138,7 @@ FixedPoint.test("${Dst}(truncatingIfNeeded:) from ${Src}(${bit_pattern})") {
test_bit_patterns=test_bit_patterns,
word_bits=32)}

#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)

${gyb.execute_template(
truncating_bit_pattern_test_template,
Expand Down Expand Up @@ -210,7 +210,7 @@ FixedPoint.test("${Dst}(bitPattern:) from ${Src}(${bit_pattern})") {
test_bit_patterns=test_bit_patterns,
word_bits=32)}

#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)

${gyb.execute_template(
bit_pattern_test_template,
Expand Down Expand Up @@ -275,7 +275,7 @@ FixedPoint.test("${Self}.hash(into:)") {
test_bit_patterns=test_bit_patterns,
word_bits=32)}

#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)

${gyb.execute_template(
hash_value_test_template,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ struct _${name}Bitmap {
% if underlyingType == 'UInt':
#if arch(i386) || arch(arm) || arch(arm64_32)
return Int(Builtin.int_ctpop_Int32(_bits._value))
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) || arch(riscv64)
return Int(Builtin.int_ctpop_Int64(_bits._value))
#endif
% elif underlyingType == 'UInt32':
Expand Down