Skip to content

Commit 72b29b8

Browse files
authored
Merge pull request #94 from vdka/public-llvm-initialize
Ensure LLVM is initialized for TargetMachine use
2 parents a202616 + 441585b commit 72b29b8

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

Sources/LLVM/Initialization.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import cllvm
33
#endif
44

5-
/// Lazy static initializer that calls LLVM initialization functions only once.
6-
let llvmInitializer: Void = {
7-
initializeLLVM()
8-
}()
5+
/// initializer that calls LLVM initialization functions only once.
6+
public func initializeLLVM() {
7+
_ = llvmInitializer
8+
}
99

1010
/// Calls all the LLVM functions to initialize:
1111
///
@@ -15,7 +15,7 @@ let llvmInitializer: Void = {
1515
/// - ASM Parsers
1616
/// - Target MCs
1717
/// - Disassemblers
18-
private func initializeLLVM() {
18+
let llvmInitializer: Void = {
1919
LLVMInitializeAllTargets()
2020
LLVMInitializeAllTargetInfos()
2121

@@ -25,4 +25,5 @@ private func initializeLLVM() {
2525
LLVMInitializeAllTargetMCs()
2626

2727
LLVMInitializeAllDisassemblers()
28-
}
28+
}()
29+

Sources/LLVM/Module.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ public final class Module: CustomStringConvertible {
5353
/// context is provided, one will be inferred.
5454
public init(name: String, context: Context? = nil) {
5555

56-
// Ensure the LLVM initializer is called when the first module
57-
// is created
58-
_ = llvmInitializer
56+
// Ensure the LLVM initializer is called when the first module is created
57+
initializeLLVM()
5958

6059
if let context = context {
6160
llvm = LLVMModuleCreateWithNameInContext(name, context.llvm)

Sources/LLVM/TargetMachine.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ public class TargetMachine {
136136
public init(triple: String? = nil, cpu: String = "", features: String = "",
137137
optLevel: CodeGenOptLevel = .default, relocMode: RelocMode = .default,
138138
codeModel: CodeModel = .default) throws {
139+
140+
// Ensure the LLVM initializer is called when the first module is created
141+
initializeLLVM()
142+
139143
self.triple = triple ?? String(cString: LLVMGetDefaultTargetTriple()!)
140144
var target: LLVMTargetRef?
141145
var error: UnsafeMutablePointer<Int8>?

0 commit comments

Comments
 (0)