10000 Wrap LLVMAttributeRef API · llvm-swift/LLVMSwift@5a4d51c · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a4d51c

Browse files
committed
Wrap LLVMAttributeRef API
1 parent adc94d2 commit 5a4d51c

File tree

1 file changed

+262
-0
lines changed

1 file changed

+262
-0
lines changed
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
#if SWIFT_PACKAGE
2+
import cllvm
3+
#endif
4+
5+
/// Enumerates the attributes of LLVM functions.
6+
public enum FunctionAttribute: String {
7+
/// This attribute indicates that, when emitting the prologue and epilogue,
8+
/// the backend should forcibly align the stack pointer.
9+
case alignstack
10+
/// This attribute indicates that the annotated function will always return
11+
/// at least a given number of bytes (or null).
12+
case allocsize
13+
/// This attribute indicates that the inliner should attempt to inline this
14+
/// function into callers whenever possible, ignoring any active inlining
15+
/// size threshold for this caller.
16+
case alwaysinline
17+
/// This indicates that the callee function at a call site should be
18+
/// recognized as a built-in function, even though the function’s declaration
19+
/// uses the nobuiltin attribute
20+
case builtin
21+
/// This attribute indicates that this function is rarely called.
22+
case cold
23+
/// In some parallel execution models, there exist operations that cannot be
24+
/// made control-dependent on any additional values.
25+
case convergent
26+
/// This attribute indicates that the function may only access memory that is
27+
/// not accessible by the module being compiled.
28+
case inaccessiblememonly
29+
/// This attribute indicates that the function may only access memory that is
30+
/// either not accessible by the module being compiled, or is pointed to by
31+
/// its pointer arguments.
32+
case inaccessiblemem_or_argmemonly
33+
/// This attribute indicates that the source code contained a hint that inlin
34+
/// inlining this function is desirable (such as the “inline” keyword in
35+
/// C/C++).
36+
case inlinehint
37+
/// This attribute indicates that the function should be added to a
38+
/// jump-instruction table at code-generation time, and that all
39+
/// address-taken references to this function should be replaced with a
40+
/// reference to the appropriate jump-instruction-table function pointer.
41+
case jumptable
42+
/// This attribute suggests that optimization passes and code generator
43+
/// passes make choices that keep the code size of this function as small as
44+
/// possible and perform optimizations that may sacrifice runtime performance
45+
/// in order to minimize the size of the generated code.
46+
case minsize
47+
/// This attribute disables prologue / epilogue emission for the function.
48+
case naked
49+
/// When this attribute is set to true, the jump tables and lookup tables
50+
/// that can be generated from a switch case lowering are disabled.
51+
case no_jump_tables = "no-jump-tables"
52+
/// This indicates that the callee function at a call site is not recognized
53+
/// as a built-in function.
54+
case nobuiltin
55+
/// This attribute indicates that calls to the function cannot be duplicated.
56+
case noduplicate
57+
/// This attributes disables implicit floating point instructions.
58+
case noimplicitfloat
59+
/// This attribute indicates that the inliner should never inline this
60+
/// function in any situation.
61+
case noinline
62+
/// This attribute suppresses lazy symbol binding for the function.
63+
case nonlazybind
64+
/// This attribute indicates that the code generator should not use a red
65+
/// zone, even if the target-specific ABI normally permits it.
66+
case noredzone
67+
/// This function attribute indicates that the function never returns
68+
/// normally.
69+
case noreturn
70+
/// This function attribute indicates that the function does not call itself
71+
/// either directly or indirectly down any possible call path.
72+
case norecurse
73+
/// This function attribute indicates that the function never raises an
74+
/// exception.
75+
case nounwind
76+
/// This function attribute indicates that most optimization passes will skip
77+
/// this function, with the exception of interprocedural optimization passes.
78+
case optnone
79+
/// This attribute suggests that optimization passes and code generator
80+
/// passes make choices that keep the code size of this function low, and
81+
/// otherwise do optimizations specifically to reduce code size as long as
82+
/// they do not significantly impact runtime performance.
83+
case optsize
84+
/// This attribute tells the code generator that the code generated for this
85+
/// function needs to follow certain conventions that make it possible for a
86+
/// runtime function to patch over it later.
87+
case patchable_function = "patchable-function"
88+
/// This attribute indicates that the function will trigger a guard region in
89+
/// the end of the stack.
90+
case probe_stack = "probe-stack"
91+
/// This attribute indicates that the function computes its result (or
92+
/// decides to unwind an exception) based strictly on its arguments, without
93+
/// dereferencing any pointer arguments or otherwise accessing any mutable
94+
/// state (e.g. memory, control registers, etc) visible to caller functions.
95+
case readnone
96+
/// This attribute indicates that the function does not write through any
97+
/// pointer arguments (including byval arguments) or otherwise modify any
98+
/// state (e.g. memory, control registers, etc) visible to caller functions.
99+
case readonly
100+
/// This attribute controls the behavior of stack probes: either the
101+
/// "probe-stack" attribute, or ABI-required stack probes, if any.
102+
case stack_proble_size = "stack-probe-size"
103+
/// This attribute disables ABI-required stack probes, if any.
104+
case no_stack_arg_probe = "no-stack-arg-probe"
105+
/// This attribute indicates that the function may write to but does not read
106+
/// read from memory.
107+
case writeonly
108+
/// This attribute indicates that the only memory accesses inside function
109+
/// are loads and stores from objects pointed to by its pointer-typed
110+
/// arguments, with arbitrary offsets.
111+
case argmemonly
112+
/// This attribute indicates that this function can return twice.
113+
case returns_twice
114+
/// This attribute indicates that SafeStack protection is enabled for this
115+
/// function.
116+
case safestack
117+
/// This attribute indicates that AddressSanitizer checks (dynamic address
118+
/// safety analysis) are enabled for this function.
119+
case sanitize_address
120+
/// This attribute indicates that MemorySanitizer checks (dynamic detection
121+
/// of accesses to uninitialized memory) are enabled for this function.
122+
case sanitize_memory
123+
/// This attribute indicates that ThreadSanitizer checks (dynamic thread
124+
/// safety analysis) are enabled for this function.
125+
case sanitize_thread
126+
/// This attribute indicates that HWAddressSanitizer checks (dynamic address
127+
/// safety analysis based on tagged pointers) are enabled for this function.
128+
case sanitize_hwaddress
129+
/// This function attribute indicates that the function does not have any
130+
/// effects besides calculating its result and does not have undefined
131+
/// behavior.
132+
case speculatable
133+
/// This attribute indicates that the function should emit a stack smashing
134+
/// protector.
135+
case ssp
136+
/// This attribute indicates that the function should always emit a stack
137+
/// smashing protector
138+
case sspreq
139+
/// This attribute indicates that the function should emit a stack smashing
140+
/// protector.
141+
case sspstrong
142+
/// This attribute indicates that the function was called from a scope that
143+
/// requires strict floating point semantics.
144+
case strictfp
145+
/// This attribute indicates that the function will delegate to some other
146+
/// function with a tail call.
147+
case thunk
148+
/// This attribute indicates that the ABI being targeted requires that an
149+
/// unwind table entry be produced for this function even if we can show
150+
/// that no exceptions passes by it.
151+
case uwtable
152+
153+
/// ID of the attribute.
154+
internal var kindID: UInt32 {
155+
return LLVMGetEnumAttributeKindForName(rawValue, rawValue.count)
156+
}
157+
}
158+
159+
/// Enumerates the parameter attributes of LLVM functions.
160+
public enum ParameterAttribute: String {
161+
/// This indicates to the code generator that the parameter or return value
162+
/// should be zero-extended to the extent required by the target’s ABI by the
163+
/// caller (for a parameter) or the callee (for a return value).
164+
case zeroext
165+
/// This indicates to the code generator that the parameter or return value
166+
/// should be sign-extended to the extent required by the target’s ABI (which
167+
/// is usually 32-bits) by the caller (for a parameter) or the callee (for a
168+
/// return value).
169+
case signext
170+
/// This indicates that this parameter or return value should be treated in a
171+
/// special target-dependent fashion while emitting code for a function call
172+
/// or return.
173+
case inreg
174+
/// This indicates that the pointer parameter should really be passed by
175+
/// value to the function.
176+
case byval
177+
/// The inalloca argument attribute allows the caller to take the address of
178+
/// outgoing stack arguments
179+
case inalloca
180+
/// This indicates that the pointer parameter specifies the address of a
181+
/// structure that is the return value of the function in the source program.
182+
case sret
183+
/// This indicates that the pointer value may be assumed by the optimizer to
184+
/// have the specified alignment.
185+
case align
186+
/// This indicates that objects accessed via pointer values based on the
187+
/// argument or return value are not also accessed, during the execution of
188+
/// the function, via pointer values not based on the argument or return
189+
/// value.
190+
case noalias
191+
/// This indicates that the callee does not make any copies of the pointer
192+
/// that outlive the callee itself.
193+
case nocapture
194+
/// This indicates that the pointer parameter can be excised using the
195+
/// trampoline intrinsics.
196+
case nest
197+
/// This indicates that the function always returns the argument as its
198+
/// return value.
199+
case returned
200+
/// This indicates that the parameter or return pointer is not null.
201+
case nonnull
202+
/// This indicates that the parameter or return pointer is dereferenceable.
203+
case dereferenceable
204+
/// This indicates that the parameter or return value isn’t both non-null and
205+
/// non-dereferenceable (up to `n` bytes) at the same time.
206+
case dereferenceable_or_null
207+
/// This indicates that the parameter is the self/context parameter.
208+
case swiftself
209+
/// This attribute is motivated to model and optimize Swift error handling.
210+
case swifterror
211+
212+
/// ID of the attribute.
213+
internal var kindID: UInt32 {
214+
return LLVMGetEnumAttributeKindForName(rawValue, rawValue.count)
215+
}
216+
}
217+
218+
extension Function {
219+
/// Adds an attribute to the function.
220+
///
221+
/// - parameter attr: The attribute to add.
222+
/// - parameter value: The optional value of the attribute.
223+
public func addAttribute(_ attr: FunctionAttribute, value: UInt64 = 0) {
224+
let ctx = LLVMGetModuleContext(LLVMGetGlobalParent(llvm))
225+
let attrRef = LLVMCreateEnumAttribute(ctx, attr.kindID, value)
226+
LLVMAddAttributeAtIndex(llvm, 0, attrRef)
227+
}
228+
229+
/// Adds an attribute to the given function's parameter.
230+
///
231+
/// - parameter attr: The attribute to add.
232+
/// - parameter value: The optional value of the attribute.
233+
/// - parameterIndex: The index of the parameter to which add the
234+
/// attribute, starting from 0.
235+
public func addParameterAttribute(_ attr: ParameterAttribute, value: UInt64 = 0,
236+
for parameterIndex: LLVMAttributeIndex) {
237+
let ctx = LLVMGetModuleContext(LLVMGetGlobalParent(llvm))
238+
let attrRef = LLVMCreateEnumAttribute(ctx, attr.kindID, value)
239+
LLVMAddAttributeAtIndex(llvm, parameterIndex + 1, attrRef)
240+
}
241+
242+
/// Removes an attribute from the function.
243+
///
244+
/// - parameter attr: The attribute to remove.
245+
public func removeAttribute(_ attr: FunctionAttribute, value: UInt64 = 0) {
246+
LLVMRemoveEnumAttributeAtIndex(llvm, 0, attr.kindID)
247+
}
248+
249+
/// Removes an attribute from the function.
250+
///
251+
/// - parameter attr: The attribute to add.
252+
253+
/// Removes an attribute from the given function's parameter.
254+
///
255+
/// - parameter attr: The attribute to remove.
256+
/// - parameter parameterIndex: The index of the parameter to which add the
257+
/// attribute, starting from 0.
258+
public func removeParameterAttribute(_ attr: ParameterAttribute,
259+
for parameterIndex: LLVMAttributeIndex) {
260+
LLVMRemoveEnumAttributeAtIndex(llvm, parameterIndex + 1, attr.kindID)
261+
}
262+
}

0 commit comments

Comments
 (0)
0