From 708cd664fc6b4e42ebead26ad7e5079cada023b3 Mon Sep 17 00:00:00 2001 From: Lucian Radu Teodorescu Date: Mon, 2 Dec 2024 16:54:33 +0200 Subject: [PATCH] Add intrinsic support for atomic store and load. Also add `void` intrinsic type. --- Sources/CodeGen/LLVM/ConcreteTypeLayout.swift | 2 ++ Sources/CodeGen/LLVM/TypeLowering.swift | 2 ++ Sources/FrontEnd/NativeInstruction.swift | 36 +++++++++++++++++++ Sources/FrontEnd/Types/BuiltinType.swift | 7 ++++ Sources/IR/Mangling/Demangler.swift | 2 ++ Sources/IR/Mangling/Mangler.swift | 2 ++ Sources/IR/Mangling/ManglingOperator.swift | 2 ++ 7 files changed, 53 insertions(+) diff --git a/Sources/CodeGen/LLVM/ConcreteTypeLayout.swift b/Sources/CodeGen/LLVM/ConcreteTypeLayout.swift index 3be0fa122..ea21b848f 100644 --- a/Sources/CodeGen/LLVM/ConcreteTypeLayout.swift +++ b/Sources/CodeGen/LLVM/ConcreteTypeLayout.swift @@ -71,6 +71,8 @@ struct ConcreteTypeLayout { self.init(size: 8, alignment: 8) case .float128: self.init(size: 16, alignment: 8) + case .void: + notLLVMRepresentable(^t) // Cannot be used in a type layout. case .module: notLLVMRepresentable(^t) } diff --git a/Sources/CodeGen/LLVM/TypeLowering.swift b/Sources/CodeGen/LLVM/TypeLowering.swift index 2af3bacf4..797ebb529 100644 --- a/Sources/CodeGen/LLVM/TypeLowering.swift +++ b/Sources/CodeGen/LLVM/TypeLowering.swift @@ -74,6 +74,8 @@ extension IR.Program { return SwiftyLLVM.FloatingPointType.fp128(in: &module) case .ptr: return module.ptr + case .void: + return SwiftyLLVM.VoidType(in: &module) case .module: notLLVMRepresentable(t) } diff --git a/Sources/FrontEnd/NativeInstruction.swift b/Sources/FrontEnd/NativeInstruction.swift index 5435e70ab..505d56e19 100644 --- a/Sources/FrontEnd/NativeInstruction.swift +++ b/Sources/FrontEnd/NativeInstruction.swift @@ -118,6 +118,18 @@ public enum NativeInstruction: Hashable { // Corresponding LLVM instruction: get_elementptr_inbounds. case advancedByBytes(byteOffset: BuiltinType) + case atomic_store_relaxed(BuiltinType) + + case atomic_store_release(BuiltinType) + + case atomic_store_seqcst(BuiltinType) + + case atomic_load_relaxed(BuiltinType) + + case atomic_load_acquire(BuiltinType) + + case atomic_load_seqcst(BuiltinType) + /// The parameters of a floating-point LLVM instruction. public struct MathFlags: OptionSet, Hashable { @@ -238,6 +250,18 @@ extension NativeInstruction { return .init(to: ^t) case .advancedByBytes(let byteOffset): return .init(.builtin(.ptr), ^byteOffset, to: .builtin(.ptr)) + case .atomic_store_relaxed(let t): + return .init(.builtin(.ptr), ^t, to: .builtin(.void)) + case .atomic_store_release(let t): + return .init(.builtin(.ptr), ^t, to: .builtin(.void)) + case .atomic_store_seqcst(let t): + return .init(.builtin(.ptr), ^t, to: .builtin(.void)) + case .atomic_load_relaxed(let t): + return .init(.builtin(.ptr), to: ^t) + case .atomic_load_acquire(let t): + return .init(.builtin(.ptr), to: ^t) + case .atomic_load_seqcst(let t): + return .init(.builtin(.ptr), to: ^t) } } @@ -331,6 +355,18 @@ extension NativeInstruction: CustomStringConvertible { return "zeroinitializer_\(t)" case .advancedByBytes(let t): return "advanced_by_bytes_\(t)" + case .atomic_store_relaxed(let t): + return "atomic_store_relaxed_\(t)" + case .atomic_store_release(let t): + return "atomic_store_release_\(t)" + case .atomic_store_seqcst(let t): + return "atomic_store_seqcst_\(t)" + case .atomic_load_relaxed(let t): + return "atomic_load_relaxed_\(t)" + case .atomic_load_acquire(let t): + return "atomic_load_acquire_\(t)" + case .atomic_load_seqcst(let t): + return "atomic_load_seqcst_\(t)" } } diff --git a/Sources/FrontEnd/Types/BuiltinType.swift b/Sources/FrontEnd/Types/BuiltinType.swift index 95581f214..e5ae0dc16 100644 --- a/Sources/FrontEnd/Types/BuiltinType.swift +++ b/Sources/FrontEnd/Types/BuiltinType.swift @@ -25,6 +25,9 @@ public enum BuiltinType: TypeProtocol { /// A built-in opaque pointer. case ptr + /// A built-in void type. + case void + /// The type of the built-in module. case module @@ -63,6 +66,8 @@ extension BuiltinType: CustomStringConvertible { return "float128" case .ptr: return "ptr" + case .void: + return "void" case .module: return "Builtin" } @@ -86,6 +91,8 @@ extension BuiltinType: LosslessStringConvertible { self = .float128 case "ptr": self = .ptr + case "void": + self = .void case "Builtin": self = .module diff --git a/Sources/IR/Mangling/Demangler.swift b/Sources/IR/Mangling/Demangler.swift index 47a4a44c8..c32a6be69 100644 --- a/Sources/IR/Mangling/Demangler.swift +++ b/Sources/IR/Mangling/Demangler.swift @@ -42,6 +42,8 @@ struct Demangler { demangled = takeBuiltinFloatType(from: &stream) case .builtinPointerType: demangled = .type(.builtin(.ptr)) + case .builtinVoidType: + demangled = .type(.builtin(.void)) case .builtinModuleType: demangled = .type(.builtin(.module)) case .builtinWordType: diff --git a/Sources/IR/Mangling/Mangler.swift b/Sources/IR/Mangling/Mangler.swift index 8715d5a6d..9609fcdb7 100644 --- a/Sources/IR/Mangling/Mangler.swift +++ b/Sources/IR/Mangling/Mangler.swift @@ -560,6 +560,8 @@ struct Mangler { append(integer: 128, to: &output) case .ptr: append(operator: .builtinPointerType, to: &output) + case .void: + append(operator: .builtinVoidType, to: &output) case .module: append(operator: .builtinModuleType, to: &output) } diff --git a/Sources/IR/Mangling/ManglingOperator.swift b/Sources/IR/Mangling/ManglingOperator.swift index 337ba7417..127324a44 100644 --- a/Sources/IR/Mangling/ManglingOperator.swift +++ b/Sources/IR/Mangling/ManglingOperator.swift @@ -72,6 +72,8 @@ public enum ManglingOperator: String { case builtinPointerType = "bpT" + case builtinVoidType = "bvT" + case builtinModuleType = "bmT" case builtinWordType = "bwT"