Skip to content
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
13 changes: 7 additions & 6 deletions Sources/LLVM/Initialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import cllvm
#endif

/// Lazy static initializer that calls LLVM initialization functions only once.
let llvmInitializer: Void = {
initializeLLVM()
}()
/// initializer that calls LLVM initialization functions only once.
public func initializeLLVM() {
_ = llvmInitializer
}

/// Calls all the LLVM functions to initialize:
///
Expand All @@ -15,7 +15,7 @@ let llvmInitializer: Void = {
/// - ASM Parsers
/// - Target MCs
/// - Disassemblers
private func initializeLLVM() {
let llvmInitializer: Void = {
LLVMInitializeAllTargets()
LLVMInitializeAllTargetInfos()

Expand All @@ -25,4 +25,5 @@ private func initializeLLVM() {
LLVMInitializeAllTargetMCs()

LLVMInitializeAllDisassemblers()
}
}()

5 changes: 2 additions & 3 deletions Sources/LLVM/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ public final class Module: CustomStringConvertible {
/// context is provided, one will be inferred.
public init(name: String, context: Context? = nil) {

// Ensure the LLVM initializer is called when the first module
// is created
_ = llvmInitializer
// Ensure the LLVM initializer is called when the first module is created
initializeLLVM()

if let context = context {
llvm = LLVMModuleCreateWithNameInContext(name, context.llvm)
Expand Down
4 changes: 4 additions & 0 deletions Sources/LLVM/TargetMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public class TargetMachine {
public init(triple: String? = nil, cpu: String = "", features: String = "",
optLevel: CodeGenOptLevel = .default, relocMode: RelocMode = .default,
codeModel: CodeModel = .default) throws {

// Ensure the LLVM initializer is called when the first module is created
initializeLLVM()

self.triple = triple ?? String(cString: LLVMGetDefaultTargetTriple()!)
var target: LLVMTargetRef?
var error: UnsafeMutablePointer<Int8>?
Expand Down