1#![allow(non_snake_case)]
2
3use std::ffi::{CStr, CString};
4use std::num::NonZero;
5use std::ptr;
6use std::string::FromUtf8Error;
7
8use libc::c_uint;
9use rustc_abi::{AddressSpace, Align, Size, WrappingRange};
10use rustc_llvm::RustString;
11
12pub(crate) use self::CallConv::*;
13pub(crate) use self::CodeGenOptSize::*;
14pub(crate) use self::conversions::*;
15pub(crate) use self::ffi::*;
16pub(crate) use self::metadata_kind::*;
17use crate::common::AsCCharPtr;
18
19mod conversions;
20pub(crate) mod diagnostic;
21pub(crate) mod enzyme_ffi;
22mod ffi;
23mod metadata_kind;
24
25pub(crate) use self::enzyme_ffi::*;
26
27impl LLVMRustResult {
28 pub(crate) fn into_result(self) -> Result<(), ()> {
29 match self {
30 LLVMRustResult::Success => Ok(()),
31 LLVMRustResult::Failure => Err(()),
32 }
33 }
34}
35
36pub(crate) fn AddFunctionAttributes<'ll>(
37 llfn: &'ll Value,
38 idx: AttributePlace,
39 attrs: &[&'ll Attribute],
40) {
41 unsafe {
42 LLVMRustAddFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len());
43 }
44}
45
46pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: &str) -> bool {
47 unsafe { LLVMRustHasFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
48}
49
50pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: &str) {
51 unsafe { LLVMRustRemoveFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
52}
53
54pub(crate) fn AddCallSiteAttributes<'ll>(
55 callsite: &'ll Value,
56 idx: AttributePlace,
57 attrs: &[&'ll Attribute],
58) {
59 unsafe {
60 LLVMRustAddCallSiteAttributes(callsite, idx.as_uint(), attrs.as_ptr(), attrs.len());
61 }
62}
63
64pub(crate) fn CreateAttrStringValue<'ll>(
65 llcx: &'ll Context,
66 attr: &str,
67 value: &str,
68) -> &'ll Attribute {
69 unsafe {
70 LLVMCreateStringAttribute(
71 llcx,
72 attr.as_c_char_ptr(),
73 attr.len().try_into().unwrap(),
74 value.as_c_char_ptr(),
75 value.len().try_into().unwrap(),
76 )
77 }
78}
79
80pub(crate) fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attribute {
81 unsafe {
82 LLVMCreateStringAttribute(
83 llcx,
84 attr.as_c_char_ptr(),
85 attr.len().try_into().unwrap(),
86 std::ptr::null(),
87 0,
88 )
89 }
90}
91
92pub(crate) fn CreateAlignmentAttr(llcx: &Context, bytes: u64) -> &Attribute {
93 unsafe { LLVMRustCreateAlignmentAttr(llcx, bytes) }
94}
95
96pub(crate) fn CreateDereferenceableAttr(llcx: &Context, bytes: u64) -> &Attribute {
97 unsafe { LLVMRustCreateDereferenceableAttr(llcx, bytes) }
98}
99
100pub(crate) fn CreateDereferenceableOrNullAttr(llcx: &Context, bytes: u64) -> &Attribute {
101 unsafe { LLVMRustCreateDereferenceableOrNullAttr(llcx, bytes) }
102}
103
104pub(crate) fn CreateByValAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
105 unsafe { LLVMRustCreateByValAttr(llcx, ty) }
106}
107
108pub(crate) fn CreateStructRetAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
109 unsafe { LLVMRustCreateStructRetAttr(llcx, ty) }
110}
111
112pub(crate) fn CreateUWTableAttr(llcx: &Context, async_: bool) -> &Attribute {
113 unsafe { LLVMRustCreateUWTableAttr(llcx, async_) }
114}
115
116pub(crate) fn CreateAllocSizeAttr(llcx: &Context, size_arg: u32) -> &Attribute {
117 unsafe { LLVMRustCreateAllocSizeAttr(llcx, size_arg) }
118}
119
120pub(crate) fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> &Attribute {
121 unsafe { LLVMRustCreateAllocKindAttr(llcx, kind_arg.bits()) }
122}
123
124pub(crate) fn CreateRangeAttr(llcx: &Context, size: Size, range: WrappingRange) -> &Attribute {
125 let lower = range.start;
126 let upper = range.end.wrapping_add(1);
128
129 let as_u64_array = |x: u128| [x as u64, (x >> 64) as u64];
131 let lower_words: [u64; 2] = as_u64_array(lower);
132 let upper_words: [u64; 2] = as_u64_array(upper);
133
134 let size_bits = size.bits();
137 if !(size_bits <= 128) {
::core::panicking::panic("assertion failed: size_bits <= 128")
};assert!(size_bits <= 128);
138 if !(size_bits.div_ceil(64) <= u64::try_from(lower_words.len()).unwrap()) {
::core::panicking::panic("assertion failed: size_bits.div_ceil(64) <= u64::try_from(lower_words.len()).unwrap()")
};assert!(size_bits.div_ceil(64) <= u64::try_from(lower_words.len()).unwrap());
141 if !(size_bits.div_ceil(64) <= u64::try_from(upper_words.len()).unwrap()) {
::core::panicking::panic("assertion failed: size_bits.div_ceil(64) <= u64::try_from(upper_words.len()).unwrap()")
};assert!(size_bits.div_ceil(64) <= u64::try_from(upper_words.len()).unwrap());
142 let size_bits = c_uint::try_from(size_bits).unwrap();
143
144 unsafe {
145 LLVMRustCreateRangeAttribute(llcx, size_bits, lower_words.as_ptr(), upper_words.as_ptr())
146 }
147}
148
149#[derive(#[automatically_derived]
impl ::core::marker::Copy for AttributePlace { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AttributePlace {
#[inline]
fn clone(&self) -> AttributePlace {
let _: ::core::clone::AssertParamIsClone<u32>;
*self
}
}Clone)]
150pub(crate) enum AttributePlace {
151 ReturnValue,
152 Argument(u32),
153 Function,
154}
155
156impl AttributePlace {
157 pub(crate) fn as_uint(self) -> c_uint {
158 match self {
159 AttributePlace::ReturnValue => 0,
160 AttributePlace::Argument(i) => 1 + i,
161 AttributePlace::Function => !0,
162 }
163 }
164}
165
166#[derive(#[automatically_derived]
impl ::core::marker::Copy for CodeGenOptSize { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CodeGenOptSize {
#[inline]
fn clone(&self) -> CodeGenOptSize { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for CodeGenOptSize {
#[inline]
fn eq(&self, other: &CodeGenOptSize) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
167#[repr(C)]
168pub(crate) enum CodeGenOptSize {
169 CodeGenOptSizeNone = 0,
170 CodeGenOptSizeDefault = 1,
171 CodeGenOptSizeAggressive = 2,
172}
173
174pub(crate) fn SetInstructionCallConv(instr: &Value, cc: CallConv) {
175 unsafe {
176 LLVMSetInstructionCallConv(instr, cc as c_uint);
177 }
178}
179pub(crate) fn SetFunctionCallConv(fn_: &Value, cc: CallConv) {
180 unsafe {
181 LLVMSetFunctionCallConv(fn_, cc as c_uint);
182 }
183}
184
185pub(crate) fn SetUniqueComdat(llmod: &Module, val: &Value) {
192 let name_buf = get_value_name(val);
193 let name =
194 CString::from_vec_with_nul(name_buf).or_else(|buf| CString::new(buf.into_bytes())).unwrap();
195 set_comdat(llmod, val, &name);
196}
197
198pub(crate) fn set_unnamed_address(global: &Value, unnamed: UnnamedAddr) {
199 LLVMSetUnnamedAddress(global, unnamed);
200}
201
202pub(crate) fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) {
203 unsafe {
204 LLVMSetThreadLocalMode(global, mode);
205 }
206}
207
208impl AttributeKind {
209 pub(crate) fn create_attr(self, llcx: &Context) -> &Attribute {
211 unsafe { LLVMRustCreateAttrNoValue(llcx, self) }
212 }
213}
214
215impl MemoryEffects {
216 pub(crate) fn create_attr(self, llcx: &Context) -> &Attribute {
218 unsafe { LLVMRustCreateMemoryEffectsAttr(llcx, self) }
219 }
220}
221
222pub(crate) fn set_section(llglobal: &Value, section_name: &CStr) {
223 unsafe {
224 LLVMSetSection(llglobal, section_name.as_ptr());
225 }
226}
227
228pub(crate) fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name_cstr: &CStr) -> &'a Value {
229 unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
230}
231
232pub(crate) fn set_initializer(llglobal: &Value, constant_val: &Value) {
233 unsafe {
234 LLVMSetInitializer(llglobal, constant_val);
235 }
236}
237
238pub(crate) fn set_global_constant(llglobal: &Value, is_constant: bool) {
239 LLVMSetGlobalConstant(llglobal, is_constant.to_llvm_bool());
240}
241
242pub(crate) fn get_linkage(llglobal: &Value) -> Linkage {
243 unsafe { LLVMGetLinkage(llglobal) }.to_rust()
244}
245
246pub(crate) fn set_linkage(llglobal: &Value, linkage: Linkage) {
247 unsafe {
248 LLVMSetLinkage(llglobal, linkage);
249 }
250}
251
252pub(crate) fn is_declaration(llglobal: &Value) -> bool {
253 unsafe { LLVMIsDeclaration(llglobal) }.is_true()
254}
255
256pub(crate) fn get_visibility(llglobal: &Value) -> Visibility {
257 unsafe { LLVMGetVisibility(llglobal) }.to_rust()
258}
259
260pub(crate) fn set_visibility(llglobal: &Value, visibility: Visibility) {
261 unsafe {
262 LLVMSetVisibility(llglobal, visibility);
263 }
264}
265
266pub(crate) fn set_alignment(llglobal: &Value, align: Align) {
267 unsafe {
268 ffi::LLVMSetAlignment(llglobal, align.bytes() as c_uint);
269 }
270}
271
272pub(crate) fn set_externally_initialized(llglobal: &Value, is_ext_init: bool) {
273 LLVMSetExternallyInitialized(llglobal, is_ext_init.to_llvm_bool());
274}
275
276pub(crate) fn set_comdat(llmod: &Module, llglobal: &Value, name: &CStr) {
281 unsafe {
282 let comdat = LLVMGetOrInsertComdat(llmod, name.as_ptr());
283 LLVMSetComdat(llglobal, comdat);
284 }
285}
286
287pub(crate) fn count_params(llfn: &Value) -> c_uint {
288 LLVMCountParams(llfn)
289}
290
291pub(crate) fn get_param(llfn: &Value, index: c_uint) -> &Value {
293 unsafe {
294 if !(index < LLVMCountParams(llfn)) {
{
::core::panicking::panic_fmt(format_args!("out of bounds argument access: {0} out of {1} arguments",
index, LLVMCountParams(llfn)));
}
};assert!(
295 index < LLVMCountParams(llfn),
296 "out of bounds argument access: {} out of {} arguments",
297 index,
298 LLVMCountParams(llfn)
299 );
300 LLVMGetParam(llfn, index)
301 }
302}
303
304pub(crate) fn get_value_name(value: &Value) -> Vec<u8> {
308 unsafe {
309 let mut len = 0;
310 let data = LLVMGetValueName2(value, &mut len);
311 std::slice::from_raw_parts(data.cast(), len).to_vec()
312 }
313}
314
315#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Intrinsic {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "Intrinsic",
"id", &&self.id)
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for Intrinsic { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Intrinsic {
#[inline]
fn clone(&self) -> Intrinsic {
let _: ::core::clone::AssertParamIsClone<NonZero<c_uint>>;
*self
}
}Clone)]
316pub(crate) struct Intrinsic {
317 id: NonZero<c_uint>,
318}
319
320impl Intrinsic {
321 pub(crate) fn lookup(name: &[u8]) -> Option<Self> {
322 let id = unsafe { LLVMLookupIntrinsicID(name.as_c_char_ptr(), name.len()) };
323 NonZero::new(id).map(|id| Self { id })
324 }
325
326 pub(crate) fn get_declaration<'ll>(
327 self,
328 llmod: &'ll Module,
329 type_params: &[&'ll Type],
330 ) -> &'ll Value {
331 unsafe {
332 LLVMGetIntrinsicDeclaration(llmod, self.id, type_params.as_ptr(), type_params.len())
333 }
334 }
335}
336
337pub(crate) fn set_value_name(value: &Value, name: &[u8]) {
339 unsafe {
340 let data = name.as_c_char_ptr();
341 LLVMSetValueName2(value, data, name.len());
342 }
343}
344
345pub(crate) fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
346 String::from_utf8(RustString::build_byte_buffer(f))
347}
348
349pub(crate) fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> {
350 RustString::build_byte_buffer(f)
351}
352
353pub(crate) fn twine_to_string(tr: &Twine) -> String {
354 unsafe {
355 build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
356 }
357}
358
359pub(crate) fn last_error() -> Option<String> {
360 unsafe {
361 let cstr = LLVMRustGetLastError();
362 if cstr.is_null() {
363 None
364 } else {
365 let err = CStr::from_ptr(cstr).to_bytes();
366 let err = String::from_utf8_lossy(err).to_string();
367 libc::free(cstr as *mut _);
368 Some(err)
369 }
370 }
371}
372
373pub(crate) struct OperandBundleBox<'a> {
376 raw: ptr::NonNull<OperandBundle<'a>>,
377}
378
379impl<'a> OperandBundleBox<'a> {
380 pub(crate) fn new(name: &str, vals: &[&'a Value]) -> Self {
381 let raw = unsafe {
382 LLVMCreateOperandBundle(
383 name.as_c_char_ptr(),
384 name.len(),
385 vals.as_ptr(),
386 vals.len() as c_uint,
387 )
388 };
389 Self { raw: ptr::NonNull::new(raw).unwrap() }
390 }
391
392 pub(crate) fn as_ref(&self) -> &OperandBundle<'a> {
397 unsafe { self.raw.as_ref() }
400 }
401}
402
403impl Drop for OperandBundleBox<'_> {
404 fn drop(&mut self) {
405 unsafe {
406 LLVMDisposeOperandBundle(self.raw);
407 }
408 }
409}
410
411pub(crate) fn add_module_flag_u32(
412 module: &Module,
413 merge_behavior: ModuleFlagMergeBehavior,
414 key: &str,
415 value: u32,
416) {
417 unsafe {
418 LLVMRustAddModuleFlagU32(module, merge_behavior, key.as_c_char_ptr(), key.len(), value);
419 }
420}
421
422pub(crate) fn add_module_flag_str(
423 module: &Module,
424 merge_behavior: ModuleFlagMergeBehavior,
425 key: &str,
426 value: &str,
427) {
428 unsafe {
429 LLVMRustAddModuleFlagString(
430 module,
431 merge_behavior,
432 key.as_c_char_ptr(),
433 key.len(),
434 value.as_c_char_ptr(),
435 value.len(),
436 );
437 }
438}
439
440pub(crate) fn set_dllimport_storage_class<'ll>(v: &'ll Value) {
441 unsafe {
442 LLVMSetDLLStorageClass(v, DLLStorageClass::DllImport);
443 }
444}
445
446pub(crate) fn set_dso_local<'ll>(v: &'ll Value) {
447 unsafe {
448 LLVMRustSetDSOLocal(v, true);
449 }
450}
451
452pub(crate) fn append_module_inline_asm<'ll>(llmod: &'ll Module, asm: &[u8]) {
455 unsafe {
456 LLVMAppendModuleInlineAsm(llmod, asm.as_ptr(), asm.len());
457 }
458}
459
460pub(crate) fn add_alias<'ll>(
462 module: &'ll Module,
463 ty: &Type,
464 address_space: AddressSpace,
465 aliasee: &Value,
466 name: &CStr,
467) -> &'ll Value {
468 unsafe { LLVMAddAlias2(module, ty, address_space.0, aliasee, name.as_ptr()) }
469}