Skip to content

Commit 76603e0

Browse files
committed
Update Context docs
1 parent cfd7854 commit 76603e0

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Sources/LLVM/Context.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#if SWIFT_PACKAGE
2+
import cllvm
3+
#endif
4+
5+
/// A `Context` is an LLVM compilation session environment.
6+
///
7+
/// A `Context` is a container for the global state of an execution of the
8+
/// LLVM environment and tooling. It contains independent copies of global and
9+
/// module-level entities like types, metadata attachments, and constants.
10+
///
11+
/// An LLVM context is needed for interacting with LLVM in a concurrent
12+
/// environment. Because a context maintains state independent of any other
13+
/// context, it is recommended that each thread of execution be assigned a unique
14+
/// context. LLVM's core infrastructure and API provides no locking guarantees
15+
/// and no atomicity guarantees.
16+
public class Context {
17+
internal let llvm: LLVMContextRef
18+
private let ownsContext: Bool
19+
20+
/// Retrieves the global context instance.
21+
///
22+
/// The global context is an particularly convenient instance managed by LLVM
23+
/// itself. It is the default context provided for any operations that
24+
/// require it.
25+
///
26+
/// - WARNING: Failure to specify the correct context in concurrent
27+
/// environments can lead to data corruption. In general, it is always
28+
/// recommended that each thread of execution attempting to access the LLVM
29+
/// API have its own `Context` instance, rather than rely on this global
30+
/// context.
31+
public static let global = Context(llvm: LLVMGetGlobalContext()!)
32+
33+
/// Creates a new `Context` object.
34+
public init() {
35+
llvm = LLVMContextCreate()
36+
ownsContext = true
37+
}
38+
39+
/// Creates a `Context` object from an `LLVMContextRef` object.
40+
public init(llvm: LLVMContextRef, ownsContext: Bool = false) {
41+
self.llvm = llvm
42+
self.ownsContext = ownsContext
43+
}
44+
45+
/// Returns whether the given context is set to discard all value names.
46+
///
47+
/// If true, only the names of GlobalValue objects will be available in
48+
/// the IR. This can be used to save memory and processing time, especially
49+
/// in release environments.
50+
public var discardValueNames: Bool {
51+
get { return LLVMContextShouldDiscardValueNames(self.llvm) != 0 }
52+
set { LLVMContextSetDiscardValueNames(self.llvm, newValue.llvm) }
53+
}
54+
55+
/// Deinitialize this value and dispose of its resources.
56+
deinit {
57+
guard self.ownsContext else {
58+
return
59+
}
60+
LLVMContextDispose(self.llvm)
61+
}
62+
}

0 commit comments

Comments
 (0)